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