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.settings.storage; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.os.Bundle; 22 import android.os.UserHandle; 23 import android.view.View; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.annotation.XmlRes; 28 import androidx.loader.app.LoaderManager; 29 30 import com.android.car.settings.R; 31 import com.android.car.settings.common.Logger; 32 import com.android.car.settings.common.SettingsFragment; 33 import com.android.settingslib.applications.ApplicationsState; 34 import com.android.settingslib.applications.StorageStatsSource; 35 36 import java.util.Arrays; 37 import java.util.List; 38 39 /** 40 * Fragment to display the applications storage information. 41 */ 42 public class AppStorageSettingsDetailsFragment extends SettingsFragment { 43 private static final Logger LOG = new Logger(AppStorageSettingsDetailsFragment.class); 44 45 public static final String EXTRA_PACKAGE_NAME = "extra_package_name"; 46 47 // Package information 48 protected PackageManager mPackageManager; 49 private String mPackageName; 50 51 // Application state info 52 private ApplicationsState.AppEntry mAppEntry; 53 private ApplicationsState mAppState; 54 private AppsStorageStatsManager mAppsStorageStatsManager; 55 56 // User info 57 private int mUserId; 58 59 /** Creates an instance of this fragment, passing packageName as an argument. */ getInstance(String packageName)60 public static AppStorageSettingsDetailsFragment getInstance(String packageName) { 61 AppStorageSettingsDetailsFragment applicationDetailFragment = 62 new AppStorageSettingsDetailsFragment(); 63 Bundle bundle = new Bundle(); 64 bundle.putString(EXTRA_PACKAGE_NAME, packageName); 65 applicationDetailFragment.setArguments(bundle); 66 return applicationDetailFragment; 67 } 68 69 @Override 70 @XmlRes getPreferenceScreenResId()71 protected int getPreferenceScreenResId() { 72 return R.xml.app_storage_settings_details_fragment; 73 } 74 75 @Override onAttach(Context context)76 public void onAttach(Context context) { 77 super.onAttach(context); 78 mUserId = UserHandle.myUserId(); 79 mPackageName = getArguments().getString(EXTRA_PACKAGE_NAME); 80 mAppState = ApplicationsState.getInstance(requireActivity().getApplication()); 81 mAppEntry = mAppState.getEntry(mPackageName, mUserId); 82 StorageStatsSource storageStatsSource = new StorageStatsSource(context); 83 StorageStatsSource.AppStorageStats stats = null; 84 mPackageManager = context.getPackageManager(); 85 try { 86 stats = storageStatsSource.getStatsForPackage(/* volumeUuid= */ null, mPackageName, 87 UserHandle.of(mUserId)); 88 } catch (Exception e) { 89 // This may happen if the package was removed during our calculation. 90 LOG.w("App unexpectedly not found", e); 91 } 92 mAppsStorageStatsManager = new AppsStorageStatsManager(context); 93 use(StorageApplicationPreferenceController.class, 94 R.string.pk_storage_application_details) 95 .setAppEntry(mAppEntry) 96 .setAppState(mAppState); 97 use(StorageApplicationActionButtonsPreferenceController.class, 98 R.string.pk_storage_application_action_buttons) 99 .setAppEntry(mAppEntry) 100 .setPackageName(mPackageName) 101 .setAppsStorageStatsManager(mAppsStorageStatsManager) 102 .setLoaderManager(LoaderManager.getInstance(this)); 103 104 List<? extends StorageSizeBasePreferenceController> preferenceControllers = Arrays.asList( 105 use(StorageApplicationSizePreferenceController.class, 106 R.string.pk_storage_application_size), 107 use(StorageApplicationTotalSizePreferenceController.class, 108 R.string.pk_storage_application_total_size), 109 use(StorageApplicationUserDataPreferenceController.class, 110 R.string.pk_storage_application_data_size), 111 use(StorageApplicationCacheSizePreferenceController.class, 112 R.string.pk_storage_application_cache_size) 113 ); 114 115 for (StorageSizeBasePreferenceController pc : preferenceControllers) { 116 pc.setAppsStorageStatsManager(mAppsStorageStatsManager); 117 pc.setAppStorageStats(stats); 118 } 119 } 120 121 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)122 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 123 super.onViewCreated(view, savedInstanceState); 124 enableRotaryScroll(); 125 } 126 } 127