Android Open Source - HomeMovies Memory Cache






From Project

Back to project page HomeMovies.

License

The source code is released under:

MIT License

If you think the Android project HomeMovies listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package il.co.All4Students.homemovies.util.imageWeb;
/*from  ww  w  . j  a v  a 2  s  .  co m*/
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.graphics.Bitmap;
import android.util.Log;

public class MemoryCache {
  // Attributes
  private static final String TAG = "MemoryCache";
  private Map<String, Bitmap> cache = Collections
      .synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
  private long size = 0;
  private long limit = 1000000;

  // Constractors
  public MemoryCache() {
    setLimit(Runtime.getRuntime().maxMemory() / 4);
  }

  // Additional Methods
  public void setLimit(long new_limit) {
    limit = new_limit;
    Log.i(TAG, "MemoryCache will use up to " + limit / 1024. / 1024. + "MB");
  }

  public Bitmap get(String id) {
    try {
      if (!cache.containsKey(id)) {
        return null;
      }
      return cache.get(id);
    } catch (NullPointerException ex) {
      ex.printStackTrace();
      return null;
    }
  }

  public void put(String id, Bitmap bitmap) {
    try {
      if (cache.containsKey(id)) {
        size -= getSizeInBytes(cache.get(id));
      }
      cache.put(id, bitmap);
      size += getSizeInBytes(bitmap);
      checkSize();
    } catch (Throwable th) {
      th.printStackTrace();
    }
  }

  private void checkSize() {
    Log.i(TAG, "cache size=" + size + " length=" + cache.size());
    if (size > limit) {
      Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator();
      while (iter.hasNext()) {
        Entry<String, Bitmap> entry = iter.next();
        size -= getSizeInBytes(entry.getValue());
        iter.remove();
        if (size <= limit) {
          break;
        }
      }
      Log.i(TAG, "Clean cache. New size " + cache.size());
    }
  }

  public void clear() {
    try {
      cache.clear();
      size = 0;
    } catch (NullPointerException ex) {
      ex.printStackTrace();
    }
  }

  long getSizeInBytes(Bitmap bitmap) {
    if (bitmap == null) {
      return 0;
    }
    return bitmap.getRowBytes() * bitmap.getHeight();
  }
}




Java Source Code List

il.co.All4Students.homemovies.OkCancelBar.java
il.co.All4Students.homemovies.ScreenEdit.java
il.co.All4Students.homemovies.ScreenGridDetail.java
il.co.All4Students.homemovies.ScreenGrid.java
il.co.All4Students.homemovies.ScreenMain.java
il.co.All4Students.homemovies.ScreenPreferences.java
il.co.All4Students.homemovies.ScreenWeb.java
il.co.All4Students.homemovies.app.AppConstants.java
il.co.All4Students.homemovies.app.ApplicationPreference.java
il.co.All4Students.homemovies.app.HomeProjectApplication.java
il.co.All4Students.homemovies.core.ItemCompareRTID.java
il.co.All4Students.homemovies.core.ItemCompareRank.java
il.co.All4Students.homemovies.core.ItemCompareSubject.java
il.co.All4Students.homemovies.core.Item.java
il.co.All4Students.homemovies.util.adapter.ItemListAdapter.java
il.co.All4Students.homemovies.util.adapter.ScreenGridAdapter.java
il.co.All4Students.homemovies.util.app.AppUtil.java
il.co.All4Students.homemovies.util.db.ItemsDbConstants.java
il.co.All4Students.homemovies.util.db.ItemsDbHelper.java
il.co.All4Students.homemovies.util.db.ItemsHandler.java
il.co.All4Students.homemovies.util.dialog.RankDialog.java
il.co.All4Students.homemovies.util.dialog.ShareDialog.java
il.co.All4Students.homemovies.util.email.EmailUtil.java
il.co.All4Students.homemovies.util.image.ExternalStorageLoader.java
il.co.All4Students.homemovies.util.imageWeb.FileCache.java
il.co.All4Students.homemovies.util.imageWeb.ImageLoader.java
il.co.All4Students.homemovies.util.imageWeb.MemoryCache.java
il.co.All4Students.homemovies.util.json.JSONHandler.java
il.co.All4Students.homemovies.util.json.JSONUtilWithTags.java
il.co.All4Students.homemovies.util.json.JSONUtil.java
il.co.All4Students.homemovies.util.log.db.LogDbConstants.java
il.co.All4Students.homemovies.util.log.db.LogDbHelper.java
il.co.All4Students.homemovies.util.log.db.LogHandler.java
il.co.All4Students.homemovies.util.log.util.AppLog.java