Search in sources :

Example 6 with CloseableReference

use of com.facebook.common.references.CloseableReference in project remusic by aa112901.

the class AlbumsDetailActivity method setAlbumart.

private void setAlbumart() {
    albumTitle.setText(albumName);
    albumArtSmall.setImageURI(Uri.parse(albumPath));
    try {
        ImageRequest imageRequest = ImageRequest.fromUri(albumPath);
        // CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
        // .getEncodedCacheKey(imageRequest);
        // BinaryResource resource = ImagePipelineFactory.getInstance()
        // .getMainDiskStorageCache().getResource(cacheKey);
        // File file = ((FileBinaryResource) resource).getFile();
        // if (file != null) {
        // new setBlurredAlbumArt().execute(ImageUtils.getArtworkQuick(file, 300, 300));
        // return;
        // }
        imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(albumPath)).setProgressiveRenderingEnabled(true).build();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, AlbumsDetailActivity.this);
        dataSource.subscribe(new BaseBitmapDataSubscriber() {

            @Override
            public void onNewResultImpl(@Nullable Bitmap bitmap) {
                // No need to do any cleanup.
                if (bitmap != null) {
                    new setBlurredAlbumArt().execute(bitmap);
                }
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {
            // No cleanup required here.
            }
        }, CallerThreadExecutor.getInstance());
    // drawable = Drawable.createFromStream( new URL(albumPath).openStream(),"src");
    } catch (Exception e) {
    }
}
Also used : Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource)

Example 7 with CloseableReference

use of com.facebook.common.references.CloseableReference in project remusic by aa112901.

the class SimpleWidgetProvider method pushUpdate.

// 更新所有的 widget
private synchronized void pushUpdate(final Context context, AppWidgetManager appWidgetManager, boolean updateProgress) {
    pushAction(context, MediaService.SEND_PROGRESS);
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.simple_control_widget_layout);
    // 将按钮与点击事件绑定
    remoteView.setOnClickPendingIntent(R.id.widget_play, getPendingIntent(context, R.id.widget_play));
    remoteView.setOnClickPendingIntent(R.id.widget_pre, getPendingIntent(context, R.id.widget_pre));
    remoteView.setOnClickPendingIntent(R.id.widget_next, getPendingIntent(context, R.id.widget_next));
    remoteView.setOnClickPendingIntent(R.id.widget_love, getPendingIntent(context, R.id.widget_love));
    remoteView.setTextViewText(R.id.widget_content, trackname == null && art == null ? "" : trackname + "-" + art);
    remoteView.setProgressBar(R.id.widget_progress, (int) duration, (int) position, false);
    isFav = false;
    long[] favlists = PlaylistsManager.getInstance(context).getPlaylistIds(IConstants.FAV_PLAYLIST);
    for (long i : favlists) {
        if (currentId == i) {
            isFav = true;
            break;
        }
    }
    if (isFav) {
        remoteView.setImageViewResource(R.id.widget_love, R.drawable.widget_unstar_selector);
    } else {
        remoteView.setImageViewResource(R.id.widget_love, R.drawable.widget_star_selector);
    }
    if (isPlaying) {
        remoteView.setImageViewResource(R.id.widget_play, R.drawable.widget_pause_selector);
    } else {
        remoteView.setImageViewResource(R.id.widget_play, R.drawable.widget_play_selector);
    }
    if (updateProgress) {
        if (albumuri == null) {
            remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
        } else {
            if (isTrackLocal) {
                Bitmap bitmap = ImageUtils.getArtworkQuick(context, Uri.parse(albumuri), 160, 160);
                if (bitmap != null) {
                    remoteView.setImageViewBitmap(R.id.widget_image, bitmap);
                } else {
                    remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
                }
            } else {
                Bitmap bitmap = albumMap.get(albumuri);
                if (bitmap != null)
                    remoteView.setImageViewBitmap(R.id.widget_image, bitmap);
            }
        }
    } else {
        if (albumuri == null) {
            remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
        } else {
            if (isTrackLocal) {
                final Bitmap bitmap = ImageUtils.getArtworkQuick(context, Uri.parse(albumuri), 160, 160);
                if (bitmap != null) {
                    remoteView.setImageViewBitmap(R.id.widget_image, bitmap);
                } else {
                    remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
                }
                albumMap.clear();
            } else {
                if (albumMap.get(albumuri) != null) {
                    remoteView.setImageViewBitmap(R.id.widget_image, albumMap.get(albumuri));
                // noBit = null;
                } else {
                    Uri uri = Uri.parse(albumuri);
                    if (uri == null) {
                        noBit = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder_disk_210);
                        albumMap.put(albumuri, noBit);
                        pushUpdate(context, AppWidgetManager.getInstance(context), false);
                    } else {
                        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri).setProgressiveRenderingEnabled(true).build();
                        ImagePipeline imagePipeline = Fresco.getImagePipeline();
                        DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);
                        dataSource.subscribe(new BaseBitmapDataSubscriber() {

                            @Override
                            public void onNewResultImpl(@Nullable Bitmap bitmap) {
                                // No need to do any cleanup.
                                if (bitmap != null) {
                                    noBit = bitmap.copy(bitmap.getConfig(), true);
                                    albumMap.put(albumuri, noBit);
                                }
                                pushUpdate(context, AppWidgetManager.getInstance(context), false);
                            }

                            @Override
                            public void onFailureImpl(DataSource dataSource) {
                                // No cleanup required here.
                                noBit = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder_disk_210);
                                albumMap.put(albumuri, noBit);
                                pushUpdate(context, AppWidgetManager.getInstance(context), false);
                            }
                        }, CallerThreadExecutor.getInstance());
                    }
                }
            }
        }
    }
    // 相当于获得所有本程序创建的appwidget
    ComponentName componentName = new ComponentName(context, SimpleWidgetProvider.class);
    appWidgetManager.updateAppWidget(componentName, remoteView);
}
Also used : CloseableReference(com.facebook.common.references.CloseableReference) Uri(android.net.Uri) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource) RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ComponentName(android.content.ComponentName) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Example 8 with CloseableReference

use of com.facebook.common.references.CloseableReference in project fresco by facebook.

the class AnimatedRepeatedPostprocessorProducerTest method testNonStaticBitmapIsPassedOn.

@Test
public void testNonStaticBitmapIsPassedOn() {
    RepeatedPostprocessorConsumer postprocessorConsumer = produceResults();
    RepeatedPostprocessorRunner repeatedPostprocessorRunner = getRunner();
    CloseableAnimatedImage sourceCloseableAnimatedImage = mock(CloseableAnimatedImage.class);
    CloseableReference<CloseableImage> sourceCloseableImageRef = CloseableReference.<CloseableImage>of(sourceCloseableAnimatedImage);
    postprocessorConsumer.onNewResult(sourceCloseableImageRef, Consumer.IS_LAST);
    sourceCloseableImageRef.close();
    mTestExecutorService.runUntilIdle();
    mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.NO_FLAGS));
    mInOrder.verifyNoMoreInteractions();
    assertEquals(1, mResults.size());
    CloseableReference<CloseableImage> res0 = mResults.get(0);
    assertTrue(CloseableReference.isValid(res0));
    assertSame(sourceCloseableAnimatedImage, res0.get());
    res0.close();
    performCancelAndVerifyOnCancellation();
    verify(sourceCloseableAnimatedImage).close();
}
Also used : RepeatedPostprocessorConsumer(com.facebook.imagepipeline.producers.PostprocessorProducer.RepeatedPostprocessorConsumer) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) RepeatedPostprocessorRunner(com.facebook.imagepipeline.request.RepeatedPostprocessorRunner)

Example 9 with CloseableReference

use of com.facebook.common.references.CloseableReference in project fresco by facebook.

the class AnimatedSingleUsePostprocessorProducerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mTestExecutorService = new TestExecutorService(new FakeClock());
    mPostprocessorProducer = new PostprocessorProducer(mInputProducer, mPlatformBitmapFactory, mTestExecutorService);
    when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor);
    when(mProducerContext.getId()).thenReturn(mRequestId);
    when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
    when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
    mResults = new ArrayList<>();
    when(mPostprocessor.getName()).thenReturn(POSTPROCESSOR_NAME);
    when(mProducerListener.requiresExtraMap(eq(mProducerContext), eq(POSTPROCESSOR_NAME))).thenReturn(true);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            mResults.add(((CloseableReference<CloseableImage>) invocation.getArguments()[0]).clone());
            return null;
        }
    }).when(mConsumer).onNewResult(any(CloseableReference.class), anyInt());
    mInOrder = inOrder(mPostprocessor, mProducerListener, mConsumer);
    mSourceBitmap = mock(Bitmap.class);
    mSourceCloseableStaticBitmap = mock(CloseableStaticBitmap.class);
    when(mSourceCloseableStaticBitmap.getUnderlyingBitmap()).thenReturn(mSourceBitmap);
    mSourceCloseableImageRef = CloseableReference.<CloseableImage>of(mSourceCloseableStaticBitmap);
    mDestinationBitmap = mock(Bitmap.class);
    mDestinationCloseableBitmapRef = CloseableReference.of(mDestinationBitmap, mBitmapResourceReleaser);
}
Also used : CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) Bitmap(android.graphics.Bitmap) FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) CloseableReference(com.facebook.common.references.CloseableReference) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap)

Example 10 with CloseableReference

use of com.facebook.common.references.CloseableReference in project fresco by facebook.

the class AnimatedImageFactoryImpl method getCloseableImage.

private CloseableImage getCloseableImage(ImageDecodeOptions options, AnimatedImage image, Bitmap.Config bitmapConfig) {
    List<CloseableReference<Bitmap>> decodedFrames = null;
    CloseableReference<Bitmap> previewBitmap = null;
    try {
        final int frameForPreview = options.useLastFrameForPreview ? image.getFrameCount() - 1 : 0;
        if (options.forceStaticImage) {
            return new CloseableStaticBitmap(createPreviewBitmap(image, bitmapConfig, frameForPreview), ImmutableQualityInfo.FULL_QUALITY, 0);
        }
        if (options.decodeAllFrames) {
            decodedFrames = decodeAllFrames(image, bitmapConfig);
            previewBitmap = CloseableReference.cloneOrNull(decodedFrames.get(frameForPreview));
        }
        if (options.decodePreviewFrame && previewBitmap == null) {
            previewBitmap = createPreviewBitmap(image, bitmapConfig, frameForPreview);
        }
        AnimatedImageResult animatedImageResult = AnimatedImageResult.newBuilder(image).setPreviewBitmap(previewBitmap).setFrameForPreview(frameForPreview).setDecodedFrames(decodedFrames).setBitmapTransformation(options.bitmapTransformation).build();
        return new CloseableAnimatedImage(animatedImageResult);
    } finally {
        CloseableReference.closeSafely(previewBitmap);
        CloseableReference.closeSafely(decodedFrames);
    }
}
Also used : Bitmap(android.graphics.Bitmap) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) CloseableReference(com.facebook.common.references.CloseableReference) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) SuppressLint(android.annotation.SuppressLint)

Aggregations

CloseableReference (com.facebook.common.references.CloseableReference)50 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)24 Bitmap (android.graphics.Bitmap)17 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)16 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)14 DataSource (com.facebook.datasource.DataSource)9 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)8 Uri (android.net.Uri)7 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)7 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)5 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)5 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)5 Nullable (javax.annotation.Nullable)5 Test (org.junit.Test)5 SuppressLint (android.annotation.SuppressLint)4 CacheKey (com.facebook.cache.common.CacheKey)3 MemoryTrimType (com.facebook.common.memory.MemoryTrimType)3 ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)3 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)3 FakeClock (com.facebook.imagepipeline.testing.FakeClock)3