Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.quicksearchbox;
     18 
     19 import android.graphics.drawable.Drawable;
     20 import android.net.Uri;
     21 import android.text.TextUtils;
     22 import android.util.Log;
     23 
     24 import java.util.WeakHashMap;
     25 
     26 /**
     27  * Icon loader that caches the results of another icon loader.
     28  *
     29  */
     30 public class CachingIconLoader implements IconLoader {
     31 
     32     private static final boolean DBG = false;
     33     private static final String TAG = "QSB.CachingIconLoader";
     34 
     35     private final IconLoader mWrapped;
     36 
     37     private final WeakHashMap<String, Drawable.ConstantState> mIconCache;
     38 
     39     /**
     40      * Creates a new caching icon loader.
     41      *
     42      * @param wrapped IconLoader whose results will be cached.
     43      */
     44     public CachingIconLoader(IconLoader wrapped) {
     45         mWrapped = wrapped;
     46         mIconCache = new WeakHashMap<String, Drawable.ConstantState>();
     47     }
     48 
     49     public Drawable getIcon(String drawableId) {
     50         if (DBG) Log.d(TAG, "getIcon(" + drawableId + ")");
     51         if (TextUtils.isEmpty(drawableId) || "0".equals(drawableId)) {
     52             return null;
     53         }
     54         Drawable drawable = checkIconCache(drawableId);
     55         if (drawable != null) {
     56             return drawable;
     57         }
     58         drawable = mWrapped.getIcon(drawableId);
     59         storeInIconCache(drawableId, drawable);
     60         return drawable;
     61     }
     62 
     63     public Uri getIconUri(String drawableId) {
     64         return mWrapped.getIconUri(drawableId);
     65     }
     66 
     67     private Drawable checkIconCache(String drawableId) {
     68         Drawable.ConstantState cached = mIconCache.get(drawableId);
     69         if (cached == null) {
     70             return null;
     71         }
     72         if (DBG) Log.d(TAG, "Found icon in cache: " + drawableId);
     73         return cached.newDrawable();
     74     }
     75 
     76     private void storeInIconCache(String resourceUri, Drawable drawable) {
     77         if (drawable != null) {
     78             mIconCache.put(resourceUri, drawable.getConstantState());
     79         }
     80     }
     81 }
     82