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.deviceinfo;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.os.storage.StorageManager;
25 import android.os.storage.VolumeInfo;
26 import android.util.SparseArray;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.loader.app.LoaderManager;
30 import androidx.loader.content.Loader;
31 
32 import com.android.settings.R;
33 import com.android.settings.Utils;
34 import com.android.settings.dashboard.DashboardFragment;
35 import com.android.settings.deviceinfo.storage.StorageAsyncLoader;
36 import com.android.settings.deviceinfo.storage.StorageAsyncLoader.AppsStorageResult;
37 import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
38 import com.android.settingslib.applications.StorageStatsSource;
39 import com.android.settingslib.core.AbstractPreferenceController;
40 import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
41 
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 /**
46  * StorageProfileFragment is a fragment which shows the storage results for a profile of the
47  * primary user.
48  */
49 public class StorageProfileFragment extends DashboardFragment
50         implements LoaderManager.LoaderCallbacks<SparseArray<AppsStorageResult>> {
51     private static final String TAG = "StorageProfileFragment";
52     public static final String USER_ID_EXTRA = "userId";
53     private static final int APPS_JOB_ID = 0;
54 
55     private VolumeInfo mVolume;
56     private int mUserId;
57     private StorageItemPreferenceController mPreferenceController;
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62         final Bundle args = getArguments();
63 
64         // Initialize the storage sizes that we can quickly calc.
65         final Context context = getActivity();
66         final StorageManager sm = context.getSystemService(StorageManager.class);
67         mVolume = Utils.maybeInitializeVolume(sm, args);
68         if (mVolume == null) {
69             getActivity().finish();
70             return;
71         }
72 
73         mPreferenceController.setVolume(mVolume);
74         mUserId = args.getInt(USER_ID_EXTRA, UserHandle.myUserId());
75         mPreferenceController.setUserId(UserHandle.of(mUserId));
76     }
77 
78     @Override
onResume()79     public void onResume() {
80         super.onResume();
81         getLoaderManager().initLoader(APPS_JOB_ID, Bundle.EMPTY, this);
82     }
83 
84     @Override
getMetricsCategory()85     public int getMetricsCategory() {
86         return SettingsEnums.SETTINGS_STORAGE_PROFILE;
87     }
88 
89     @Override
getLogTag()90     protected String getLogTag() {
91         return TAG;
92     }
93 
94     @Override
getPreferenceScreenResId()95     protected int getPreferenceScreenResId() {
96         return R.xml.storage_profile_fragment;
97     }
98 
99     @Override
createPreferenceControllers(Context context)100     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
101         final List<AbstractPreferenceController> controllers = new ArrayList<>();
102         final StorageManager sm = context.getSystemService(StorageManager.class);
103         mPreferenceController =
104                 new StorageItemPreferenceController(
105                         context,
106                         this,
107                         mVolume,
108                         new StorageManagerVolumeProvider(sm),
109                         /* isWorkProfile */ true);
110         controllers.add(mPreferenceController);
111         return controllers;
112     }
113 
114     @Override
onCreateLoader(int id, Bundle args)115     public Loader<SparseArray<AppsStorageResult>> onCreateLoader(int id, Bundle args) {
116         final Context context = getContext();
117         return new StorageAsyncLoader(context,
118                 context.getSystemService(UserManager.class),
119                 mVolume.fsUuid,
120                 new StorageStatsSource(context),
121                 context.getPackageManager());
122     }
123 
124     @Override
onLoadFinished(Loader<SparseArray<AppsStorageResult>> loader, SparseArray<AppsStorageResult> result)125     public void onLoadFinished(Loader<SparseArray<AppsStorageResult>> loader,
126             SparseArray<AppsStorageResult> result) {
127         mPreferenceController.onLoadFinished(result, mUserId);
128     }
129 
130     @Override
onLoaderReset(Loader<SparseArray<AppsStorageResult>> loader)131     public void onLoaderReset(Loader<SparseArray<AppsStorageResult>> loader) {
132     }
133 
134     @VisibleForTesting
setPreferenceController(StorageItemPreferenceController controller)135     void setPreferenceController(StorageItemPreferenceController controller) {
136         mPreferenceController = controller;
137     }
138 }
139