use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.
the class LocalExifThumbnailProducer method produceResults.
@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
final ImageRequest imageRequest = producerContext.getImageRequest();
final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<EncodedImage>(consumer, listener, PRODUCER_NAME, requestId) {
@Override
protected EncodedImage getResult() throws Exception {
final Uri sourceUri = imageRequest.getSourceUri();
final ExifInterface exifInterface = getExifInterface(sourceUri);
if (exifInterface == null || !exifInterface.hasThumbnail()) {
return null;
}
byte[] bytes = exifInterface.getThumbnail();
PooledByteBuffer pooledByteBuffer = mPooledByteBufferFactory.newByteBuffer(bytes);
return buildEncodedImage(pooledByteBuffer, exifInterface);
}
@Override
protected void disposeResult(EncodedImage result) {
EncodedImage.closeSafely(result);
}
@Override
protected Map<String, String> getExtraMapOnSuccess(final EncodedImage result) {
return ImmutableMap.of(CREATED_THUMBNAIL, Boolean.toString(result != null));
}
};
producerContext.addCallbacks(new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
cancellableProducerRunnable.cancel();
}
});
mExecutor.execute(cancellableProducerRunnable);
}
use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.
the class LocalVideoThumbnailProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
final ImageRequest imageRequest = producerContext.getImageRequest();
final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<CloseableReference<CloseableImage>>(consumer, listener, PRODUCER_NAME, requestId) {
@Override
protected CloseableReference<CloseableImage> getResult() throws Exception {
Bitmap thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(imageRequest.getSourceFile().getPath(), calculateKind(imageRequest));
if (thumbnailBitmap == null) {
return null;
}
return CloseableReference.<CloseableImage>of(new CloseableStaticBitmap(thumbnailBitmap, SimpleBitmapReleaser.getInstance(), ImmutableQualityInfo.FULL_QUALITY, 0));
}
@Override
protected Map<String, String> getExtraMapOnSuccess(final CloseableReference<CloseableImage> result) {
return ImmutableMap.of(CREATED_THUMBNAIL, String.valueOf(result != null));
}
@Override
protected void disposeResult(CloseableReference<CloseableImage> result) {
CloseableReference.closeSafely(result);
}
};
producerContext.addCallbacks(new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
cancellableProducerRunnable.cancel();
}
});
mExecutor.execute(cancellableProducerRunnable);
}
use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.
the class MediaVariationsFallbackProducer method produceResults.
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
final ImageRequest imageRequest = producerContext.getImageRequest();
final ResizeOptions resizeOptions = imageRequest.getResizeOptions();
final MediaVariations mediaVariations = imageRequest.getMediaVariations();
if (!imageRequest.isDiskCacheEnabled() || resizeOptions == null || resizeOptions.height <= 0 || resizeOptions.width <= 0) {
startInputProducerWithExistingConsumer(consumer, producerContext);
return;
}
final String mediaId;
@MediaVariations.Source final String source;
if (mediaVariations == null) {
if (mMediaIdExtractor == null) {
mediaId = null;
source = null;
} else {
mediaId = mMediaIdExtractor.getMediaIdFrom(imageRequest.getSourceUri());
source = MediaVariations.SOURCE_ID_EXTRACTOR;
}
} else {
mediaId = mediaVariations.getMediaId();
source = MediaVariations.SOURCE_INDEX_DB;
}
if (mediaVariations == null && mediaId == null) {
startInputProducerWithExistingConsumer(consumer, producerContext);
return;
}
producerContext.getListener().onProducerStart(producerContext.getId(), PRODUCER_NAME);
final AtomicBoolean isCancelled = new AtomicBoolean(false);
if (mediaVariations != null && mediaVariations.getVariantsCount() > 0) {
chooseFromVariants(consumer, producerContext, imageRequest, mediaVariations, resizeOptions, isCancelled);
} else {
MediaVariations.Builder mediaVariationsBuilder = MediaVariations.newBuilderForMediaId(mediaId).setForceRequestForSpecifiedUri(mediaVariations != null && mediaVariations.shouldForceRequestForSpecifiedUri()).setSource(source);
Task<MediaVariations> indexedMediaVariationsTask = mMediaVariationsIndex.getCachedVariants(mediaId, mediaVariationsBuilder);
indexedMediaVariationsTask.continueWith(new Continuation<MediaVariations, Object>() {
@Override
public Object then(Task<MediaVariations> task) throws Exception {
if (task.isCancelled() || task.isFaulted()) {
return task;
} else {
try {
if (task.getResult() == null) {
startInputProducerWithWrappedConsumer(consumer, producerContext, mediaId);
return null;
} else {
return chooseFromVariants(consumer, producerContext, imageRequest, task.getResult(), resizeOptions, isCancelled);
}
} catch (Exception e) {
return null;
}
}
}
});
}
subscribeTaskForRequestCancellation(isCancelled, producerContext);
}
use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.
the class MediaVariationsFallbackProducer method attemptCacheReadForVariant.
private Task attemptCacheReadForVariant(final Consumer<EncodedImage> consumer, final ProducerContext producerContext, ImageRequest imageRequest, MediaVariations mediaVariations, List<MediaVariations.Variant> sortedVariants, int index, AtomicBoolean isCancelled) {
final MediaVariations.Variant variant = sortedVariants.get(index);
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, variant.getUri(), producerContext.getCallerContext());
final ImageRequest.CacheChoice cacheChoice;
if (variant.getCacheChoice() == null) {
cacheChoice = imageRequest.getCacheChoice();
} else {
cacheChoice = variant.getCacheChoice();
}
final BufferedDiskCache preferredCache = cacheChoice == ImageRequest.CacheChoice.SMALL ? mSmallImageBufferedDiskCache : mDefaultBufferedDiskCache;
Task<EncodedImage> readTask = preferredCache.get(cacheKey, isCancelled);
Continuation<EncodedImage, Void> continuation = onFinishDiskReads(consumer, producerContext, imageRequest, mediaVariations, sortedVariants, index, isCancelled);
return readTask.continueWith(continuation);
}
use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.
the class PostprocessedBitmapMemoryCacheProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
final ImageRequest imageRequest = producerContext.getImageRequest();
final Object callerContext = producerContext.getCallerContext();
// If there's no postprocessor or the postprocessor doesn't require caching, forward results.
final Postprocessor postprocessor = imageRequest.getPostprocessor();
if (postprocessor == null || postprocessor.getPostprocessorCacheKey() == null) {
mInputProducer.produceResults(consumer, producerContext);
return;
}
listener.onProducerStart(requestId, getProducerName());
final CacheKey cacheKey = mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest, callerContext);
CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
if (cachedReference != null) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "true") : null);
consumer.onProgressUpdate(1.0f);
consumer.onNewResult(cachedReference, true);
cachedReference.close();
} else {
final boolean isRepeatedProcessor = postprocessor instanceof RepeatedPostprocessor;
Consumer<CloseableReference<CloseableImage>> cachedConsumer = new CachedPostprocessorConsumer(consumer, cacheKey, isRepeatedProcessor, mMemoryCache);
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
mInputProducer.produceResults(cachedConsumer, producerContext);
}
}
Aggregations