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