Add input method subtype selector and IME settings dialog

Bug: 3351762
Change-Id: Ic1767faac6d4470a89cacb851d449ac53b2f8205
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index bc42dff..fb8c9b3 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -123,6 +123,9 @@
     private AlertDialog mOptionsDialog;
 
     private InputMethodManager mImm;
+    private Resources mResources;
+    private SharedPreferences mPrefs;
+    private String mInputMethodId;
     private KeyboardSwitcher mKeyboardSwitcher;
     private SubtypeSwitcher mSubtypeSwitcher;
     private VoiceIMEConnector mVoiceConnector;
@@ -132,9 +135,6 @@
     private ContactsDictionary mContactsDictionary;
     private AutoDictionary mAutoDictionary;
 
-    private Resources mResources;
-    private SharedPreferences mPrefs;
-
     // These variables are initialized according to the {@link EditorInfo#inputType}.
     private boolean mAutoSpace;
     private boolean mInputTypeNoAutoCorrect;
@@ -156,6 +156,7 @@
     private boolean mPopupOn;
     private boolean mAutoCap;
     private boolean mQuickFixes;
+    private boolean mConfigEnableShowSubtypeSettings;
     private boolean mConfigSwipeDownDismissKeyboardEnabled;
     private int mConfigDelayBeforeFadeoutLanguageOnSpacebar;
     private int mConfigDurationOfFadeoutLanguageOnSpacebar;
@@ -350,6 +351,7 @@
         super.onCreate();
 
         mImm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
+        mInputMethodId = Utils.getInputMethodId(mImm, getApplicationInfo().packageName);
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
         mKeyboardSwitcher = KeyboardSwitcher.getInstance();
 
@@ -365,6 +367,8 @@
             mReCorrectionEnabled = res.getBoolean(R.bool.default_recorrection_enabled);
         }
 
+        mConfigEnableShowSubtypeSettings = res.getBoolean(
+                R.bool.config_enable_show_subtype_settings);
         mConfigSwipeDownDismissKeyboardEnabled = res.getBoolean(
                 R.bool.config_swipe_down_dismiss_keyboard_enabled);
         mConfigDelayBeforeFadeoutLanguageOnSpacebar = res.getInteger(
@@ -462,6 +466,8 @@
             commitTyped(ic);
             if (ic != null) ic.finishComposingText(); // For voice input
             mOrientation = conf.orientation;
+            if (isShowingOptionDialog())
+                mOptionsDialog.dismiss();
         }
 
         mConfigurationChanging = true;
@@ -1044,7 +1050,9 @@
 
     private void onSettingsKeyPressed() {
         if (!isShowingOptionDialog()) {
-            if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) {
+            if (!mConfigEnableShowSubtypeSettings) {
+                showSubtypeSelectorAndSettings();
+            } else if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) {
                 showOptionsMenu();
             } else {
                 launchSettings();
@@ -2183,7 +2191,48 @@
         return mSuggestPuncs.contains(String.valueOf((char)code));
     }
 
+    private void showSubtypeSelectorAndSettings() {
+        showOptionsMenuInternal(new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface di, int position) {
+                di.dismiss();
+                switch (position) {
+                case POS_SETTINGS:
+                    launchSettings();
+                    break;
+                case POS_METHOD:
+                    Intent intent = new Intent(
+                            android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
+                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
+                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
+                            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                    intent.putExtra(android.provider.Settings.EXTRA_INPUT_METHOD_ID,
+                            mInputMethodId);
+                    startActivity(intent);
+                    break;
+                }
+            }
+        });
+    }
+
     private void showOptionsMenu() {
+        showOptionsMenuInternal(new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface di, int position) {
+                di.dismiss();
+                switch (position) {
+                case POS_SETTINGS:
+                    launchSettings();
+                    break;
+                case POS_METHOD:
+                    mImm.showInputMethodPicker();
+                    break;
+                }
+            }
+        });
+    }
+
+    private void showOptionsMenuInternal(DialogInterface.OnClickListener listener) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setCancelable(true);
         builder.setIcon(R.drawable.ic_dialog_keyboard);
@@ -2191,22 +2240,7 @@
         CharSequence itemSettings = getString(R.string.english_ime_settings);
         CharSequence itemInputMethod = getString(R.string.selectInputMethod);
         builder.setItems(new CharSequence[] {
-                itemInputMethod, itemSettings},
-                new DialogInterface.OnClickListener() {
-
-            @Override
-            public void onClick(DialogInterface di, int position) {
-                di.dismiss();
-                switch (position) {
-                    case POS_SETTINGS:
-                        launchSettings();
-                        break;
-                    case POS_METHOD:
-                        mImm.showInputMethodPicker();
-                        break;
-                }
-            }
-        });
+                itemInputMethod, itemSettings}, listener);
         builder.setTitle(mResources.getString(R.string.english_ime_input_options));
         mOptionsDialog = builder.create();
         Window window = mOptionsDialog.getWindow();
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index d2582b1..5059860 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -23,6 +23,7 @@
 import android.os.Process;
 import android.text.format.DateUtils;
 import android.util.Log;
+import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
 
 import java.io.BufferedReader;
@@ -97,6 +98,13 @@
                 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
     }
 
+    public static String getInputMethodId(InputMethodManager imm, String packageName) {
+        for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
+            if (imi.getPackageName().equals(packageName))
+                return imi.getId();
+        }
+        throw new RuntimeException("Can not find input method id for " + packageName);
+    }
 
     public static boolean shouldBlockedBySafetyNetForAutoCorrection(SuggestedWords suggestions) {
         // Safety net for auto correction.