Refactor MoreKeysKeyboardView a bit

Bug: 12491371
Change-Id: I3ce1e6557e41a94146b882751f75ae4b5f6bc73d
diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
index 4a2b37e..50c82e5 100644
--- a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
@@ -110,25 +110,31 @@
     @Override
     public void onDownEvent(final int x, final int y, final int pointerId, final long eventTime) {
         mActivePointerId = pointerId;
-        onMoveKeyInternal(x, y, pointerId);
+        mCurrentKey = detectKey(x, y, pointerId);
     }
 
     @Override
-    public void onMoveEvent(int x, int y, final int pointerId, long eventTime) {
+    public void onMoveEvent(final int x, final int y, final int pointerId, final long eventTime) {
         if (mActivePointerId != pointerId) {
             return;
         }
         final boolean hasOldKey = (mCurrentKey != null);
-        onMoveKeyInternal(x, y, pointerId);
+        mCurrentKey = detectKey(x, y, pointerId);
         if (hasOldKey && mCurrentKey == null) {
-            // If the pointer has moved too far away from any target then cancel the panel.
+            // A more keys keyboard is canceled when detecting no key.
             mController.onCancelMoreKeysPanel();
         }
     }
 
     @Override
     public void onUpEvent(final int x, final int y, final int pointerId, final long eventTime) {
-        if (mCurrentKey != null && mActivePointerId == pointerId) {
+        if (mActivePointerId != pointerId) {
+            return;
+        }
+        // Calling {@link #detectKey(int,int,int)} here is harmless because the last move event and
+        // the following up event share the same coordinates.
+        mCurrentKey = detectKey(x, y, pointerId);
+        if (mCurrentKey != null) {
             updateReleaseKeyGraphics(mCurrentKey);
             onKeyInput(mCurrentKey, x, y);
             mCurrentKey = null;
@@ -152,23 +158,22 @@
         }
     }
 
-    private void onMoveKeyInternal(int x, int y, int pointerId) {
-        if (mActivePointerId != pointerId) {
-            // Ignore old pointers when newer pointer is active.
-            return;
-        }
+    private Key detectKey(int x, int y, int pointerId) {
         final Key oldKey = mCurrentKey;
         final Key newKey = mKeyDetector.detectHitKey(x, y);
-        if (newKey != oldKey) {
-            mCurrentKey = newKey;
-            invalidateKey(mCurrentKey);
-            if (oldKey != null) {
-                updateReleaseKeyGraphics(oldKey);
-            }
-            if (newKey != null) {
-                updatePressKeyGraphics(newKey);
-            }
+        if (newKey == oldKey) {
+            return newKey;
         }
+        // A new key is detected.
+        if (oldKey != null) {
+            updateReleaseKeyGraphics(oldKey);
+            invalidateKey(oldKey);
+        }
+        if (newKey != null) {
+            updatePressKeyGraphics(newKey);
+            invalidateKey(newKey);
+        }
+        return newKey;
     }
 
     private void updateReleaseKeyGraphics(final Key key) {
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index f807611..7bf2f6c 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -1033,8 +1033,7 @@
                 final int translatedY = mMoreKeysPanel.translateY(y);
                 mMoreKeysPanel.onUpEvent(translatedX, translatedY, mPointerId, eventTime);
             }
-            mMoreKeysPanel.dismissMoreKeysPanel();
-            mMoreKeysPanel = null;
+            dismissMoreKeysPanel();
             return;
         }
 
@@ -1101,10 +1100,7 @@
         sTimerProxy.cancelKeyTimersOf(this);
         setReleasedKeyGraphics(mCurrentKey);
         resetKeySelectionByDraggingFinger();
-        if (isShowingMoreKeysPanel()) {
-            mMoreKeysPanel.dismissMoreKeysPanel();
-            mMoreKeysPanel = null;
-        }
+        dismissMoreKeysPanel();
     }
 
     private boolean isMajorEnoughMoveToBeOnNewKey(final int x, final int y, final long eventTime,