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