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