1 /* 2 * Copyright (C) 2017 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.settings.applications; 18 19 import android.content.Context; 20 import android.support.annotation.Nullable; 21 import android.support.annotation.StringRes; 22 import android.support.v7.preference.Preference; 23 import android.text.format.Formatter; 24 25 import com.android.internal.util.Preconditions; 26 import com.android.settingslib.applications.StorageStatsSource; 27 28 /** 29 * Handles setting the sizes for the app info screen. 30 */ 31 public class AppStorageSizesController { 32 private final Preference mTotalSize; 33 private final Preference mAppSize; 34 private final Preference mDataSize; 35 private final Preference mCacheSize; 36 private final @StringRes int mComputing; 37 private final @StringRes int mError; 38 39 @Nullable 40 private StorageStatsSource.AppStorageStats mLastResult; 41 private boolean mLastResultFailed; 42 private boolean mCachedCleared; 43 private boolean mDataCleared; 44 private long mLastCodeSize = -1; 45 private long mLastDataSize = -1; 46 private long mLastCacheSize = -1; 47 private long mLastTotalSize = -1; 48 AppStorageSizesController(Preference total, Preference app, Preference data, Preference cache, @StringRes int computing, @StringRes int error)49 private AppStorageSizesController(Preference total, Preference app, 50 Preference data, Preference cache, @StringRes int computing, @StringRes int error) { 51 mTotalSize = total; 52 mAppSize = app; 53 mDataSize = data; 54 mCacheSize = cache; 55 mComputing = computing; 56 mError = error; 57 } 58 59 /** 60 * Updates the UI using storage stats. 61 * @param context Context to use to fetch strings 62 */ updateUi(Context context)63 public void updateUi(Context context) { 64 if (mLastResult == null) { 65 int errorRes = mLastResultFailed ? mError : mComputing; 66 67 mAppSize.setSummary(errorRes); 68 mDataSize.setSummary(errorRes); 69 mCacheSize.setSummary(errorRes); 70 mTotalSize.setSummary(errorRes); 71 } else { 72 long codeSize = mLastResult.getCodeBytes(); 73 long dataSize = 74 mDataCleared ? 0 : mLastResult.getDataBytes() - mLastResult.getCacheBytes(); 75 if (mLastCodeSize != codeSize) { 76 mLastCodeSize = codeSize; 77 mAppSize.setSummary(getSizeStr(context, codeSize)); 78 } 79 if (mLastDataSize != dataSize) { 80 mLastDataSize = dataSize; 81 mDataSize.setSummary(getSizeStr(context, dataSize)); 82 } 83 long cacheSize = (mDataCleared || mCachedCleared) ? 0 : mLastResult.getCacheBytes(); 84 if (mLastCacheSize != cacheSize) { 85 mLastCacheSize = cacheSize; 86 mCacheSize.setSummary(getSizeStr(context, cacheSize)); 87 } 88 89 long totalSize = codeSize + dataSize + cacheSize; 90 if (mLastTotalSize != totalSize) { 91 mLastTotalSize = totalSize; 92 mTotalSize.setSummary(getSizeStr(context, totalSize)); 93 } 94 } 95 } 96 97 /** 98 * Sets a result for the controller to use to update the UI. 99 * @param result A result for the UI. If null, count as a failed calculation. 100 */ setResult(StorageStatsSource.AppStorageStats result)101 public void setResult(StorageStatsSource.AppStorageStats result) { 102 mLastResult = result; 103 mLastResultFailed = result == null; 104 } 105 106 /** 107 * Sets if we have cleared the cache and should zero the cache bytes. 108 * When the cache is cleared, the cache directories are recreated. These directories have 109 * some size, but are empty. We zero this out to best match user expectations. 110 */ setCacheCleared(boolean isCleared)111 public void setCacheCleared(boolean isCleared) { 112 mCachedCleared = isCleared; 113 } 114 115 /** 116 * Sets if we have cleared data and should zero the data bytes. 117 * When the data is cleared, the directory are recreated. Directories have some size, but are 118 * empty. We zero this out to best match user expectations. 119 */ setDataCleared(boolean isCleared)120 public void setDataCleared(boolean isCleared) { 121 mDataCleared = isCleared; 122 } 123 124 /** 125 * Returns the last result calculated, if it exists. If it does not, returns null. 126 */ getLastResult()127 public StorageStatsSource.AppStorageStats getLastResult() { 128 return mLastResult; 129 } 130 getSizeStr(Context context, long size)131 private String getSizeStr(Context context, long size) { 132 return Formatter.formatFileSize(context, size); 133 } 134 135 public static class Builder { 136 private Preference mTotalSize; 137 private Preference mAppSize; 138 private Preference mDataSize; 139 private Preference mCacheSize; 140 private @StringRes int mComputing; 141 private @StringRes int mError; 142 setAppSizePreference(Preference preference)143 public Builder setAppSizePreference(Preference preference) { 144 mAppSize = preference; 145 return this; 146 } 147 setDataSizePreference(Preference preference)148 public Builder setDataSizePreference(Preference preference) { 149 mDataSize = preference; 150 return this; 151 } 152 setCacheSizePreference(Preference preference)153 public Builder setCacheSizePreference(Preference preference) { 154 mCacheSize = preference; 155 return this; 156 } 157 setTotalSizePreference(Preference preference)158 public Builder setTotalSizePreference(Preference preference) { 159 mTotalSize = preference; 160 return this; 161 } 162 setComputingString(@tringRes int sequence)163 public Builder setComputingString(@StringRes int sequence) { 164 mComputing = sequence; 165 return this; 166 } 167 setErrorString(@tringRes int sequence)168 public Builder setErrorString(@StringRes int sequence) { 169 mError = sequence; 170 return this; 171 } 172 build()173 public AppStorageSizesController build() { 174 return new AppStorageSizesController( 175 Preconditions.checkNotNull(mTotalSize), 176 Preconditions.checkNotNull(mAppSize), 177 Preconditions.checkNotNull(mDataSize), 178 Preconditions.checkNotNull(mCacheSize), 179 mComputing, 180 mError); 181 } 182 } 183 } 184