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