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.storage; 18 19 import android.app.ActivityManager; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.os.SystemProperties; 23 import android.provider.Settings; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.fragment.app.FragmentManager; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.deletionhelper.ActivationWarningFragment; 32 import com.android.settings.overlay.FeatureFactory; 33 import com.android.settings.widget.GenericSwitchController; 34 import com.android.settings.widget.PrimarySwitchPreference; 35 import com.android.settings.widget.SwitchWidgetController; 36 import com.android.settingslib.Utils; 37 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 38 import com.android.settingslib.core.lifecycle.LifecycleObserver; 39 import com.android.settingslib.core.lifecycle.events.OnResume; 40 41 public class AutomaticStorageManagementSwitchPreferenceController extends 42 BasePreferenceController implements LifecycleObserver, OnResume, 43 SwitchWidgetController.OnSwitchChangeListener { 44 @VisibleForTesting 45 static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY = "ro.storage_manager.enabled"; 46 private final MetricsFeatureProvider mMetricsFeatureProvider; 47 private PrimarySwitchPreference mSwitch; 48 private GenericSwitchController mSwitchController; 49 private FragmentManager mFragmentManager; 50 AutomaticStorageManagementSwitchPreferenceController(Context context, String key)51 public AutomaticStorageManagementSwitchPreferenceController(Context context, String key) { 52 super(context, key); 53 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 54 } 55 setFragmentManager( FragmentManager fragmentManager)56 public AutomaticStorageManagementSwitchPreferenceController setFragmentManager( 57 FragmentManager fragmentManager) { 58 mFragmentManager = fragmentManager; 59 return this; 60 } 61 62 @Override displayPreference(PreferenceScreen screen)63 public void displayPreference(PreferenceScreen screen) { 64 super.displayPreference(screen); 65 mSwitch = screen.findPreference(getPreferenceKey()); 66 } 67 68 @Override getAvailabilityStatus()69 public int getAvailabilityStatus() { 70 if (!mContext.getResources().getBoolean(R.bool.config_show_smart_storage_toggle)) { 71 return UNSUPPORTED_ON_DEVICE; 72 } 73 return !ActivityManager.isLowRamDeviceStatic() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 74 } 75 76 @Override onResume()77 public void onResume() { 78 if (!isAvailable()) { 79 return; 80 } 81 mSwitch.setChecked(Utils.isStorageManagerEnabled(mContext)); 82 83 if (mSwitch != null) { 84 mSwitchController = new GenericSwitchController(mSwitch); 85 mSwitchController.setListener(this); 86 mSwitchController.startListening(); 87 } 88 } 89 90 @Override onSwitchToggled(boolean isChecked)91 public boolean onSwitchToggled(boolean isChecked) { 92 mMetricsFeatureProvider.action(mContext, 93 SettingsEnums.ACTION_TOGGLE_STORAGE_MANAGER, isChecked); 94 Settings.Secure.putInt(mContext.getContentResolver(), 95 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 96 isChecked ? 1 : 0); 97 98 final boolean storageManagerEnabledByDefault = 99 SystemProperties.getBoolean(STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, false); 100 final boolean storageManagerDisabledByPolicy = 101 Settings.Secure.getInt( 102 mContext.getContentResolver(), 103 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_TURNED_OFF_BY_POLICY, 104 0) 105 != 0; 106 // Show warning if it is disabled by default and turning it on or if it was disabled by 107 // policy and we're turning it on. 108 if (isChecked && (!storageManagerEnabledByDefault || storageManagerDisabledByPolicy)) { 109 ActivationWarningFragment fragment = ActivationWarningFragment.newInstance(); 110 fragment.show(mFragmentManager, ActivationWarningFragment.TAG); 111 } 112 113 return true; 114 } 115 } 116