Exemplo n.º 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;
   }
 }
Exemplo n.º 2
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);
  }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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);
    }
  }
Exemplo n.º 6
0
 /** Read the images from the cache */
 private Bitmap getImageFromCache(String key) {
   return mLruCache.get(key);
 }
Exemplo n.º 7
0
 public boolean hasKeyInMemory(String url, int preferredWidth, int preferredHeight) {
   synchronized (mMemoryCache) {
     return mMemoryCache.get(getCacheKey(url, preferredWidth, preferredHeight)) != null;
   }
 }
Exemplo n.º 8
0
 public boolean hasKeyInMemory(String url) {
   synchronized (mMemoryCache) {
     return mMemoryCache.get(getCacheKey(url)) != null;
   }
 }