Android Open Source - paperchains Sound Cloud Url Fetcher Task






From Project

Back to project page paperchains.

License

The source code is released under:

Apache License

If you think the Android project paperchains listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014 Simon Robinson//w w w .  j a v  a2  s . c o  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ac.robinson.paperchains;

import android.os.AsyncTask;

import com.soundcloud.api.Endpoints;
import com.soundcloud.playerapi.ApiWrapper;
import com.soundcloud.playerapi.Request;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.ref.WeakReference;

class SoundCloudUrlFetcherTask extends AsyncTask<Long, Void, String> {

  private final WeakReference<PaperChainsActivity> mContext;
  private final ApiWrapper mWrapper;

  private int errorReason = -1;

  public SoundCloudUrlFetcherTask(PaperChainsActivity context, ApiWrapper wrapper) {
    mContext = new WeakReference<>(context);
    mWrapper = wrapper;
  }

  @Override
  protected String doInBackground(Long... trackIds) {
    if (trackIds.length < 1) {
      return null;
    }
    final long trackId = trackIds[0];

    try {
      HttpResponse trackResponse = mWrapper.get(Request.to(Endpoints.TRACK_DETAILS, trackId));
      if (trackResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        JSONObject trackJSON = new JSONObject(EntityUtils.toString(trackResponse.getEntity()));
        if (trackJSON.getBoolean("streamable")) { // should always be the case
          HttpResponse streamResponse = mWrapper.get(Request.to("/tracks/%d/stream", trackId));
          JSONObject streamJSON = new JSONObject(EntityUtils.toString(streamResponse.getEntity()));
          return streamJSON.getString("location");
        }
        errorReason = R.string.hint_soundcloud_load_too_early;
        return null;
      } else {
        errorReason = R.string.hint_soundcloud_load_too_early;
        return null;
      }
    } catch (IOException e) {
      return null;
    } catch (ParseException e) {
      return null;
    } catch (JSONException e) {
      return null;
    }
  }

  @Override
  protected void onPostExecute(String url) {
    // notify the activity
    PaperChainsActivity activity = mContext.get();
    if (activity != null) {
      if (url != null) {
        activity.streamAudioLoadCompleted(url);
      } else {
        activity.streamAudioLoadFailed(errorReason == -1 ? R.string.hint_soundcloud_load_failed : errorReason);
      }
    }
  }
}




Java Source Code List

ac.robinson.paperchains.AudioRecorderCircleButton.java
ac.robinson.paperchains.PaperChainsActivity.java
ac.robinson.paperchains.PaperChainsView.java
ac.robinson.paperchains.SoundCloudLoginActivity.java
ac.robinson.paperchains.SoundCloudUploadTask.java
ac.robinson.paperchains.SoundCloudUrlFetcherTask.java
com.github.lassana.recorder.ApiHelper.java
com.github.lassana.recorder.AudioRecorder.java
com.github.lassana.recorder.Mp4ParserWrapper.java
com.sonyericsson.zoom.AspectQuotient.java
com.sonyericsson.zoom.DynamicZoomControl.java
com.sonyericsson.zoom.Dynamics.java
com.sonyericsson.zoom.LongPressZoomListener.java
com.sonyericsson.zoom.SpringDynamics.java
com.sonyericsson.zoom.ZoomState.java
com.soundcloud.playerapi.ApiWrapper.java
com.soundcloud.playerapi.CloudAPI.java
com.soundcloud.playerapi.CountingMultipartEntity.java
com.soundcloud.playerapi.DecompressingEntity.java
com.soundcloud.playerapi.Endpoints.java
com.soundcloud.playerapi.Env.java
com.soundcloud.playerapi.GzipDecompressingEntity.java
com.soundcloud.playerapi.Http.java
com.soundcloud.playerapi.OAuth2HttpRequestInterceptor.java
com.soundcloud.playerapi.OAuth2Scheme.java
com.soundcloud.playerapi.Params.java
com.soundcloud.playerapi.Request.java
com.soundcloud.playerapi.Stream.java
com.soundcloud.playerapi.Token.java