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