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