• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
22 import android.content.Intent;
23 import android.content.res.Resources;
24 import android.os.Bundle;
25 import android.os.storage.StorageManager;
26 import android.provider.Settings;
27 import android.text.format.DateUtils;
28 import android.text.format.Formatter;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.Switch;
32 import android.support.v14.preference.SwitchPreference;
33 import android.support.v7.preference.DropDownPreference;
34 import android.support.v7.preference.Preference;
35 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
36 import android.support.v7.preference.PreferenceScreen;
37 
38 import com.android.internal.logging.MetricsLogger;
39 import com.android.internal.logging.MetricsProto.MetricsEvent;
40 import com.android.settings.SettingsActivity;
41 import com.android.settings.SettingsPreferenceFragment;
42 import com.android.settings.R;
43 import com.android.settings.widget.SwitchBar;
44 import com.android.settings.widget.SwitchBar.OnSwitchChangeListener;
45 
46 /**
47  * AutomaticStorageManagerSettings is the Settings screen for configuration and management of the
48  * automatic storage manager.
49  */
50 public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment implements
51         OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
52     public static final int DEFAULT_DAYS_TO_RETAIN = 90;
53 
54     private static final String KEY_DAYS = "days";
55     private static final String KEY_DELETION_HELPER = "deletion_helper";
56     private static final String KEY_FREED = "freed_bytes";
57     private static final String KEY_STORAGE_MANAGER_SWITCH = "storage_manager_active";
58 
59     private DropDownPreference mDaysToRetain;
60     private Preference mFreedBytes;
61     private Preference mDeletionHelper;
62     private SwitchPreference mStorageManagerSwitch;
63 
64     @Override
onCreate(Bundle savedInstanceState)65     public void onCreate(Bundle savedInstanceState) {
66         super.onCreate(savedInstanceState);
67         addPreferencesFromResource(R.xml.automatic_storage_management_settings);
68     }
69 
70     @Override
onActivityCreated(Bundle savedInstanceState)71     public void onActivityCreated(Bundle savedInstanceState) {
72         super.onActivityCreated(savedInstanceState);
73         mDaysToRetain = (DropDownPreference) findPreference(KEY_DAYS);
74         mDaysToRetain.setOnPreferenceChangeListener(this);
75 
76         mFreedBytes = findPreference(KEY_FREED);
77 
78         mDeletionHelper = findPreference(KEY_DELETION_HELPER);
79         mDeletionHelper.setOnPreferenceClickListener(this);
80 
81         mStorageManagerSwitch = (SwitchPreference) findPreference(KEY_STORAGE_MANAGER_SWITCH);
82         mStorageManagerSwitch.setOnPreferenceChangeListener(this);
83 
84         ContentResolver cr = getContentResolver();
85         int value = Settings.Secure.getInt(cr,
86                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
87                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT);
88         String[] stringValues =
89                 getResources().getStringArray(R.array.automatic_storage_management_days_values);
90         mDaysToRetain.setValue(stringValues[daysValueToIndex(value, stringValues)]);
91 
92         long freedBytes = Settings.Secure.getLong(cr,
93                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED,
94                 0);
95         long lastRunMillis = Settings.Secure.getLong(cr,
96                 Settings.Secure.AUTOMATIC_STORAGE_MANAGER_LAST_RUN,
97                 0);
98         if (freedBytes == 0 || lastRunMillis == 0) {
99             mFreedBytes.setVisible(false);
100         } else {
101             Activity activity = getActivity();
102             mFreedBytes.setSummary(activity.getString(
103                     R.string.automatic_storage_manager_freed_bytes,
104                     Formatter.formatFileSize(activity, freedBytes),
105                     DateUtils.formatDateTime(activity, lastRunMillis, DateUtils.FORMAT_SHOW_DATE)));
106         }
107     }
108 
109     @Override
onResume()110     public void onResume() {
111         super.onResume();
112         boolean isChecked =
113                 Settings.Secure.getInt(getContentResolver(),
114                         Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
115         mStorageManagerSwitch.setChecked(isChecked);
116         mDaysToRetain.setEnabled(isChecked);
117     }
118 
119     @Override
onPreferenceChange(Preference preference, Object newValue)120     public boolean onPreferenceChange(Preference preference, Object newValue) {
121         switch (preference.getKey()) {
122             case KEY_STORAGE_MANAGER_SWITCH:
123                 boolean checked = (boolean) newValue;
124                 MetricsLogger.action(getContext(), MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER,
125                         checked);
126                 mDaysToRetain.setEnabled(checked);
127                 Settings.Secure.putInt(getContentResolver(),
128                         Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, checked ? 1 : 0);
129                 break;
130             case KEY_DAYS:
131                 Settings.Secure.putInt(getContentResolver(),
132                         Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
133                         Integer.parseInt((String) newValue));
134                 break;
135         }
136         return true;
137     }
138 
139     @Override
getMetricsCategory()140     protected int getMetricsCategory() {
141         return MetricsEvent.STORAGE_MANAGER_SETTINGS;
142     }
143 
144     @Override
onPreferenceClick(Preference preference)145     public boolean onPreferenceClick(Preference preference) {
146         if (KEY_DELETION_HELPER.equals(preference.getKey())) {
147             Intent intent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
148             getContext().startActivity(intent);
149         }
150         return true;
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