From 5f922caff80d5067c5af2bbbae2731ef25c9572a Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Thu, 30 Dec 2010 17:19:55 +0900
Subject: [PATCH] Snap back to the previous keyboard when sliding input is
 canceled

Bug: 3316517
Change-Id: Iffaad1eb93b6a014d8445f3e27b0e24c20967daf
---
 .../keyboard/KeyboardActionListener.java      | 12 ++++++------
 .../keyboard/KeyboardSwitcher.java            | 19 ++++++++++++++-----
 .../inputmethod/keyboard/KeyboardView.java    |  3 ++-
 .../android/inputmethod/latin/LatinIME.java   |  7 +++----
 4 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java
index 848cc96939..00f3a51531 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java
@@ -20,7 +20,7 @@ public interface KeyboardActionListener {
 
     /**
      * Called when the user presses a key. This is sent before the
-     * {@link #onKey} is called. For keys that repeat, this is only
+     * {@link #onCodeInput} is called. For keys that repeat, this is only
      * called once.
      *
      * @param primaryCode
@@ -31,7 +31,7 @@ public interface KeyboardActionListener {
 
     /**
      * Called when the user releases a key. This is sent after the
-     * {@link #onKey} is called. For keys that repeat, this is only
+     * {@link #onCodeInput} is called. For keys that repeat, this is only
      * called once.
      *
      * @param primaryCode
@@ -54,11 +54,11 @@ public interface KeyboardActionListener {
      *            accidental presses of a key adjacent to the intended
      *            key.
      * @param x
-     *            x-coordinate pixel of touched event. If onKey is not called by onTouchEvent,
-     *            the value should be NOT_A_TOUCH_COORDINATE.
+     *            x-coordinate pixel of touched event. If {@link #onCodeInput} is not called by
+     *            onTouchEvent, the value should be NOT_A_TOUCH_COORDINATE.
      * @param y
-     *            y-coordinate pixel of touched event. If onKey is not called by onTouchEvent,
-     *            the value should be NOT_A_TOUCH_COORDINATE.
+     *            y-coordinate pixel of touched event. If {@link #onCodeInput} is not called by
+     *            onTouchEvent, the value should be NOT_A_TOUCH_COORDINATE.
      */
     void onCodeInput(int primaryCode, int[] keyCodes, int x, int y);
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index d085030a12..331c8db6ac 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -505,6 +505,12 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
         mSymbolKeyState.onOtherKeyPressed();
     }
 
+    public void onCancelInput() {
+        // Snap back to the previous keyboard mode if the user cancels sliding input.
+        if (mAutoModeSwitchState == AUTO_MODE_SWITCH_STATE_MOMENTARY && getPointerCount() == 1)
+            changeKeyboardMode();
+    }
+
     private void toggleShiftInSymbol() {
         if (isAlphabetMode())
             return;
@@ -563,11 +569,12 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
         switch (mAutoModeSwitchState) {
         case AUTO_MODE_SWITCH_STATE_MOMENTARY:
             // Only distinct multi touch devices can be in this state.
-            // On non-distinct multi touch devices, mode change key is handled by {@link onKey},
-            // not by {@link onPress} and {@link onRelease}. So, on such devices,
-            // {@link mAutoModeSwitchState} starts from {@link AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN},
-            // or {@link AUTO_MODE_SWITCH_STATE_ALPHA}, not from
-            // {@link AUTO_MODE_SWITCH_STATE_MOMENTARY}.
+            // On non-distinct multi touch devices, mode change key is handled by
+            // {@link LatinIME#onCodeInput}, not by {@link LatinIME#onPress} and
+            // {@link LatinIME#onRelease}. So, on such devices, {@link #mAutoModeSwitchState} starts
+            // from {@link #AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN}, or
+            // {@link #AUTO_MODE_SWITCH_STATE_ALPHA}, not from
+            // {@link #AUTO_MODE_SWITCH_STATE_MOMENTARY}.
             if (key == Keyboard.CODE_SWITCH_ALPHA_SYMBOL) {
                 // Detected only the mode change key has been pressed, and then released.
                 if (mIsSymbols) {
@@ -578,6 +585,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
             } else if (getPointerCount() == 1) {
                 // Snap back to the previous keyboard mode if the user pressed the mode change key
                 // and slid to other key, then released the finger.
+                // If the user cancels the sliding input, snapping back to the previous keyboard
+                // mode is handled by {@link #onCancelInput}.
                 changeKeyboardMode();
             } else {
                 // Chording input is being started. The keyboard mode will be snapped back to the
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index dd552f0fc5..e0835d7edf 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -550,7 +550,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy {
     }
 
     /**
-     * When enabled, calls to {@link KeyboardActionListener#onKey} will include key
+     * When enabled, calls to {@link KeyboardActionListener#onCodeInput} will include key
      * codes for adjacent keys.  When disabled, only the primary key code will be
      * reported.
      * @param enabled whether or not the proximity correction is enabled
@@ -1106,6 +1106,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy {
 
             @Override
             public void onCancelInput() {
+                mKeyboardActionListener.onCancelInput();
                 dismissPopupKeyboard();
             }
 
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 51b56ec14c..e785eb76f3 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1048,8 +1048,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
         return mOptionsDialog != null && mOptionsDialog.isShowing();
     }
 
-    // Implementation of KeyboardViewListener
-
+    // Implementation of {@link KeyboardActionListener}.
     @Override
     public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) {
         long when = SystemClock.uptimeMillis();
@@ -1132,7 +1131,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
         ic.commitText(text, 1);
         ic.endBatchEdit();
         mKeyboardSwitcher.updateShiftState();
-        mKeyboardSwitcher.onKey(0); // dummy key code.
+        mKeyboardSwitcher.onKey(Keyboard.CODE_DUMMY);
         mJustReverted = false;
         mJustAddedAutoSpace = false;
         mEnteredText = text;
@@ -1141,6 +1140,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
     @Override
     public void onCancelInput() {
         // User released a finger outside any key
+        mKeyboardSwitcher.onCancelInput();
     }
 
     private void handleBackspace() {
@@ -1836,7 +1836,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
     private void sendSpace() {
         sendKeyChar((char)Keyboard.CODE_SPACE);
         mKeyboardSwitcher.updateShiftState();
-        //onKey(KEY_SPACE[0], KEY_SPACE);
     }
 
     public boolean preferCapitalization() {
-- 
GitLab