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