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