am 1325ddba: Merge "Guard possible NumberFormatException"

* commit '1325ddbae318d75b62f59beb09b989291838be48':
  Guard possible NumberFormatException
main
Tadashi G. Takaoka 2014-03-18 00:29:41 -07:00 committed by Android Git Automerger
commit 79ff39fccd
4 changed files with 26 additions and 14 deletions

View File

@ -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_ICS = 0;
public static final int THEME_INDEX_GB = 1; public static final int THEME_INDEX_GB = 1;
public static final int THEME_INDEX_KLP = 2; 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 = { public static final KeyboardTheme[] KEYBOARD_THEMES = {
new KeyboardTheme(THEME_INDEX_ICS, R.style.KeyboardTheme_ICS), new KeyboardTheme(THEME_INDEX_ICS, R.style.KeyboardTheme_ICS),
new KeyboardTheme(THEME_INDEX_GB, R.style.KeyboardTheme_GB), new KeyboardTheme(THEME_INDEX_GB, R.style.KeyboardTheme_GB),
@ -88,7 +88,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
* what user actually typed. */ * what user actually typed. */
private boolean mIsAutoCorrectionActive; private boolean mIsAutoCorrectionActive;
private KeyboardTheme mKeyboardTheme = KEYBOARD_THEMES[THEME_INDEX_DEFAULT]; private KeyboardTheme mKeyboardTheme = KEYBOARD_THEMES[DEFAULT_THEME_INDEX];
private Context mThemeContext; private Context mThemeContext;
private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();

View File

@ -23,6 +23,7 @@ import android.content.res.Resources;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
import com.android.inputmethod.latin.InputAttributes; import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.R; 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) { public static int readKeyboardThemeIndex(final SharedPreferences prefs, final Resources res) {
final String defaultThemeIndex = res.getString( final int defaultThemeIndex = readDefaultKeyboardThemeIndex(res);
R.string.config_default_keyboard_theme_index); final String themeIndexString = prefs.getString(PREF_KEYBOARD_LAYOUT, null);
final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultThemeIndex);
try { try {
return Integer.valueOf(themeIndex); return Integer.parseInt(themeIndexString);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
// Format error, returns default keyboard theme index. // 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); + 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, public static int resetAndGetDefaultKeyboardThemeIndex(final SharedPreferences prefs,
final Resources res) { final Resources res) {
final String defaultThemeIndex = res.getString( final int defaultThemeIndex = readDefaultKeyboardThemeIndex(res);
R.string.config_default_keyboard_theme_index); prefs.edit().putString(PREF_KEYBOARD_LAYOUT, Integer.toString(defaultThemeIndex)).apply();
prefs.edit().putString(PREF_KEYBOARD_LAYOUT, defaultThemeIndex).apply(); return defaultThemeIndex;
return Integer.valueOf(defaultThemeIndex);
} }
public static String readPrefAdditionalSubtypes(final SharedPreferences prefs, public static String readPrefAdditionalSubtypes(final SharedPreferences prefs,

View File

@ -285,7 +285,7 @@ public final class SettingsValues {
// When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off. // When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off.
final float autoCorrectionThreshold; final float autoCorrectionThreshold;
try { try {
final int arrayIndex = Integer.valueOf(currentAutoCorrectionSetting); final int arrayIndex = Integer.parseInt(currentAutoCorrectionSetting);
if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) { if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) {
final String val = autoCorrectionThresholdValues[arrayIndex]; final String val = autoCorrectionThresholdValues[arrayIndex];
if (FLOAT_MAX_VALUE_MARKER_STRING.equals(val)) { if (FLOAT_MAX_VALUE_MARKER_STRING.equals(val)) {

View File

@ -42,7 +42,7 @@ import java.util.Locale;
@SmallTest @SmallTest
public class KeyboardLayoutSetTestsBase extends AndroidTestCase { public class KeyboardLayoutSetTestsBase extends AndroidTestCase {
private static final KeyboardTheme DEFAULT_KEYBOARD_THEME = 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. // All input method subtypes of LatinIME.
private final ArrayList<InputMethodSubtype> mAllSubtypesList = CollectionUtils.newArrayList(); private final ArrayList<InputMethodSubtype> mAllSubtypesList = CollectionUtils.newArrayList();