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.app.Activity; 20 import android.content.ContentResolver; 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.text.format.DateUtils; 27 import android.text.format.Formatter; 28 import android.view.LayoutInflater; 29 import android.view.View; 30 import android.view.ViewGroup; 31 32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 33 import com.android.settings.R; 34 import com.android.settings.SettingsActivity; 35 import com.android.settings.SettingsPreferenceFragment; 36 import com.android.settings.widget.SwitchBar; 37 38 /** 39 * AutomaticStorageManagerSettings is the Settings screen for configuration and management of the 40 * automatic storage manager. 41 */ 42 public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment 43 implements OnPreferenceChangeListener { 44 private static final String KEY_DAYS = "days"; 45 private static final String KEY_FREED = "freed_bytes"; 46 private static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY = 47 "ro.storage_manager.enabled"; 48 49 private AutomaticStorageManagerSwitchBarController mSwitchController; 50 private DropDownPreference mDaysToRetain; 51 private Preference mFreedBytes; 52 private SwitchBar mSwitchBar; 53 54 @Override onCreate(Bundle savedInstanceState)55 public void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 addPreferencesFromResource(R.xml.automatic_storage_management_settings); 58 } 59 60 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)61 public View onCreateView( 62 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 63 View view = super.onCreateView(inflater, container, savedInstanceState); 64 65 initializeDaysToRetainPreference(); 66 initializeFreedBytesPreference(); 67 initializeSwitchBar(); 68 69 return view; 70 } 71 initializeDaysToRetainPreference()72 private void initializeDaysToRetainPreference() { 73 mDaysToRetain = (DropDownPreference) findPreference(KEY_DAYS); 74 mDaysToRetain.setOnPreferenceChangeListener(this); 75 76 ContentResolver cr = getContentResolver(); 77 int photosDaysToRetain = Settings.Secure.getInt(cr, 78 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, 79 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT); 80 String[] stringValues = 81 getResources().getStringArray(R.array.automatic_storage_management_days_values); 82 mDaysToRetain.setValue(stringValues[daysValueToIndex(photosDaysToRetain, stringValues)]); 83 } 84 initializeSwitchBar()85 private void initializeSwitchBar() { 86 final SettingsActivity activity = (SettingsActivity) getActivity(); 87 mSwitchBar = activity.getSwitchBar(); 88 mSwitchBar.show(); 89 mSwitchController = 90 new AutomaticStorageManagerSwitchBarController( 91 getContext(), 92 mSwitchBar, 93 mMetricsFeatureProvider, 94 mDaysToRetain, 95 getFragmentManager()); 96 } 97 initializeFreedBytesPreference()98 private void initializeFreedBytesPreference() { 99 ContentResolver cr = getContentResolver(); 100 mFreedBytes = findPreference(KEY_FREED); 101 long freedBytes = Settings.Secure.getLong(cr, 102 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED, 103 0); 104 long lastRunMillis = Settings.Secure.getLong(cr, 105 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_LAST_RUN, 106 0); 107 if (freedBytes == 0 || lastRunMillis == 0) { 108 mFreedBytes.setVisible(false); 109 } else { 110 final Activity activity = getActivity(); 111 mFreedBytes.setSummary( 112 activity.getString( 113 R.string.automatic_storage_manager_freed_bytes, 114 Formatter.formatFileSize(activity, freedBytes), 115 DateUtils.formatDateTime( 116 activity, lastRunMillis, DateUtils.FORMAT_SHOW_DATE))); 117 } 118 } 119 120 @Override onResume()121 public void onResume() { 122 super.onResume(); 123 boolean isStorageManagerChecked = 124 Settings.Secure.getInt(getContentResolver(), 125 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0; 126 mDaysToRetain.setEnabled(isStorageManagerChecked); 127 } 128 129 @Override onDestroyView()130 public void onDestroyView() { 131 super.onDestroyView(); 132 133 mSwitchBar.hide(); 134 mSwitchController.tearDown(); 135 } 136 137 @Override onPreferenceChange(Preference preference, Object newValue)138 public boolean onPreferenceChange(Preference preference, Object newValue) { 139 if (KEY_DAYS.equals(preference.getKey())) { 140 Settings.Secure.putInt( 141 getContentResolver(), 142 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, 143 Integer.parseInt((String) newValue)); 144 } 145 return true; 146 } 147 148 @Override getMetricsCategory()149 public int getMetricsCategory() { 150 return MetricsEvent.STORAGE_MANAGER_SETTINGS; 151 } 152 153 @Override getHelpResource()154 protected int getHelpResource() { 155 return R.string.help_uri_storage; 156 } 157 daysValueToIndex(int value, String[] indices)158 private static int daysValueToIndex(int value, String[] indices) { 159 for (int i = 0; i < indices.length; i++) { 160 int thisValue = Integer.parseInt(indices[i]); 161 if (value == thisValue) { 162 return i; 163 } 164 } 165 return indices.length - 1; 166 } 167 } 168