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.annotation.VisibleForTesting; 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settingslib.deviceinfo.PrivateStorageInfo; 29 import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider; 30 import com.android.settingslib.utils.ThreadUtils; 31 32 import java.text.NumberFormat; 33 import java.util.concurrent.Future; 34 35 public class TopLevelStoragePreferenceController extends BasePreferenceController { 36 37 private final StorageManager mStorageManager; 38 private final StorageManagerVolumeProvider mStorageManagerVolumeProvider; 39 TopLevelStoragePreferenceController(Context context, String preferenceKey)40 public TopLevelStoragePreferenceController(Context context, String preferenceKey) { 41 super(context, preferenceKey); 42 mStorageManager = mContext.getSystemService(StorageManager.class); 43 mStorageManagerVolumeProvider = new StorageManagerVolumeProvider(mStorageManager); 44 } 45 46 @Override getAvailabilityStatus()47 public int getAvailabilityStatus() { 48 return AVAILABLE; 49 } 50 51 @Override refreshSummary(Preference preference)52 protected void refreshSummary(Preference preference) { 53 if (preference == null) { 54 return; 55 } 56 57 refreshSummaryThread(preference); 58 } 59 60 @VisibleForTesting refreshSummaryThread(Preference preference)61 protected Future refreshSummaryThread(Preference preference) { 62 return ThreadUtils.postOnBackgroundThread(() -> { 63 final NumberFormat percentageFormat = NumberFormat.getPercentInstance(); 64 final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo( 65 getStorageManagerVolumeProvider()); 66 final double privateUsedBytes = info.totalBytes - info.freeBytes; 67 68 ThreadUtils.postOnMainThread(() -> { 69 preference.setSummary(mContext.getString(R.string.storage_summary, 70 percentageFormat.format(privateUsedBytes / info.totalBytes), 71 Formatter.formatFileSize(mContext, info.freeBytes))); 72 }); 73 }); 74 } 75 76 77 @VisibleForTesting getStorageManagerVolumeProvider()78 protected StorageManagerVolumeProvider getStorageManagerVolumeProvider() { 79 return mStorageManagerVolumeProvider; 80 } 81 } 82