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.appinfo; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.os.Bundle; 22 import android.os.UserHandle; 23 import android.text.format.Formatter; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.loader.app.LoaderManager; 27 import androidx.loader.content.Loader; 28 import androidx.preference.Preference; 29 30 import com.android.settings.R; 31 import com.android.settings.SettingsPreferenceFragment; 32 import com.android.settings.applications.AppStorageSettings; 33 import com.android.settings.applications.FetchPackageStorageAsyncLoader; 34 import com.android.settingslib.applications.ApplicationsState; 35 import com.android.settingslib.applications.StorageStatsSource; 36 import com.android.settingslib.core.lifecycle.LifecycleObserver; 37 import com.android.settingslib.core.lifecycle.events.OnPause; 38 import com.android.settingslib.core.lifecycle.events.OnResume; 39 40 public class AppStoragePreferenceController extends AppInfoPreferenceControllerBase 41 implements LoaderManager.LoaderCallbacks<StorageStatsSource.AppStorageStats>, 42 LifecycleObserver, OnResume, OnPause { 43 44 private StorageStatsSource.AppStorageStats mLastResult; 45 AppStoragePreferenceController(Context context, String key)46 public AppStoragePreferenceController(Context context, String key) { 47 super(context, key); 48 } 49 50 @Override updateState(Preference preference)51 public void updateState(Preference preference) { 52 final ApplicationsState.AppEntry entry = mParent.getAppEntry(); 53 if (entry != null && entry.info != null) { 54 final boolean isExternal = 55 (entry.info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0; 56 preference.setSummary(getStorageSummary(mLastResult, isExternal)); 57 } 58 } 59 60 @Override onResume()61 public void onResume() { 62 mParent.getLoaderManager().restartLoader(mParent.LOADER_STORAGE, Bundle.EMPTY, this); 63 } 64 65 @Override onPause()66 public void onPause() { 67 mParent.getLoaderManager().destroyLoader(mParent.LOADER_STORAGE); 68 } 69 70 @Override getDetailFragmentClass()71 protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 72 return AppStorageSettings.class; 73 } 74 75 @VisibleForTesting getStorageSummary( StorageStatsSource.AppStorageStats stats, boolean isExternal)76 CharSequence getStorageSummary( 77 StorageStatsSource.AppStorageStats stats, boolean isExternal) { 78 if (stats == null) { 79 return mContext.getText(R.string.computing_size); 80 } 81 final CharSequence storageType = mContext.getString(isExternal 82 ? R.string.storage_type_external 83 : R.string.storage_type_internal); 84 return mContext.getString(R.string.storage_summary_format, 85 Formatter.formatFileSize(mContext, stats.getTotalBytes()), 86 storageType.toString()); 87 } 88 89 @Override onCreateLoader(int id, Bundle args)90 public Loader<StorageStatsSource.AppStorageStats> onCreateLoader(int id, Bundle args) { 91 return new FetchPackageStorageAsyncLoader(mContext, new StorageStatsSource(mContext), 92 mParent.getAppEntry().info, UserHandle.of(UserHandle.myUserId())); 93 } 94 95 @Override onLoadFinished(Loader<StorageStatsSource.AppStorageStats> loader, StorageStatsSource.AppStorageStats result)96 public void onLoadFinished(Loader<StorageStatsSource.AppStorageStats> loader, 97 StorageStatsSource.AppStorageStats result) { 98 mLastResult = result; 99 updateState(mPreference); 100 } 101 102 @Override onLoaderReset(Loader<StorageStatsSource.AppStorageStats> loader)103 public void onLoaderReset(Loader<StorageStatsSource.AppStorageStats> loader) { 104 } 105 106 } 107