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.deletionhelper; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.os.SystemProperties; 22 import android.provider.Settings; 23 import android.widget.Switch; 24 25 import androidx.fragment.app.FragmentManager; 26 import androidx.preference.Preference; 27 28 import com.android.internal.util.Preconditions; 29 import com.android.settings.widget.SettingsMainSwitchBar; 30 import com.android.settingslib.Utils; 31 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 32 import com.android.settingslib.widget.OnMainSwitchChangeListener; 33 34 /** Handles the logic for flipping the storage management toggle on a {@link SwitchBar}. */ 35 public class AutomaticStorageManagerSwitchBarController 36 implements OnMainSwitchChangeListener { 37 private static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY = 38 "ro.storage_manager.enabled"; 39 40 private Context mContext; 41 private SettingsMainSwitchBar mSwitchBar; 42 private MetricsFeatureProvider mMetrics; 43 private Preference mDaysToRetainPreference; 44 private FragmentManager mFragmentManager; 45 AutomaticStorageManagerSwitchBarController( Context context, SettingsMainSwitchBar switchBar, MetricsFeatureProvider metrics, Preference daysToRetainPreference, FragmentManager fragmentManager)46 public AutomaticStorageManagerSwitchBarController( 47 Context context, 48 SettingsMainSwitchBar switchBar, 49 MetricsFeatureProvider metrics, 50 Preference daysToRetainPreference, 51 FragmentManager fragmentManager) { 52 mContext = Preconditions.checkNotNull(context); 53 mSwitchBar = Preconditions.checkNotNull(switchBar); 54 mMetrics = Preconditions.checkNotNull(metrics); 55 mDaysToRetainPreference = Preconditions.checkNotNull(daysToRetainPreference); 56 mFragmentManager = Preconditions.checkNotNull(fragmentManager); 57 58 initializeCheckedStatus(); 59 } 60 initializeCheckedStatus()61 private void initializeCheckedStatus() { 62 mSwitchBar.setChecked(Utils.isStorageManagerEnabled(mContext)); 63 mSwitchBar.addOnSwitchChangeListener(this); 64 } 65 66 @Override onSwitchChanged(Switch switchView, boolean isChecked)67 public void onSwitchChanged(Switch switchView, boolean isChecked) { 68 mMetrics.action(mContext, SettingsEnums.ACTION_TOGGLE_STORAGE_MANAGER, isChecked); 69 mDaysToRetainPreference.setEnabled(isChecked); 70 Settings.Secure.putInt( 71 mContext.getContentResolver(), 72 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 73 isChecked ? 1 : 0); 74 // Only show a warning if enabling. 75 if (isChecked) { 76 maybeShowWarning(); 77 } 78 } 79 80 /** Unregisters the controller from listening to further events. */ tearDown()81 public void tearDown() { 82 mSwitchBar.removeOnSwitchChangeListener(this); 83 } 84 maybeShowWarning()85 private void maybeShowWarning() { 86 // If the storage manager is on by default, we don't need to show the additional dialog. 87 if (SystemProperties.getBoolean(STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, false)) { 88 return; 89 } 90 ActivationWarningFragment fragment = ActivationWarningFragment.newInstance(); 91 fragment.show(mFragmentManager, ActivationWarningFragment.TAG); 92 } 93 } 94