Search in sources :

Example 81 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class JobScheduler method updateJob.

/**
 * Updates the job.
 *
 * <p>This just updates the job, but it doesn't schedule it. In order to be executed, the job has
 * to be scheduled after being set. In case there was a previous job scheduled that has not yet
 * started, this new job will be executed instead.
 *
 * @return whether the job was successfully updated.
 */
public boolean updateJob(@Nullable EncodedImage encodedImage, @Consumer.Status int status) {
    if (!shouldProcess(encodedImage, status)) {
        return false;
    }
    EncodedImage oldEncodedImage;
    synchronized (this) {
        oldEncodedImage = mEncodedImage;
        this.mEncodedImage = EncodedImage.cloneOrNull(encodedImage);
        this.mStatus = status;
    }
    EncodedImage.closeSafely(oldEncodedImage);
    return true;
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 82 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class JobScheduler method clearJob.

/**
 * Clears the currently set job.
 *
 * <p>In case the currently set job has been scheduled but not started yet, the job won't be
 * executed.
 */
public void clearJob() {
    EncodedImage oldEncodedImage;
    synchronized (this) {
        oldEncodedImage = mEncodedImage;
        mEncodedImage = null;
        mStatus = 0;
    }
    EncodedImage.closeSafely(oldEncodedImage);
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 83 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class JobScheduler method doJob.

private void doJob() {
    long now = SystemClock.uptimeMillis();
    EncodedImage input;
    int status;
    synchronized (this) {
        input = mEncodedImage;
        status = mStatus;
        mEncodedImage = null;
        this.mStatus = 0;
        mJobState = JobState.RUNNING;
        mJobStartTime = now;
    }
    try {
        // we need to do a check in case the job got cleared in the meantime
        if (shouldProcess(input, status)) {
            mJobRunnable.run(input, status);
        }
    } finally {
        EncodedImage.closeSafely(input);
        onJobFinished();
    }
}
Also used : EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 84 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class LocalContentUriFetchProducer method getEncodedImage.

@Override
protected EncodedImage getEncodedImage(ImageRequest imageRequest) throws IOException {
    Uri uri = imageRequest.getSourceUri();
    if (UriUtil.isLocalContactUri(uri)) {
        final InputStream inputStream;
        if (uri.toString().endsWith("/photo")) {
            inputStream = mContentResolver.openInputStream(uri);
        } else if (uri.toString().endsWith("/display_photo")) {
            try {
                AssetFileDescriptor fd = mContentResolver.openAssetFileDescriptor(uri, "r");
                Preconditions.checkNotNull(fd);
                inputStream = fd.createInputStream();
            } catch (IOException e) {
                throw new IOException("Contact photo does not exist: " + uri);
            }
        } else {
            inputStream = ContactsContract.Contacts.openContactPhotoInputStream(mContentResolver, uri);
            if (inputStream == null) {
                throw new IOException("Contact photo does not exist: " + uri);
            }
        }
        Preconditions.checkNotNull(inputStream);
        // If a Contact URI is provided, use the special helper to open that contact's photo.
        return getEncodedImage(inputStream, EncodedImage.UNKNOWN_STREAM_SIZE);
    }
    if (UriUtil.isLocalCameraUri(uri)) {
        EncodedImage cameraImage = this.getCameraImage(uri);
        if (cameraImage != null) {
            return cameraImage;
        }
    }
    return getEncodedImage(Preconditions.checkNotNull(mContentResolver.openInputStream(uri)), EncodedImage.UNKNOWN_STREAM_SIZE);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Uri(android.net.Uri) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 85 with EncodedImage

use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.

the class LocalExifThumbnailProducer method produceResults.

@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final ProducerListener2 listener = producerContext.getProducerListener();
    final ImageRequest imageRequest = producerContext.getImageRequest();
    producerContext.putOriginExtra("local", "exif");
    final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<EncodedImage>(consumer, listener, producerContext, PRODUCER_NAME) {

        @Override
        @Nullable
        protected EncodedImage getResult() throws Exception {
            final Uri sourceUri = imageRequest.getSourceUri();
            final ExifInterface exifInterface = getExifInterface(sourceUri);
            if (exifInterface == null || !exifInterface.hasThumbnail()) {
                return null;
            }
            byte[] bytes = Preconditions.checkNotNull(exifInterface.getThumbnail());
            PooledByteBuffer pooledByteBuffer = mPooledByteBufferFactory.newByteBuffer(bytes);
            return buildEncodedImage(pooledByteBuffer, exifInterface);
        }

        @Override
        protected void disposeResult(@Nullable EncodedImage result) {
            EncodedImage.closeSafely(result);
        }

        @Override
        protected Map<String, String> getExtraMapOnSuccess(@Nullable 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);
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ExifInterface(android.media.ExifInterface) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) Uri(android.net.Uri) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) Nullable(javax.annotation.Nullable)

Aggregations

EncodedImage (com.facebook.imagepipeline.image.EncodedImage)108 Test (org.junit.Test)32 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)20 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)12 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)12 CacheKey (com.facebook.cache.common.CacheKey)11 Rect (android.graphics.Rect)9 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)9 InputStream (java.io.InputStream)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)9 AnimatedDrawableBackend (com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend)8 AnimatedImageCompositor (com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor)8 ImageDecodeOptions (com.facebook.imagepipeline.common.ImageDecodeOptions)8 Before (org.junit.Before)8 Bitmap (android.graphics.Bitmap)7 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)7 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)7 FakeClock (com.facebook.imagepipeline.testing.FakeClock)6 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)6