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 17 package com.android.settings.deletionhelper; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.provider.Settings; 23 import android.support.v7.preference.DropDownPreference; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.Preference.OnPreferenceChangeListener; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 31 import com.android.settings.R; 32 import com.android.settings.SettingsActivity; 33 import com.android.settings.Utils; 34 import com.android.settings.dashboard.DashboardFragment; 35 import com.android.settings.search.BaseSearchIndexProvider; 36 import com.android.settings.search.Indexable; 37 import com.android.settings.widget.SwitchBar; 38 import com.android.settingslib.core.AbstractPreferenceController; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 /** 44 * AutomaticStorageManagerSettings is the Settings screen for configuration and management of the 45 * automatic storage manager. 46 */ 47 public class AutomaticStorageManagerSettings extends DashboardFragment 48 implements OnPreferenceChangeListener { 49 private static final String KEY_DAYS = "days"; 50 51 private AutomaticStorageManagerSwitchBarController mSwitchController; 52 private DropDownPreference mDaysToRetain; 53 private SwitchBar mSwitchBar; 54 55 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)56 public View onCreateView( 57 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 58 View view = super.onCreateView(inflater, container, savedInstanceState); 59 60 initializeDaysToRetainPreference(); 61 initializeSwitchBar(); 62 63 return view; 64 } 65 initializeDaysToRetainPreference()66 private void initializeDaysToRetainPreference() { 67 mDaysToRetain = (DropDownPreference) findPreference(KEY_DAYS); 68 mDaysToRetain.setOnPreferenceChangeListener(this); 69 70 ContentResolver cr = getContentResolver(); 71 int photosDaysToRetain = 72 Settings.Secure.getInt( 73 cr, 74 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, 75 Utils.getDefaultStorageManagerDaysToRetain(getResources())); 76 String[] stringValues = 77 getResources().getStringArray(R.array.automatic_storage_management_days_values); 78 mDaysToRetain.setValue(stringValues[daysValueToIndex(photosDaysToRetain, stringValues)]); 79 } 80 initializeSwitchBar()81 private void initializeSwitchBar() { 82 final SettingsActivity activity = (SettingsActivity) getActivity(); 83 mSwitchBar = activity.getSwitchBar(); 84 mSwitchBar.setSwitchBarText(R.string.automatic_storage_manager_master_switch_title, 85 R.string.automatic_storage_manager_master_switch_title); 86 mSwitchBar.show(); 87 mSwitchController = 88 new AutomaticStorageManagerSwitchBarController( 89 getContext(), 90 mSwitchBar, 91 mMetricsFeatureProvider, 92 mDaysToRetain, 93 getFragmentManager()); 94 } 95 96 @Override onResume()97 public void onResume() { 98 super.onResume(); 99 mDaysToRetain.setEnabled(Utils.isStorageManagerEnabled(getContext())); 100 } 101 102 @Override getLogTag()103 protected String getLogTag() { 104 return null; 105 } 106 107 @Override getPreferenceScreenResId()108 protected int getPreferenceScreenResId() { 109 return R.xml.automatic_storage_management_settings; 110 } 111 112 @Override createPreferenceControllers(Context context)113 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 114 return buildPreferenceControllers(context); 115 } 116 117 @Override onDestroyView()118 public void onDestroyView() { 119 super.onDestroyView(); 120 121 mSwitchBar.hide(); 122 mSwitchController.tearDown(); 123 } 124 125 @Override onPreferenceChange(Preference preference, Object newValue)126 public boolean onPreferenceChange(Preference preference, Object newValue) { 127 if (KEY_DAYS.equals(preference.getKey())) { 128 Settings.Secure.putInt( 129 getContentResolver(), 130 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, 131 Integer.parseInt((String) newValue)); 132 } 133 return true; 134 } 135 136 @Override getMetricsCategory()137 public int getMetricsCategory() { 138 return MetricsEvent.STORAGE_MANAGER_SETTINGS; 139 } 140 141 @Override getHelpResource()142 public int getHelpResource() { 143 return R.string.help_uri_storage; 144 } 145 daysValueToIndex(int value, String[] indices)146 private static int daysValueToIndex(int value, String[] indices) { 147 for (int i = 0; i < indices.length; i++) { 148 int thisValue = Integer.parseInt(indices[i]); 149 if (value == thisValue) { 150 return i; 151 } 152 } 153 return indices.length - 1; 154 } 155 buildPreferenceControllers(Context context)156 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) { 157 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 158 controllers.add(new AutomaticStorageManagerDescriptionPreferenceController(context)); 159 return controllers; 160 } 161 162 /** For Search. */ 163 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 164 new BaseSearchIndexProvider() { 165 @Override 166 protected boolean isPageSearchEnabled(Context context) { 167 return false; 168 } 169 170 @Override 171 public List<AbstractPreferenceController> createPreferenceControllers( 172 Context context) { 173 return buildPreferenceControllers(context); 174 } 175 }; 176 } 177