From 1931c1cab1c2b077030210f2dd7a1839da7211bc Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Tue, 2 Oct 2012 18:40:06 +0900
Subject: [PATCH] Fix language switch key behavior

Bug: 7075923
Change-Id: I306e1ff5d9f873541e84e1fafb64b9473947f623
---
 .../android/inputmethod/latin/LatinIME.java   | 48 ++++++++++++-------
 .../latin/RichInputMethodManager.java         |  5 ++
 2 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 3dfffc4a6a..0711a23866 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -56,7 +56,6 @@ import android.view.WindowManager;
 import android.view.inputmethod.CompletionInfo;
 import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.EditorInfo;
-import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
@@ -142,7 +141,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
     private SharedPreferences mPrefs;
     @UsedForTesting final KeyboardSwitcher mKeyboardSwitcher;
     private final SubtypeSwitcher mSubtypeSwitcher;
-    private boolean mShouldSwitchToLastSubtype = true;
+    private final SubtypeState mSubtypeState = new SubtypeState();
 
     private boolean mIsMainDictionaryAvailable;
     private UserBinaryDictionary mUserDictionary;
@@ -366,6 +365,33 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
         }
     }
 
+    static final class SubtypeState {
+        private InputMethodSubtype mLastActiveSubtype;
+        private boolean mCurrentSubtypeUsed;
+
+        public void currentSubtypeUsed() {
+            mCurrentSubtypeUsed = true;
+        }
+
+        public void switchSubtype(final IBinder token, final RichInputMethodManager richImm) {
+            final InputMethodSubtype currentSubtype = richImm.getInputMethodManager()
+                    .getCurrentInputMethodSubtype();
+            final InputMethodSubtype lastActiveSubtype = mLastActiveSubtype;
+            final boolean currentSubtypeUsed = mCurrentSubtypeUsed;
+            if (currentSubtypeUsed) {
+                mLastActiveSubtype = currentSubtype;
+                mCurrentSubtypeUsed = false;
+            }
+            if (currentSubtypeUsed
+                    && richImm.checkIfSubtypeBelongsToThisImeAndEnabled(lastActiveSubtype)
+                    && !currentSubtype.equals(lastActiveSubtype)) {
+                richImm.setInputMethodAndSubtype(token, lastActiveSubtype);
+                return;
+            }
+            richImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
+        }
+    }
+
     public LatinIME() {
         super();
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
@@ -897,6 +923,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
         // Make a note of the cursor position
         mLastSelectionStart = newSelStart;
         mLastSelectionEnd = newSelEnd;
+        mSubtypeState.currentSubtypeUsed();
     }
 
     /**
@@ -1244,20 +1271,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
             mRichImm.switchToNextInputMethod(token, false /* onlyCurrentIme */);
             return;
         }
-        if (mShouldSwitchToLastSubtype) {
-            final InputMethodManager imm = mRichImm.getInputMethodManager();
-            final InputMethodSubtype lastSubtype = imm.getLastInputMethodSubtype();
-            final boolean lastSubtypeBelongsToThisIme =
-                    mRichImm.checkIfSubtypeBelongsToThisImeAndEnabled(lastSubtype);
-            if (lastSubtypeBelongsToThisIme && imm.switchToLastInputMethod(token)) {
-                mShouldSwitchToLastSubtype = false;
-            } else {
-                mRichImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
-                mShouldSwitchToLastSubtype = true;
-            }
-        } else {
-            mRichImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
-        }
+        mSubtypeState.switchSubtype(token, mRichImm);
     }
 
     private void sendDownUpKeyEventForBackwardCompatibility(final int code) {
@@ -1326,7 +1340,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
             handleBackspace(spaceState);
             mDeleteCount++;
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             LatinImeLogger.logOnDelete(x, y);
             break;
         case Constants.CODE_SHIFT:
@@ -1382,7 +1395,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
                 handleCharacter(primaryCode, keyX, keyY, spaceState);
             }
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             break;
         }
         switcher.onCodeInput(primaryCode);
diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
index d7055f7475..b3832303d0 100644
--- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
+++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
@@ -206,6 +206,11 @@ public final class RichInputMethodManager {
         return null;
     }
 
+    public void setInputMethodAndSubtype(final IBinder token, final InputMethodSubtype subtype) {
+        mImmWrapper.mImm.setInputMethodAndSubtype(
+                token, mInputMethodInfoOfThisIme.getId(), subtype);
+    }
+
     public void setAdditionalInputMethodSubtypes(final InputMethodSubtype[] subtypes) {
         mImmWrapper.mImm.setAdditionalInputMethodSubtypes(
                 mInputMethodInfoOfThisIme.getId(), subtypes);
-- 
GitLab