Search in sources :

Example 1 with BaseProducerContextCallbacks

use of com.facebook.imagepipeline.producers.BaseProducerContextCallbacks in project fresco by facebook.

the class VolleyNetworkFetcher method fetch.

@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);
}
Also used : VolleyError(com.android.volley.VolleyError) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.android.volley.Request) IOException(java.io.IOException) BaseProducerContextCallbacks(com.facebook.imagepipeline.producers.BaseProducerContextCallbacks) Response(com.android.volley.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestFilter(com.android.volley.RequestQueue.RequestFilter)

Example 2 with BaseProducerContextCallbacks

use of com.facebook.imagepipeline.producers.BaseProducerContextCallbacks in project fresco by facebook.

the class OkHttpNetworkFetcher method fetchWithRequest.

protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, final Request request) {
    final Call call = mOkHttpClient.newCall(request);
    fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                call.cancel();
            } else {
                mCancellationExecutor.execute(new Runnable() {

                    @Override
                    public void run() {
                        call.cancel();
                    }
                });
            }
        }
    });
    call.enqueue(new com.squareup.okhttp.Callback() {

        @Override
        public void onResponse(Response response) {
            fetchState.responseTime = SystemClock.uptimeMillis();
            final ResponseBody body = response.body();
            try {
                if (!response.isSuccessful()) {
                    handleException(call, new IOException("Unexpected HTTP code " + response), callback);
                    return;
                }
                long contentLength = body.contentLength();
                if (contentLength < 0) {
                    contentLength = 0;
                }
                callback.onResponse(body.byteStream(), (int) contentLength);
            } catch (Exception e) {
                handleException(call, e, callback);
            } finally {
                try {
                    body.close();
                } catch (Exception e) {
                    FLog.w(TAG, "Exception when closing response body", e);
                }
            }
        }

        @Override
        public void onFailure(final Request request, final IOException e) {
            handleException(call, e, callback);
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) Call(com.squareup.okhttp.Call) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) BaseProducerContextCallbacks(com.facebook.imagepipeline.producers.BaseProducerContextCallbacks) ResponseBody(com.squareup.okhttp.ResponseBody)

Example 3 with BaseProducerContextCallbacks

use of com.facebook.imagepipeline.producers.BaseProducerContextCallbacks in project fresco by facebook.

the class OkHttpNetworkFetcher method fetchWithRequest.

protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, final Request request) {
    final Call call = mCallFactory.newCall(request);
    fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                call.cancel();
            } else {
                mCancellationExecutor.execute(new Runnable() {

                    @Override
                    public void run() {
                        call.cancel();
                    }
                });
            }
        }
    });
    call.enqueue(new okhttp3.Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            fetchState.responseTime = SystemClock.elapsedRealtime();
            final ResponseBody body = response.body();
            try {
                if (!response.isSuccessful()) {
                    handleException(call, new IOException("Unexpected HTTP code " + response), callback);
                    return;
                }
                long contentLength = body.contentLength();
                if (contentLength < 0) {
                    contentLength = 0;
                }
                callback.onResponse(body.byteStream(), (int) contentLength);
            } catch (Exception e) {
                handleException(call, e, callback);
            } finally {
                try {
                    body.close();
                } catch (Exception e) {
                    FLog.w(TAG, "Exception when closing response body", e);
                }
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            handleException(call, e, callback);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) IOException(java.io.IOException) IOException(java.io.IOException) BaseProducerContextCallbacks(com.facebook.imagepipeline.producers.BaseProducerContextCallbacks) ResponseBody(okhttp3.ResponseBody)

Aggregations

BaseProducerContextCallbacks (com.facebook.imagepipeline.producers.BaseProducerContextCallbacks)3 IOException (java.io.IOException)3 Request (com.android.volley.Request)1 RequestFilter (com.android.volley.RequestQueue.RequestFilter)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 Call (com.squareup.okhttp.Call)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 ResponseBody (com.squareup.okhttp.ResponseBody)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Call (okhttp3.Call)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1