From c403a46f6d787b79768895272d53d296100677dd Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Tue, 23 Aug 2011 15:57:51 +0900
Subject: [PATCH] Extract sudden jumping touch event hack into separate class

Bug: 5182291
Change-Id: I6a88ed4df3ec98e31ea4966d82da56f7fca342ac
---
 ...sudden-jumping-touch-event-device-list.xml | 27 ++++++++++
 .../keyboard/KeyboardSwitcher.java            |  8 +--
 .../inputmethod/keyboard/LatinKeyboard.java   |  2 +-
 .../keyboard/LatinKeyboardBaseView.java       | 13 ++++-
 ...va => SuddenJumpingTouchEventHandler.java} | 54 ++++++++++---------
 .../android/inputmethod/latin/LatinIME.java   | 12 ++---
 6 files changed, 80 insertions(+), 36 deletions(-)
 create mode 100644 java/res/values/sudden-jumping-touch-event-device-list.xml
 rename java/src/com/android/inputmethod/keyboard/{LatinKeyboardView.java => SuddenJumpingTouchEventHandler.java} (78%)

diff --git a/java/res/values/sudden-jumping-touch-event-device-list.xml b/java/res/values/sudden-jumping-touch-event-device-list.xml
new file mode 100644
index 000000000..275a262d9
--- /dev/null
+++ b/java/res/values/sudden-jumping-touch-event-device-list.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+<resources>
+    <string-array name="sudden_jumping_touch_event_device_list">
+        <!-- Nexus One -->
+        <item>passion</item>
+        <!-- Droid -->
+        <item>sholes</item>
+    </string-array>
+</resources>
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 8bf82807a..ec19cce6c 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -61,7 +61,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
     private SharedPreferences mPrefs;
 
     private View mCurrentInputView;
-    private LatinKeyboardView mKeyboardView;
+    private LatinKeyboardBaseView mKeyboardView;
     private LatinIME mInputMethodService;
     private String mPackageName;
     private Resources mResources;
@@ -745,7 +745,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
         }
     }
 
-    public LatinKeyboardView getKeyboardView() {
+    public LatinKeyboardBaseView getKeyboardView() {
         return mKeyboardView;
     }
 
@@ -781,7 +781,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
             }
         }
 
-        mKeyboardView = (LatinKeyboardView) mCurrentInputView.findViewById(R.id.keyboard_view);
+        mKeyboardView = (LatinKeyboardBaseView) mCurrentInputView.findViewById(R.id.keyboard_view);
         mKeyboardView.setKeyboardActionListener(mInputMethodService);
 
         // This always needs to be set since the accessibility state can
@@ -819,7 +819,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
             final LatinKeyboard keyboard = getLatinKeyboard();
             if (keyboard != null && keyboard.needsAutoCorrectionSpacebarLed()) {
                 final Key invalidatedKey = keyboard.onAutoCorrectionStateChanged(isAutoCorrection);
-                final LatinKeyboardView keyboardView = getKeyboardView();
+                final LatinKeyboardBaseView keyboardView = getKeyboardView();
                 if (keyboardView != null)
                     keyboardView.invalidateKey(invalidatedKey);
             }
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java
index 3b3e1f87e..d31d3e39f 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java
@@ -154,7 +154,7 @@ public class LatinKeyboard extends Keyboard {
         return newColor;
     }
 
-    public void updateShortcutKey(boolean available, LatinKeyboardView view) {
+    public void updateShortcutKey(boolean available, LatinKeyboardBaseView view) {
         if (mShortcutKey == null)
             return;
         mShortcutKey.setEnabled(available);
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java
index 4a7b2bd60..4b85bcbec 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java
@@ -53,11 +53,14 @@ import java.util.WeakHashMap;
  * @attr ref R.styleable#KeyboardView_verticalCorrection
  * @attr ref R.styleable#KeyboardView_popupLayout
  */
-public class LatinKeyboardBaseView extends KeyboardView implements PointerTracker.KeyEventHandler {
+public class LatinKeyboardBaseView extends KeyboardView implements PointerTracker.KeyEventHandler,
+        SuddenJumpingTouchEventHandler.ProcessMotionEvent {
     private static final String TAG = LatinKeyboardBaseView.class.getSimpleName();
 
     private static final boolean ENABLE_CAPSLOCK_BY_DOUBLETAP = true;
 
+    private final SuddenJumpingTouchEventHandler mTouchScreenRegulator;
+
     // Timing constants
     private final int mKeyRepeatInterval;
 
@@ -213,6 +216,8 @@ public class LatinKeyboardBaseView extends KeyboardView implements PointerTracke
     public LatinKeyboardBaseView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
 
+        mTouchScreenRegulator = new SuddenJumpingTouchEventHandler(getContext(), this);
+
         final TypedArray a = context.obtainStyledAttributes(
                 attrs, R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
         mVerticalCorrection = a.getDimensionPixelOffset(
@@ -300,6 +305,7 @@ public class LatinKeyboardBaseView extends KeyboardView implements PointerTracke
                 keyboard, -getPaddingLeft(), -getPaddingTop() + mVerticalCorrection);
         mKeyDetector.setProximityThreshold(keyboard.mMostCommonKeyWidth);
         PointerTracker.setKeyDetector(mKeyDetector);
+        mTouchScreenRegulator.setKeyboard(keyboard);
         mPopupPanelCache.clear();
     }
 
@@ -481,6 +487,11 @@ public class LatinKeyboardBaseView extends KeyboardView implements PointerTracke
 
     @Override
     public boolean onTouchEvent(MotionEvent me) {
+        return mTouchScreenRegulator.onTouchEvent(me);
+    }
+
+    @Override
+    public boolean processMotionEvent(MotionEvent me) {
         final boolean nonDistinctMultitouch = !mHasDistinctMultitouch;
         final int action = me.getActionMasked();
         final int pointerCount = me.getPointerCount();
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java
similarity index 78%
rename from java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
rename to java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java
index 42ce7c440..5344c27d4 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2011 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
@@ -17,19 +17,24 @@
 package com.android.inputmethod.keyboard;
 
 import android.content.Context;
-import android.graphics.Canvas;
-import android.util.AttributeSet;
+import android.os.Build;
 import android.util.Log;
 import android.view.MotionEvent;
 
 import com.android.inputmethod.latin.LatinImeLogger;
-import com.android.inputmethod.latin.Utils;
+import com.android.inputmethod.latin.R;
 
-// TODO: We should remove this class
-public class LatinKeyboardView extends LatinKeyboardBaseView {
-    private static final String TAG = LatinKeyboardView.class.getSimpleName();
+public class SuddenJumpingTouchEventHandler {
+    private static final String TAG = SuddenJumpingTouchEventHandler.class.getSimpleName();
     private static boolean DEBUG_MODE = LatinImeLogger.sDBG;
 
+    public interface ProcessMotionEvent {
+        public boolean processMotionEvent(MotionEvent me);
+    }
+
+    private final ProcessMotionEvent mView;
+    private final boolean mNeedsSuddenJumpingHack;
+
     /** Whether we've started dropping move events because we found a big jump */
     private boolean mDroppingEvents;
     /**
@@ -42,17 +47,23 @@ public class LatinKeyboardView extends LatinKeyboardBaseView {
     private int mLastX;
     private int mLastY;
 
-    public LatinKeyboardView(Context context, AttributeSet attrs) {
-        super(context, attrs);
+    public SuddenJumpingTouchEventHandler(Context context, ProcessMotionEvent view) {
+        mView = view;
+        final String[] deviceList = context.getResources().getStringArray(
+                R.array.sudden_jumping_touch_event_device_list);
+        mNeedsSuddenJumpingHack = needsSuddenJumpingHack(Build.DEVICE, deviceList);
     }
 
-    public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {
-        super(context, attrs, defStyle);
+    private static boolean needsSuddenJumpingHack(String deviceName, String[] deviceList) {
+        for (String device : deviceList) {
+            if (device.equalsIgnoreCase(deviceName)) {
+                return true;
+            }
+        }
+        return false;
     }
 
-    @Override
     public void setKeyboard(Keyboard newKeyboard) {
-        super.setKeyboard(newKeyboard);
         // One-seventh of the keyboard width seems like a reasonable threshold
         final int jumpThreshold = newKeyboard.mOccupiedWidth / 7;
         mJumpThresholdSquare = jumpThreshold * jumpThreshold;
@@ -69,9 +80,8 @@ public class LatinKeyboardView extends LatinKeyboardBaseView {
      * @return true if the event was consumed, so that it doesn't continue to be handled by
      * {@link LatinKeyboardBaseView}.
      */
-    private boolean handleSuddenJump(MotionEvent me) {
-        // If device has distinct multi touch panel, there is no need to check sudden jump.
-        if (hasDistinctMultitouch())
+    private boolean handleSuddenJumping(MotionEvent me) {
+        if (!mNeedsSuddenJumpingHack)
             return false;
         final int action = me.getAction();
         final int x = (int) me.getX();
@@ -107,7 +117,7 @@ public class LatinKeyboardView extends LatinKeyboardBaseView {
                             me.getEventTime(), me.getEventTime(),
                             MotionEvent.ACTION_UP,
                             mLastX, mLastY, me.getMetaState());
-                    super.onTouchEvent(translated);
+                    mView.processMotionEvent(translated);
                     translated.recycle();
                 }
                 result = true;
@@ -123,7 +133,7 @@ public class LatinKeyboardView extends LatinKeyboardBaseView {
                 MotionEvent translated = MotionEvent.obtain(me.getEventTime(), me.getEventTime(),
                         MotionEvent.ACTION_DOWN,
                         x, y, me.getMetaState());
-                super.onTouchEvent(translated);
+                mView.processMotionEvent(translated);
                 translated.recycle();
                 mDroppingEvents = false;
                 // Let the up event get processed as well, result = false
@@ -136,17 +146,13 @@ public class LatinKeyboardView extends LatinKeyboardBaseView {
         return result;
     }
 
-    @Override
     public boolean onTouchEvent(MotionEvent me) {
-        if (getKeyboard() == null) return true;
-
         // If there was a sudden jump, return without processing the actual motion event.
-        if (handleSuddenJump(me)) {
+        if (handleSuddenJumping(me)) {
             if (DEBUG_MODE)
                 Log.w(TAG, "onTouchEvent: ignore sudden jump " + me);
             return true;
         }
-
-        return super.onTouchEvent(me);
+        return mView.processMotionEvent(me);
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index afbdd36a9..e72b43ae9 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -67,7 +67,7 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher;
 import com.android.inputmethod.keyboard.KeyboardSwitcher.KeyboardLayoutState;
 import com.android.inputmethod.keyboard.KeyboardView;
 import com.android.inputmethod.keyboard.LatinKeyboard;
-import com.android.inputmethod.keyboard.LatinKeyboardView;
+import com.android.inputmethod.keyboard.LatinKeyboardBaseView;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -252,7 +252,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         public void handleMessage(Message msg) {
             final LatinIME latinIme = getOuterInstance();
             final KeyboardSwitcher switcher = latinIme.mKeyboardSwitcher;
-            final LatinKeyboardView inputView = switcher.getKeyboardView();
+            final LatinKeyboardBaseView inputView = switcher.getKeyboardView();
             switch (msg.what) {
             case MSG_UPDATE_SUGGESTIONS:
                 latinIme.updateSuggestions();
@@ -365,7 +365,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
             final LatinIME latinIme = getOuterInstance();
             removeMessages(MSG_FADEOUT_LANGUAGE_ON_SPACEBAR);
             removeMessages(MSG_DISMISS_LANGUAGE_ON_SPACEBAR);
-            final LatinKeyboardView inputView = latinIme.mKeyboardSwitcher.getKeyboardView();
+            final LatinKeyboardBaseView inputView = latinIme.mKeyboardSwitcher.getKeyboardView();
             if (inputView != null) {
                 final LatinKeyboard keyboard = latinIme.mKeyboardSwitcher.getLatinKeyboard();
                 // The language is always displayed when the delay is negative.
@@ -656,7 +656,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         }
 
         final KeyboardSwitcher switcher = mKeyboardSwitcher;
-        LatinKeyboardView inputView = switcher.getKeyboardView();
+        LatinKeyboardBaseView inputView = switcher.getKeyboardView();
 
         if (DEBUG) {
             Log.d(TAG, "onStartInputView: attribute:" + ((attribute == null) ? "none"
@@ -1544,7 +1544,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         commitTyped(getCurrentInputConnection());
         mVoiceProxy.handleClose();
         requestHideSelf(0);
-        LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
+        LatinKeyboardBaseView inputView = mKeyboardSwitcher.getKeyboardView();
         if (inputView != null)
             inputView.closing();
     }
@@ -2116,7 +2116,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         if (!mSettingsValues.mVibrateOn) {
             return;
         }
-        LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
+        LatinKeyboardBaseView inputView = mKeyboardSwitcher.getKeyboardView();
         if (inputView != null) {
             inputView.performHapticFeedback(
                     HapticFeedbackConstants.KEYBOARD_TAP,