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.dashboard.DashboardFragment; 36 import com.android.settings.deviceinfo.storage.StorageAsyncLoader; 37 import com.android.settings.deviceinfo.storage.StorageAsyncLoader.AppsStorageResult; 38 import com.android.settings.deviceinfo.storage.StorageItemPreferenceController; 39 import com.android.settingslib.applications.StorageStatsSource; 40 import com.android.settingslib.core.AbstractPreferenceController; 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 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 80 public void onResume() { 81 super.onResume(); 82 getLoaderManager().initLoader(APPS_JOB_ID, Bundle.EMPTY, this); 83 } 84 85 @Override 86 public int getMetricsCategory() { 87 return MetricsProto.MetricsEvent.SETTINGS_STORAGE_PROFILE; 88 } 89 90 @Override 91 protected String getLogTag() { 92 return TAG; 93 } 94 95 @Override 96 protected int getPreferenceScreenResId() { 97 return R.xml.storage_profile_fragment; 98 } 99 100 @Override 101 protected List<AbstractPreferenceController> getPreferenceControllers(Context context) { 102 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 103 final StorageManager sm = context.getSystemService(StorageManager.class); 104 mPreferenceController = 105 new StorageItemPreferenceController( 106 context, 107 this, 108 mVolume, 109 new StorageManagerVolumeProvider(sm), 110 /* isWorkProfile */ true); 111 controllers.add(mPreferenceController); 112 return controllers; 113 } 114 115 @Override 116 public Loader<SparseArray<AppsStorageResult>> onCreateLoader(int id, Bundle args) { 117 Context context = getContext(); 118 return new StorageAsyncLoader(context, 119 new UserManagerWrapperImpl(context.getSystemService(UserManager.class)), 120 mVolume.fsUuid, 121 new StorageStatsSource(context), 122 new PackageManagerWrapperImpl(context.getPackageManager())); 123 } 124 125 @Override 126 public void onLoadFinished(Loader<SparseArray<AppsStorageResult>> loader, 127 SparseArray<AppsStorageResult> result) { 128 mPreferenceController.onLoadFinished(result, mUserId); 129 } 130 131 @Override 132 public void onLoaderReset(Loader<SparseArray<AppsStorageResult>> loader) { 133 } 134 135 @VisibleForTesting 136 void setPreferenceController(StorageItemPreferenceController controller) { 137 mPreferenceController = controller; 138 } 139 } 140