public FileCache(Context context, Util util) { // Find the dir to save cached images if (android.os.Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED)) cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "Obliquity"); else cacheDir = context.getCacheDir(); if (!cacheDir.exists()) cacheDir.mkdirs(); mUtil = util; running = false; tempDirSize = 0; mContext = context; if ((mDirSize = mUtil.getLong(KEY, -99)) == -99) initDirSize(); // If Util has no dirSize calculated if (DEBUG) Log.d(TAG, "MAX CACHE SIZE : " + maxDirectorySize + " MB"); setupDirectoryOverflow(); if (DEBUG) Log.d(TAG, "FileCache mDirSize : " + mDirSize / 1024. / 1024. + "MB"); }
// Deletes 5MB worth of data from old files // Consider doing it in a thread. Probably cpu intensive task private void cleanCache(boolean recursiveCall) { if (DEBUG) Log.e(TAG, "Cache Full!. Clearning up 2MB of files"); if (recursiveCall) setupDirectoryOverflow(); // Rebuilt tree if this is a recursive call int delBytes = 0; File f; String filename; int i = 0; while (delBytes <= cleanupDirCache) { filename = pQueue.poll(); if (DEBUG) Log.d(TAG, "Deleting file : " + filename); if (filename == null) break; f = new File(cacheDir, filename); if (f != null) { delBytes += f.length(); f.delete(); i++; } } mDirSize -= delBytes; if (!recursiveCall && delBytes < cleanupDirCache) // If deletedBytes was less than 5MB, probably because the // priorityQueue is not built properly cleanCache(true); // Try again with rebuilding of whole priority queue if (DEBUG) Log.d(TAG, delBytes / 1024. + " kb of files deleted | No. of files deleted : " + i); }