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