1 /*
2  * Copyright (C) 2018 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.deviceinfo;
18 
19 import android.content.Context;
20 import android.os.UserManager;
21 import android.os.storage.StorageManager;
22 import android.text.format.Formatter;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.Utils;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settings.dashboard.profileselector.ProfileSelectFragment.ProfileType;
31 import com.android.settings.deviceinfo.storage.StorageCacheHelper;
32 import com.android.settingslib.deviceinfo.PrivateStorageInfo;
33 import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
34 import com.android.settingslib.utils.ThreadUtils;
35 
36 import java.text.NumberFormat;
37 import java.util.concurrent.Future;
38 
39 public class TopLevelStoragePreferenceController extends BasePreferenceController {
40 
41     private final StorageManager mStorageManager;
42     private final StorageManagerVolumeProvider mStorageManagerVolumeProvider;
43 
TopLevelStoragePreferenceController(Context context, String preferenceKey)44     public TopLevelStoragePreferenceController(Context context, String preferenceKey) {
45         super(context, preferenceKey);
46         mStorageManager = mContext.getSystemService(StorageManager.class);
47         mStorageManagerVolumeProvider = new StorageManagerVolumeProvider(mStorageManager);
48     }
49 
50     @Override
getAvailabilityStatus()51     public int getAvailabilityStatus() {
52         return AVAILABLE;
53     }
54 
55     @Override
refreshSummary(Preference preference)56     protected void refreshSummary(Preference preference) {
57         if (preference == null) {
58             return;
59         }
60 
61         refreshSummaryThread(preference);
62     }
63 
64     @VisibleForTesting
refreshSummaryThread(Preference preference)65     protected Future refreshSummaryThread(Preference preference) {
66         int userId = Utils.getCurrentUserIdOfType(
67                 mContext.getSystemService(UserManager.class), ProfileType.PERSONAL);
68         final StorageCacheHelper storageCacheHelper = new StorageCacheHelper(mContext, userId);
69         long cachedUsedSize = storageCacheHelper.retrieveUsedSize();
70         long cachedTotalSize = storageCacheHelper.retrieveCachedSize().totalSize;
71         if (cachedUsedSize != 0 && cachedTotalSize != 0) {
72             preference.setSummary(getSummary(cachedUsedSize, cachedTotalSize));
73         }
74 
75         return ThreadUtils.postOnBackgroundThread(() -> {
76             final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(
77                     getStorageManagerVolumeProvider());
78 
79             long usedBytes = info.totalBytes - info.freeBytes;
80             storageCacheHelper.cacheUsedSize(usedBytes);
81             ThreadUtils.postOnMainThread(() -> {
82                 preference.setSummary(
83                         getSummary(usedBytes, info.totalBytes));
84             });
85         });
86     }
87 
88     @VisibleForTesting
getStorageManagerVolumeProvider()89     protected StorageManagerVolumeProvider getStorageManagerVolumeProvider() {
90         return mStorageManagerVolumeProvider;
91     }
92 
getSummary(long usedBytes, long totalBytes)93     private String getSummary(long usedBytes, long totalBytes) {
94         NumberFormat percentageFormat = NumberFormat.getPercentInstance();
95 
96         return mContext.getString(R.string.storage_summary,
97                 totalBytes == 0L ? "0" : percentageFormat.format(((double) usedBytes) / totalBytes),
98                 Formatter.formatFileSize(mContext, totalBytes - usedBytes));
99     }
100 }
101