com.facebook.imagepipeline.producers.BaseProducerContextCallbacks Java Examples

The following examples show how to use com.facebook.imagepipeline.producers.BaseProducerContextCallbacks. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VolleyNetworkFetcher.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void fetch(final VolleyNetworkFetchState fetchState, final Callback callback) {
  fetchState.submitTime = SystemClock.elapsedRealtime();

  final RawRequest request =
      new RawRequest(
          fetchState.getUri().toString(),
          new Response.Listener<byte[]>() {
            @Override
            public void onResponse(byte[] bytes) {
              fetchState.responseTime = SystemClock.uptimeMillis();

              try {
                InputStream is = new ByteArrayInputStream(bytes);
                callback.onResponse(is, bytes.length);
              } catch (IOException e) {
                callback.onFailure(e);
              }
            }
          },
          new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
              callback.onFailure(volleyError);
            }
          });

  fetchState
      .getContext()
      .addCallbacks(
          new BaseProducerContextCallbacks() {
            @Override
            public void onCancellationRequested() {
              mRequestQueue.cancelAll(
                  new RequestFilter() {
                    @Override
                    public boolean apply(Request<?> candidate) {
                      return candidate != null
                          && request.getSequence() == candidate.getSequence();
                    }
                  });
            }
          });

  mRequestQueue.add(request);
}