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