aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2014-04-14 18:28:08 +0900
committerTadashi G. Takaoka <takaoka@google.com>2014-04-14 18:59:54 +0900
commit21eafd7910182a31372fb92895f057cff28a8480 (patch)
tree4f1a8c129aa5dbe2fe09197f0ae6b8f407c1aa25 /java
parent3568fca95f92d5081755da88a5f6ca4cf518e2b2 (diff)
downloadlatinime-21eafd7910182a31372fb92895f057cff28a8480.tar.gz
latinime-21eafd7910182a31372fb92895f057cff28a8480.tar.xz
latinime-21eafd7910182a31372fb92895f057cff28a8480.zip
Use KeyboardTheme id insteand of index
This CL must be checked in together with I771216f8bd. Bug: 14042743 Change-Id: I45c3acf9242985fb03721a9a6d377fbccc8e2019
Diffstat (limited to 'java')
-rw-r--r--java/res/values/config-common.xml4
-rw-r--r--java/res/values/donottranslate.xml8
-rw-r--r--java/res/xml/prefs.xml6
-rw-r--r--java/res/xml/prefs_for_debug.xml2
-rw-r--r--java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java19
-rw-r--r--java/src/com/android/inputmethod/keyboard/KeyboardTheme.java59
-rw-r--r--java/src/com/android/inputmethod/latin/settings/Settings.java37
7 files changed, 62 insertions, 73 deletions
diff --git a/java/res/values/config-common.xml b/java/res/values/config-common.xml
index 20d5860e3..3fe4b947c 100644
--- a/java/res/values/config-common.xml
+++ b/java/res/values/config-common.xml
@@ -24,8 +24,8 @@
at input history to suggest a hopefully helpful suggestions for the next word? -->
<bool name="config_default_next_word_prediction">true</bool>
- <!-- This configuration is an index of {@link KeyboardSwitcher#KEYBOARD_THEMES[]}. -->
- <string name="config_default_keyboard_theme_index" translatable="false">2</string>
+ <!-- This configuration must be aligned with {@link KeyboardTheme#DEFAULT_THEME_ID}. -->
+ <string name="config_default_keyboard_theme_id" translatable="false">2</string>
<integer name="config_delay_update_shift_state">100</integer>
<integer name="config_double_space_period_timeout">1100</integer>
diff --git a/java/res/values/donottranslate.xml b/java/res/values/donottranslate.xml
index 4be5863f2..99775104d 100644
--- a/java/res/values/donottranslate.xml
+++ b/java/res/values/donottranslate.xml
@@ -58,7 +58,7 @@
<item>@string/layout_gingerbread</item>
<item>@string/layout_klp</item>
</string-array>
- <!-- An element must be an index of {@link KeyboardSwitcher#KEYBOARD_THEMES[]}. -->
+ <!-- An element must be a keyboard theme id of {@link KeyboardTheme#THEME_ID_*}. -->
<string-array name="keyboard_layout_modes_values">
<item>0</item>
<item>1</item>
@@ -66,12 +66,12 @@
</string-array>
<!-- For keyboard color scheme option dialog. -->
- <string-array name="keyboard_color_schemes">
+ <string-array name="keyboard_theme_names">
<item>@string/keyboard_color_scheme_white</item>
<item>@string/keyboard_color_scheme_blue</item>
</string-array>
- <!-- An element must be an index of {@link KeyboardSwitcher#KEYBOARD_THEMES[]}. -->
- <string-array name="keyboard_color_schemes_values">
+ <!-- An element must be a keyboard theme id of {@link KeyboardTheme#THEME_ID_*}. -->
+ <string-array name="keyboard_theme_ids">
<item>2</item>
<item>0</item>
</string-array>
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index a39ce4ae8..3e3bedf15 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -168,9 +168,9 @@
android:key="pref_keyboard_layout_20110916"
android:title="@string/keyboard_color_scheme"
android:persistent="true"
- android:entryValues="@array/keyboard_color_schemes_values"
- android:entries="@array/keyboard_color_schemes"
- android:defaultValue="@string/config_default_keyboard_theme_index" />
+ android:entryValues="@array/keyboard_theme_ids"
+ android:entries="@array/keyboard_theme_names"
+ android:defaultValue="@string/config_default_keyboard_theme_id" />
<PreferenceScreen
android:fragment="com.android.inputmethod.latin.settings.AdditionalSubtypeSettings"
android:key="custom_input_styles"
diff --git a/java/res/xml/prefs_for_debug.xml b/java/res/xml/prefs_for_debug.xml
index 7b2b8eab4..7fab15bab 100644
--- a/java/res/xml/prefs_for_debug.xml
+++ b/java/res/xml/prefs_for_debug.xml
@@ -33,7 +33,7 @@
android:persistent="true"
android:entryValues="@array/keyboard_layout_modes_values"
android:entries="@array/keyboard_layout_modes"
- android:defaultValue="@string/config_default_keyboard_theme_index" />
+ android:defaultValue="@string/config_default_keyboard_theme_id" />
<CheckBoxPreference
android:key="debug_mode"
android:title="@string/prefs_debug_mode"
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 6074a8106..6131a9b17 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -38,7 +38,6 @@ import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputMethodManager;
import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.WordComposer;
-import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.ResourceUtils;
@@ -66,8 +65,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
* what user actually typed. */
private boolean mIsAutoCorrectionActive;
- private KeyboardTheme mKeyboardTheme =
- KeyboardTheme.KEYBOARD_THEMES[KeyboardTheme.DEFAULT_THEME_INDEX];
+ private KeyboardTheme mKeyboardTheme = KeyboardTheme.getDefaultKeyboardTheme();
private Context mThemeContext;
private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();
@@ -96,25 +94,12 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
public void updateKeyboardTheme() {
final boolean themeUpdated = updateKeyboardThemeAndContextThemeWrapper(
- mLatinIME, getKeyboardTheme(mLatinIME, mPrefs));
+ mLatinIME, KeyboardTheme.getKeyboardTheme(mPrefs));
if (themeUpdated && mKeyboardView != null) {
mLatinIME.setInputView(onCreateInputView(mIsHardwareAcceleratedDrawingEnabled));
}
}
- private static KeyboardTheme getKeyboardTheme(final Context context,
- final SharedPreferences prefs) {
- final Resources res = context.getResources();
- final int index = Settings.readKeyboardThemeIndex(prefs, res);
- if (index >= 0 && index < KeyboardTheme.KEYBOARD_THEMES.length) {
- return KeyboardTheme.KEYBOARD_THEMES[index];
- }
- final int defaultThemeIndex = Settings.resetAndGetDefaultKeyboardThemeIndex(prefs, res);
- Log.w(TAG, "Illegal keyboard theme in preference: " + index + ", default to "
- + defaultThemeIndex);
- return KeyboardTheme.KEYBOARD_THEMES[defaultThemeIndex];
- }
-
private boolean updateKeyboardThemeAndContextThemeWrapper(final Context context,
final KeyboardTheme keyboardTheme) {
if (mThemeContext == null || mKeyboardTheme.mThemeId != keyboardTheme.mThemeId) {
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java b/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
index d15942728..6ec9d99b2 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
@@ -16,18 +16,24 @@
package com.android.inputmethod.keyboard;
+import android.content.SharedPreferences;
+import android.util.Log;
+
import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.settings.Settings;
public final class KeyboardTheme {
- public static final int THEME_INDEX_ICS = 0;
- public static final int THEME_INDEX_GB = 1;
- public static final int THEME_INDEX_KLP = 2;
- public static final int DEFAULT_THEME_INDEX = THEME_INDEX_KLP;
-
- public static final KeyboardTheme[] KEYBOARD_THEMES = {
- new KeyboardTheme(THEME_INDEX_ICS, R.style.KeyboardTheme_ICS),
- new KeyboardTheme(THEME_INDEX_GB, R.style.KeyboardTheme_GB),
- new KeyboardTheme(THEME_INDEX_KLP, R.style.KeyboardTheme_KLP),
+ private static final String TAG = KeyboardTheme.class.getSimpleName();
+
+ public static final int THEME_ID_ICS = 0;
+ public static final int THEME_ID_GB = 1;
+ public static final int THEME_ID_KLP = 2;
+ private static final int DEFAULT_THEME_ID = THEME_ID_KLP;
+
+ private static final KeyboardTheme[] KEYBOARD_THEMES = {
+ new KeyboardTheme(THEME_ID_ICS, R.style.KeyboardTheme_ICS),
+ new KeyboardTheme(THEME_ID_GB, R.style.KeyboardTheme_GB),
+ new KeyboardTheme(THEME_ID_KLP, R.style.KeyboardTheme_KLP),
};
public final int mThemeId;
@@ -39,4 +45,39 @@ public final class KeyboardTheme {
mThemeId = themeId;
mStyleId = styleId;
}
+
+ private static KeyboardTheme searchKeyboardTheme(final int themeId) {
+ // TODO: This search algorithm isn't optimal if there are many themes.
+ for (final KeyboardTheme theme : KEYBOARD_THEMES) {
+ if (theme.mThemeId == themeId) {
+ return theme;
+ }
+ }
+ return null;
+ }
+
+ public static KeyboardTheme getDefaultKeyboardTheme() {
+ return searchKeyboardTheme(DEFAULT_THEME_ID);
+ }
+
+ public static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs) {
+ final String themeIdString = prefs.getString(Settings.PREF_KEYBOARD_LAYOUT, null);
+ if (themeIdString == null) {
+ return getDefaultKeyboardTheme();
+ }
+ try {
+ final int themeId = Integer.parseInt(themeIdString);
+ final KeyboardTheme theme = searchKeyboardTheme(themeId);
+ if (theme != null) {
+ return theme;
+ }
+ Log.w(TAG, "Unknown keyboard theme in preference: " + themeIdString);
+ } catch (final NumberFormatException e) {
+ Log.w(TAG, "Illegal keyboard theme in preference: " + themeIdString);
+ }
+ // Reset preference to default value.
+ final String defaultThemeIdString = Integer.toString(DEFAULT_THEME_ID);
+ prefs.edit().putString(Settings.PREF_KEYBOARD_LAYOUT, defaultThemeIdString).apply();
+ return getDefaultKeyboardTheme();
+ }
}
diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java
index 4cb02284a..353b7463d 100644
--- a/java/src/com/android/inputmethod/latin/settings/Settings.java
+++ b/java/src/com/android/inputmethod/latin/settings/Settings.java
@@ -23,7 +23,6 @@ import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;
-import com.android.inputmethod.keyboard.KeyboardTheme;
import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.R;
@@ -270,42 +269,6 @@ 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 int defaultThemeIndex = readDefaultKeyboardThemeIndex(res);
- final String themeIndexString = prefs.getString(PREF_KEYBOARD_LAYOUT, null);
- if (themeIndexString == null) {
- return defaultThemeIndex;
- }
- try {
- return Integer.parseInt(themeIndexString);
- } catch (final NumberFormatException e) {
- // Format error, returns default keyboard theme index.
- Log.e(TAG, "Illegal keyboard theme in preference: " + themeIndexString + ", default to "
- + defaultThemeIndex, e);
- }
- return defaultThemeIndex;
- }
-
- private static int readDefaultKeyboardThemeIndex(final Resources res) {
- final String defaultThemeIndexString = res.getString(
- R.string.config_default_keyboard_theme_index);
- try {
- return Integer.parseInt(defaultThemeIndexString);
- } catch (final NumberFormatException e) {
- final int defaultThemeIndex = KeyboardTheme.DEFAULT_THEME_INDEX;
- Log.e(TAG, "Corrupted default keyoard theme in resource: " + defaultThemeIndexString
- + ", default to " + defaultThemeIndex, e);
- return defaultThemeIndex;
- }
- }
-
- public static int resetAndGetDefaultKeyboardThemeIndex(final SharedPreferences prefs,
- final Resources res) {
- final int defaultThemeIndex = readDefaultKeyboardThemeIndex(res);
- prefs.edit().putString(PREF_KEYBOARD_LAYOUT, Integer.toString(defaultThemeIndex)).apply();
- return defaultThemeIndex;
- }
-
public static String readPrefAdditionalSubtypes(final SharedPreferences prefs,
final Resources res) {
final String predefinedPrefSubtypes = AdditionalSubtypeUtils.createPrefSubtypes(