Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2014 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.inputmethod.latin.settings;
     18 
     19 import android.app.backup.BackupManager;
     20 import android.content.Context;
     21 import android.content.SharedPreferences;
     22 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
     23 import android.content.res.Resources;
     24 import android.os.Bundle;
     25 import android.preference.ListPreference;
     26 import android.preference.Preference;
     27 import android.preference.PreferenceFragment;
     28 import android.preference.PreferenceScreen;
     29 import android.util.Log;
     30 
     31 /**
     32  * A base abstract class for a {@link PreferenceFragment} that implements a nested
     33  * {@link PreferenceScreen} of the main preference screen.
     34  */
     35 public abstract class SubScreenFragment extends PreferenceFragment
     36         implements OnSharedPreferenceChangeListener {
     37     private OnSharedPreferenceChangeListener mSharedPreferenceChangeListener;
     38 
     39     static void setPreferenceEnabled(final String prefKey, final boolean enabled,
     40             final PreferenceScreen screen) {
     41         final Preference preference = screen.findPreference(prefKey);
     42         if (preference != null) {
     43             preference.setEnabled(enabled);
     44         }
     45     }
     46 
     47     static void removePreference(final String prefKey, final PreferenceScreen screen) {
     48         final Preference preference = screen.findPreference(prefKey);
     49         if (preference != null) {
     50             screen.removePreference(preference);
     51         }
     52     }
     53 
     54     static void updateListPreferenceSummaryToCurrentValue(final String prefKey,
     55             final PreferenceScreen screen) {
     56         // Because the "%s" summary trick of {@link ListPreference} doesn't work properly before
     57         // KitKat, we need to update the summary programmatically.
     58         final ListPreference listPreference = (ListPreference)screen.findPreference(prefKey);
     59         if (listPreference == null) {
     60             return;
     61         }
     62         final CharSequence entries[] = listPreference.getEntries();
     63         final int entryIndex = listPreference.findIndexOfValue(listPreference.getValue());
     64         listPreference.setSummary(entryIndex < 0 ? null : entries[entryIndex]);
     65     }
     66 
     67     final void setPreferenceEnabled(final String prefKey, final boolean enabled) {
     68         setPreferenceEnabled(prefKey, enabled, getPreferenceScreen());
     69     }
     70 
     71     final void removePreference(final String prefKey) {
     72         removePreference(prefKey, getPreferenceScreen());
     73     }
     74 
     75     final void updateListPreferenceSummaryToCurrentValue(final String prefKey) {
     76         updateListPreferenceSummaryToCurrentValue(prefKey, getPreferenceScreen());
     77     }
     78 
     79     final SharedPreferences getSharedPreferences() {
     80         return getPreferenceManager().getSharedPreferences();
     81     }
     82 
     83     /**
     84      * Gets the application name to display on the UI.
     85      */
     86     final String getApplicationName() {
     87         final Context context = getActivity();
     88         final Resources res = getResources();
     89         final int applicationLabelRes = context.getApplicationInfo().labelRes;
     90         return res.getString(applicationLabelRes);
     91     }
     92 
     93     @Override
     94     public void addPreferencesFromResource(final int preferencesResId) {
     95         super.addPreferencesFromResource(preferencesResId);
     96         TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences(
     97                 getPreferenceScreen());
     98     }
     99 
    100     @Override
    101     public void onCreate(final Bundle savedInstanceState) {
    102         super.onCreate(savedInstanceState);
    103         mSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
    104             @Override
    105             public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
    106                 final SubScreenFragment fragment = SubScreenFragment.this;
    107                 final Context context = fragment.getActivity();
    108                 if (context == null || fragment.getPreferenceScreen() == null) {
    109                     final String tag = fragment.getClass().getSimpleName();
    110                     // TODO: Introduce a static function to register this class and ensure that
    111                     // onCreate must be called before "onSharedPreferenceChanged" is called.
    112                     Log.w(tag, "onSharedPreferenceChanged called before activity starts.");
    113                     return;
    114                 }
    115                 new BackupManager(context).dataChanged();
    116                 fragment.onSharedPreferenceChanged(prefs, key);
    117             }
    118         };
    119         getSharedPreferences().registerOnSharedPreferenceChangeListener(
    120                 mSharedPreferenceChangeListener);
    121     }
    122 
    123     @Override
    124     public void onDestroy() {
    125         getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
    126                 mSharedPreferenceChangeListener);
    127         super.onDestroy();
    128     }
    129 
    130     @Override
    131     public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
    132         // This method may be overridden by an extended class.
    133     }
    134 }
    135