public IImage getImageForUri(Uri uri) { if (!isChildImageUri(uri)) return null; // Find the id of the input URI. long matchId; try { matchId = ContentUris.parseId(uri); } catch (NumberFormatException ex) { Log.i(TAG, "fail to get id in: " + uri, ex); return null; } // TODO: design a better method to get URI of specified ID Cursor cursor = getCursor(); if (cursor == null) return null; synchronized (this) { cursor.moveToPosition(-1); // before first for (int i = 0; cursor.moveToNext(); ++i) { if (getImageId(cursor) == matchId) { BaseImage image = mCache.get(i); if (image == null) { image = loadImageFromCursor(cursor); mCache.put(i, image); } return image; } } return null; } }
public IImage getImageAt(int i) { BaseImage result = mCache.get(i); if (result == null) { Cursor cursor = getCursor(); if (cursor == null) return null; synchronized (this) { result = cursor.moveToPosition(i) ? loadImageFromCursor(cursor) : null; mCache.put(i, result); } } return result; }
/** * Add a bitmap to memory cache. * * @param data * @param bitmap * @param cornerRadio */ public void addBitmapToMenCache(String data, Bitmap bitmap, int cornerRadio) { if (data == null || bitmap == null) { return; } // Create a memory cache key with data url and corner radio. String key = createKey(data, cornerRadio); // Add to memory cache if (mMemoryCache != null && mMemoryCache.get(key) == null) { mMemoryCache.put(key, bitmap); } }
public void onTrimMemory(int level) { synchronized (mMemoryCache) { if (mMemoryCache != null) { if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) { // 60 // Nearing middle of list of cached background apps mMemoryCache.evictAll(); } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { // 40 // Entering list of cached background apps mMemoryCache.trimToSize(mMemoryCache.size() / 2); } } } }
public void onLowMemory() { if (mMemoryCache != null) { synchronized (mMemoryCache) { mMemoryCache.evictAll(); } } }
public synchronized T getLock(T key) { T value = super.get(key); if (value == null) { value = key; super.put(key, value); } return value; }
@Override protected void entryRemoved( boolean evicted, String key, CacheEntry oldValue, CacheEntry newValue) { super.entryRemoved(evicted, key, oldValue, newValue); if (newValue != null) return; if (loading) return; new File(directory, key).delete(); }
public void queryCache(String url, ImageCacheListener listener, DownloadRequest downloadRequest) { if (url == null) { listener.onImageNotFound(this, url, downloadRequest); return; } String fullSizeCacheKey = getCacheKey(url); String scaledCacheKey = ""; int desiredWidth = downloadRequest.getListener().getDesiredWidth(); int desiredHeight = downloadRequest.getListener().getDesiredHeight(); if (desiredWidth > 0 && desiredHeight > 0) { scaledCacheKey = getCacheKey(url, desiredWidth, desiredHeight); synchronized (mMemoryCache) { Bitmap scaledBitmap = mMemoryCache.get(scaledCacheKey); if (scaledBitmap != null) { listener.onImageFound(this, scaledBitmap, url, downloadRequest); return; } } } // First check the in-memory cache... synchronized (mMemoryCache) { Bitmap cachedBitmap = mMemoryCache.get(fullSizeCacheKey); if (cachedBitmap != null) { // ...notify listener immediately, no need to go async listener.onImageFound(this, cachedBitmap, url, downloadRequest); return; } } if (mDiskCache != null) { ThreadPoolAsyncTaskRunner.runTaskOnPool( ThreadPoolAsyncTaskRunner.THUMBNAIL_THREAD_POOL, new BitmapDecoderTask(url, listener, downloadRequest), (Object[]) null); return; } listener.onImageNotFound(this, url, downloadRequest); }
public List<Bitmap> removeByPrefix(String cacheKeyPrefix) { synchronized (mMemoryCache) { List<Bitmap> bitmaps = mMemoryCache.removeByPrefix(getCacheKey(cacheKeyPrefix)); // TODO: Try to remove from disk cache return bitmaps; } }
@Override protected void trimToSize(int maxSize) { // Before removing the non recycled bitmaps, first we remove all the // Recycled Bitmaps if (size > maxSize) { evictionCount = evictionCount + cleanRecycledBitmaps(); } super.trimToSize(maxSize); }
public void remove(String cacheKey) { synchronized (mMemoryCache) { mMemoryCache.remove(getCacheKey(cacheKey)); } try { mDiskCache.remove(getCacheKey(cacheKey)); } catch (IOException e) { e.printStackTrace(); } }
public void clear() { try { mDiskCache.delete(); openDiskCache(); } catch (IOException e) { e.printStackTrace(); } synchronized (mMemoryCache) { mMemoryCache.evictAll(); } }
/** * Get bitmap from memory cache. * * @param data * @param cornerRadio * @return */ public Bitmap getBitmapFromMemCache(String data, int cornerRadio) { if (mMemoryCache != null) { String key = createKey(data, cornerRadio); final Bitmap memBitmap = mMemoryCache.get(key); if (memBitmap != null) { if (ImageWorker.DEBUG) { Log.d(TAG, "Memory cache hit"); } return memBitmap; } } return null; }
public BaseImageList(ContentResolver resolver, Uri uri, int sort, String bucketId) { mSort = sort; mBaseUri = uri; mBucketId = bucketId; mContentResolver = resolver; mCursor = createCursor(); if (mCursor == null) { Log.w(TAG, "createCursor returns null."); } // TODO: We need to clear the cache because we may "reopen" the image // list. After we implement the image list state, we can remove this // kind of usage. mCache.clear(); }
public boolean hasKeyInMemory(String url, int preferredWidth, int preferredHeight) { synchronized (mMemoryCache) { return mMemoryCache.get(getCacheKey(url, preferredWidth, preferredHeight)) != null; } }
/** Add images to the cache */ private void addImageToCache(String key, Bitmap bitmap) { if (getImageFromCache(key) == null && bitmap != null) mLruCache.put(key, bitmap); }
/** Read the images from the cache */ private Bitmap getImageFromCache(String key) { return mLruCache.get(key); }
public void clearMemoryCache() { synchronized (mMemoryCache) { mMemoryCache.evictAll(); } }
/** Clean caches, both memory cache and disk cache. */ public void cleanCaches() { mDiskCache.clearCache(); mMemoryCache.evictAll(); }
protected void invalidateCache() { mCache.clear(); }
/** Clean memory cache. */ public void cleanMemCache() { mMemoryCache.evictAll(); }
public boolean hasKeyInMemory(String url) { synchronized (mMemoryCache) { return mMemoryCache.get(getCacheKey(url)) != null; } }
public void storeToMemory(Bitmap bitmap, String cacheKey) { synchronized (mMemoryCache) { mMemoryCache.put(cacheKey, bitmap); } }