2010-08-31 15:27:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
import com.android.inputmethod.keyboard.KeyboardView.UIHandler;
|
|
|
|
import com.android.inputmethod.latin.R;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-10-08 03:51:15 +00:00
|
|
|
import android.content.res.Resources;
|
2010-09-01 16:35:24 +00:00
|
|
|
import android.util.Log;
|
2010-09-06 05:26:46 +00:00
|
|
|
import android.view.MotionEvent;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
|
|
|
public class PointerTracker {
|
2010-09-01 16:35:24 +00:00
|
|
|
private static final String TAG = "PointerTracker";
|
|
|
|
private static final boolean DEBUG = false;
|
2010-09-06 05:26:46 +00:00
|
|
|
private static final boolean DEBUG_MOVE = false;
|
2010-09-01 16:35:24 +00:00
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
public interface UIProxy {
|
|
|
|
public void invalidateKey(Key key);
|
|
|
|
public void showPreview(int keyIndex, PointerTracker tracker);
|
2010-10-08 03:51:15 +00:00
|
|
|
public boolean hasDistinctMultitouch();
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 16:07:11 +00:00
|
|
|
public final int mPointerId;
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
// Timing constants
|
2010-10-08 03:51:15 +00:00
|
|
|
private final int mDelayBeforeKeyRepeatStart;
|
|
|
|
private final int mLongPressKeyTimeout;
|
2010-10-22 10:35:23 +00:00
|
|
|
private final int mLongPressShiftKeyTimeout;
|
2010-10-08 03:51:15 +00:00
|
|
|
private final int mMultiTapKeyTimeout;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
|
|
|
// Miscellaneous constants
|
2010-12-02 09:46:21 +00:00
|
|
|
private static final int NOT_A_KEY = KeyDetector.NOT_A_KEY;
|
2010-12-02 11:54:32 +00:00
|
|
|
private static final int[] KEY_DELETE = { Keyboard.CODE_DELETE };
|
2010-08-31 15:27:04 +00:00
|
|
|
|
|
|
|
private final UIProxy mProxy;
|
|
|
|
private final UIHandler mHandler;
|
2010-09-01 14:18:39 +00:00
|
|
|
private final KeyDetector mKeyDetector;
|
2010-12-02 09:46:21 +00:00
|
|
|
private KeyboardActionListener mListener;
|
2010-09-06 05:26:46 +00:00
|
|
|
private final boolean mHasDistinctMultitouch;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
private Keyboard mKeyboard;
|
2010-08-31 15:27:04 +00:00
|
|
|
private Key[] mKeys;
|
2010-09-13 10:26:23 +00:00
|
|
|
private int mKeyHysteresisDistanceSquared = -1;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-09-24 11:45:24 +00:00
|
|
|
private final KeyState mKeyState;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-09-03 03:51:03 +00:00
|
|
|
// true if event is already translated to a key action (long press or mini-keyboard)
|
|
|
|
private boolean mKeyAlreadyProcessed;
|
|
|
|
|
2010-09-07 03:20:03 +00:00
|
|
|
// true if this pointer is repeatable key
|
|
|
|
private boolean mIsRepeatableKey;
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
// For multi-tap
|
|
|
|
private int mLastSentIndex;
|
|
|
|
private int mTapCount;
|
|
|
|
private long mLastTapTime;
|
|
|
|
private boolean mInMultiTap;
|
|
|
|
private final StringBuilder mPreviewLabel = new StringBuilder(1);
|
|
|
|
|
|
|
|
// pressed key
|
2010-09-01 05:25:19 +00:00
|
|
|
private int mPreviousKey = NOT_A_KEY;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-09-24 11:45:24 +00:00
|
|
|
// This class keeps track of a key index and a position where this pointer is.
|
|
|
|
private static class KeyState {
|
|
|
|
private final KeyDetector mKeyDetector;
|
|
|
|
|
|
|
|
// The position and time at which first down event occurred.
|
|
|
|
private int mStartX;
|
|
|
|
private int mStartY;
|
|
|
|
private long mDownTime;
|
|
|
|
|
|
|
|
// The current key index where this pointer is.
|
|
|
|
private int mKeyIndex = NOT_A_KEY;
|
|
|
|
// The position where mKeyIndex was recognized for the first time.
|
|
|
|
private int mKeyX;
|
|
|
|
private int mKeyY;
|
|
|
|
|
|
|
|
// Last pointer position.
|
|
|
|
private int mLastX;
|
|
|
|
private int mLastY;
|
|
|
|
|
|
|
|
public KeyState(KeyDetector keyDetecor) {
|
|
|
|
mKeyDetector = keyDetecor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyIndex() {
|
|
|
|
return mKeyIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyX() {
|
|
|
|
return mKeyX;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyY() {
|
|
|
|
return mKeyY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getStartX() {
|
|
|
|
return mStartX;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getStartY() {
|
|
|
|
return mStartY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getDownTime() {
|
|
|
|
return mDownTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLastX() {
|
|
|
|
return mLastX;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLastY() {
|
|
|
|
return mLastY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int onDownKey(int x, int y, long eventTime) {
|
|
|
|
mStartX = x;
|
|
|
|
mStartY = y;
|
|
|
|
mDownTime = eventTime;
|
|
|
|
|
|
|
|
return onMoveToNewKey(onMoveKeyInternal(x, y), x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int onMoveKeyInternal(int x, int y) {
|
|
|
|
mLastX = x;
|
|
|
|
mLastY = y;
|
|
|
|
return mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int onMoveKey(int x, int y) {
|
|
|
|
return onMoveKeyInternal(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int onMoveToNewKey(int keyIndex, int x, int y) {
|
|
|
|
mKeyIndex = keyIndex;
|
|
|
|
mKeyX = x;
|
|
|
|
mKeyY = y;
|
|
|
|
return keyIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int onUpKey(int x, int y) {
|
|
|
|
return onMoveKeyInternal(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSetKeyboard() {
|
|
|
|
mKeyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(mKeyX, mKeyY, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-06 05:26:46 +00:00
|
|
|
public PointerTracker(int id, UIHandler handler, KeyDetector keyDetector, UIProxy proxy,
|
2010-10-08 03:51:15 +00:00
|
|
|
Resources res) {
|
2010-08-31 15:27:04 +00:00
|
|
|
if (proxy == null || handler == null || keyDetector == null)
|
|
|
|
throw new NullPointerException();
|
2010-09-01 16:07:11 +00:00
|
|
|
mPointerId = id;
|
2010-08-31 15:27:04 +00:00
|
|
|
mProxy = proxy;
|
|
|
|
mHandler = handler;
|
|
|
|
mKeyDetector = keyDetector;
|
2010-09-24 11:45:24 +00:00
|
|
|
mKeyState = new KeyState(keyDetector);
|
2010-10-08 03:51:15 +00:00
|
|
|
mHasDistinctMultitouch = proxy.hasDistinctMultitouch();
|
|
|
|
mDelayBeforeKeyRepeatStart = res.getInteger(R.integer.config_delay_before_key_repeat_start);
|
|
|
|
mLongPressKeyTimeout = res.getInteger(R.integer.config_long_press_key_timeout);
|
2010-10-22 10:35:23 +00:00
|
|
|
mLongPressShiftKeyTimeout = res.getInteger(R.integer.config_long_press_shift_key_timeout);
|
2010-10-08 03:51:15 +00:00
|
|
|
mMultiTapKeyTimeout = res.getInteger(R.integer.config_multi_tap_key_timeout);
|
2010-08-31 15:27:04 +00:00
|
|
|
resetMultiTap();
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public void setOnKeyboardActionListener(KeyboardActionListener listener) {
|
2010-08-31 15:27:04 +00:00
|
|
|
mListener = listener;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public void setKeyboard(Keyboard keyboard, Key[] keys, float keyHysteresisDistance) {
|
2010-10-02 06:17:27 +00:00
|
|
|
if (keyboard == null || keys == null || keyHysteresisDistance < 0)
|
2010-08-31 15:27:04 +00:00
|
|
|
throw new IllegalArgumentException();
|
2010-10-02 06:17:27 +00:00
|
|
|
mKeyboard = keyboard;
|
2010-08-31 15:27:04 +00:00
|
|
|
mKeys = keys;
|
2010-09-13 10:26:23 +00:00
|
|
|
mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance);
|
2010-09-09 05:43:17 +00:00
|
|
|
// Update current key index because keyboard layout has been changed.
|
2010-09-24 11:45:24 +00:00
|
|
|
mKeyState.onSetKeyboard();
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 08:50:24 +00:00
|
|
|
private boolean isValidKeyIndex(int keyIndex) {
|
|
|
|
return keyIndex >= 0 && keyIndex < mKeys.length;
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
public Key getKey(int keyIndex) {
|
2010-09-01 08:50:24 +00:00
|
|
|
return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null;
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 06:02:29 +00:00
|
|
|
private boolean isModifierInternal(int keyIndex) {
|
|
|
|
Key key = getKey(keyIndex);
|
2010-09-01 16:35:24 +00:00
|
|
|
if (key == null)
|
|
|
|
return false;
|
2010-12-02 11:54:32 +00:00
|
|
|
int primaryCode = key.mCodes[0];
|
|
|
|
return primaryCode == Keyboard.CODE_SHIFT
|
2010-12-06 03:12:27 +00:00
|
|
|
|| primaryCode == Keyboard.CODE_SWITCH_ALPHA_SYMBOL;
|
2010-09-01 16:35:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 06:02:29 +00:00
|
|
|
public boolean isModifier() {
|
2010-09-24 11:45:24 +00:00
|
|
|
return isModifierInternal(mKeyState.getKeyIndex());
|
2010-09-15 06:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOnModifierKey(int x, int y) {
|
|
|
|
return isModifierInternal(mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null));
|
|
|
|
}
|
|
|
|
|
2010-12-02 13:59:10 +00:00
|
|
|
public boolean isOnShiftKey(int x, int y) {
|
|
|
|
final Key key = getKey(mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null));
|
|
|
|
return key != null && key.mCodes[0] == Keyboard.CODE_SHIFT;
|
|
|
|
}
|
|
|
|
|
2010-09-21 07:55:18 +00:00
|
|
|
public boolean isSpaceKey(int keyIndex) {
|
|
|
|
Key key = getKey(keyIndex);
|
2010-12-02 11:54:32 +00:00
|
|
|
return key != null && key.mCodes[0] == Keyboard.CODE_SPACE;
|
2010-09-21 07:55:18 +00:00
|
|
|
}
|
|
|
|
|
2010-10-22 10:35:23 +00:00
|
|
|
public void releaseKey() {
|
|
|
|
updateKeyGraphics(NOT_A_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateKeyGraphics(int keyIndex) {
|
2010-08-31 15:27:04 +00:00
|
|
|
int oldKeyIndex = mPreviousKey;
|
|
|
|
mPreviousKey = keyIndex;
|
|
|
|
if (keyIndex != oldKeyIndex) {
|
2010-09-01 08:50:24 +00:00
|
|
|
if (isValidKeyIndex(oldKeyIndex)) {
|
2010-08-31 15:27:04 +00:00
|
|
|
// if new key index is not a key, old key was just released inside of the key.
|
|
|
|
final boolean inside = (keyIndex == NOT_A_KEY);
|
|
|
|
mKeys[oldKeyIndex].onReleased(inside);
|
|
|
|
mProxy.invalidateKey(mKeys[oldKeyIndex]);
|
|
|
|
}
|
2010-09-01 08:50:24 +00:00
|
|
|
if (isValidKeyIndex(keyIndex)) {
|
2010-08-31 15:27:04 +00:00
|
|
|
mKeys[keyIndex].onPressed();
|
|
|
|
mProxy.invalidateKey(mKeys[keyIndex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-03 03:51:03 +00:00
|
|
|
public void setAlreadyProcessed() {
|
|
|
|
mKeyAlreadyProcessed = true;
|
|
|
|
}
|
|
|
|
|
2010-09-06 05:26:46 +00:00
|
|
|
public void onTouchEvent(int action, int x, int y, long eventTime) {
|
|
|
|
switch (action) {
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
onMoveEvent(x, y, eventTime);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
case MotionEvent.ACTION_POINTER_DOWN:
|
|
|
|
onDownEvent(x, y, eventTime);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_POINTER_UP:
|
|
|
|
onUpEvent(x, y, eventTime);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
onCancelEvent(x, y, eventTime);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-02 12:54:26 +00:00
|
|
|
public void onDownEvent(int x, int y, long eventTime) {
|
2010-09-09 05:43:17 +00:00
|
|
|
if (DEBUG)
|
|
|
|
debugLog("onDownEvent:", x, y);
|
2010-09-24 11:45:24 +00:00
|
|
|
int keyIndex = mKeyState.onDownKey(x, y, eventTime);
|
2010-09-03 03:51:03 +00:00
|
|
|
mKeyAlreadyProcessed = false;
|
2010-09-07 03:20:03 +00:00
|
|
|
mIsRepeatableKey = false;
|
2010-08-31 15:27:04 +00:00
|
|
|
checkMultiTap(eventTime, keyIndex);
|
|
|
|
if (mListener != null) {
|
2010-10-28 18:15:02 +00:00
|
|
|
if (isValidKeyIndex(keyIndex)) {
|
2010-12-02 11:54:32 +00:00
|
|
|
mListener.onPress(mKeys[keyIndex].mCodes[0]);
|
2010-10-28 18:15:02 +00:00
|
|
|
// This onPress call may have changed keyboard layout and have updated mKeyIndex.
|
|
|
|
// If that's the case, mKeyIndex has been updated in setKeyboard().
|
|
|
|
keyIndex = mKeyState.getKeyIndex();
|
|
|
|
}
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-09-01 08:50:24 +00:00
|
|
|
if (isValidKeyIndex(keyIndex)) {
|
2010-12-02 11:54:32 +00:00
|
|
|
if (mKeys[keyIndex].mRepeatable) {
|
2010-09-01 08:50:24 +00:00
|
|
|
repeatKey(keyIndex);
|
2010-10-08 03:51:15 +00:00
|
|
|
mHandler.startKeyRepeatTimer(mDelayBeforeKeyRepeatStart, keyIndex, this);
|
2010-09-07 03:20:03 +00:00
|
|
|
mIsRepeatableKey = true;
|
2010-09-01 08:50:24 +00:00
|
|
|
}
|
2010-10-12 11:24:53 +00:00
|
|
|
startLongPressTimer(keyIndex);
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-10-22 10:35:23 +00:00
|
|
|
showKeyPreviewAndUpdateKeyGraphics(keyIndex);
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 12:54:26 +00:00
|
|
|
public void onMoveEvent(int x, int y, long eventTime) {
|
2010-09-09 05:43:17 +00:00
|
|
|
if (DEBUG_MOVE)
|
|
|
|
debugLog("onMoveEvent:", x, y);
|
2010-09-03 08:54:37 +00:00
|
|
|
if (mKeyAlreadyProcessed)
|
|
|
|
return;
|
2010-09-24 11:45:24 +00:00
|
|
|
KeyState keyState = mKeyState;
|
2010-11-09 19:57:41 +00:00
|
|
|
final int keyIndex = keyState.onMoveKey(x, y);
|
|
|
|
final Key oldKey = getKey(keyState.getKeyIndex());
|
|
|
|
if (isValidKeyIndex(keyIndex)) {
|
|
|
|
if (oldKey == null) {
|
2010-09-24 11:45:24 +00:00
|
|
|
keyState.onMoveToNewKey(keyIndex, x, y);
|
2010-10-12 11:24:53 +00:00
|
|
|
startLongPressTimer(keyIndex);
|
2010-09-24 11:45:24 +00:00
|
|
|
} else if (!isMinorMoveBounce(x, y, keyIndex)) {
|
2010-11-09 19:57:41 +00:00
|
|
|
if (mListener != null)
|
2010-12-02 11:54:32 +00:00
|
|
|
mListener.onRelease(oldKey.mCodes[0]);
|
2010-08-31 15:27:04 +00:00
|
|
|
resetMultiTap();
|
2010-09-24 11:45:24 +00:00
|
|
|
keyState.onMoveToNewKey(keyIndex, x, y);
|
2010-10-12 11:24:53 +00:00
|
|
|
startLongPressTimer(keyIndex);
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-11-09 19:57:41 +00:00
|
|
|
if (oldKey != null) {
|
|
|
|
if (mListener != null)
|
2010-12-02 11:54:32 +00:00
|
|
|
mListener.onRelease(oldKey.mCodes[0]);
|
2010-09-24 11:45:24 +00:00
|
|
|
keyState.onMoveToNewKey(keyIndex, x ,y);
|
2010-11-09 19:57:41 +00:00
|
|
|
mHandler.cancelLongPressTimers();
|
2010-09-24 11:45:24 +00:00
|
|
|
} else if (!isMinorMoveBounce(x, y, keyIndex)) {
|
2010-09-02 15:48:16 +00:00
|
|
|
resetMultiTap();
|
2010-09-24 11:45:24 +00:00
|
|
|
keyState.onMoveToNewKey(keyIndex, x ,y);
|
2010-11-09 19:57:41 +00:00
|
|
|
mHandler.cancelLongPressTimers();
|
2010-09-02 15:48:16 +00:00
|
|
|
}
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-10-22 10:35:23 +00:00
|
|
|
showKeyPreviewAndUpdateKeyGraphics(mKeyState.getKeyIndex());
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 12:54:26 +00:00
|
|
|
public void onUpEvent(int x, int y, long eventTime) {
|
2010-09-01 16:35:24 +00:00
|
|
|
if (DEBUG)
|
2010-09-02 12:54:26 +00:00
|
|
|
debugLog("onUpEvent :", x, y);
|
2010-10-22 10:35:23 +00:00
|
|
|
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
|
2010-09-09 05:43:17 +00:00
|
|
|
if (mKeyAlreadyProcessed)
|
|
|
|
return;
|
2010-08-31 15:27:04 +00:00
|
|
|
mHandler.cancelKeyTimers();
|
|
|
|
mHandler.cancelPopupPreview();
|
2010-09-24 11:45:24 +00:00
|
|
|
int keyIndex = mKeyState.onUpKey(x, y);
|
|
|
|
if (isMinorMoveBounce(x, y, keyIndex)) {
|
|
|
|
// Use previous fixed key index and coordinates.
|
|
|
|
keyIndex = mKeyState.getKeyIndex();
|
|
|
|
x = mKeyState.getKeyX();
|
|
|
|
y = mKeyState.getKeyY();
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-09-07 03:20:03 +00:00
|
|
|
if (!mIsRepeatableKey) {
|
2010-09-24 11:45:24 +00:00
|
|
|
detectAndSendKey(keyIndex, x, y, eventTime);
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-09-24 11:45:24 +00:00
|
|
|
|
2010-09-01 08:50:24 +00:00
|
|
|
if (isValidKeyIndex(keyIndex))
|
2010-08-31 15:27:04 +00:00
|
|
|
mProxy.invalidateKey(mKeys[keyIndex]);
|
|
|
|
}
|
|
|
|
|
2010-09-02 12:54:26 +00:00
|
|
|
public void onCancelEvent(int x, int y, long eventTime) {
|
2010-09-01 16:35:24 +00:00
|
|
|
if (DEBUG)
|
2010-09-02 12:54:26 +00:00
|
|
|
debugLog("onCancelEvt:", x, y);
|
2010-08-31 15:27:04 +00:00
|
|
|
mHandler.cancelKeyTimers();
|
|
|
|
mHandler.cancelPopupPreview();
|
2010-10-22 10:35:23 +00:00
|
|
|
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
|
2010-09-24 11:45:24 +00:00
|
|
|
int keyIndex = mKeyState.getKeyIndex();
|
2010-09-01 08:50:24 +00:00
|
|
|
if (isValidKeyIndex(keyIndex))
|
2010-08-31 15:27:04 +00:00
|
|
|
mProxy.invalidateKey(mKeys[keyIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void repeatKey(int keyIndex) {
|
2010-09-01 08:50:24 +00:00
|
|
|
Key key = getKey(keyIndex);
|
|
|
|
if (key != null) {
|
|
|
|
// While key is repeating, because there is no need to handle multi-tap key, we can
|
|
|
|
// pass -1 as eventTime argument.
|
2010-12-02 11:54:32 +00:00
|
|
|
detectAndSendKey(keyIndex, key.mX, key.mY, -1);
|
2010-09-01 08:50:24 +00:00
|
|
|
}
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 12:54:26 +00:00
|
|
|
public int getLastX() {
|
2010-09-24 11:45:24 +00:00
|
|
|
return mKeyState.getLastX();
|
2010-09-02 12:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getLastY() {
|
2010-09-24 11:45:24 +00:00
|
|
|
return mKeyState.getLastY();
|
2010-09-02 12:54:26 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 15:48:16 +00:00
|
|
|
public long getDownTime() {
|
2010-09-24 11:45:24 +00:00
|
|
|
return mKeyState.getDownTime();
|
2010-09-02 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
// These package scope methods are only for debugging purpose.
|
|
|
|
/* package */ int getStartX() {
|
2010-09-24 11:45:24 +00:00
|
|
|
return mKeyState.getStartX();
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ int getStartY() {
|
2010-09-24 11:45:24 +00:00
|
|
|
return mKeyState.getStartY();
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-09-24 11:45:24 +00:00
|
|
|
private boolean isMinorMoveBounce(int x, int y, int newKey) {
|
2010-09-13 10:26:23 +00:00
|
|
|
if (mKeys == null || mKeyHysteresisDistanceSquared < 0)
|
2010-08-31 15:27:04 +00:00
|
|
|
throw new IllegalStateException("keyboard and/or hysteresis not set");
|
2010-09-24 11:45:24 +00:00
|
|
|
int curKey = mKeyState.getKeyIndex();
|
2010-08-31 15:27:04 +00:00
|
|
|
if (newKey == curKey) {
|
|
|
|
return true;
|
2010-09-01 08:50:24 +00:00
|
|
|
} else if (isValidKeyIndex(curKey)) {
|
2010-11-05 09:41:12 +00:00
|
|
|
return mKeys[curKey].squaredDistanceToEdge(x, y) < mKeyHysteresisDistanceSquared;
|
2010-08-31 15:27:04 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 10:35:23 +00:00
|
|
|
private void showKeyPreviewAndUpdateKeyGraphics(int keyIndex) {
|
|
|
|
updateKeyGraphics(keyIndex);
|
2010-09-06 05:26:46 +00:00
|
|
|
// The modifier key, such as shift key, should not be shown as preview when multi-touch is
|
2010-11-09 19:57:41 +00:00
|
|
|
// supported. On the other hand, if multi-touch is not supported, the modifier key should
|
2010-09-06 05:26:46 +00:00
|
|
|
// be shown as preview.
|
2010-09-09 05:43:17 +00:00
|
|
|
if (mHasDistinctMultitouch && isModifier()) {
|
|
|
|
mProxy.showPreview(NOT_A_KEY, this);
|
|
|
|
} else {
|
2010-09-01 16:35:24 +00:00
|
|
|
mProxy.showPreview(keyIndex, this);
|
2010-09-09 05:43:17 +00:00
|
|
|
}
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 06:17:27 +00:00
|
|
|
private void startLongPressTimer(int keyIndex) {
|
|
|
|
Key key = getKey(keyIndex);
|
2010-12-02 11:54:32 +00:00
|
|
|
if (key.mCodes[0] == Keyboard.CODE_SHIFT) {
|
2010-10-22 10:35:23 +00:00
|
|
|
mHandler.startLongPressShiftTimer(mLongPressShiftKeyTimeout, keyIndex, this);
|
|
|
|
} else {
|
|
|
|
mHandler.startLongPressTimer(mLongPressKeyTimeout, keyIndex, this);
|
|
|
|
}
|
2010-10-02 06:17:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
private void detectAndSendKey(int index, int x, int y, long eventTime) {
|
2010-12-02 09:46:21 +00:00
|
|
|
final KeyboardActionListener listener = mListener;
|
2010-09-07 06:37:59 +00:00
|
|
|
final Key key = getKey(index);
|
|
|
|
|
|
|
|
if (key == null) {
|
|
|
|
if (listener != null)
|
|
|
|
listener.onCancel();
|
|
|
|
} else {
|
2010-12-02 11:54:32 +00:00
|
|
|
if (key.mOutputText != null) {
|
2010-08-31 15:27:04 +00:00
|
|
|
if (listener != null) {
|
2010-12-02 11:54:32 +00:00
|
|
|
listener.onText(key.mOutputText);
|
2010-08-31 15:27:04 +00:00
|
|
|
listener.onRelease(NOT_A_KEY);
|
|
|
|
}
|
|
|
|
} else {
|
2010-12-02 11:54:32 +00:00
|
|
|
int code = key.mCodes[0];
|
2010-08-31 15:27:04 +00:00
|
|
|
//TextEntryState.keyPressedAt(key, x, y);
|
|
|
|
int[] codes = mKeyDetector.newCodeArray();
|
|
|
|
mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes);
|
|
|
|
// Multi-tap
|
|
|
|
if (mInMultiTap) {
|
|
|
|
if (mTapCount != -1) {
|
2010-12-02 11:54:32 +00:00
|
|
|
mListener.onKey(Keyboard.CODE_DELETE, KEY_DELETE, x, y);
|
2010-08-31 15:27:04 +00:00
|
|
|
} else {
|
|
|
|
mTapCount = 0;
|
|
|
|
}
|
2010-12-02 11:54:32 +00:00
|
|
|
code = key.mCodes[mTapCount];
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-10-02 06:17:27 +00:00
|
|
|
|
2010-11-13 08:16:34 +00:00
|
|
|
// If keyboard is in manual temporary upper case state and key has manual temporary
|
|
|
|
// shift code, alternate character code should be sent.
|
2010-12-02 11:54:32 +00:00
|
|
|
if (mKeyboard.isManualTemporaryUpperCase()
|
|
|
|
&& key.mManualTemporaryUpperCaseCode != 0) {
|
|
|
|
code = key.mManualTemporaryUpperCaseCode;
|
2010-10-02 06:17:27 +00:00
|
|
|
codes[0] = code;
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
/*
|
|
|
|
* Swap the first and second values in the codes array if the primary code is not
|
|
|
|
* the first value but the second value in the array. This happens when key
|
|
|
|
* debouncing is in effect.
|
|
|
|
*/
|
|
|
|
if (codes.length >= 2 && codes[0] != code && codes[1] == code) {
|
|
|
|
codes[1] = codes[0];
|
|
|
|
codes[0] = code;
|
|
|
|
}
|
|
|
|
if (listener != null) {
|
|
|
|
listener.onKey(code, codes, x, y);
|
|
|
|
listener.onRelease(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mLastSentIndex = index;
|
|
|
|
mLastTapTime = eventTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle multi-tap keys by producing the key label for the current multi-tap state.
|
|
|
|
*/
|
|
|
|
public CharSequence getPreviewText(Key key) {
|
|
|
|
if (mInMultiTap) {
|
|
|
|
// Multi-tap
|
|
|
|
mPreviewLabel.setLength(0);
|
2010-12-02 11:54:32 +00:00
|
|
|
mPreviewLabel.append((char) key.mCodes[mTapCount < 0 ? 0 : mTapCount]);
|
2010-08-31 15:27:04 +00:00
|
|
|
return mPreviewLabel;
|
|
|
|
} else {
|
2010-12-02 11:54:32 +00:00
|
|
|
return key.mLabel;
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void resetMultiTap() {
|
|
|
|
mLastSentIndex = NOT_A_KEY;
|
|
|
|
mTapCount = 0;
|
|
|
|
mLastTapTime = -1;
|
|
|
|
mInMultiTap = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkMultiTap(long eventTime, int keyIndex) {
|
2010-09-01 08:50:24 +00:00
|
|
|
Key key = getKey(keyIndex);
|
|
|
|
if (key == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
final boolean isMultiTap =
|
2010-10-08 03:51:15 +00:00
|
|
|
(eventTime < mLastTapTime + mMultiTapKeyTimeout && keyIndex == mLastSentIndex);
|
2010-12-02 11:54:32 +00:00
|
|
|
if (key.mCodes.length > 1) {
|
2010-08-31 15:27:04 +00:00
|
|
|
mInMultiTap = true;
|
2010-09-01 08:50:24 +00:00
|
|
|
if (isMultiTap) {
|
2010-12-02 11:54:32 +00:00
|
|
|
mTapCount = (mTapCount + 1) % key.mCodes.length;
|
2010-08-31 15:27:04 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
mTapCount = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-09-01 08:50:24 +00:00
|
|
|
if (!isMultiTap) {
|
2010-08-31 15:27:04 +00:00
|
|
|
resetMultiTap();
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 11:28:31 +00:00
|
|
|
|
|
|
|
private void debugLog(String title, int x, int y) {
|
2010-09-24 11:45:24 +00:00
|
|
|
int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
|
|
|
|
Key key = getKey(keyIndex);
|
2010-09-02 11:28:31 +00:00
|
|
|
final String code;
|
|
|
|
if (key == null) {
|
|
|
|
code = "----";
|
|
|
|
} else {
|
2010-12-02 11:54:32 +00:00
|
|
|
int primaryCode = key.mCodes[0];
|
2010-09-02 11:28:31 +00:00
|
|
|
code = String.format((primaryCode < 0) ? "%4d" : "0x%02x", primaryCode);
|
|
|
|
}
|
2010-09-28 19:17:36 +00:00
|
|
|
Log.d(TAG, String.format("%s%s[%d] %3d,%3d %3d(%s) %s", title,
|
|
|
|
(mKeyAlreadyProcessed ? "-" : " "), mPointerId, x, y, keyIndex, code,
|
|
|
|
(isModifier() ? "modifier" : "")));
|
2010-09-02 11:28:31 +00:00
|
|
|
}
|
2010-09-24 11:45:24 +00:00
|
|
|
}
|