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);
}
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);
}
});
}
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);
}
});
}
Aggregations