Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2010 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.gallery3d.util;
     18 
     19 import com.android.gallery3d.common.BlobCache;
     20 
     21 import android.content.Context;
     22 import android.content.SharedPreferences;
     23 import android.preference.PreferenceManager;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.util.HashMap;
     28 
     29 public class CacheManager {
     30     private static final String TAG = "CacheManager";
     31     private static final String KEY_CACHE_UP_TO_DATE = "cache-up-to-date";
     32     private static HashMap<String, BlobCache> sCacheMap =
     33             new HashMap<String, BlobCache>();
     34     private static boolean sOldCheckDone = false;
     35 
     36     // Return null when we cannot instantiate a BlobCache, e.g.:
     37     // there is no SD card found.
     38     // This can only be called from data thread.
     39     public static BlobCache getCache(Context context, String filename,
     40             int maxEntries, int maxBytes, int version) {
     41         synchronized (sCacheMap) {
     42             if (!sOldCheckDone) {
     43                 removeOldFilesIfNecessary(context);
     44                 sOldCheckDone = true;
     45             }
     46             BlobCache cache = sCacheMap.get(filename);
     47             if (cache == null) {
     48                 File cacheDir = context.getExternalCacheDir();
     49                 String path = cacheDir.getAbsolutePath() + "/" + filename;
     50                 try {
     51                     cache = new BlobCache(path, maxEntries, maxBytes, false,
     52                             version);
     53                     sCacheMap.put(filename, cache);
     54                 } catch (IOException e) {
     55                     Log.e(TAG, "Cannot instantiate cache!", e);
     56                 }
     57             }
     58             return cache;
     59         }
     60     }
     61 
     62     // Removes the old files if the data is wiped.
     63     private static void removeOldFilesIfNecessary(Context context) {
     64         SharedPreferences pref = PreferenceManager
     65                 .getDefaultSharedPreferences(context);
     66         int n = 0;
     67         try {
     68             n = pref.getInt(KEY_CACHE_UP_TO_DATE, 0);
     69         } catch (Throwable t) {
     70             // ignore.
     71         }
     72         if (n != 0) return;
     73         pref.edit().putInt(KEY_CACHE_UP_TO_DATE, 1).commit();
     74 
     75         File cacheDir = context.getExternalCacheDir();
     76         String prefix = cacheDir.getAbsolutePath() + "/";
     77 
     78         BlobCache.deleteFiles(prefix + "imgcache");
     79         BlobCache.deleteFiles(prefix + "rev_geocoding");
     80         BlobCache.deleteFiles(prefix + "bookmark");
     81     }
     82 }
     83