From ce875664e0564f3e26d130a4d3e8b8e9a2e8a368 Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Tue, 22 Jan 2013 13:29:30 +0900
Subject: [PATCH] Clean up Settings and SettingsFragment a bit

Change-Id: I93bf3cb1ea7e8fc09f4ad34b0bdd74f5f5ff1a68
---
 java/res/values-sw600dp/config.xml            |  4 +-
 java/res/values-sw768dp/config.xml            |  4 +-
 java/res/values/config.xml                    |  4 +-
 java/res/xml/prefs.xml                        |  2 +-
 .../android/inputmethod/latin/Settings.java   | 46 +++++++++++++++--
 .../inputmethod/latin/SettingsFragment.java   | 51 +++++++------------
 .../inputmethod/latin/SettingsValues.java     | 26 ++--------
 7 files changed, 70 insertions(+), 67 deletions(-)

diff --git a/java/res/values-sw600dp/config.xml b/java/res/values-sw600dp/config.xml
index b47eacb15e..9527fd62ad 100644
--- a/java/res/values-sw600dp/config.xml
+++ b/java/res/values-sw600dp/config.xml
@@ -22,10 +22,10 @@
     <!-- Device form factor. This value must be aligned with {@link KeyboardId.FORM_FACTOR_TABLET7} -->
     <integer name="config_device_form_factor">1</integer>
     <bool name="config_enable_show_voice_key_option">false</bool>
-    <bool name="config_enable_show_popup_on_keypress_option">false</bool>
+    <bool name="config_enable_show_option_of_key_preview_popup">false</bool>
     <bool name="config_enable_bigram_suggestions_option">false</bool>
     <!-- Whether or not Popup on key press is enabled by default -->
-    <bool name="config_default_popup_preview">false</bool>
+    <bool name="config_default_key_preview_popup">false</bool>
     <bool name="config_default_sound_enabled">true</bool>
     <bool name="config_auto_correction_spacebar_led_enabled">false</bool>
     <!-- The language is never displayed if == 0, always displayed if < 0 -->
diff --git a/java/res/values-sw768dp/config.xml b/java/res/values-sw768dp/config.xml
index 5e7954106f..3c2c198558 100644
--- a/java/res/values-sw768dp/config.xml
+++ b/java/res/values-sw768dp/config.xml
@@ -22,10 +22,10 @@
     <!-- Device form factor. This value must be aligned with {@link KeyboardId.FORM_FACTOR_TABLET10} -->
     <integer name="config_device_form_factor">2</integer>
     <bool name="config_enable_show_voice_key_option">false</bool>
-    <bool name="config_enable_show_popup_on_keypress_option">false</bool>
+    <bool name="config_enable_show_option_of_key_preview_popup">false</bool>
     <bool name="config_enable_bigram_suggestions_option">false</bool>
     <!-- Whether or not Popup on key press is enabled by default -->
-    <bool name="config_default_popup_preview">false</bool>
+    <bool name="config_default_key_preview_popup">false</bool>
     <bool name="config_default_sound_enabled">true</bool>
     <bool name="config_auto_correction_spacebar_led_enabled">false</bool>
     <!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. -->
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index 82166df926..d248a68a64 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -23,11 +23,11 @@
     <integer name="config_device_form_factor">0</integer>
     <bool name="config_use_fullscreen_mode">false</bool>
     <bool name="config_enable_show_voice_key_option">true</bool>
-    <bool name="config_enable_show_popup_on_keypress_option">true</bool>
+    <bool name="config_enable_show_option_of_key_preview_popup">true</bool>
     <!-- TODO: Disable the following configuration for production. -->
     <bool name="config_enable_usability_study_mode_option">true</bool>
     <!-- Whether or not Popup on key press is enabled by default -->
-    <bool name="config_default_popup_preview">true</bool>
+    <bool name="config_default_key_preview_popup">true</bool>
     <!-- Default value for next word prediction: after entering a word and a space only, should we look
          at input history to suggest a hopefully helpful suggestions for the next word? -->
     <bool name="config_default_next_word_prediction">true</bool>
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index cff7505f41..da6e601071 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -42,7 +42,7 @@
             android:key="popup_on"
             android:title="@string/popup_on_keypress"
             android:persistent="true"
-            android:defaultValue="@bool/config_default_popup_preview" />
+            android:defaultValue="@bool/config_default_key_preview_popup" />
         <ListPreference
             android:key="voice_mode"
             android:title="@string/voice_input"
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index 49e19c2724..02b44c7f69 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -126,13 +126,49 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
     }
 
     // Accessed from the settings interface, hence public
+    public static boolean readKeypressSoundEnabled(final SharedPreferences prefs,
+            final Resources res) {
+        return prefs.getBoolean(Settings.PREF_SOUND_ON,
+                res.getBoolean(R.bool.config_default_sound_enabled));
+    }
+
+    public static boolean readVibrationEnabled(final SharedPreferences prefs,
+            final Resources res) {
+        final boolean hasVibrator = AudioAndHapticFeedbackManager.getInstance().hasVibrator();
+        return hasVibrator && prefs.getBoolean(PREF_VIBRATE_ON,
+                res.getBoolean(R.bool.config_default_vibration_enabled));
+    }
+
+    public static boolean readAutoCorrectEnabled(final String currentAutoCorrectionSetting,
+            final Resources res) {
+        final String autoCorrectionOff = res.getString(
+                R.string.auto_correction_threshold_mode_index_off);
+        return !currentAutoCorrectionSetting.equals(autoCorrectionOff);
+    }
+
+    public static boolean readFromBuildConfigIfGestureInputEnabled(final Resources res) {
+        return res.getBoolean(R.bool.config_gesture_input_enabled_by_build_config);
+    }
+
+    public static boolean readGestureInputEnabled(final SharedPreferences prefs,
+            final Resources res) {
+        return readFromBuildConfigIfGestureInputEnabled(res)
+                && prefs.getBoolean(Settings.PREF_GESTURE_INPUT, true);
+    }
+
+    public static boolean readFromBuildConfigIfToShowKeyPreviewPopupSettingsOption(
+            final Resources res) {
+        return res.getBoolean(R.bool.config_enable_show_option_of_key_preview_popup);
+    }
+
     public static boolean readKeyPreviewPopupEnabled(final SharedPreferences prefs,
             final Resources res) {
-        final boolean showPopupOption = res.getBoolean(
-                R.bool.config_enable_show_popup_on_keypress_option);
-        if (!showPopupOption) return res.getBoolean(R.bool.config_default_popup_preview);
-        return prefs.getBoolean(PREF_POPUP_ON,
-                res.getBoolean(R.bool.config_default_popup_preview));
+        final boolean defaultKeyPreviewPopup = res.getBoolean(
+                R.bool.config_default_key_preview_popup);
+        if (!readFromBuildConfigIfToShowKeyPreviewPopupSettingsOption(res)) {
+            return defaultKeyPreviewPopup;
+        }
+        return prefs.getBoolean(PREF_POPUP_ON, defaultKeyPreviewPopup);
     }
 
     public static int readKeyPreviewPopupDismissDelay(final SharedPreferences prefs,
diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java
index f0c51e1943..edd064c0b5 100644
--- a/java/src/com/android/inputmethod/latin/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java
@@ -114,23 +114,22 @@ public final class SettingsFragment extends InputMethodSettingsFragment
             removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS, advancedSettings);
         }
 
-        final boolean showKeyPreviewPopupOption = res.getBoolean(
-                R.bool.config_enable_show_popup_on_keypress_option);
         mKeyPreviewPopupDismissDelay =
                 (ListPreference) findPreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
-        if (!showKeyPreviewPopupOption) {
+        if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupSettingsOption(res)) {
             removePreference(Settings.PREF_POPUP_ON, generalSettings);
             removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, advancedSettings);
         } else {
-            final String[] entries = new String[] {
-                    res.getString(R.string.key_preview_popup_dismiss_no_delay),
-                    res.getString(R.string.key_preview_popup_dismiss_default_delay),
-            };
             final String popupDismissDelayDefaultValue = Integer.toString(res.getInteger(
                     R.integer.config_key_preview_linger_timeout));
-            mKeyPreviewPopupDismissDelay.setEntries(entries);
-            mKeyPreviewPopupDismissDelay.setEntryValues(
-                    new String[] { "0", popupDismissDelayDefaultValue });
+            mKeyPreviewPopupDismissDelay.setEntries(new String[] {
+                    res.getString(R.string.key_preview_popup_dismiss_no_delay),
+                    res.getString(R.string.key_preview_popup_dismiss_default_delay),
+            });
+            mKeyPreviewPopupDismissDelay.setEntryValues(new String[] {
+                    "0",
+                    popupDismissDelayDefaultValue
+            });
             if (null == mKeyPreviewPopupDismissDelay.getValue()) {
                 mKeyPreviewPopupDismissDelay.setValue(popupDismissDelayDefaultValue);
             }
@@ -152,9 +151,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment
             textCorrectionGroup.removePreference(dictionaryLink);
         }
 
-        final boolean gestureInputEnabledByBuildConfig = res.getBoolean(
-                R.bool.config_gesture_input_enabled_by_build_config);
-        if (!gestureInputEnabledByBuildConfig) {
+        if (!Settings.readFromBuildConfigIfGestureInputEnabled(res)) {
             removePreference(Settings.PREF_GESTURE_SETTINGS, getPreferenceScreen());
         }
 
@@ -188,23 +185,17 @@ public final class SettingsFragment extends InputMethodSettingsFragment
     @Override
     public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
         (new BackupManager(getActivity())).dataChanged();
+        final Resources res = getResources();
         if (key.equals(Settings.PREF_POPUP_ON)) {
             setPreferenceEnabled(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY,
-                    prefs.getBoolean(Settings.PREF_POPUP_ON, true));
+                    Settings.readKeyPreviewPopupEnabled(prefs, res));
         } else if (key.equals(Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY)) {
             setPreferenceEnabled(Settings.PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST,
                     Settings.readShowsLanguageSwitchKey(prefs));
         } else if (key.equals(Settings.PREF_GESTURE_INPUT)) {
-            final boolean gestureInputEnabledByConfig = getResources().getBoolean(
-                    R.bool.config_gesture_input_enabled_by_build_config);
-            if (gestureInputEnabledByConfig) {
-                final boolean gestureInputEnabledByUser = prefs.getBoolean(
-                        Settings.PREF_GESTURE_INPUT, true);
-                setPreferenceEnabled(Settings.PREF_GESTURE_PREVIEW_TRAIL,
-                        gestureInputEnabledByUser);
-                setPreferenceEnabled(Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT,
-                        gestureInputEnabledByUser);
-            }
+            final boolean gestureInputEnabled = Settings.readGestureInputEnabled(prefs, res);
+            setPreferenceEnabled(Settings.PREF_GESTURE_PREVIEW_TRAIL, gestureInputEnabled);
+            setPreferenceEnabled(Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, gestureInputEnabled);
         }
         ensureConsistencyOfAutoCorrectionSettings();
         updateVoiceModeSummary();
@@ -258,16 +249,10 @@ public final class SettingsFragment extends InputMethodSettingsFragment
 
     private void refreshEnablingsOfKeypressSoundAndVibrationSettings(
             final SharedPreferences sp, final Resources res) {
-        final boolean hasVibratorHardware =
-                AudioAndHapticFeedbackManager.getInstance().hasVibrator();
-        final boolean vibrateOnByUser = sp.getBoolean(Settings.PREF_VIBRATE_ON,
-                res.getBoolean(R.bool.config_default_vibration_enabled));
         setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS,
-                hasVibratorHardware && vibrateOnByUser);
-
-        final boolean soundOn = sp.getBoolean(Settings.PREF_SOUND_ON,
-                res.getBoolean(R.bool.config_default_sound_enabled));
-        setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME, soundOn);
+                Settings.readVibrationEnabled(sp, res));
+        setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME,
+                Settings.readKeypressSoundEnabled(sp, res));
     }
 
     private void setupKeypressVibrationDurationSettings(final SharedPreferences sp,
diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java
index f43382cb52..728f6b2812 100644
--- a/java/src/com/android/inputmethod/latin/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/SettingsValues.java
@@ -104,9 +104,8 @@ public final class SettingsValues {
 
         // Get the settings preferences
         mAutoCap = prefs.getBoolean(Settings.PREF_AUTO_CAP, true);
-        mVibrateOn = readVibrationEnabled(prefs, res);
-        mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON,
-                res.getBoolean(R.bool.config_default_sound_enabled));
+        mVibrateOn = Settings.readVibrationEnabled(prefs, res);
+        mSoundOn = Settings.readKeypressSoundEnabled(prefs, res);
         mKeyPreviewPopupOn = Settings.readKeyPreviewPopupEnabled(prefs, res);
         mSlidingKeyInputPreviewEnabled = prefs.getBoolean(
                 Settings.PREF_SLIDING_KEY_INPUT_PREVIEW, true);
@@ -121,7 +120,7 @@ public final class SettingsValues {
         mShowsLanguageSwitchKey = Settings.readShowsLanguageSwitchKey(prefs);
         mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true);
         mUseDoubleSpacePeriod = prefs.getBoolean(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true);
-        mAutoCorrectEnabled = readAutoCorrectEnabled(res, autoCorrectionThresholdRawValue);
+        mAutoCorrectEnabled = Settings.readAutoCorrectEnabled(autoCorrectionThresholdRawValue, res);
         mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res);
 
         // Compute other readable settings
@@ -133,10 +132,7 @@ public final class SettingsValues {
                 autoCorrectionThresholdRawValue);
         mVoiceKeyEnabled = mVoiceMode != null && !mVoiceMode.equals(voiceModeOff);
         mVoiceKeyOnMain = mVoiceMode != null && mVoiceMode.equals(voiceModeMain);
-        final boolean gestureInputEnabledByBuildConfig = res.getBoolean(
-                R.bool.config_gesture_input_enabled_by_build_config);
-        mGestureInputEnabled = gestureInputEnabledByBuildConfig
-                && prefs.getBoolean(Settings.PREF_GESTURE_INPUT, true);
+        mGestureInputEnabled = Settings.readGestureInputEnabled(prefs, res);
         mGesturePreviewTrailEnabled = prefs.getBoolean(Settings.PREF_GESTURE_PREVIEW_TRAIL, true);
         mGestureFloatingPreviewTextEnabled = prefs.getBoolean(
                 Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, true);
@@ -250,20 +246,6 @@ public final class SettingsValues {
         throw new RuntimeException("Bug: visibility string is not configured correctly");
     }
 
-    private static boolean readVibrationEnabled(final SharedPreferences prefs,
-            final Resources res) {
-        final boolean hasVibrator = AudioAndHapticFeedbackManager.getInstance().hasVibrator();
-        return hasVibrator && prefs.getBoolean(Settings.PREF_VIBRATE_ON,
-                res.getBoolean(R.bool.config_default_vibration_enabled));
-    }
-
-    private static boolean readAutoCorrectEnabled(final Resources res,
-            final String currentAutoCorrectionSetting) {
-        final String autoCorrectionOff = res.getString(
-                R.string.auto_correction_threshold_mode_index_off);
-        return !currentAutoCorrectionSetting.equals(autoCorrectionOff);
-    }
-
     private static boolean readBigramPredictionEnabled(final SharedPreferences prefs,
             final Resources res) {
         return prefs.getBoolean(Settings.PREF_BIGRAM_PREDICTIONS, res.getBoolean(
-- 
GitLab