Search in sources :

Example 1 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project remusic by aa112901.

the class PlaylistActivity method setAlbumart.

private void setAlbumart() {
    playlistTitleView.setText(playlistName);
    if (albumPath == null) {
        albumArtSmall.setImageResource(R.drawable.placeholder_disk_210);
    } else {
        albumArtSmall.setImageURI(Uri.parse(albumPath));
    }
    try {
        if (isLocalPlaylist && !URLUtil.isNetworkUrl(albumPath)) {
            new setBlurredAlbumArt().execute(ImageUtils.getArtworkQuick(PlaylistActivity.this, Uri.parse(albumPath), 300, 300));
            L.D(d, TAG, "albumpath = " + albumPath);
        } else {
            // drawable = Drawable.createFromStream( new URL(albumPath).openStream(),"src");
            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));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) File(java.io.File) CacheKey(com.facebook.cache.common.CacheKey) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource)

Example 2 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DiskStorageCache method endInsert.

/**
 * Commits the provided temp file to the cache, renaming it to match the cache's hashing
 * convention.
 */
private BinaryResource endInsert(final DiskStorage.Inserter inserter, final CacheKey key, String resourceId) throws IOException {
    synchronized (mLock) {
        BinaryResource resource = inserter.commit(key);
        mResourceIndex.add(resourceId);
        mCacheStats.increment(resource.size(), 1);
        return resource;
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource)

Example 3 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DiskStorageCache method getResource.

/**
 * Retrieves the file corresponding to the mKey, if it is in the cache. Also touches the item,
 * thus changing its LRU timestamp. If the file is not present in the file cache, returns null.
 *
 * <p>This should NOT be called on the UI thread.
 *
 * @param key the mKey to check
 * @return The resource if present in cache, otherwise null
 */
@Override
@Nullable
public BinaryResource getResource(final CacheKey key) {
    String resourceId = null;
    SettableCacheEvent cacheEvent = SettableCacheEvent.obtain().setCacheKey(key);
    try {
        synchronized (mLock) {
            BinaryResource resource = null;
            List<String> resourceIds = CacheKeyUtil.getResourceIds(key);
            for (int i = 0; i < resourceIds.size(); i++) {
                resourceId = resourceIds.get(i);
                cacheEvent.setResourceId(resourceId);
                resource = mStorage.getResource(resourceId, key);
                if (resource != null) {
                    break;
                }
            }
            if (resource == null) {
                mCacheEventListener.onMiss(cacheEvent);
                mResourceIndex.remove(resourceId);
            } else {
                Preconditions.checkNotNull(resourceId);
                mCacheEventListener.onHit(cacheEvent);
                mResourceIndex.add(resourceId);
            }
            return resource;
        }
    } catch (IOException ioe) {
        mCacheErrorLogger.logError(CacheErrorLogger.CacheErrorCategory.GENERIC_IO, TAG, "getResource", ioe);
        cacheEvent.setException(ioe);
        mCacheEventListener.onReadException(cacheEvent);
        return null;
    } finally {
        cacheEvent.recycle();
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 4 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DiskStorageCacheTest method testCacheEventListener.

@Test
public void testCacheEventListener() throws Exception {
    // 1. Add first cache file
    CacheKey key1 = new SimpleCacheKey("foo");
    int value1Size = 101;
    byte[] value1 = new byte[value1Size];
    // just so it's not all zeros for the equality test below.
    value1[80] = 'c';
    BinaryResource resource1 = mCache.insert(key1, WriterCallbacks.from(value1));
    verifyListenerOnWriteAttempt(key1);
    String resourceId1 = verifyListenerOnWriteSuccessAndGetResourceId(key1, value1Size);
    BinaryResource resource1Again = mCache.getResource(key1);
    assertEquals(resource1, resource1Again);
    verifyListenerOnHit(key1, resourceId1);
    BinaryResource resource1Again2 = mCache.getResource(key1);
    assertEquals(resource1, resource1Again2);
    verifyListenerOnHit(key1, resourceId1);
    SimpleCacheKey missingKey = new SimpleCacheKey("nonexistent_key");
    BinaryResource res2 = mCache.getResource(missingKey);
    assertNull(res2);
    verifyListenerOnMiss(missingKey);
    mCache.clearAll();
    verify(mCacheEventListener).onCleared();
    verifyNoMoreInteractions(mCacheEventListener);
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 5 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DiskStorageCacheTest method testCacheFileWithIOException.

@Test
public void testCacheFileWithIOException() throws IOException {
    CacheKey key1 = new SimpleCacheKey("aaa");
    // Before inserting, make sure files not exist.
    final BinaryResource resource1 = getResource(key1);
    assertNull(resource1);
    // 1. Should not create cache files if IOException happens in the middle.
    final IOException writeException = new IOException();
    try {
        mCache.insert(key1, new WriterCallback() {

            @Override
            public void write(OutputStream os) throws IOException {
                throw writeException;
            }
        });
        fail();
    } catch (IOException e) {
        assertNull(getResource(key1));
    }
    verifyListenerOnWriteAttempt(key1);
    verifyListenerOnWriteException(key1, writeException);
    // 2. Test a read failure from DiskStorage
    CacheKey key2 = new SimpleCacheKey("bbb");
    int value2Size = 42;
    byte[] value2 = new byte[value2Size];
    value2[25] = 'b';
    mCache.insert(key2, WriterCallbacks.from(value2));
    verifyListenerOnWriteAttempt(key2);
    String resourceId2 = verifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size);
    ((DiskStorageWithReadFailures) mStorage).setPoisonResourceId(resourceId2);
    assertNull(mCache.getResource(key2));
    verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);
    assertFalse(mCache.probe(key2));
    verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);
    verifyNoMoreInteractions(mCacheEventListener);
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) WriterCallback(com.facebook.cache.common.WriterCallback) OutputStream(java.io.OutputStream) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) IOException(java.io.IOException) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Aggregations

BinaryResource (com.facebook.binaryresource.BinaryResource)13 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)6 CacheKey (com.facebook.cache.common.CacheKey)5 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)5 File (java.io.File)4 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)3 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)3 Bitmap (android.graphics.Bitmap)2 CloseableReference (com.facebook.common.references.CloseableReference)2 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Nullable (javax.annotation.Nullable)2 WriterCallback (com.facebook.cache.common.WriterCallback)1 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)1 DataSource (com.facebook.datasource.DataSource)1 ImagePipelineFactory (com.facebook.imagepipeline.core.ImagePipelineFactory)1