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