1 /* 2 * Copyright (C) 2022 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.storage; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.graphics.drawable.Drawable; 23 import android.os.UserHandle; 24 import android.os.storage.StorageManager; 25 import android.text.TextUtils; 26 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.overlay.FeatureFactory; 32 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 33 34 /** 35 * Preference controller to control the storage management preference. 36 */ 37 public class ManageStoragePreferenceController extends BasePreferenceController { 38 39 private int mUserId; 40 private Drawable mManageStorageDrawable; 41 ManageStoragePreferenceController(Context context, String preferenceKey)42 public ManageStoragePreferenceController(Context context, String preferenceKey) { 43 super(context, preferenceKey); 44 } 45 46 /** 47 * Set user ID. 48 */ setUserId(int userId)49 public void setUserId(int userId) { 50 mUserId = userId; 51 mManageStorageDrawable = StorageUtils.getManageStorageIcon(mContext, userId); 52 } 53 54 @Override getAvailabilityStatus()55 public int getAvailabilityStatus() { 56 return mManageStorageDrawable == null ? CONDITIONALLY_UNAVAILABLE : AVAILABLE; 57 } 58 59 @Override displayPreference(PreferenceScreen screen)60 public void displayPreference(PreferenceScreen screen) { 61 super.displayPreference(screen); 62 63 Preference preference = screen.findPreference(getPreferenceKey()); 64 preference.setIcon(mManageStorageDrawable); 65 } 66 67 @Override handlePreferenceTreeClick(Preference preference)68 public boolean handlePreferenceTreeClick(Preference preference) { 69 if (!TextUtils.equals(getPreferenceKey(), preference.getKey())) { 70 return super.handlePreferenceTreeClick(preference); 71 } 72 73 final MetricsFeatureProvider metricsFeatureProvider = 74 FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 75 metricsFeatureProvider.action(mContext, SettingsEnums.STORAGE_FREE_UP_SPACE_NOW); 76 77 final Intent intent = new Intent(StorageManager.ACTION_MANAGE_STORAGE); 78 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 79 mContext.startActivityAsUser(intent, new UserHandle(mUserId)); 80 return true; 81 } 82 } 83