com.inter.trade.view.slideplayview.AbImageCache.java Source code

Java tutorial

Introduction

Here is the source code for com.inter.trade.view.slideplayview.AbImageCache.java

Source

/*
 * Copyright (C) 2013 www.418log.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.inter.trade.view.slideplayview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import com.inter.trade.view.slideplayview.util.AbMd5;
import com.inter.trade.view.slideplayview.util.AbStrUtil;

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

// TODO: Auto-generated Javadoc
/**
 * ??.
 *
 * @author zhaoqp
 * @date2013-5-23 ?10:10:53
 * @version v1.0
 */

public class AbImageCache {

    /** The tag. */
    private static String TAG = "AbImageCache";

    /** The Constant D. */
    private static final boolean D = AbAppData.DEBUG;

    /** ?8MB. */
    public static int cacheSize = 8 * 1024 * 1024;

    /** ?,LruCache. */
    private static final LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
            if (D)
                Log.d(TAG, "LruCache:" + key);
        }

    };

    /***/
    private static final HashMap<String, Runnable> runRunnableCache = new HashMap<String, Runnable>();

    /***/
    private static final List<HashMap<String, Runnable>> waitRunnableList = new ArrayList<HashMap<String, Runnable>>();

    /**?*/
    public static final ReentrantLock lock = new ReentrantLock();

    /**
     * ???Bitmap.
     *
     * @param key the key
     * @return the bitmap from mem cache
     */
    public static Bitmap getBitmapFromCache(String key) {
        return bitmapCache.get(key);
    }

    /**
     * ??.
     *
     * @param key  urlkey
     * @param bitmap the bitmap
     */
    public static void addBitmapToCache(String key, Bitmap bitmap) {
        try {
            if (D)
                Log.d(TAG, "?:" + key);
            lock.lock();
            if (AbStrUtil.isEmpty(key)) {
                return;
            }

            if (getBitmapFromCache(key) == null && bitmap != null) {
                bitmapCache.put(key, bitmap);
                if (D)
                    Log.d(TAG, ":" + key + "," + bitmap);
                if (D)
                    Log.d(TAG, "??:" + key + "," + getBitmapFromCache(key));
            }
            //
            removeRunRunnableFromCache(key);
            if (D)
                Log.d(TAG, ":" + waitRunnableList.size());
            //
            removeWaitRunnableFromCache(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * ??.
     *
     * @param key  urlkey
     */
    public static void removeBitmapFromCache(String key) {
        try {
            lock.lock();
            if (getBitmapFromCache(key) != null) {
                bitmapCache.remove(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * ??Bitmap.
     */
    public static void removeAllBitmapFromCache() {
        bitmapCache.evictAll();
    }

    /**
      * ?urlkey,key+???.
      * @param url ?.
      * @param width .
      * @param height .
      * @param type ?.
      */
    public static String getCacheKey(String url, int width, int height, int type) {
        return AbMd5.MD5(new StringBuilder(url.length() + 12).append("#W").append(width).append("#H").append(height)
                .append("#T").append(type).append(url).toString());
    }

    /**
    * ???.
    *
    * @param key the key
    * @return the runnable
    */
    public static Runnable getRunRunnableFromCache(String key) {
        return runRunnableCache.get(key);
    }

    /**
    * ??.
    *
    * @param key  urlkey
    * @param runnable the runnable
    */
    public static void addToRunRunnableCache(String key, Runnable runnable) {
        try {
            lock.lock();
            if (AbStrUtil.isEmpty(key) || runnable == null) {
                return;
            }
            if (getRunRunnableFromCache(key) == null) {
                runRunnableCache.put(key, runnable);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * ??.
     *
     * @param key  urlkey
     */
    public static void removeRunRunnableFromCache(String key) {
        if (getRunRunnableFromCache(key) != null) {
            runRunnableCache.remove(key);
        }
    }

    /**
     * ???.
     *
     * @param key the key
     * @return the runnable
     */
    public static Runnable getWaitRunnableFromCache(String key) {
        return runRunnableCache.get(key);
    }

    /**
    * ??.
    *
    * @param key  urlkey
    * @param runnable the runnable
    */
    public static void addToWaitRunnableCache(String key, Runnable runnable) {
        try {
            lock.lock();
            if (AbStrUtil.isEmpty(key) || runnable == null) {
                return;
            }
            HashMap<String, Runnable> runnableMap = new HashMap<String, Runnable>();
            runnableMap.put(key, runnable);
            waitRunnableList.add(runnableMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * ??.
     *
     * @param key  urlkey
     */
    public static void removeWaitRunnableFromCache(String key) {
        try {
            lock.lock();
            for (int i = 0; i < waitRunnableList.size(); i++) {
                HashMap<String, Runnable> runnableMap = waitRunnableList.get(i);
                Runnable runnable = runnableMap.get(key);
                if (runnable != null) {
                    if (D)
                        Log.d(TAG, ":" + runnable);
                    synchronized (runnable) {
                        runnable.notify();
                    }
                    waitRunnableList.remove(runnableMap);
                    i--;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}