From c403a46f6d787b79768895272d53d296100677dd Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 23 Aug 2011 15:57:51 +0900 Subject: Extract sudden jumping touch event hack into separate class Bug: 5182291 Change-Id: I6a88ed4df3ec98e31ea4966d82da56f7fca342ac --- .../inputmethod/keyboard/KeyboardSwitcher.java | 8 +- .../inputmethod/keyboard/LatinKeyboard.java | 2 +- .../keyboard/LatinKeyboardBaseView.java | 13 +- .../inputmethod/keyboard/LatinKeyboardView.java | 152 -------------------- .../keyboard/SuddenJumpingTouchEventHandler.java | 158 +++++++++++++++++++++ .../com/android/inputmethod/latin/LatinIME.java | 12 +- 6 files changed, 181 insertions(+), 164 deletions(-) delete mode 100644 java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java create mode 100644 java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java (limited to 'java/src') 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/LatinKeyboardView.java deleted file mode 100644 index 42ce7c440..000000000 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package com.android.inputmethod.keyboard; - -import android.content.Context; -import android.graphics.Canvas; -import android.util.AttributeSet; -import android.util.Log; -import android.view.MotionEvent; - -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.Utils; - -// TODO: We should remove this class -public class LatinKeyboardView extends LatinKeyboardBaseView { - private static final String TAG = LatinKeyboardView.class.getSimpleName(); - private static boolean DEBUG_MODE = LatinImeLogger.sDBG; - - /** Whether we've started dropping move events because we found a big jump */ - private boolean mDroppingEvents; - /** - * Whether multi-touch disambiguation needs to be disabled if a real multi-touch event has - * occured - */ - private boolean mDisableDisambiguation; - /** The distance threshold at which we start treating the touch session as a multi-touch */ - private int mJumpThresholdSquare = Integer.MAX_VALUE; - private int mLastX; - private int mLastY; - - public LatinKeyboardView(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @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; - } - - /** - * This function checks to see if we need to handle any sudden jumps in the pointer location - * that could be due to a multi-touch being treated as a move by the firmware or hardware. - * Once a sudden jump is detected, all subsequent move events are discarded - * until an UP is received.

- * When a sudden jump is detected, an UP event is simulated at the last position and when - * the sudden moves subside, a DOWN event is simulated for the second key. - * @param me the motion event - * @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()) - return false; - final int action = me.getAction(); - final int x = (int) me.getX(); - final int y = (int) me.getY(); - boolean result = false; - - // Real multi-touch event? Stop looking for sudden jumps - if (me.getPointerCount() > 1) { - mDisableDisambiguation = true; - } - if (mDisableDisambiguation) { - // If UP, reset the multi-touch flag - if (action == MotionEvent.ACTION_UP) mDisableDisambiguation = false; - return false; - } - - switch (action) { - case MotionEvent.ACTION_DOWN: - // Reset the "session" - mDroppingEvents = false; - mDisableDisambiguation = false; - break; - case MotionEvent.ACTION_MOVE: - // Is this a big jump? - final int distanceSquare = (mLastX - x) * (mLastX - x) + (mLastY - y) * (mLastY - y); - // Check the distance. - if (distanceSquare > mJumpThresholdSquare) { - // If we're not yet dropping events, start dropping and send an UP event - if (!mDroppingEvents) { - mDroppingEvents = true; - // Send an up event - MotionEvent translated = MotionEvent.obtain( - me.getEventTime(), me.getEventTime(), - MotionEvent.ACTION_UP, - mLastX, mLastY, me.getMetaState()); - super.onTouchEvent(translated); - translated.recycle(); - } - result = true; - } else if (mDroppingEvents) { - // If moves are small and we're already dropping events, continue dropping - result = true; - } - break; - case MotionEvent.ACTION_UP: - if (mDroppingEvents) { - // Send a down event first, as we dropped a bunch of sudden jumps and assume that - // the user is releasing the touch on the second key. - MotionEvent translated = MotionEvent.obtain(me.getEventTime(), me.getEventTime(), - MotionEvent.ACTION_DOWN, - x, y, me.getMetaState()); - super.onTouchEvent(translated); - translated.recycle(); - mDroppingEvents = false; - // Let the up event get processed as well, result = false - } - break; - } - // Track the previous coordinate - mLastX = x; - mLastY = y; - 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 (DEBUG_MODE) - Log.w(TAG, "onTouchEvent: ignore sudden jump " + me); - return true; - } - - return super.onTouchEvent(me); - } -} diff --git a/java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java b/java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java new file mode 100644 index 000000000..5344c27d4 --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/SuddenJumpingTouchEventHandler.java @@ -0,0 +1,158 @@ +/* + * 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 + * 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. + */ + +package com.android.inputmethod.keyboard; + +import android.content.Context; +import android.os.Build; +import android.util.Log; +import android.view.MotionEvent; + +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.R; + +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; + /** + * Whether multi-touch disambiguation needs to be disabled if a real multi-touch event has + * occured + */ + private boolean mDisableDisambiguation; + /** The distance threshold at which we start treating the touch session as a multi-touch */ + private int mJumpThresholdSquare = Integer.MAX_VALUE; + private int mLastX; + private int mLastY; + + 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); + } + + private static boolean needsSuddenJumpingHack(String deviceName, String[] deviceList) { + for (String device : deviceList) { + if (device.equalsIgnoreCase(deviceName)) { + return true; + } + } + return false; + } + + public void setKeyboard(Keyboard newKeyboard) { + // One-seventh of the keyboard width seems like a reasonable threshold + final int jumpThreshold = newKeyboard.mOccupiedWidth / 7; + mJumpThresholdSquare = jumpThreshold * jumpThreshold; + } + + /** + * This function checks to see if we need to handle any sudden jumps in the pointer location + * that could be due to a multi-touch being treated as a move by the firmware or hardware. + * Once a sudden jump is detected, all subsequent move events are discarded + * until an UP is received.

+ * When a sudden jump is detected, an UP event is simulated at the last position and when + * the sudden moves subside, a DOWN event is simulated for the second key. + * @param me the motion event + * @return true if the event was consumed, so that it doesn't continue to be handled by + * {@link LatinKeyboardBaseView}. + */ + private boolean handleSuddenJumping(MotionEvent me) { + if (!mNeedsSuddenJumpingHack) + return false; + final int action = me.getAction(); + final int x = (int) me.getX(); + final int y = (int) me.getY(); + boolean result = false; + + // Real multi-touch event? Stop looking for sudden jumps + if (me.getPointerCount() > 1) { + mDisableDisambiguation = true; + } + if (mDisableDisambiguation) { + // If UP, reset the multi-touch flag + if (action == MotionEvent.ACTION_UP) mDisableDisambiguation = false; + return false; + } + + switch (action) { + case MotionEvent.ACTION_DOWN: + // Reset the "session" + mDroppingEvents = false; + mDisableDisambiguation = false; + break; + case MotionEvent.ACTION_MOVE: + // Is this a big jump? + final int distanceSquare = (mLastX - x) * (mLastX - x) + (mLastY - y) * (mLastY - y); + // Check the distance. + if (distanceSquare > mJumpThresholdSquare) { + // If we're not yet dropping events, start dropping and send an UP event + if (!mDroppingEvents) { + mDroppingEvents = true; + // Send an up event + MotionEvent translated = MotionEvent.obtain( + me.getEventTime(), me.getEventTime(), + MotionEvent.ACTION_UP, + mLastX, mLastY, me.getMetaState()); + mView.processMotionEvent(translated); + translated.recycle(); + } + result = true; + } else if (mDroppingEvents) { + // If moves are small and we're already dropping events, continue dropping + result = true; + } + break; + case MotionEvent.ACTION_UP: + if (mDroppingEvents) { + // Send a down event first, as we dropped a bunch of sudden jumps and assume that + // the user is releasing the touch on the second key. + MotionEvent translated = MotionEvent.obtain(me.getEventTime(), me.getEventTime(), + MotionEvent.ACTION_DOWN, + x, y, me.getMetaState()); + mView.processMotionEvent(translated); + translated.recycle(); + mDroppingEvents = false; + // Let the up event get processed as well, result = false + } + break; + } + // Track the previous coordinate + mLastX = x; + mLastY = y; + return result; + } + + public boolean onTouchEvent(MotionEvent me) { + // If there was a sudden jump, return without processing the actual motion event. + if (handleSuddenJumping(me)) { + if (DEBUG_MODE) + Log.w(TAG, "onTouchEvent: ignore sudden jump " + me); + return true; + } + 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, -- cgit v1.2.3-83-g751a