AutomaticStorageManagerSettings.java revision aad643caf24f6bfe7a04a0422d10102246877925
1/** 2 * Copyright (C) 2016 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 17package com.android.settings.deletionhelper; 18 19import android.app.Activity; 20import android.content.Context; 21import android.content.Intent; 22import android.content.res.Resources; 23import android.os.Bundle; 24import android.provider.Settings; 25import android.util.Log; 26import android.view.View; 27import android.widget.Switch; 28import android.support.v14.preference.SwitchPreference; 29import android.support.v7.preference.DropDownPreference; 30import android.support.v7.preference.Preference; 31import android.support.v7.preference.Preference.OnPreferenceChangeListener; 32import android.support.v7.preference.PreferenceScreen; 33 34import com.android.internal.logging.MetricsProto.MetricsEvent; 35import com.android.settings.SettingsActivity; 36import com.android.settings.SettingsPreferenceFragment; 37import com.android.settings.R; 38import com.android.settings.widget.SwitchBar; 39import com.android.settings.widget.SwitchBar.OnSwitchChangeListener; 40 41/** 42 * AutomaticStorageManagerSettings is the Settings screen for configuration and management of the 43 * automatic storage manager. 44 */ 45public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment implements 46 OnPreferenceChangeListener, Preference.OnPreferenceClickListener { 47 public static final int DEFAULT_DAYS_TO_RETAIN = 90; 48 49 private static final String KEY_DAYS = "days"; 50 private static final String KEY_DELETION_HELPER = "deletion_helper"; 51 private static final String KEY_STORAGE_MANAGER_SWITCH = "storage_manager_active"; 52 53 private DropDownPreference mDaysToRetain; 54 private Preference mDeletionHelper; 55 private SwitchPreference mStorageManagerSwitch; 56 57 @Override 58 public void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 addPreferencesFromResource(R.xml.automatic_storage_management_settings); 61 } 62 63 @Override 64 public void onActivityCreated(Bundle savedInstanceState) { 65 super.onActivityCreated(savedInstanceState); 66 mDaysToRetain = (DropDownPreference) findPreference(KEY_DAYS); 67 mDaysToRetain.setOnPreferenceChangeListener(this); 68 69 mDeletionHelper = findPreference(KEY_DELETION_HELPER); 70 mDeletionHelper.setOnPreferenceClickListener(this); 71 72 mStorageManagerSwitch = (SwitchPreference) findPreference(KEY_STORAGE_MANAGER_SWITCH); 73 mStorageManagerSwitch.setOnPreferenceChangeListener(this); 74 75 int value = Settings.Secure.getInt(getContentResolver(), 76 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, 77 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT); 78 String[] stringValues = 79 getResources().getStringArray(R.array.automatic_storage_management_days_values); 80 mDaysToRetain.setValue(stringValues[daysValueToIndex(value, stringValues)]); 81 } 82 83 @Override 84 public void onResume() { 85 super.onResume(); 86 boolean isChecked = 87 Settings.Secure.getInt(getContentResolver(), 88 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0; 89 mStorageManagerSwitch.setChecked(isChecked); 90 mDaysToRetain.setEnabled(isChecked); 91 } 92 93 @Override 94 public boolean onPreferenceChange(Preference preference, Object newValue) { 95 switch (preference.getKey()) { 96 case KEY_STORAGE_MANAGER_SWITCH: 97 boolean checked = (boolean) newValue; 98 mDaysToRetain.setEnabled(checked); 99 Settings.Secure.putInt(getContentResolver(), 100 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, checked ? 1 : 0); 101 break; 102 case KEY_DAYS: 103 Settings.Secure.putInt(getContentResolver(), 104 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, 105 Integer.parseInt((String) newValue)); 106 break; 107 } 108 return true; 109 } 110 111 @Override 112 protected int getMetricsCategory() { 113 return MetricsEvent.STORAGE_MANAGER_SETTINGS; 114 } 115 116 @Override 117 public boolean onPreferenceClick(Preference preference) { 118 if (KEY_DELETION_HELPER.equals(preference.getKey())) { 119 Intent intent = new Intent(Settings.ACTION_DELETION_HELPER_SETTINGS); 120 getContext().startActivity(intent); 121 } 122 return true; 123 } 124 125 private static int daysValueToIndex(int value, String[] indices) { 126 for (int i = 0; i < indices.length; i++) { 127 int thisValue = Integer.parseInt(indices[i]); 128 if (value == thisValue) { 129 return i; 130 } 131 } 132 return indices.length - 1; 133 } 134} 135