From d2c6151d4d78e5e7d09080efd14e0a8f5933afb9 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Mon, 17 Mar 2014 19:14:52 +0900 Subject: [PATCH] Guard possible NumberFormatException Bug: 13472379 Change-Id: Ib7640b3d8816d8d7fb4747d2288eedd94bc49b06 --- .../keyboard/KeyboardSwitcher.java | 4 +-- .../inputmethod/latin/settings/Settings.java | 32 +++++++++++++------ .../latin/settings/SettingsValues.java | 2 +- .../keyboard/KeyboardLayoutSetTestsBase.java | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 2711d249b..66cb9e35d 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -60,7 +60,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { 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 THEME_INDEX_DEFAULT = THEME_INDEX_KLP; + 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), @@ -88,7 +88,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { * what user actually typed. */ private boolean mIsAutoCorrectionActive; - private KeyboardTheme mKeyboardTheme = KEYBOARD_THEMES[THEME_INDEX_DEFAULT]; + private KeyboardTheme mKeyboardTheme = KEYBOARD_THEMES[DEFAULT_THEME_INDEX]; private Context mThemeContext; private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java index b51c765f0..964bf2246 100644 --- a/java/src/com/android/inputmethod/latin/settings/Settings.java +++ b/java/src/com/android/inputmethod/latin/settings/Settings.java @@ -23,6 +23,7 @@ import android.content.res.Resources; import android.preference.PreferenceManager; import android.util.Log; +import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; import com.android.inputmethod.latin.InputAttributes; import com.android.inputmethod.latin.R; @@ -270,25 +271,36 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang } 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); + final int defaultThemeIndex = readDefaultKeyboardThemeIndex(res); + final String themeIndexString = prefs.getString(PREF_KEYBOARD_LAYOUT, null); try { - return Integer.valueOf(themeIndex); + return Integer.parseInt(themeIndexString); } catch (final NumberFormatException e) { // Format error, returns default keyboard theme index. - Log.e(TAG, "Illegal keyboard theme in preference: " + themeIndex + ", default to " + Log.e(TAG, "Illegal keyboard theme in preference: " + themeIndexString + ", default to " + defaultThemeIndex, e); - return Integer.valueOf(defaultThemeIndex); + } + 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 = KeyboardSwitcher.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 String defaultThemeIndex = res.getString( - R.string.config_default_keyboard_theme_index); - prefs.edit().putString(PREF_KEYBOARD_LAYOUT, defaultThemeIndex).apply(); - return Integer.valueOf(defaultThemeIndex); + final int defaultThemeIndex = readDefaultKeyboardThemeIndex(res); + prefs.edit().putString(PREF_KEYBOARD_LAYOUT, Integer.toString(defaultThemeIndex)).apply(); + return defaultThemeIndex; } public static String readPrefAdditionalSubtypes(final SharedPreferences prefs, diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java index 50fbbb19f..5a652a557 100644 --- a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java @@ -285,7 +285,7 @@ public final class SettingsValues { // When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off. final float autoCorrectionThreshold; try { - final int arrayIndex = Integer.valueOf(currentAutoCorrectionSetting); + final int arrayIndex = Integer.parseInt(currentAutoCorrectionSetting); if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) { final String val = autoCorrectionThresholdValues[arrayIndex]; if (FLOAT_MAX_VALUE_MARKER_STRING.equals(val)) { diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java index 0993c4b67..a4c69e023 100644 --- a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java +++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java @@ -42,7 +42,7 @@ import java.util.Locale; @SmallTest public class KeyboardLayoutSetTestsBase extends AndroidTestCase { private static final KeyboardTheme DEFAULT_KEYBOARD_THEME = - KeyboardSwitcher.KEYBOARD_THEMES[KeyboardSwitcher.THEME_INDEX_DEFAULT]; + KeyboardSwitcher.KEYBOARD_THEMES[KeyboardSwitcher.DEFAULT_THEME_INDEX]; // All input method subtypes of LatinIME. private final ArrayList mAllSubtypesList = CollectionUtils.newArrayList();