Turn off the Settings key by default

bug:5094401
Change-Id: Ieb42fdfa79be2afcfa6634e7404c17737ef6927d
diff --git a/java/res/values/donottranslate.xml b/java/res/values/donottranslate.xml
index 4cf9303..a70c795 100644
--- a/java/res/values/donottranslate.xml
+++ b/java/res/values/donottranslate.xml
@@ -50,26 +50,6 @@
     <!-- Character for close candidates pane (BLACK UP-POINTING TRIANGLE) -->
     <string name="label_close_candidates_pane">\u25b2</string>
 
-    <!-- Option values to show/hide the settings key in onscreen keyboard -->
-    <!-- Automatically decide to show or hide the settings key -->
-    <string name="settings_key_mode_auto">0</string>
-    <!-- Always show the settings key -->
-    <string name="settings_key_mode_always_show">1</string>
-    <!-- Always hide the settings key -->
-    <string name="settings_key_mode_always_hide">2</string>
-    <!-- Array of the settings key mode values -->
-    <string-array name="settings_key_modes_values">
-        <item>@string/settings_key_mode_auto</item>
-        <item>@string/settings_key_mode_always_show</item>
-        <item>@string/settings_key_mode_always_hide</item>
-    </string-array>
-    <!-- Array of the settings key modes -->
-    <string-array name="settings_key_modes">
-        <item>@string/settings_key_mode_auto_name</item>
-        <item>@string/settings_key_mode_always_show_name</item>
-        <item>@string/settings_key_mode_always_hide_name</item>
-    </string-array>
-
     <!--  Always show the suggestion strip -->
     <string name="prefs_suggestion_visibility_show_value">0</string>
     <!--  Show the suggestion strip only on portrait mode -->
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index e5b85fe..2535818 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -82,12 +82,6 @@
 
     <!-- Option to show/hide the settings key -->
     <string name="prefs_settings_key">Show settings key</string>
-    <!-- Option to automatically decide to show/hide the settings key -->
-    <string name="settings_key_mode_auto_name">Automatic</string>
-    <!-- Option to always show the settings key -->
-    <string name="settings_key_mode_always_show_name">Always show</string>
-    <!-- Option to always hide the settings key -->
-    <string name="settings_key_mode_always_hide_name">Always hide</string>
 
     <!-- Option to decide the auto correction threshold score -->
     <!-- Option to enable auto correction [CHAR LIMIT=20]-->
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index 8ccb46f..552e3cf 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -49,13 +49,11 @@
             android:summary="@string/prefs_enable_recorrection_summary"
             android:persistent="true"
             android:defaultValue="@bool/config_default_recorrection_enabled" />
-        <ListPreference
-            android:key="settings_key"
+        <CheckBoxPreference
+            android:key="show_settings_key"
             android:title="@string/prefs_settings_key"
             android:persistent="true"
-            android:entryValues="@array/settings_key_modes_values"
-            android:entries="@array/settings_key_modes"
-            android:defaultValue="@string/settings_key_mode_auto" />
+            android:defaultValue="false" />
         <ListPreference
             android:key="voice_mode"
             android:title="@string/voice_input"
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 6cbd251..ee9eb7b 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -99,14 +99,6 @@
     private static final int SWITCH_STATE_CHORDING_SYMBOL = 6;
     private int mSwitchState = SWITCH_STATE_ALPHA;
 
-    private static final int SETTINGS_KEY_MODE_AUTO = R.string.settings_key_mode_auto;
-    private static final int SETTINGS_KEY_MODE_ALWAYS_SHOW =
-            R.string.settings_key_mode_always_show;
-    // NOTE: No need to have SETTINGS_KEY_MODE_ALWAYS_HIDE here because it's not being referred to
-    // in the source code now.
-    // Default is SETTINGS_KEY_MODE_AUTO.
-    private static final int DEFAULT_SETTINGS_KEY_MODE = SETTINGS_KEY_MODE_AUTO;
-
     private int mThemeIndex = -1;
     private Context mThemeContext;
 
@@ -228,21 +220,12 @@
         }
     }
 
-    public void loadKeyboard(EditorInfo attribute, Settings.Values settings) {
+    public void loadKeyboard(EditorInfo editorInfo, Settings.Values settingsValues) {
         mSwitchState = SWITCH_STATE_ALPHA;
         try {
-            final Locale locale = mSubtypeSwitcher.getInputLocale();
-            final Configuration conf = mResources.getConfiguration();
-            final int width = mWindowWidthCache.getWidth(conf);
-            final int orientation = conf.orientation;
-            final boolean voiceKeyEnabled = settings.isVoiceKeyEnabled(attribute);
-            final boolean voiceKeyOnMain = settings.isVoiceKeyOnMain();
-            mMainKeyboardId = getKeyboardId(attribute, locale, orientation, width,
-                    false, false, voiceKeyEnabled, voiceKeyOnMain);
-            mSymbolsKeyboardId = getKeyboardId(attribute, locale, orientation, width,
-                    true, false, voiceKeyEnabled, voiceKeyOnMain);
-            mSymbolsShiftedKeyboardId = getKeyboardId(attribute, locale, orientation, width,
-                    true, true, voiceKeyEnabled, voiceKeyOnMain);
+            mMainKeyboardId = getKeyboardId(editorInfo, false, false, settingsValues);
+            mSymbolsKeyboardId = getKeyboardId(editorInfo, true, false, settingsValues);
+            mSymbolsShiftedKeyboardId = getKeyboardId(editorInfo, true, true, settingsValues);
             setKeyboard(getKeyboard(mMainKeyboardId));
         } catch (RuntimeException e) {
             Log.w(TAG, "loading keyboard failed: " + mMainKeyboardId, e);
@@ -309,10 +292,9 @@
         return keyboard;
     }
 
-    private KeyboardId getKeyboardId(EditorInfo attribute, Locale locale, final int orientation,
-            final int width, final boolean isSymbols, final boolean isShift,
-            final boolean voiceKeyEnabled, final boolean voiceKeyOnMain) {
-        final int mode = Utils.getKeyboardMode(attribute);
+    private KeyboardId getKeyboardId(EditorInfo editorInfo, final boolean isSymbols,
+            final boolean isShift, Settings.Values settingsValues) {
+        final int mode = Utils.getKeyboardMode(editorInfo);
         final int xmlId;
         switch (mode) {
         case KeyboardId.MODE_PHONE:
@@ -330,16 +312,20 @@
             break;
         }
 
+        final boolean settingsKeyEnabled = settingsValues.isSettingsKeyEnabled(editorInfo);
+        final boolean voiceKeyEnabled = settingsValues.isVoiceKeyEnabled(editorInfo);
+        final boolean voiceKeyOnMain = settingsValues.isVoiceKeyOnMain();
         final boolean noSettingsKey = Utils.inPrivateImeOptions(
-                mPackageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, attribute);
-        final boolean hasSettingsKey = getSettingsKeyMode(mPrefs, mResources) && !noSettingsKey;
-        final int f2KeyMode = getF2KeyMode(mPrefs, mResources, mPackageName, attribute);
+                mPackageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, editorInfo);
+        final boolean hasSettingsKey = settingsKeyEnabled && !noSettingsKey;
+        final int f2KeyMode = getF2KeyMode(settingsKeyEnabled, noSettingsKey);
         final boolean hasVoiceKey = voiceKeyEnabled && (isSymbols != voiceKeyOnMain);
+        final Configuration conf = mResources.getConfiguration();
 
         return new KeyboardId(
-                mResources.getResourceEntryName(xmlId), xmlId, locale,
-                orientation, width, mode, attribute, hasSettingsKey, f2KeyMode, noSettingsKey,
-                voiceKeyEnabled, hasVoiceKey);
+                mResources.getResourceEntryName(xmlId), xmlId, mSubtypeSwitcher.getInputLocale(),
+                conf.orientation, mWindowWidthCache.getWidth(conf), mode, editorInfo,
+                hasSettingsKey, f2KeyMode, noSettingsKey, voiceKeyEnabled, hasVoiceKey);
     }
 
     public int getKeyboardMode() {
@@ -828,9 +814,9 @@
     @Override
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
         if (PREF_KEYBOARD_LAYOUT.equals(key)) {
-            final int layoutId = getKeyboardThemeIndex(mInputMethodService, sharedPreferences);
-            postSetInputView(createInputView(layoutId, false));
-        } else if (Settings.PREF_SETTINGS_KEY.equals(key)) {
+            final int themeIndex = getKeyboardThemeIndex(mInputMethodService, sharedPreferences);
+            postSetInputView(createInputView(themeIndex, false));
+        } else if (Settings.PREF_SHOW_SETTINGS_KEY.equals(key)) {
             postSetInputView(createInputView(mThemeIndex, true));
         }
     }
@@ -848,39 +834,18 @@
         }
     }
 
-    private static boolean getSettingsKeyMode(SharedPreferences prefs, Resources res) {
-        final boolean showSettingsKeyOption = res.getBoolean(
-                R.bool.config_enable_show_settings_key_option);
-        if (showSettingsKeyOption) {
-            final String settingsKeyMode = prefs.getString(Settings.PREF_SETTINGS_KEY,
-                    res.getString(DEFAULT_SETTINGS_KEY_MODE));
-            // We show the settings key when 1) SETTINGS_KEY_MODE_ALWAYS_SHOW or
-            // 2) SETTINGS_KEY_MODE_AUTO and there are two or more enabled IMEs on the system
-            if (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW))
-                    || (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_AUTO))
-                            && Utils.hasMultipleEnabledIMEsOrSubtypes(
-                                    (InputMethodManagerCompatWrapper.getInstance())))) {
-                return true;
-            }
-            return false;
-        }
-        // If the show settings key option is disabled, we always try showing the settings key.
-        return true;
-    }
-
-    private static int getF2KeyMode(SharedPreferences prefs, Resources res, String packageName,
-            EditorInfo attribute) {
-        final boolean clobberSettingsKey = Utils.inPrivateImeOptions(
-                packageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, attribute);
-        final String settingsKeyMode = prefs.getString(Settings.PREF_SETTINGS_KEY,
-                res.getString(DEFAULT_SETTINGS_KEY_MODE));
-        if (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_AUTO))) {
-            return clobberSettingsKey ? KeyboardId.F2KEY_MODE_SHORTCUT_IME
-                    : KeyboardId.F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS;
-        } else if (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW))) {
-            return clobberSettingsKey ? KeyboardId.F2KEY_MODE_NONE : KeyboardId.F2KEY_MODE_SETTINGS;
-        } else { // SETTINGS_KEY_MODE_ALWAYS_HIDE
+    private static int getF2KeyMode(boolean settingsKeyEnabled, boolean noSettingsKey) {
+        if (noSettingsKey) {
+            // Never shows the Settings key
             return KeyboardId.F2KEY_MODE_SHORTCUT_IME;
         }
+
+        if (settingsKeyEnabled) {
+            return KeyboardId.F2KEY_MODE_SETTINGS;
+        } else {
+            // It should be alright to fall back to the Settings key on 7-inch layouts
+            // even when the Settings key is not explicitly enabled.
+            return KeyboardId.F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS;
+        }
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index e44ae29..35af91b 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -61,7 +61,7 @@
     public static final String PREF_KEY_PREVIEW_POPUP_ON = "popup_on";
     public static final String PREF_RECORRECTION_ENABLED = "recorrection_enabled";
     public static final String PREF_AUTO_CAP = "auto_cap";
-    public static final String PREF_SETTINGS_KEY = "settings_key";
+    public static final String PREF_SHOW_SETTINGS_KEY = "show_settings_key";
     public static final String PREF_VOICE_SETTINGS_KEY = "voice_mode";
     public static final String PREF_INPUT_LANGUAGE = "input_language";
     public static final String PREF_SELECTED_LANGUAGES = "selected_languages";
@@ -118,6 +118,7 @@
         public final boolean mBigramPredictionEnabled;
         public final boolean mUseContactsDict;
 
+        private final boolean mShowSettingsKey;
         private final boolean mVoiceKeyEnabled;
         private final boolean mVoiceKeyOnMain;
 
@@ -165,21 +166,17 @@
             mVibrateOn = hasVibrator && prefs.getBoolean(Settings.PREF_VIBRATE_ON, false);
             mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON,
                     res.getBoolean(R.bool.config_default_sound_enabled));
-
             mKeyPreviewPopupOn = isKeyPreviewPopupEnabled(prefs, res);
             mKeyPreviewPopupDismissDelay = getKeyPreviewPopupDismissDelay(prefs, res);
             mAutoCap = prefs.getBoolean(Settings.PREF_AUTO_CAP, true);
-
             mAutoCorrectEnabled = isAutoCorrectEnabled(prefs, res);
             mBigramSuggestionEnabled = mAutoCorrectEnabled
                     && isBigramSuggestionEnabled(prefs, res, mAutoCorrectEnabled);
             mBigramPredictionEnabled = mBigramSuggestionEnabled
                     && isBigramPredictionEnabled(prefs, res);
-
             mAutoCorrectionThreshold = getAutoCorrectionThreshold(prefs, res);
-
             mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true);
-
+            mShowSettingsKey = prefs.getBoolean(Settings.PREF_SHOW_SETTINGS_KEY, false);
             final String voiceModeMain = res.getString(R.string.voice_mode_main);
             final String voiceModeOff = res.getString(R.string.voice_mode_off);
             final String voiceMode = prefs.getString(PREF_VOICE_SETTINGS_KEY, voiceModeMain);
@@ -284,6 +281,10 @@
             return builder.setIsPunctuationSuggestions().build();
         }
 
+        public boolean isSettingsKeyEnabled(EditorInfo attribute) {
+            return mShowSettingsKey;
+        }
+
         public boolean isVoiceKeyEnabled(EditorInfo attribute) {
             final boolean shortcutImeEnabled = SubtypeSwitcher.getInstance().isShortcutImeEnabled();
             final int inputType = (attribute != null) ? attribute.inputType : 0;
@@ -298,7 +299,7 @@
 
     private PreferenceScreen mInputLanguageSelection;
     private ListPreference mVoicePreference;
-    private ListPreference mSettingsKeyPreference;
+    private CheckBoxPreference mShowSettingsKeyPreference;
     private ListPreference mShowCorrectionSuggestionsPreference;
     private ListPreference mAutoCorrectionThreshold;
     private ListPreference mKeyPreviewPopupDismissDelay;
@@ -345,7 +346,7 @@
         mInputLanguageSelection = (PreferenceScreen) findPreference(PREF_SUBTYPES);
         mInputLanguageSelection.setOnPreferenceClickListener(this);
         mVoicePreference = (ListPreference) findPreference(PREF_VOICE_SETTINGS_KEY);
-        mSettingsKeyPreference = (ListPreference) findPreference(PREF_SETTINGS_KEY);
+        mShowSettingsKeyPreference = (CheckBoxPreference) findPreference(PREF_SHOW_SETTINGS_KEY);
         mShowCorrectionSuggestionsPreference =
                 (ListPreference) findPreference(PREF_SHOW_SUGGESTIONS_SETTING);
         SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
@@ -376,7 +377,7 @@
         final boolean showSettingsKeyOption = res.getBoolean(
                 R.bool.config_enable_show_settings_key_option);
         if (!showSettingsKeyOption) {
-            generalSettings.removePreference(mSettingsKeyPreference);
+            generalSettings.removePreference(mShowSettingsKeyPreference);
         }
 
         final boolean showVoiceKeyOption = res.getBoolean(
@@ -457,7 +458,6 @@
         } else {
             getPreferenceScreen().removePreference(mVoicePreference);
         }
-        updateSettingsKeySummary();
         updateShowCorrectionSuggestionsSummary();
         updateKeyPreviewPopupDelaySummary();
     }
@@ -489,7 +489,6 @@
         mVoiceOn = !(prefs.getString(PREF_VOICE_SETTINGS_KEY, mVoiceModeOff)
                 .equals(mVoiceModeOff));
         updateVoiceModeSummary();
-        updateSettingsKeySummary();
         updateShowCorrectionSuggestionsSummary();
         updateKeyPreviewPopupDelaySummary();
     }
@@ -513,12 +512,6 @@
                         mShowCorrectionSuggestionsPreference.getValue())]);
     }
 
-    private void updateSettingsKeySummary() {
-        mSettingsKeyPreference.setSummary(
-                getResources().getStringArray(R.array.settings_key_modes)
-                [mSettingsKeyPreference.findIndexOfValue(mSettingsKeyPreference.getValue())]);
-    }
-
     private void updateKeyPreviewPopupDelaySummary() {
         final ListPreference lp = mKeyPreviewPopupDismissDelay;
         lp.setSummary(lp.getEntries()[lp.findIndexOfValue(lp.getValue())]);
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index 6bdc0a8..23ef1a0 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -546,11 +546,11 @@
         }
     }
 
-    public static int getKeyboardMode(EditorInfo attribute) {
-        if (attribute == null)
+    public static int getKeyboardMode(EditorInfo editorInfo) {
+        if (editorInfo == null)
             return KeyboardId.MODE_TEXT;
 
-        final int inputType = attribute.inputType;
+        final int inputType = editorInfo.inputType;
         final int variation = inputType & InputType.TYPE_MASK_VARIATION;
 
         switch (inputType & InputType.TYPE_MASK_CLASS) {
@@ -587,11 +587,11 @@
     }
 
     public static boolean inPrivateImeOptions(String packageName, String key,
-            EditorInfo attribute) {
-        if (attribute == null)
+            EditorInfo editorInfo) {
+        if (editorInfo == null)
             return false;
         return containsInCsv(packageName != null ? packageName + "." + key : key,
-                attribute.privateImeOptions);
+                editorInfo.privateImeOptions);
     }
 
     /**