From cadb2128f54b49be31bb4dc06374afe81ed028b7 Mon Sep 17 00:00:00 2001
From: Ken Wakasa <kwakasa@google.com>
Date: Sat, 6 Aug 2011 10:45:19 +0900
Subject: [PATCH] Fix issues with long-pressing the spacebar

bug: 5114433
Change-Id: I18f2147724a08965147bafe93e11fc86c7c59d33
---
 java/res/values/keycodes.xml                  |  2 +-
 .../inputmethod/keyboard/Keyboard.java        |  3 +-
 .../keyboard/KeyboardActionListener.java      |  6 +++
 .../keyboard/LatinKeyboardView.java           | 10 ++++-
 .../inputmethod/keyboard/PointerTracker.java  |  2 +
 .../keyboard/PopupMiniKeyboardView.java       |  2 +
 .../android/inputmethod/latin/LatinIME.java   | 37 +++++++++++--------
 .../com/android/inputmethod/latin/Utils.java  | 35 +++++++++++-------
 8 files changed, 62 insertions(+), 35 deletions(-)

diff --git a/java/res/values/keycodes.xml b/java/res/values/keycodes.xml
index ee345291a3..59cc075318 100644
--- a/java/res/values/keycodes.xml
+++ b/java/res/values/keycodes.xml
@@ -27,5 +27,5 @@
     <integer name="key_switch_alpha_symbol">-2</integer>
     <integer name="key_delete">-5</integer>
     <integer name="key_settings">-6</integer>
-    <integer name="key_shortcut">-8</integer>
+    <integer name="key_shortcut">-7</integer>
 </resources>
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index 518bc8e9e9..f8e08b06a7 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -79,8 +79,7 @@ public class Keyboard {
     public static final int CODE_CANCEL = -4;
     public static final int CODE_DELETE = -5;
     public static final int CODE_SETTINGS = -6;
-    public static final int CODE_SETTINGS_LONGPRESS = -7;
-    public static final int CODE_SHORTCUT = -8;
+    public static final int CODE_SHORTCUT = -7;
     // Code value representing the code is not specified.
     public static final int CODE_UNSPECIFIED = -99;
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java
index 905f779c0b..8640912891 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java
@@ -70,4 +70,10 @@ public interface KeyboardActionListener {
      * Called when user released a finger outside any key.
      */
     public void onCancelInput();
+
+    /**
+     * Send a non-"code input" custom request to the listener.
+     * @return true if the request has been consumed, false otherwise.
+     */
+    public boolean onCustomRequest(int requestCode);
 }
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
index 04096778b4..dad37e7288 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
@@ -23,6 +23,7 @@ import android.util.Log;
 import android.view.MotionEvent;
 
 import com.android.inputmethod.deprecated.VoiceProxy;
+import com.android.inputmethod.latin.LatinIME;
 import com.android.inputmethod.latin.LatinImeLogger;
 import com.android.inputmethod.latin.Utils;
 
@@ -99,9 +100,14 @@ public class LatinKeyboardView extends LatinKeyboardBaseView {
             }
         }
         if (primaryCode == Keyboard.CODE_SETTINGS || primaryCode == Keyboard.CODE_SPACE) {
-            tracker.onLongPressed();
             // Both long pressing settings key and space key invoke IME switcher dialog.
-            return invokeOnKey(Keyboard.CODE_SETTINGS_LONGPRESS);
+            if (getKeyboardActionListener().onCustomRequest(
+                    LatinIME.CODE_SHOW_INPUT_METHOD_PICKER)) {
+                tracker.onLongPressed();
+                return true;
+            } else {
+                return super.onLongPress(key, tracker);
+            }
         } else {
             return super.onLongPress(key, tracker);
         }
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index dc2d6e4b01..1f8119a0fb 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -151,6 +151,8 @@ public class PointerTracker {
         public void onTextInput(CharSequence text) {}
         @Override
         public void onCancelInput() {}
+        @Override
+        public boolean onCustomRequest(int requestCode) { return false; }
     };
 
     public static void init(boolean hasDistinctMultitouch, Context context) {
diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java
index dfaaa707c2..fb932e3e82 100644
--- a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java
@@ -79,6 +79,8 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel {
         public void onRelease(int primaryCode, boolean withSliding) {
             mParentKeyboardView.getKeyboardActionListener().onRelease(primaryCode, withSliding);
         }
+        @Override
+        public boolean onCustomRequest(int requestCode) { return false; }
     };
 
     public PopupMiniKeyboardView(Context context, AttributeSet attrs) {
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 6c91c454c0..5ccbf3fa26 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -430,8 +430,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         public boolean postStartInputView(EditorInfo attribute) {
             if (hasMessages(MSG_CONFIRM_ORIENTATION_CHANGE) || hasMessages(MSG_START_INPUT_VIEW)) {
                 removeMessages(MSG_START_INPUT_VIEW);
-                // Postpone onStartInputView 20ms afterward and see if orientation change has
-                // finished.
+                // Postpone onStartInputView by ACCUMULATE_START_INPUT_VIEW_DELAY and see if
+                // orientation change has finished.
                 sendMessageDelayed(obtainMessage(MSG_START_INPUT_VIEW, attribute),
                         ACCUMULATE_START_INPUT_VIEW_DELAY);
                 return true;
@@ -1152,25 +1152,33 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
     }
 
     private void onSettingsKeyPressed() {
-        if (isShowingOptionDialog())
-            return;
+        if (isShowingOptionDialog()) return;
         if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) {
             showSubtypeSelectorAndSettings();
-        } else if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) {
+        } else if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm,
+                false /* should exclude auxiliary subtypes */)) {
             showOptionsMenu();
         } else {
             launchSettings();
         }
     }
 
-    private void onSettingsKeyLongPressed() {
-        if (!isShowingOptionDialog()) {
-            if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) {
+    // Virtual codes representing custom requests.  These are used in onCustomRequest() below.
+    public static final int CODE_SHOW_INPUT_METHOD_PICKER = 1;
+
+    @Override
+    public boolean onCustomRequest(int requestCode) {
+        if (isShowingOptionDialog()) return false;
+        switch (requestCode) {
+        case CODE_SHOW_INPUT_METHOD_PICKER:
+            if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm,
+                    true /* should include auxiliary subtypes */)) {
                 mImm.showInputMethodPicker();
-            } else {
-                launchSettings();
+                return true;
             }
+            return false;
         }
+        return false;
     }
 
     private boolean isShowingOptionDialog() {
@@ -1214,9 +1222,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         case Keyboard.CODE_SETTINGS:
             onSettingsKeyPressed();
             break;
-        case Keyboard.CODE_SETTINGS_LONGPRESS:
-            onSettingsKeyLongPressed();
-            break;
         case Keyboard.CODE_CAPSLOCK:
             switcher.toggleCapsLock();
             break;
@@ -2135,14 +2140,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
     }
 
     protected void launchSettings() {
-        launchSettings(Settings.class);
+        launchSettingsClass(Settings.class);
     }
 
     public void launchDebugSettings() {
-        launchSettings(DebugSettings.class);
+        launchSettingsClass(DebugSettings.class);
     }
 
-    protected void launchSettings(Class<? extends PreferenceActivity> settingsClass) {
+    protected void launchSettingsClass(Class<? extends PreferenceActivity> settingsClass) {
         handleClose();
         Intent intent = new Intent();
         intent.setClass(LatinIME.this, settingsClass);
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index 16a2b0e5f3..c07793c330 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -111,35 +111,42 @@ public class Utils {
         }
     }
 
-    public static boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManagerCompatWrapper imm) {
+    public static boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManagerCompatWrapper imm,
+            boolean shouldIncludeAuxiliarySubtypes) {
         final List<InputMethodInfoCompatWrapper> enabledImis = imm.getEnabledInputMethodList();
 
-        // Filters out IMEs that have auxiliary subtypes only (including either implicitly or
-        // explicitly enabled ones).
-        final ArrayList<InputMethodInfoCompatWrapper> filteredImis =
-                new ArrayList<InputMethodInfoCompatWrapper>();
+        // Number of the filtered IMEs
+        int filteredImisCount = 0;
 
-        outerloop:
         for (InputMethodInfoCompatWrapper imi : enabledImis) {
             // We can return true immediately after we find two or more filtered IMEs.
-            if (filteredImis.size() > 1) return true;
+            if (filteredImisCount > 1) return true;
             final List<InputMethodSubtypeCompatWrapper> subtypes =
                     imm.getEnabledInputMethodSubtypeList(imi, true);
-            // IMEs that have no subtypes should be included.
+            // IMEs that have no subtypes should be counted.
             if (subtypes.isEmpty()) {
-                filteredImis.add(imi);
+                ++filteredImisCount;
                 continue;
             }
-            // IMEs that have one or more non-auxiliary subtypes should be included.
+
+            int auxCount = 0;
             for (InputMethodSubtypeCompatWrapper subtype : subtypes) {
-                if (!subtype.isAuxiliary()) {
-                    filteredImis.add(imi);
-                    continue outerloop;
+                if (subtype.isAuxiliary()) {
+                    ++auxCount;
                 }
             }
+            final int nonAuxCount = subtypes.size() - auxCount;
+
+            // IMEs that have one or more non-auxiliary subtypes should be counted.
+            // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary
+            // subtypes should be counted as well.
+            if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) {
+                ++filteredImisCount;
+                continue;
+            }
         }
 
-        return filteredImis.size() > 1
+        return filteredImisCount > 1
         // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
         // input method subtype (The current IME should be LatinIME.)
                 || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
-- 
GitLab