Home | History | Annotate | Download | only in deletionhelper
      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
     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 
     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 
     81     private void initializeSwitchBar() {
     82         final SettingsActivity activity = (SettingsActivity) getActivity();
     83         mSwitchBar = activity.getSwitchBar();
     84         mSwitchBar.show();
     85         mSwitchController =
     86                 new AutomaticStorageManagerSwitchBarController(
     87                         getContext(),
     88                         mSwitchBar,
     89                         mMetricsFeatureProvider,
     90                         mDaysToRetain,
     91                         getFragmentManager());
     92     }
     93 
     94     @Override
     95     public void onResume() {
     96         super.onResume();
     97         mDaysToRetain.setEnabled(isStorageManagerEnabled());
     98     }
     99 
    100     @Override
    101     protected String getLogTag() {
    102         return null;
    103     }
    104 
    105     @Override
    106     protected int getPreferenceScreenResId() {
    107         return R.xml.automatic_storage_management_settings;
    108     }
    109 
    110     @Override
    111     protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
    112         return buildPreferenceControllers(context);
    113     }
    114 
    115     @Override
    116     public void onDestroyView() {
    117         super.onDestroyView();
    118 
    119         mSwitchBar.hide();
    120         mSwitchController.tearDown();
    121     }
    122 
    123     @Override
    124     public boolean onPreferenceChange(Preference preference, Object newValue) {
    125         if (KEY_DAYS.equals(preference.getKey())) {
    126             Settings.Secure.putInt(
    127                     getContentResolver(),
    128                     Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
    129                     Integer.parseInt((String) newValue));
    130         }
    131         return true;
    132     }
    133 
    134     @Override
    135     public int getMetricsCategory() {
    136         return MetricsEvent.STORAGE_MANAGER_SETTINGS;
    137     }
    138 
    139     @Override
    140     protected int getHelpResource() {
    141         return R.string.help_uri_storage;
    142     }
    143 
    144     private static int daysValueToIndex(int value, String[] indices) {
    145         for (int i = 0; i < indices.length; i++) {
    146             int thisValue = Integer.parseInt(indices[i]);
    147             if (value == thisValue) {
    148                 return i;
    149             }
    150         }
    151         return indices.length - 1;
    152     }
    153 
    154     private boolean isStorageManagerEnabled() {
    155         return Settings.Secure.getInt(
    156                         getContentResolver(), Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)
    157                 != 0;
    158     }
    159 
    160     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
    161         final List<AbstractPreferenceController> controllers = new ArrayList<>();
    162         controllers.add(new AutomaticStorageManagerDescriptionPreferenceController(context));
    163         return controllers;
    164     }
    165 
    166     /** For Search. */
    167     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
    168             new BaseSearchIndexProvider() {
    169                 @Override
    170                 protected boolean isPageSearchEnabled(Context context) {
    171                     return false;
    172                 }
    173 
    174                 @Override
    175                 public List<AbstractPreferenceController> getPreferenceControllers(
    176                         Context context) {
    177                     return buildPreferenceControllers(context);
    178                 }
    179             };
    180 }
    181