• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.storage.StorageManager;
21 import android.text.format.Formatter;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settingslib.deviceinfo.PrivateStorageInfo;
28 import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
29 import com.android.settingslib.utils.ThreadUtils;
30 
31 import java.text.NumberFormat;
32 
33 public class TopLevelStoragePreferenceController extends BasePreferenceController {
34 
35     private final StorageManager mStorageManager;
36     private final StorageManagerVolumeProvider mStorageManagerVolumeProvider;
37 
TopLevelStoragePreferenceController(Context context, String preferenceKey)38     public TopLevelStoragePreferenceController(Context context, String preferenceKey) {
39         super(context, preferenceKey);
40         mStorageManager = mContext.getSystemService(StorageManager.class);
41         mStorageManagerVolumeProvider = new StorageManagerVolumeProvider(mStorageManager);
42     }
43 
44     @Override
getAvailabilityStatus()45     public int getAvailabilityStatus() {
46         return AVAILABLE_UNSEARCHABLE;
47     }
48 
49     @Override
refreshSummary(Preference preference)50     protected void refreshSummary(Preference preference) {
51         if (preference == null) {
52             return;
53         }
54 
55         ThreadUtils.postOnBackgroundThread(() -> {
56             final NumberFormat percentageFormat = NumberFormat.getPercentInstance();
57             final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(
58                     mStorageManagerVolumeProvider);
59             final double privateUsedBytes = info.totalBytes - info.freeBytes;
60 
61             ThreadUtils.postOnMainThread(() -> {
62                 preference.setSummary(mContext.getString(R.string.storage_summary,
63                         percentageFormat.format(privateUsedBytes / info.totalBytes),
64                         Formatter.formatFileSize(mContext, info.freeBytes)));
65             });
66         });
67     }
68 }
69