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