CacheStorageUsageInfo.java revision 2b3ee0ea07246b859a5b75d8a6102a7cce7ec838
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 17package com.android.gallery3d.ui; 18 19import android.content.Context; 20import android.os.StatFs; 21 22import com.android.gallery3d.app.GalleryActivity; 23import com.android.gallery3d.util.ThreadPool.JobContext; 24 25import java.io.File; 26 27public class CacheStorageUsageInfo { 28 private static final String TAG = "CacheStorageUsageInfo"; 29 30 // number of bytes the storage has. 31 private long mTotalBytes; 32 33 // number of bytes already used. 34 private long mUsedBytes; 35 36 // number of bytes used for the cache (should be less then usedBytes). 37 private long mUsedCacheBytes; 38 39 // number of bytes used for the cache if all pending downloads (and removals) are completed. 40 private long mTargetCacheBytes; 41 42 private GalleryActivity mActivity; 43 private Context mContext; 44 private long mUserChangeDelta; 45 46 public CacheStorageUsageInfo(GalleryActivity activity) { 47 mActivity = activity; 48 mContext = activity.getAndroidContext(); 49 } 50 51 public void increaseTargetCacheSize(long delta) { 52 mUserChangeDelta += delta; 53 } 54 55 public void loadStorageInfo(JobContext jc) { 56 File cacheDir = mContext.getExternalCacheDir(); 57 if (cacheDir == null) { 58 cacheDir = mContext.getCacheDir(); 59 } 60 61 String path = cacheDir.getAbsolutePath(); 62 StatFs stat = new StatFs(path); 63 long blockSize = stat.getBlockSize(); 64 long availableBlocks = stat.getAvailableBlocks(); 65 long totalBlocks = stat.getBlockCount(); 66 67 mTotalBytes = blockSize * totalBlocks; 68 mUsedBytes = blockSize * (totalBlocks - availableBlocks); 69 mUsedCacheBytes = mActivity.getDataManager().getTotalUsedCacheSize(); 70 mTargetCacheBytes = mActivity.getDataManager().getTotalTargetCacheSize(); 71 } 72 73 public long getTotalBytes() { 74 return mTotalBytes; 75 } 76 77 public long getExpectedUsedBytes() { 78 return mUsedBytes - mUsedCacheBytes + mTargetCacheBytes + mUserChangeDelta; 79 } 80 81 public long getUsedBytes() { 82 // Should it be usedBytes - usedCacheBytes + targetCacheBytes ? 83 return mUsedBytes; 84 } 85 86 public long getFreeBytes() { 87 return mTotalBytes - mUsedBytes; 88 } 89} 90