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