Android Open Source - PinterestLikeApp Cache Manager






From Project

Back to project page PinterestLikeApp.

License

The source code is released under:

MIT License

If you think the Android project PinterestLikeApp 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 com.dreamtale.pintrestlike.utils;
/* w  w w  . j  a va2s  . com*/

import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;

public class CacheManager
{
    private final static String LOG_TAG = "CacheManager";
    
    private final int maxMemory = (int)Runtime.getRuntime().maxMemory();
    
    private final int cacheSize = UIConfig.MODE_GRID_VIEW == UIConfig.getInstance().getUIMode() ? maxMemory / 30 : maxMemory / 5;
    
    private LruCache<String, Bitmap> memoryCache = null;
    
    public static CacheManager manager = null;
    
    public static CacheManager getInstance()
    {
        if (null == manager)
        {
            init();
        }
        return manager;
    }
    
    private static synchronized void init()
    {
        if (null == manager)
        {
            manager = new CacheManager();
        }
    }
    
    private CacheManager()
    {
        memoryCache = new LruCache<String, Bitmap>(cacheSize)
        {

            @Override
            public void trimToSize(int maxSize)
            {
                super.trimToSize(maxSize);
            }

            @Override
            protected void entryRemoved(boolean evicted, String key,
                    Bitmap oldValue, Bitmap newValue)
            {
                super.entryRemoved(evicted, key, oldValue, newValue);
            }

            @Override
            protected Bitmap create(String key)
            {
                return super.create(key);
            }

            @Override
            protected int sizeOf(String key, Bitmap value)
            {
                return value.getByteCount();
            }
            
        };
    }
    
    public Bitmap getBitmap(String key)
    {
        return getBitmapFromMemoryCache(key);
    }
    
    public void addBitmap2MemoryCache(String key, Bitmap value)
    {
        if (null == getBitmapFromMemoryCache(key))
        {
            Log.d(LOG_TAG, "addBitmap2MemoryCache");
            memoryCache.put(key, value);
        }
    }
    
    public Bitmap getBitmapFromMemoryCache(String key)
    {
        Bitmap bitmap = memoryCache.get(key);
        if (null != bitmap)
        {
            Log.d(LOG_TAG, "memory cache hit");
        }
        return bitmap;
    }
    
    public void clearCache()
    {
        memoryCache.evictAll();
    }
}




Java Source Code List

com.dreamtale.pintrestlike.activity.BluetoothDeviceListActivity.java
com.dreamtale.pintrestlike.activity.DetailActivity.java
com.dreamtale.pintrestlike.activity.MainActivity.java
com.dreamtale.pintrestlike.activity.WelcomeActivity.java
com.dreamtale.pintrestlike.data.ImageAdapter.java
com.dreamtale.pintrestlike.data.ImageInfoProvider.java
com.dreamtale.pintrestlike.data.ImageInfo.java
com.dreamtale.pintrestlike.fragment.ImageDetailFragment.java
com.dreamtale.pintrestlike.parser.ImageParser.java
com.dreamtale.pintrestlike.share.BluetoothService.java
com.dreamtale.pintrestlike.utils.CacheManager.java
com.dreamtale.pintrestlike.utils.ImageDownloader.java
com.dreamtale.pintrestlike.utils.ImageUtils.java
com.dreamtale.pintrestlike.utils.IntentConstant.java
com.dreamtale.pintrestlike.utils.UIConfig.java
com.dreamtale.pintrestlike.widget.BluetoothDeviceListDialog.java
com.dreamtale.pintrestlike.widget.ItemView.java
com.dreamtale.pintrestlike.widget.PinterestScrollView.java
com.dreamtale.pintrestlike.widget.PintrestGridView.java