package net.miz_hi.smileessence.cache;

import android.graphics.Bitmap;
import android.util.LruCache;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import net.miz_hi.smileessence.Client;
import net.miz_hi.smileessence.util.LogHelper;
import net.miz_hi.smileessence.util.UiHandler;

public class ImageCache implements ImageLoader.ImageCache
{

    private static ImageCache instance;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader imageLoader;

    static
    {
        instance = new ImageCache();
    }

    private ImageCache()
    {
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        int cacheSize = maxMemory / 4;       // 最大メモリに依存
        lruCache = new LruCache<String, Bitmap>(cacheSize);
        imageLoader = new ImageLoader(Client.getRequestQueue(), this);
    }

    public static ImageCache getInstance()
    {
        if (instance == null)
        {
            instance = new ImageCache();
        }
        return instance;
    }

    public ImageLoader getImageLoader()
    {
        return imageLoader;
    }

    public static void setImageToView(String imageUrl, NetworkImageView view)
    {
        view.setImageUrl(imageUrl, getInstance().imageLoader);
    }

    public static void preCache(final String imageUrl)
    {
        new UiHandler()
        {
            @Override
            public void run()
            {
                instance.imageLoader.get(imageUrl, new ImageLoader.ImageListener()
                {
                    @Override
                    public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate)
                    {
                    }

                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                    }
                });
            }
        }.post();
    }

    public static void clearCache()
    {
        instance.lruCache.evictAll();
    }

    @Override
    public Bitmap getBitmap(String url)
    {
        return lruCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap)
    {
        LogHelper.d("image cache: " + url);
        lruCache.put(url, bitmap);
    }
}

@@ -1089,89 +1089,8 @@
  %7D%0A%0A
-    public ImageLoader getImageLoader()%0A    %7B%0A        return imageLoader;%0A    %7D%0A%0A
     
@@ -1416,24 +1416,29 @@
         
-i
+getI
 nstance
+()
 .imageLo
@@ -1932,24 +1932,29 @@
         
-i
+getI
 nstance
+()
 .lruCach

Refactor ImageCache