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
|
|
|
|
2010-12-18 11:04:44 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
public class PointerTracker {
|
2010-12-18 11:04:44 +00:00
|
|
|
private static final String TAG = PointerTracker.class.getSimpleName();
|
2010-12-21 05:01:13 +00:00
|
|
|
private static final boolean ENABLE_ASSERTION = false;
|
2010-12-18 11:04:44 +00:00
|
|
|
private static final boolean DEBUG_EVENT = false;
|
|
|
|
private static final boolean DEBUG_MOVE_EVENT = false;
|
|
|
|
private static final boolean DEBUG_LISTENER = 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-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-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-18 11:04:44 +00:00
|
|
|
private KeyboardActionListener mListener = EMPTY_LISTENER;
|
2010-12-17 07:56:15 +00:00
|
|
|
private final KeyboardSwitcher mKeyboardSwitcher;
|
2010-09-06 05:26:46 +00:00
|
|
|
private final boolean mHasDistinctMultitouch;
|
2010-12-18 09:48:32 +00:00
|
|
|
private final boolean mConfigSlidingKeyInputEnabled;
|
2010-08-31 15:27:04 +00:00
|
|
|
|
2010-12-17 05:13:01 +00:00
|
|
|
private final int mTouchNoiseThresholdMillis;
|
|
|
|
private final int mTouchNoiseThresholdDistanceSquared;
|
|
|
|
|
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-12-18 11:04:44 +00:00
|
|
|
private final PointerTrackerKeyState 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-12-20 07:21:54 +00:00
|
|
|
// true if this pointer is in sliding key input
|
|
|
|
private boolean mIsInSlidingKeyInput;
|
|
|
|
|
2010-12-18 09:48:32 +00:00
|
|
|
// true if sliding key is allowed.
|
|
|
|
private boolean mIsAllowedSlidingKeyInput;
|
|
|
|
|
2010-08-31 15:27:04 +00:00
|
|
|
// 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-12-18 11:04:44 +00:00
|
|
|
// Empty {@link KeyboardActionListener}
|
|
|
|
private static final KeyboardActionListener EMPTY_LISTENER = new KeyboardActionListener() {
|
|
|
|
@Override
|
|
|
|
public void onPress(int primaryCode) {}
|
|
|
|
@Override
|
|
|
|
public void onRelease(int primaryCode) {}
|
|
|
|
@Override
|
2010-12-20 07:13:57 +00:00
|
|
|
public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) {}
|
2010-12-18 11:04:44 +00:00
|
|
|
@Override
|
2010-12-20 07:13:57 +00:00
|
|
|
public void onTextInput(CharSequence text) {}
|
2010-12-18 11:04:44 +00:00
|
|
|
@Override
|
2010-12-20 07:13:57 +00:00
|
|
|
public void onCancelInput() {}
|
2010-12-18 11:04:44 +00:00
|
|
|
@Override
|
2010-12-20 07:13:57 +00:00
|
|
|
public void onSwipeDown() {}
|
2010-12-18 11:04:44 +00:00
|
|
|
};
|
2010-09-24 11:45:24 +00:00
|
|
|
|
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-12-17 07:56:15 +00:00
|
|
|
mKeyboardSwitcher = KeyboardSwitcher.getInstance();
|
2010-12-18 11:04:44 +00:00
|
|
|
mKeyState = new PointerTrackerKeyState(keyDetector);
|
2010-10-08 03:51:15 +00:00
|
|
|
mHasDistinctMultitouch = proxy.hasDistinctMultitouch();
|
2010-12-18 09:48:32 +00:00
|
|
|
mConfigSlidingKeyInputEnabled = res.getBoolean(R.bool.config_sliding_key_input_enabled);
|
2010-10-08 03:51:15 +00:00
|
|
|
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-12-17 05:13:01 +00:00
|
|
|
mTouchNoiseThresholdMillis = res.getInteger(R.integer.config_touch_noise_threshold_millis);
|
|
|
|
final float touchNoiseThresholdDistance = res.getDimension(
|
|
|
|
R.dimen.config_touch_noise_threshold_distance);
|
|
|
|
mTouchNoiseThresholdDistanceSquared = (int)(
|
|
|
|
touchNoiseThresholdDistance * touchNoiseThresholdDistance);
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
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-18 11:04:44 +00:00
|
|
|
private void callListenerOnPress(int primaryCode) {
|
|
|
|
if (DEBUG_LISTENER)
|
|
|
|
Log.d(TAG, "onPress : " + keyCodePrintable(primaryCode));
|
|
|
|
mListener.onPress(primaryCode);
|
|
|
|
}
|
|
|
|
|
2010-12-20 07:13:57 +00:00
|
|
|
private void callListenerOnCodeInput(int primaryCode, int[] keyCodes, int x, int y) {
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_LISTENER)
|
2010-12-20 07:13:57 +00:00
|
|
|
Log.d(TAG, "onCodeInput: " + keyCodePrintable(primaryCode)
|
2010-12-18 11:04:44 +00:00
|
|
|
+ " codes="+ Arrays.toString(keyCodes) + " x=" + x + " y=" + y);
|
2010-12-20 07:13:57 +00:00
|
|
|
mListener.onCodeInput(primaryCode, keyCodes, x, y);
|
2010-12-18 11:04:44 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 07:13:57 +00:00
|
|
|
private void callListenerOnTextInput(CharSequence text) {
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_LISTENER)
|
2010-12-20 07:13:57 +00:00
|
|
|
Log.d(TAG, "onTextInput: text=" + text);
|
|
|
|
mListener.onTextInput(text);
|
2010-12-18 11:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void callListenerOnRelease(int primaryCode) {
|
|
|
|
if (DEBUG_LISTENER)
|
|
|
|
Log.d(TAG, "onRelease : " + keyCodePrintable(primaryCode));
|
|
|
|
mListener.onRelease(primaryCode);
|
|
|
|
}
|
|
|
|
|
2010-12-20 07:13:57 +00:00
|
|
|
private void callListenerOnCancelInput() {
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_LISTENER)
|
2010-12-20 07:13:57 +00:00
|
|
|
Log.d(TAG, "onCancelInput");
|
|
|
|
mListener.onCancelInput();
|
2010-12-18 11:04:44 +00:00
|
|
|
}
|
|
|
|
|
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-12-20 07:21:54 +00:00
|
|
|
public boolean isInSlidingKeyInput() {
|
|
|
|
return mIsInSlidingKeyInput;
|
|
|
|
}
|
|
|
|
|
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-12-18 11:04:44 +00:00
|
|
|
private static boolean isModifierCode(int primaryCode) {
|
2010-12-02 11:54:32 +00:00
|
|
|
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-12-18 11:04:44 +00:00
|
|
|
private boolean isModifierInternal(int keyIndex) {
|
|
|
|
final Key key = getKey(keyIndex);
|
2010-12-20 11:30:26 +00:00
|
|
|
return key == null ? false : isModifierCode(key.mCode);
|
2010-12-18 11:04:44 +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
|
|
|
}
|
|
|
|
|
2010-12-21 05:01:13 +00:00
|
|
|
private boolean isOnModifierKey(int x, int y) {
|
2010-09-15 06:02:29 +00:00
|
|
|
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));
|
2010-12-20 11:30:26 +00:00
|
|
|
return key != null && key.mCode == Keyboard.CODE_SHIFT;
|
2010-12-02 13:59:10 +00:00
|
|
|
}
|
|
|
|
|
2010-09-21 07:55:18 +00:00
|
|
|
public boolean isSpaceKey(int keyIndex) {
|
|
|
|
Key key = getKey(keyIndex);
|
2010-12-20 11:30:26 +00:00
|
|
|
return key != null && key.mCode == 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-12-21 05:01:13 +00:00
|
|
|
private void checkAssertion(PointerTrackerQueue queue) {
|
|
|
|
if (mHasDistinctMultitouch && queue == null)
|
|
|
|
throw new RuntimeException(
|
|
|
|
"PointerTrackerQueue must be passed on distinct multi touch device");
|
|
|
|
if (!mHasDistinctMultitouch && queue != null)
|
|
|
|
throw new RuntimeException(
|
|
|
|
"PointerTrackerQueue must be null on non-distinct multi touch device");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onTouchEvent(int action, int x, int y, long eventTime, PointerTrackerQueue queue) {
|
2010-09-06 05:26:46 +00:00
|
|
|
switch (action) {
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
2010-12-21 05:01:13 +00:00
|
|
|
onMoveEvent(x, y, eventTime, queue);
|
2010-09-06 05:26:46 +00:00
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
case MotionEvent.ACTION_POINTER_DOWN:
|
2010-12-21 05:01:13 +00:00
|
|
|
onDownEvent(x, y, eventTime, queue);
|
2010-09-06 05:26:46 +00:00
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_POINTER_UP:
|
2010-12-21 05:01:13 +00:00
|
|
|
onUpEvent(x, y, eventTime, queue);
|
2010-09-06 05:26:46 +00:00
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
2010-12-21 05:01:13 +00:00
|
|
|
onCancelEvent(x, y, eventTime, queue);
|
2010-09-06 05:26:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 05:01:13 +00:00
|
|
|
public void onDownEvent(int x, int y, long eventTime, PointerTrackerQueue queue) {
|
|
|
|
if (ENABLE_ASSERTION) checkAssertion(queue);
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_EVENT)
|
|
|
|
printTouchEvent("onDownEvent:", x, y, eventTime);
|
2010-12-21 05:01:13 +00:00
|
|
|
|
2010-12-17 05:13:01 +00:00
|
|
|
// TODO: up-to-down filter, if (down-up) is less than threshold, removeMessage(UP, this) in
|
|
|
|
// Handler, and just ignore this down event.
|
|
|
|
// TODO: down-to-up filter, just record down time. do not enqueue pointer now.
|
|
|
|
|
|
|
|
// Naive up-to-down noise filter.
|
|
|
|
final long deltaT = eventTime - mKeyState.getUpTime();
|
|
|
|
if (deltaT < mTouchNoiseThresholdMillis) {
|
|
|
|
final int dx = x - mKeyState.getLastX();
|
|
|
|
final int dy = y - mKeyState.getLastY();
|
|
|
|
final int distanceSquared = (dx * dx + dy * dy);
|
|
|
|
if (distanceSquared < mTouchNoiseThresholdDistanceSquared) {
|
|
|
|
Log.w(TAG, "onDownEvent: ignore potential noise: time=" + deltaT
|
|
|
|
+ " distance=" + distanceSquared);
|
|
|
|
setAlreadyProcessed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 05:01:13 +00:00
|
|
|
if (queue != null) {
|
|
|
|
if (isOnModifierKey(x, y)) {
|
|
|
|
// Before processing a down event of modifier key, all pointers already being
|
|
|
|
// tracked should be released.
|
|
|
|
queue.releaseAllPointers(eventTime);
|
|
|
|
}
|
|
|
|
queue.add(this);
|
|
|
|
}
|
|
|
|
onDownEventInternal(x, y, eventTime);
|
|
|
|
}
|
|
|
|
|
2010-12-17 05:13:01 +00:00
|
|
|
private void onDownEventInternal(int x, int y, long eventTime) {
|
2010-09-24 11:45:24 +00:00
|
|
|
int keyIndex = mKeyState.onDownKey(x, y, eventTime);
|
2010-12-18 09:48:32 +00:00
|
|
|
// Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding
|
2010-12-18 11:04:44 +00:00
|
|
|
// from modifier key, or 3) this pointer is on mini-keyboard.
|
2010-12-18 09:48:32 +00:00
|
|
|
mIsAllowedSlidingKeyInput = mConfigSlidingKeyInputEnabled || isModifierInternal(keyIndex)
|
|
|
|
|| mKeyDetector instanceof MiniKeyboardKeyDetector;
|
2010-09-03 03:51:03 +00:00
|
|
|
mKeyAlreadyProcessed = false;
|
2010-09-07 03:20:03 +00:00
|
|
|
mIsRepeatableKey = false;
|
2010-12-20 07:21:54 +00:00
|
|
|
mIsInSlidingKeyInput = false;
|
2010-12-18 11:04:44 +00:00
|
|
|
if (isValidKeyIndex(keyIndex)) {
|
2010-12-20 11:30:26 +00:00
|
|
|
callListenerOnPress(mKeys[keyIndex].mCode);
|
2010-12-18 11:04:44 +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-12-21 05:01:13 +00:00
|
|
|
public void onMoveEvent(int x, int y, long eventTime, PointerTrackerQueue queue) {
|
|
|
|
if (ENABLE_ASSERTION) checkAssertion(queue);
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_MOVE_EVENT)
|
|
|
|
printTouchEvent("onMoveEvent:", x, y, eventTime);
|
2010-09-03 08:54:37 +00:00
|
|
|
if (mKeyAlreadyProcessed)
|
|
|
|
return;
|
2010-12-18 11:04:44 +00:00
|
|
|
final PointerTrackerKeyState keyState = mKeyState;
|
2010-12-17 05:13:01 +00:00
|
|
|
|
|
|
|
// TODO: down-to-up filter, if (eventTime-downTime) is less than threshold, just ignore
|
|
|
|
// this move event. Otherwise fire {@link onDownEventInternal} and continue.
|
|
|
|
|
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-12-17 07:56:15 +00:00
|
|
|
// The pointer has been slid in to the new key, but the finger was not on any keys.
|
|
|
|
// In this case, we must call onPress() to notify that the new key is being pressed.
|
2010-12-20 11:30:26 +00:00
|
|
|
callListenerOnPress(getKey(keyIndex).mCode);
|
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-12-17 07:56:15 +00:00
|
|
|
// The pointer has been slid in to the new key from the previous key, we must call
|
|
|
|
// onRelease() first to notify that the previous key has been released, then call
|
|
|
|
// onPress() to notify that the new key is being pressed.
|
2010-12-20 07:21:54 +00:00
|
|
|
mIsInSlidingKeyInput = true;
|
2010-12-20 11:30:26 +00:00
|
|
|
callListenerOnRelease(oldKey.mCode);
|
2010-12-19 11:27:11 +00:00
|
|
|
mHandler.cancelLongPressTimers();
|
2010-12-18 09:48:32 +00:00
|
|
|
if (mIsAllowedSlidingKeyInput) {
|
2010-12-20 11:30:26 +00:00
|
|
|
callListenerOnPress(getKey(keyIndex).mCode);
|
2010-12-18 09:48:32 +00:00
|
|
|
keyState.onMoveToNewKey(keyIndex, x, y);
|
|
|
|
startLongPressTimer(keyIndex);
|
|
|
|
} else {
|
|
|
|
setAlreadyProcessed();
|
|
|
|
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-12-19 10:41:36 +00:00
|
|
|
if (oldKey != null && !isMinorMoveBounce(x, y, keyIndex)) {
|
2010-12-17 07:56:15 +00:00
|
|
|
// The pointer has been slid out from the previous key, we must call onRelease() to
|
|
|
|
// notify that the previous key has been released.
|
2010-12-20 07:21:54 +00:00
|
|
|
mIsInSlidingKeyInput = true;
|
2010-12-20 11:30:26 +00:00
|
|
|
callListenerOnRelease(oldKey.mCode);
|
2010-12-19 11:27:11 +00:00
|
|
|
mHandler.cancelLongPressTimers();
|
2010-12-18 09:48:32 +00:00
|
|
|
if (mIsAllowedSlidingKeyInput) {
|
|
|
|
keyState.onMoveToNewKey(keyIndex, x ,y);
|
|
|
|
} else {
|
|
|
|
setAlreadyProcessed();
|
|
|
|
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
|
|
|
|
return;
|
|
|
|
}
|
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-12-17 05:13:01 +00:00
|
|
|
// TODO: up-to-down filter, if delayed UP message is fired, invoke {@link onUpEventInternal}.
|
|
|
|
|
2010-12-21 05:01:13 +00:00
|
|
|
public void onUpEvent(int x, int y, long eventTime, PointerTrackerQueue queue) {
|
|
|
|
if (ENABLE_ASSERTION) checkAssertion(queue);
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_EVENT)
|
|
|
|
printTouchEvent("onUpEvent :", x, y, eventTime);
|
2010-12-21 05:01:13 +00:00
|
|
|
|
2010-12-17 05:13:01 +00:00
|
|
|
// TODO: up-to-down filter, just sendDelayedMessage(UP, this) to Handler.
|
|
|
|
// TODO: down-to-up filter, if (eventTime-downTime) is less than threshold, just ignore
|
|
|
|
// this up event. Otherwise fire {@link onDownEventInternal} and {@link onUpEventInternal}.
|
|
|
|
|
2010-12-21 05:01:13 +00:00
|
|
|
if (queue != null) {
|
|
|
|
if (isModifier()) {
|
|
|
|
// Before processing an up event of modifier key, all pointers already being
|
|
|
|
// tracked should be released.
|
|
|
|
queue.releaseAllPointersExcept(this, eventTime);
|
|
|
|
} else {
|
|
|
|
queue.releaseAllPointersOlderThan(this, eventTime);
|
|
|
|
}
|
|
|
|
queue.remove(this);
|
|
|
|
}
|
|
|
|
onUpEventInternal(x, y, eventTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onUpEventForRelease(int x, int y, long eventTime) {
|
|
|
|
onUpEventInternal(x, y, eventTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onUpEventInternal(int pointX, int pointY, long eventTime) {
|
|
|
|
int x = pointX;
|
|
|
|
int y = pointY;
|
2010-08-31 15:27:04 +00:00
|
|
|
mHandler.cancelKeyTimers();
|
|
|
|
mHandler.cancelPopupPreview();
|
2010-12-20 07:21:54 +00:00
|
|
|
showKeyPreviewAndUpdateKeyGraphics(NOT_A_KEY);
|
|
|
|
mIsInSlidingKeyInput = false;
|
2010-12-19 11:27:11 +00:00
|
|
|
if (mKeyAlreadyProcessed)
|
|
|
|
return;
|
2010-12-18 11:04:44 +00:00
|
|
|
final PointerTrackerKeyState keyState = mKeyState;
|
2010-12-17 05:13:01 +00:00
|
|
|
int keyIndex = keyState.onUpKey(x, y, eventTime);
|
2010-09-24 11:45:24 +00:00
|
|
|
if (isMinorMoveBounce(x, y, keyIndex)) {
|
|
|
|
// Use previous fixed key index and coordinates.
|
2010-12-18 11:04:44 +00:00
|
|
|
keyIndex = keyState.getKeyIndex();
|
|
|
|
x = keyState.getKeyX();
|
|
|
|
y = keyState.getKeyY();
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-09-07 03:20:03 +00:00
|
|
|
if (!mIsRepeatableKey) {
|
2010-12-20 11:30:26 +00:00
|
|
|
detectAndSendKey(keyIndex, x, y);
|
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-12-21 05:01:13 +00:00
|
|
|
public void onCancelEvent(int x, int y, long eventTime, PointerTrackerQueue queue) {
|
|
|
|
if (ENABLE_ASSERTION) checkAssertion(queue);
|
2010-12-18 11:04:44 +00:00
|
|
|
if (DEBUG_EVENT)
|
|
|
|
printTouchEvent("onCancelEvt:", x, y, eventTime);
|
2010-12-21 05:01:13 +00:00
|
|
|
|
|
|
|
if (queue != null)
|
|
|
|
queue.remove(this);
|
2010-12-17 05:13:01 +00:00
|
|
|
onCancelEventInternal();
|
2010-12-21 05:01:13 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 05:13:01 +00:00
|
|
|
private void onCancelEventInternal() {
|
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-12-20 07:21:54 +00:00
|
|
|
mIsInSlidingKeyInput = false;
|
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) {
|
2010-12-20 11:30:26 +00:00
|
|
|
detectAndSendKey(keyIndex, key.mX, key.mY);
|
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-20 11:30:26 +00:00
|
|
|
if (key.mCode == Keyboard.CODE_SHIFT) {
|
2010-10-22 10:35:23 +00:00
|
|
|
mHandler.startLongPressShiftTimer(mLongPressShiftKeyTimeout, keyIndex, this);
|
2010-12-21 08:13:15 +00:00
|
|
|
} else if (key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY
|
|
|
|
&& mKeyboard.isManualTemporaryUpperCase()) {
|
|
|
|
// We need not start long press timer on the key which has manual temporary upper case
|
|
|
|
// code defined and the keyboard is in manual temporary upper case mode.
|
|
|
|
return;
|
2010-12-17 07:56:15 +00:00
|
|
|
} else if (mKeyboardSwitcher.isInMomentaryAutoModeSwitchState()) {
|
|
|
|
// We use longer timeout for sliding finger input started from the symbols mode key.
|
|
|
|
mHandler.startLongPressTimer(mLongPressKeyTimeout * 2, keyIndex, this);
|
2010-10-22 10:35:23 +00:00
|
|
|
} else {
|
|
|
|
mHandler.startLongPressTimer(mLongPressKeyTimeout, keyIndex, this);
|
|
|
|
}
|
2010-10-02 06:17:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 11:30:26 +00:00
|
|
|
private void detectAndSendKey(int index, int x, int y) {
|
2010-09-07 06:37:59 +00:00
|
|
|
final Key key = getKey(index);
|
|
|
|
if (key == null) {
|
2010-12-20 07:13:57 +00:00
|
|
|
callListenerOnCancelInput();
|
2010-12-18 11:04:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.mOutputText != null) {
|
2010-12-20 07:13:57 +00:00
|
|
|
callListenerOnTextInput(key.mOutputText);
|
2010-12-20 11:30:26 +00:00
|
|
|
callListenerOnRelease(key.mCode);
|
2010-09-07 06:37:59 +00:00
|
|
|
} else {
|
2010-12-20 11:30:26 +00:00
|
|
|
int code = key.mCode;
|
2010-12-18 11:04:44 +00:00
|
|
|
final int[] codes = mKeyDetector.newCodeArray();
|
|
|
|
mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes);
|
2010-10-02 06:17:27 +00:00
|
|
|
|
2010-12-18 11:04:44 +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-21 08:13:15 +00:00
|
|
|
if (mKeyboard.isManualTemporaryUpperCase()
|
|
|
|
&& key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY) {
|
2010-12-18 11:04:44 +00:00
|
|
|
code = key.mManualTemporaryUpperCaseCode;
|
|
|
|
codes[0] = code;
|
|
|
|
}
|
2010-10-02 06:17:27 +00:00
|
|
|
|
2010-12-18 11:04:44 +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;
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-12-20 07:13:57 +00:00
|
|
|
callListenerOnCodeInput(code, codes, x, y);
|
2010-12-18 11:04:44 +00:00
|
|
|
callListenerOnRelease(code);
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public CharSequence getPreviewText(Key key) {
|
2010-12-20 11:30:26 +00:00
|
|
|
return key.mLabel;
|
2010-08-31 15:27:04 +00:00
|
|
|
}
|
2010-09-02 11:28:31 +00:00
|
|
|
|
2010-12-18 11:04:44 +00:00
|
|
|
private long mPreviousEventTime;
|
|
|
|
|
|
|
|
private void printTouchEvent(String title, int x, int y, long eventTime) {
|
|
|
|
final int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
|
|
|
|
final Key key = getKey(keyIndex);
|
2010-12-20 11:30:26 +00:00
|
|
|
final String code = (key == null) ? "----" : keyCodePrintable(key.mCode);
|
2010-12-18 11:04:44 +00:00
|
|
|
final long delta = eventTime - mPreviousEventTime;
|
|
|
|
Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %3d(%s)", title,
|
|
|
|
(mKeyAlreadyProcessed ? "-" : " "), mPointerId, x, y, delta, keyIndex, code));
|
|
|
|
mPreviousEventTime = eventTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String keyCodePrintable(int primaryCode) {
|
|
|
|
final String modifier = isModifierCode(primaryCode) ? " modifier" : "";
|
|
|
|
return String.format((primaryCode < 0) ? "%4d" : "0x%02x", primaryCode) + modifier;
|
2010-09-02 11:28:31 +00:00
|
|
|
}
|
2010-09-24 11:45:24 +00:00
|
|
|
}
|