Пример #1
0
 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;
   }
 }
Пример #2
0
 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;
 }
Пример #3
0
  /**
   * 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);
    }
  }
Пример #4
0
 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);
       }
     }
   }
 }
Пример #5
0
 public void onLowMemory() {
   if (mMemoryCache != null) {
     synchronized (mMemoryCache) {
       mMemoryCache.evictAll();
     }
   }
 }
Пример #6
0
 public synchronized T getLock(T key) {
   T value = super.get(key);
   if (value == null) {
     value = key;
     super.put(key, value);
   }
   return value;
 }
Пример #7
0
 @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();
 }
Пример #8
0
  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);
  }
Пример #9
0
  public List<Bitmap> removeByPrefix(String cacheKeyPrefix) {
    synchronized (mMemoryCache) {
      List<Bitmap> bitmaps = mMemoryCache.removeByPrefix(getCacheKey(cacheKeyPrefix));

      // TODO: Try to remove from disk cache

      return bitmaps;
    }
  }
Пример #10
0
 @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);
 }
Пример #11
0
  public void remove(String cacheKey) {
    synchronized (mMemoryCache) {
      mMemoryCache.remove(getCacheKey(cacheKey));
    }

    try {
      mDiskCache.remove(getCacheKey(cacheKey));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #12
0
 public void clear() {
   try {
     mDiskCache.delete();
     openDiskCache();
   } catch (IOException e) {
     e.printStackTrace();
   }
   synchronized (mMemoryCache) {
     mMemoryCache.evictAll();
   }
 }
Пример #13
0
 /**
  * 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;
 }
Пример #14
0
  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();
  }
Пример #15
0
 public boolean hasKeyInMemory(String url, int preferredWidth, int preferredHeight) {
   synchronized (mMemoryCache) {
     return mMemoryCache.get(getCacheKey(url, preferredWidth, preferredHeight)) != null;
   }
 }
Пример #16
0
 /** Add images to the cache */
 private void addImageToCache(String key, Bitmap bitmap) {
   if (getImageFromCache(key) == null && bitmap != null) mLruCache.put(key, bitmap);
 }
Пример #17
0
 /** Read the images from the cache */
 private Bitmap getImageFromCache(String key) {
   return mLruCache.get(key);
 }
Пример #18
0
 public void clearMemoryCache() {
   synchronized (mMemoryCache) {
     mMemoryCache.evictAll();
   }
 }
Пример #19
0
 /** Clean caches, both memory cache and disk cache. */
 public void cleanCaches() {
   mDiskCache.clearCache();
   mMemoryCache.evictAll();
 }
Пример #20
0
 protected void invalidateCache() {
   mCache.clear();
 }
Пример #21
0
 /** Clean memory cache. */
 public void cleanMemCache() {
   mMemoryCache.evictAll();
 }
Пример #22
0
 public boolean hasKeyInMemory(String url) {
   synchronized (mMemoryCache) {
     return mMemoryCache.get(getCacheKey(url)) != null;
   }
 }
Пример #23
0
 public void storeToMemory(Bitmap bitmap, String cacheKey) {
   synchronized (mMemoryCache) {
     mMemoryCache.put(cacheKey, bitmap);
   }
 }