From b0b198244254aa398608b5a2488fe8bacdd6bfd2 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 22 Nov 2013 12:32:52 +0900 Subject: [PATCH] Update Color Scheme summary explicitly Bug: 11622614 Change-Id: I5464054425e4d688eaa39f96ba9a3a3c613c6f42 --- java/res/xml/prefs.xml | 1 - .../keyboard/KeyboardSwitcher.java | 39 ++++++++----------- .../inputmethod/latin/settings/Settings.java | 23 +++++++++++ .../latin/settings/SettingsFragment.java | 21 ++++++++++ 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml index d2b447545..bf3b62353 100644 --- a/java/res/xml/prefs.xml +++ b/java/res/xml/prefs.xml @@ -161,7 +161,6 @@ = 0 && index < KEYBOARD_THEMES.length) { - return KEYBOARD_THEMES[index]; - } - } catch (NumberFormatException e) { - // Format error, keyboard theme is default to 0. + final Resources res = context.getResources(); + final int index = Settings.readKeyboardThemeIndex(prefs, res); + if (index >= 0 && index < KEYBOARD_THEMES.length) { + return KEYBOARD_THEMES[index]; } - Log.w(TAG, "Illegal keyboard theme in preference: " + themeIndex + ", default to " - + defaultIndex); - return KEYBOARD_THEMES[Integer.valueOf(defaultIndex)]; + final int defaultThemeIndex = Settings.resetAndGetDefaultKeyboardThemeIndex(prefs, res); + Log.w(TAG, "Illegal keyboard theme in preference: " + index + ", default to " + + defaultThemeIndex); + return KEYBOARD_THEMES[defaultThemeIndex]; } private boolean updateKeyboardThemeAndContextThemeWrapper(final Context context, diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java index dc005bbdf..df2c6907f 100644 --- a/java/src/com/android/inputmethod/latin/settings/Settings.java +++ b/java/src/com/android/inputmethod/latin/settings/Settings.java @@ -65,6 +65,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang "pref_show_language_switch_key"; public static final String PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST = "pref_include_other_imes_in_language_switch_list"; + public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20110916"; public static final String PREF_CUSTOM_INPUT_STYLES = "custom_input_styles"; public static final String PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY = "pref_key_preview_popup_dismiss_delay"; @@ -262,6 +263,28 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang return prefs.getBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, true); } + public static int readKeyboardThemeIndex(final SharedPreferences prefs, final Resources res) { + final String defaultThemeIndex = res.getString( + R.string.config_default_keyboard_theme_index); + final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultThemeIndex); + try { + return Integer.valueOf(themeIndex); + } catch (final NumberFormatException e) { + // Format error, returns default keyboard theme index. + Log.e(TAG, "Illegal keyboard theme in preference: " + themeIndex + ", default to " + + defaultThemeIndex, e); + return Integer.valueOf(defaultThemeIndex); + } + } + + public static int resetAndGetDefaultKeyboardThemeIndex(final SharedPreferences prefs, + final Resources res) { + final String defaultThemeIndex = res.getString( + R.string.config_default_keyboard_theme_index); + prefs.edit().putString(PREF_KEYBOARD_LAYOUT, defaultThemeIndex).apply(); + return Integer.valueOf(defaultThemeIndex); + } + public static String readPrefAdditionalSubtypes(final SharedPreferences prefs, final Resources res) { final String predefinedPrefSubtypes = AdditionalSubtypeUtils.createPrefSubtypes( diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java index cb7dda655..5c60a7350 100644 --- a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java +++ b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java @@ -255,6 +255,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment } updateShowCorrectionSuggestionsSummary(); updateKeyPreviewPopupDelaySummary(); + updateColorSchemeSummary(prefs, getResources()); updateCustomInputStylesSummary(); } @@ -288,6 +289,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment ensureConsistencyOfAutoCorrectionSettings(); updateShowCorrectionSuggestionsSummary(); updateKeyPreviewPopupDelaySummary(); + updateColorSchemeSummary(prefs, res); refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, getResources()); } @@ -305,6 +307,25 @@ public final class SettingsFragment extends InputMethodSettingsFragment mShowCorrectionSuggestionsPreference.getValue())]); } + private void updateColorSchemeSummary(final SharedPreferences prefs, final Resources res) { + // Because the "%s" summary trick of {@link ListPreference} doesn't work properly before + // KitKat, we need to update the summary by code. + final Preference preference = findPreference(Settings.PREF_KEYBOARD_LAYOUT); + if (!(preference instanceof ListPreference)) { + Log.w(TAG, "Can't find Keyboard Color Scheme preference"); + return; + } + final ListPreference colorSchemePreference = (ListPreference)preference; + final int themeIndex = Settings.readKeyboardThemeIndex(prefs, res); + int entryIndex = colorSchemePreference.findIndexOfValue(Integer.toString(themeIndex)); + if (entryIndex < 0) { + final int defaultThemeIndex = Settings.resetAndGetDefaultKeyboardThemeIndex(prefs, res); + entryIndex = colorSchemePreference.findIndexOfValue( + Integer.toString(defaultThemeIndex)); + } + colorSchemePreference.setSummary(colorSchemePreference.getEntries()[entryIndex]); + } + private void updateCustomInputStylesSummary() { final PreferenceScreen customInputStyles = (PreferenceScreen)findPreference(Settings.PREF_CUSTOM_INPUT_STYLES);