PointerTracker.java revision b305e6775a214f1cc16e584484e26a47eb8baa52
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.keyboard;
18
19import android.content.res.TypedArray;
20import android.os.SystemClock;
21import android.util.Log;
22import android.view.MotionEvent;
23
24import com.android.inputmethod.accessibility.AccessibilityUtils;
25import com.android.inputmethod.keyboard.internal.GestureStroke;
26import com.android.inputmethod.keyboard.internal.GestureStrokeWithPreviewPoints;
27import com.android.inputmethod.keyboard.internal.PointerTrackerQueue;
28import com.android.inputmethod.latin.CollectionUtils;
29import com.android.inputmethod.latin.InputPointers;
30import com.android.inputmethod.latin.LatinImeLogger;
31import com.android.inputmethod.latin.R;
32import com.android.inputmethod.latin.define.ProductionFlag;
33import com.android.inputmethod.research.ResearchLogger;
34
35import java.util.ArrayList;
36
37public class PointerTracker implements PointerTrackerQueue.Element {
38    private static final String TAG = PointerTracker.class.getSimpleName();
39    private static final boolean DEBUG_EVENT = false;
40    private static final boolean DEBUG_MOVE_EVENT = false;
41    private static final boolean DEBUG_LISTENER = false;
42    private static boolean DEBUG_MODE = LatinImeLogger.sDBG;
43
44    /** True if {@link PointerTracker}s should handle gesture events. */
45    private static boolean sShouldHandleGesture = false;
46    private static boolean sMainDictionaryAvailable = false;
47    private static boolean sGestureHandlingEnabledByInputField = false;
48    private static boolean sGestureHandlingEnabledByUser = false;
49    private static boolean sGestureOffWhileFastTyping = false;
50
51    public interface KeyEventHandler {
52        /**
53         * Get KeyDetector object that is used for this PointerTracker.
54         * @return the KeyDetector object that is used for this PointerTracker
55         */
56        public KeyDetector getKeyDetector();
57
58        /**
59         * Get KeyboardActionListener object that is used to register key code and so on.
60         * @return the KeyboardActionListner for this PointerTracker
61         */
62        public KeyboardActionListener getKeyboardActionListener();
63
64        /**
65         * Get DrawingProxy object that is used for this PointerTracker.
66         * @return the DrawingProxy object that is used for this PointerTracker
67         */
68        public DrawingProxy getDrawingProxy();
69
70        /**
71         * Get TimerProxy object that handles key repeat and long press timer event for this
72         * PointerTracker.
73         * @return the TimerProxy object that handles key repeat and long press timer event.
74         */
75        public TimerProxy getTimerProxy();
76    }
77
78    public interface DrawingProxy extends MoreKeysPanel.Controller {
79        public void invalidateKey(Key key);
80        public void showKeyPreview(PointerTracker tracker);
81        public void dismissKeyPreview(PointerTracker tracker);
82        public void showGesturePreviewTrail(PointerTracker tracker, boolean isOldestTracker);
83    }
84
85    public interface TimerProxy {
86        public void startTypingStateTimer(Key typedKey);
87        public boolean isTypingState();
88        public void startGestureOffWhileFastTypingTimer();
89        public void startKeyRepeatTimer(PointerTracker tracker);
90        public void startLongPressTimer(PointerTracker tracker);
91        public void startLongPressTimer(int code);
92        public void cancelLongPressTimer();
93        public void startDoubleTapTimer();
94        public void cancelDoubleTapTimer();
95        public boolean isInDoubleTapTimeout();
96        public void cancelKeyTimers();
97
98        public static class Adapter implements TimerProxy {
99            @Override
100            public void startTypingStateTimer(Key typedKey) {}
101            @Override
102            public boolean isTypingState() { return false; }
103            @Override
104            public void startGestureOffWhileFastTypingTimer() {}
105            @Override
106            public void startKeyRepeatTimer(PointerTracker tracker) {}
107            @Override
108            public void startLongPressTimer(PointerTracker tracker) {}
109            @Override
110            public void startLongPressTimer(int code) {}
111            @Override
112            public void cancelLongPressTimer() {}
113            @Override
114            public void startDoubleTapTimer() {}
115            @Override
116            public void cancelDoubleTapTimer() {}
117            @Override
118            public boolean isInDoubleTapTimeout() { return false; }
119            @Override
120            public void cancelKeyTimers() {}
121        }
122    }
123
124    static class PointerTrackerParams {
125        public final boolean mSlidingKeyInputEnabled;
126        public final int mTouchNoiseThresholdTime;
127        public final float mTouchNoiseThresholdDistance;
128        public final int mTouchNoiseThresholdDistanceSquared;
129
130        public static final PointerTrackerParams DEFAULT = new PointerTrackerParams();
131
132        private PointerTrackerParams() {
133            mSlidingKeyInputEnabled = false;
134            mTouchNoiseThresholdTime = 0;
135            mTouchNoiseThresholdDistance = 0.0f;
136            mTouchNoiseThresholdDistanceSquared = 0;
137        }
138
139        public PointerTrackerParams(TypedArray mainKeyboardViewAttr) {
140            mSlidingKeyInputEnabled = mainKeyboardViewAttr.getBoolean(
141                    R.styleable.MainKeyboardView_slidingKeyInputEnable, false);
142            mTouchNoiseThresholdTime = mainKeyboardViewAttr.getInt(
143                    R.styleable.MainKeyboardView_touchNoiseThresholdTime, 0);
144            final float touchNouseThresholdDistance = mainKeyboardViewAttr.getDimension(
145                    R.styleable.MainKeyboardView_touchNoiseThresholdDistance, 0);
146            mTouchNoiseThresholdDistance = touchNouseThresholdDistance;
147            mTouchNoiseThresholdDistanceSquared =
148                    (int)(touchNouseThresholdDistance * touchNouseThresholdDistance);
149        }
150    }
151
152    // Parameters for pointer handling.
153    private static PointerTrackerParams sParams;
154    private static boolean sNeedsPhantomSuddenMoveEventHack;
155
156    private static final ArrayList<PointerTracker> sTrackers = CollectionUtils.newArrayList();
157    private static PointerTrackerQueue sPointerTrackerQueue;
158
159    public final int mPointerId;
160
161    private DrawingProxy mDrawingProxy;
162    private TimerProxy mTimerProxy;
163    private KeyDetector mKeyDetector;
164    private KeyboardActionListener mListener = EMPTY_LISTENER;
165
166    private Keyboard mKeyboard;
167    private int mKeyQuarterWidthSquared;
168
169    private boolean mIsDetectingGesture = false; // per PointerTracker.
170    private static boolean sInGesture = false;
171    private static long sGestureFirstDownTime;
172    private static final InputPointers sAggregratedPointers = new InputPointers(
173            GestureStroke.DEFAULT_CAPACITY);
174    private static int sLastRecognitionPointSize = 0;
175    private static long sLastRecognitionTime = 0;
176
177    // The position and time at which first down event occurred.
178    private long mDownTime;
179    private long mUpTime;
180
181    // The current key where this pointer is.
182    private Key mCurrentKey = null;
183    // The position where the current key was recognized for the first time.
184    private int mKeyX;
185    private int mKeyY;
186
187    // Last pointer position.
188    private int mLastX;
189    private int mLastY;
190
191    // true if keyboard layout has been changed.
192    private boolean mKeyboardLayoutHasBeenChanged;
193
194    // true if event is already translated to a key action.
195    private boolean mKeyAlreadyProcessed;
196
197    // true if this pointer has been long-pressed and is showing a more keys panel.
198    private boolean mIsShowingMoreKeysPanel;
199
200    // true if this pointer is in sliding key input
201    boolean mIsInSlidingKeyInput;
202
203    // true if sliding key is allowed.
204    private boolean mIsAllowedSlidingKeyInput;
205
206    // ignore modifier key if true
207    private boolean mIgnoreModifierKey;
208
209    // Empty {@link KeyboardActionListener}
210    private static final KeyboardActionListener EMPTY_LISTENER =
211            new KeyboardActionListener.Adapter();
212
213    private final GestureStrokeWithPreviewPoints mGestureStrokeWithPreviewPoints;
214
215    public static void init(boolean hasDistinctMultitouch,
216            boolean needsPhantomSuddenMoveEventHack) {
217        if (hasDistinctMultitouch) {
218            sPointerTrackerQueue = new PointerTrackerQueue();
219        } else {
220            sPointerTrackerQueue = null;
221        }
222        sNeedsPhantomSuddenMoveEventHack = needsPhantomSuddenMoveEventHack;
223        sParams = PointerTrackerParams.DEFAULT;
224    }
225
226    public static void setParameters(final TypedArray mainKeyboardViewAttr) {
227        sParams = new PointerTrackerParams(mainKeyboardViewAttr);
228    }
229
230    private static void updateGestureHandlingMode() {
231        sShouldHandleGesture = sMainDictionaryAvailable
232                && !sGestureOffWhileFastTyping
233                && sGestureHandlingEnabledByInputField
234                && sGestureHandlingEnabledByUser
235                && !AccessibilityUtils.getInstance().isTouchExplorationEnabled();
236    }
237
238    // Note that this method is called from a non-UI thread.
239    public static void setMainDictionaryAvailability(final boolean mainDictionaryAvailable) {
240        sMainDictionaryAvailable = mainDictionaryAvailable;
241        updateGestureHandlingMode();
242    }
243
244    public static void setGestureHandlingEnabledByUser(final boolean gestureHandlingEnabledByUser) {
245        sGestureHandlingEnabledByUser = gestureHandlingEnabledByUser;
246        updateGestureHandlingMode();
247    }
248
249    public static void setGestureOffWhileFastTyping() {
250        sGestureOffWhileFastTyping = true;
251        updateGestureHandlingMode();
252    }
253
254    public static void clearGestureOffWhileFastTyping() {
255        sGestureOffWhileFastTyping = false;
256        updateGestureHandlingMode();
257    }
258
259    public static PointerTracker getPointerTracker(final int id, final KeyEventHandler handler) {
260        final ArrayList<PointerTracker> trackers = sTrackers;
261
262        // Create pointer trackers until we can get 'id+1'-th tracker, if needed.
263        for (int i = trackers.size(); i <= id; i++) {
264            final PointerTracker tracker = new PointerTracker(i, handler);
265            trackers.add(tracker);
266        }
267
268        return trackers.get(id);
269    }
270
271    public static boolean isAnyInSlidingKeyInput() {
272        return sPointerTrackerQueue != null ? sPointerTrackerQueue.isAnyInSlidingKeyInput() : false;
273    }
274
275    public static void setKeyboardActionListener(final KeyboardActionListener listener) {
276        final int trackersSize = sTrackers.size();
277        for (int i = 0; i < trackersSize; ++i) {
278            final PointerTracker tracker = sTrackers.get(i);
279            tracker.mListener = listener;
280        }
281    }
282
283    public static void setKeyDetector(final KeyDetector keyDetector) {
284        final int trackersSize = sTrackers.size();
285        for (int i = 0; i < trackersSize; ++i) {
286            final PointerTracker tracker = sTrackers.get(i);
287            tracker.setKeyDetectorInner(keyDetector);
288            // Mark that keyboard layout has been changed.
289            tracker.mKeyboardLayoutHasBeenChanged = true;
290        }
291        final Keyboard keyboard = keyDetector.getKeyboard();
292        sGestureHandlingEnabledByInputField = !keyboard.mId.passwordInput();
293        updateGestureHandlingMode();
294    }
295
296    public static void setReleasedKeyGraphicsToAllKeys() {
297        final int trackersSize = sTrackers.size();
298        for (int i = 0; i < trackersSize; ++i) {
299            final PointerTracker tracker = sTrackers.get(i);
300            tracker.setReleasedKeyGraphics(tracker.mCurrentKey);
301        }
302    }
303
304    private PointerTracker(final int id, final KeyEventHandler handler) {
305        if (handler == null) {
306            throw new NullPointerException();
307        }
308        mPointerId = id;
309        mGestureStrokeWithPreviewPoints = new GestureStrokeWithPreviewPoints(id);
310        setKeyDetectorInner(handler.getKeyDetector());
311        mListener = handler.getKeyboardActionListener();
312        mDrawingProxy = handler.getDrawingProxy();
313        mTimerProxy = handler.getTimerProxy();
314    }
315
316    // Returns true if keyboard has been changed by this callback.
317    private boolean callListenerOnPressAndCheckKeyboardLayoutChange(final Key key) {
318        if (sInGesture) {
319            return false;
320        }
321        final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
322        if (DEBUG_LISTENER) {
323            Log.d(TAG, "onPress    : " + KeyDetector.printableCode(key)
324                    + " ignoreModifier=" + ignoreModifierKey
325                    + " enabled=" + key.isEnabled());
326        }
327        if (ignoreModifierKey) {
328            return false;
329        }
330        if (key.isEnabled()) {
331            mListener.onPressKey(key.mCode);
332            final boolean keyboardLayoutHasBeenChanged = mKeyboardLayoutHasBeenChanged;
333            mKeyboardLayoutHasBeenChanged = false;
334            mTimerProxy.startTypingStateTimer(key);
335            return keyboardLayoutHasBeenChanged;
336        }
337        return false;
338    }
339
340    // Note that we need primaryCode argument because the keyboard may in shifted state and the
341    // primaryCode is different from {@link Key#mCode}.
342    private void callListenerOnCodeInput(final Key key, final int primaryCode, final int x,
343            final int y) {
344        final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
345        final boolean altersCode = key.altCodeWhileTyping() && mTimerProxy.isTypingState();
346        final int code = altersCode ? key.getAltCode() : primaryCode;
347        if (DEBUG_LISTENER) {
348            Log.d(TAG, "onCodeInput: " + Keyboard.printableCode(code)
349                    + " text=" + key.getOutputText() + " x=" + x + " y=" + y
350                    + " ignoreModifier=" + ignoreModifierKey + " altersCode=" + altersCode
351                    + " enabled=" + key.isEnabled());
352        }
353        if (ProductionFlag.IS_EXPERIMENTAL) {
354            ResearchLogger.pointerTracker_callListenerOnCodeInput(key, x, y, ignoreModifierKey,
355                    altersCode, code);
356        }
357        if (ignoreModifierKey) {
358            return;
359        }
360        // Even if the key is disabled, it should respond if it is in the altCodeWhileTyping state.
361        if (key.isEnabled() || altersCode) {
362            if (code == Keyboard.CODE_OUTPUT_TEXT) {
363                mListener.onTextInput(key.getOutputText());
364                mTimerProxy.startGestureOffWhileFastTypingTimer();
365            } else if (code != Keyboard.CODE_UNSPECIFIED) {
366                mListener.onCodeInput(code, x, y);
367                mTimerProxy.startGestureOffWhileFastTypingTimer();
368            }
369        }
370    }
371
372    // Note that we need primaryCode argument because the keyboard may in shifted state and the
373    // primaryCode is different from {@link Key#mCode}.
374    private void callListenerOnRelease(final Key key, final int primaryCode,
375            final boolean withSliding) {
376        if (sInGesture) {
377            return;
378        }
379        final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
380        if (DEBUG_LISTENER) {
381            Log.d(TAG, "onRelease  : " + Keyboard.printableCode(primaryCode)
382                    + " sliding=" + withSliding + " ignoreModifier=" + ignoreModifierKey
383                    + " enabled="+ key.isEnabled());
384        }
385        if (ProductionFlag.IS_EXPERIMENTAL) {
386            ResearchLogger.pointerTracker_callListenerOnRelease(key, primaryCode, withSliding,
387                    ignoreModifierKey);
388        }
389        if (ignoreModifierKey) {
390            return;
391        }
392        if (key.isEnabled()) {
393            mListener.onReleaseKey(primaryCode, withSliding);
394        }
395    }
396
397    private void callListenerOnCancelInput() {
398        if (DEBUG_LISTENER) {
399            Log.d(TAG, "onCancelInput");
400        }
401        if (ProductionFlag.IS_EXPERIMENTAL) {
402            ResearchLogger.pointerTracker_callListenerOnCancelInput();
403        }
404        mListener.onCancelInput();
405    }
406
407    private void setKeyDetectorInner(final KeyDetector keyDetector) {
408        mKeyDetector = keyDetector;
409        mKeyboard = keyDetector.getKeyboard();
410        mGestureStrokeWithPreviewPoints.setKeyboardGeometry(mKeyboard.mMostCommonKeyWidth);
411        final Key newKey = mKeyDetector.detectHitKey(mKeyX, mKeyY);
412        if (newKey != mCurrentKey) {
413            if (mDrawingProxy != null) {
414                setReleasedKeyGraphics(mCurrentKey);
415            }
416            // Keep {@link #mCurrentKey} that comes from previous keyboard.
417        }
418        final int keyQuarterWidth = mKeyboard.mMostCommonKeyWidth / 4;
419        mKeyQuarterWidthSquared = keyQuarterWidth * keyQuarterWidth;
420    }
421
422    @Override
423    public boolean isInSlidingKeyInput() {
424        return mIsInSlidingKeyInput;
425    }
426
427    public Key getKey() {
428        return mCurrentKey;
429    }
430
431    @Override
432    public boolean isModifier() {
433        return mCurrentKey != null && mCurrentKey.isModifier();
434    }
435
436    public Key getKeyOn(final int x, final int y) {
437        return mKeyDetector.detectHitKey(x, y);
438    }
439
440    private void setReleasedKeyGraphics(final Key key) {
441        mDrawingProxy.dismissKeyPreview(this);
442        if (key == null) {
443            return;
444        }
445
446        // Even if the key is disabled, update the key release graphics just in case.
447        updateReleaseKeyGraphics(key);
448
449        if (key.isShift()) {
450            for (final Key shiftKey : mKeyboard.mShiftKeys) {
451                if (shiftKey != key) {
452                    updateReleaseKeyGraphics(shiftKey);
453                }
454            }
455        }
456
457        if (key.altCodeWhileTyping()) {
458            final int altCode = key.getAltCode();
459            final Key altKey = mKeyboard.getKey(altCode);
460            if (altKey != null) {
461                updateReleaseKeyGraphics(altKey);
462            }
463            for (final Key k : mKeyboard.mAltCodeKeysWhileTyping) {
464                if (k != key && k.getAltCode() == altCode) {
465                    updateReleaseKeyGraphics(k);
466                }
467            }
468        }
469    }
470
471    private void setPressedKeyGraphics(final Key key) {
472        if (key == null) {
473            return;
474        }
475
476        // Even if the key is disabled, it should respond if it is in the altCodeWhileTyping state.
477        final boolean altersCode = key.altCodeWhileTyping() && mTimerProxy.isTypingState();
478        final boolean needsToUpdateGraphics = key.isEnabled() || altersCode;
479        if (!needsToUpdateGraphics) {
480            return;
481        }
482
483        if (!key.noKeyPreview() && !sInGesture) {
484            mDrawingProxy.showKeyPreview(this);
485        }
486        updatePressKeyGraphics(key);
487
488        if (key.isShift()) {
489            for (final Key shiftKey : mKeyboard.mShiftKeys) {
490                if (shiftKey != key) {
491                    updatePressKeyGraphics(shiftKey);
492                }
493            }
494        }
495
496        if (key.altCodeWhileTyping() && mTimerProxy.isTypingState()) {
497            final int altCode = key.getAltCode();
498            final Key altKey = mKeyboard.getKey(altCode);
499            if (altKey != null) {
500                updatePressKeyGraphics(altKey);
501            }
502            for (final Key k : mKeyboard.mAltCodeKeysWhileTyping) {
503                if (k != key && k.getAltCode() == altCode) {
504                    updatePressKeyGraphics(k);
505                }
506            }
507        }
508    }
509
510    private void updateReleaseKeyGraphics(final Key key) {
511        key.onReleased();
512        mDrawingProxy.invalidateKey(key);
513    }
514
515    private void updatePressKeyGraphics(final Key key) {
516        key.onPressed();
517        mDrawingProxy.invalidateKey(key);
518    }
519
520    public GestureStrokeWithPreviewPoints getGestureStrokeWithPreviewPoints() {
521        return mGestureStrokeWithPreviewPoints;
522    }
523
524    public int getLastX() {
525        return mLastX;
526    }
527
528    public int getLastY() {
529        return mLastY;
530    }
531
532    public long getDownTime() {
533        return mDownTime;
534    }
535
536    private Key onDownKey(final int x, final int y, final long eventTime) {
537        mDownTime = eventTime;
538        return onMoveToNewKey(onMoveKeyInternal(x, y), x, y);
539    }
540
541    private Key onMoveKeyInternal(final int x, final int y) {
542        mLastX = x;
543        mLastY = y;
544        return mKeyDetector.detectHitKey(x, y);
545    }
546
547    private Key onMoveKey(final int x, final int y) {
548        return onMoveKeyInternal(x, y);
549    }
550
551    private Key onMoveToNewKey(final Key newKey, final int x, final int y) {
552        mCurrentKey = newKey;
553        mKeyX = x;
554        mKeyY = y;
555        return newKey;
556    }
557
558    private static int getActivePointerTrackerCount() {
559        return (sPointerTrackerQueue == null) ? 1 : sPointerTrackerQueue.size();
560    }
561
562    private void mayStartBatchInput() {
563        if (sInGesture || !mGestureStrokeWithPreviewPoints.isStartOfAGesture()) {
564            return;
565        }
566        if (DEBUG_LISTENER) {
567            Log.d(TAG, "onStartBatchInput");
568        }
569        sInGesture = true;
570        mListener.onStartBatchInput();
571        final boolean isOldestTracker = sPointerTrackerQueue.getOldestElement() == this;
572        mDrawingProxy.showGesturePreviewTrail(this, isOldestTracker);
573    }
574
575    private void updateBatchInput(final long eventTime) {
576        synchronized (sAggregratedPointers) {
577            mGestureStrokeWithPreviewPoints.appendIncrementalBatchPoints(sAggregratedPointers);
578            final int size = sAggregratedPointers.getPointerSize();
579            if (size > sLastRecognitionPointSize
580                    && GestureStroke.hasRecognitionTimePast(eventTime, sLastRecognitionTime)) {
581                sLastRecognitionPointSize = size;
582                sLastRecognitionTime = eventTime;
583                if (DEBUG_LISTENER) {
584                    Log.d(TAG, "onUpdateBatchInput: batchPoints=" + size);
585                }
586                mListener.onUpdateBatchInput(sAggregratedPointers);
587            }
588        }
589        final boolean isOldestTracker = sPointerTrackerQueue.getOldestElement() == this;
590        mDrawingProxy.showGesturePreviewTrail(this, isOldestTracker);
591    }
592
593    private void mayEndBatchInput() {
594        synchronized (sAggregratedPointers) {
595            mGestureStrokeWithPreviewPoints.appendAllBatchPoints(sAggregratedPointers);
596            mGestureStrokeWithPreviewPoints.reset();
597            if (getActivePointerTrackerCount() == 1) {
598                if (DEBUG_LISTENER) {
599                    Log.d(TAG, "onEndBatchInput: batchPoints="
600                            + sAggregratedPointers.getPointerSize());
601                }
602                sInGesture = false;
603                mListener.onEndBatchInput(sAggregratedPointers);
604                clearBatchInputPointsOfAllPointerTrackers();
605            }
606        }
607        final boolean isOldestTracker = sPointerTrackerQueue.getOldestElement() == this;
608        mDrawingProxy.showGesturePreviewTrail(this, isOldestTracker);
609    }
610
611    private static void abortBatchInput() {
612        clearBatchInputPointsOfAllPointerTrackers();
613    }
614
615    private static void clearBatchInputPointsOfAllPointerTrackers() {
616        final int trackersSize = sTrackers.size();
617        for (int i = 0; i < trackersSize; ++i) {
618            final PointerTracker tracker = sTrackers.get(i);
619            tracker.mGestureStrokeWithPreviewPoints.reset();
620        }
621        sAggregratedPointers.reset();
622        sLastRecognitionPointSize = 0;
623        sLastRecognitionTime = 0;
624    }
625
626    public void processMotionEvent(final int action, final int x, final int y, final long eventTime,
627            final KeyEventHandler handler) {
628        switch (action) {
629        case MotionEvent.ACTION_DOWN:
630        case MotionEvent.ACTION_POINTER_DOWN:
631            onDownEvent(x, y, eventTime, handler);
632            break;
633        case MotionEvent.ACTION_UP:
634        case MotionEvent.ACTION_POINTER_UP:
635            onUpEvent(x, y, eventTime);
636            break;
637        case MotionEvent.ACTION_MOVE:
638            onMoveEvent(x, y, eventTime, null);
639            break;
640        case MotionEvent.ACTION_CANCEL:
641            onCancelEvent(x, y, eventTime);
642            break;
643        }
644    }
645
646    public void onDownEvent(final int x, final int y, final long eventTime,
647            final KeyEventHandler handler) {
648        if (DEBUG_EVENT) {
649            printTouchEvent("onDownEvent:", x, y, eventTime);
650        }
651
652        mDrawingProxy = handler.getDrawingProxy();
653        mTimerProxy = handler.getTimerProxy();
654        setKeyboardActionListener(handler.getKeyboardActionListener());
655        setKeyDetectorInner(handler.getKeyDetector());
656        // Naive up-to-down noise filter.
657        final long deltaT = eventTime - mUpTime;
658        if (deltaT < sParams.mTouchNoiseThresholdTime) {
659            final int dx = x - mLastX;
660            final int dy = y - mLastY;
661            final int distanceSquared = (dx * dx + dy * dy);
662            if (distanceSquared < sParams.mTouchNoiseThresholdDistanceSquared) {
663                if (DEBUG_MODE)
664                    Log.w(TAG, "onDownEvent: ignore potential noise: time=" + deltaT
665                            + " distance=" + distanceSquared);
666                if (ProductionFlag.IS_EXPERIMENTAL) {
667                    ResearchLogger.pointerTracker_onDownEvent(deltaT, distanceSquared);
668                }
669                mKeyAlreadyProcessed = true;
670                return;
671            }
672        }
673
674        final Key key = getKeyOn(x, y);
675        final PointerTrackerQueue queue = sPointerTrackerQueue;
676        if (queue != null) {
677            if (key != null && key.isModifier()) {
678                // Before processing a down event of modifier key, all pointers already being
679                // tracked should be released.
680                queue.releaseAllPointers(eventTime);
681            }
682            queue.add(this);
683        }
684        onDownEventInternal(x, y, eventTime);
685        if (!sShouldHandleGesture) {
686            return;
687        }
688        // A gesture should start only from the letter key.
689        mIsDetectingGesture = (mKeyboard != null) && mKeyboard.mId.isAlphabetKeyboard()
690                && !mIsShowingMoreKeysPanel && key != null && Keyboard.isLetterCode(key.mCode);
691        if (mIsDetectingGesture) {
692            if (getActivePointerTrackerCount() == 1) {
693                sGestureFirstDownTime = eventTime;
694            }
695            onGestureDownEvent(x, y, eventTime);
696        }
697    }
698
699    private void onGestureDownEvent(final int x, final int y, final long eventTime) {
700        mIsDetectingGesture = true;
701        final int elapsedTimeFromFirstDown = (int)(eventTime - sGestureFirstDownTime);
702        mGestureStrokeWithPreviewPoints.addPoint(x, y, elapsedTimeFromFirstDown,
703                true /* isMajorEvent */);
704    }
705
706    private void onDownEventInternal(final int x, final int y, final long eventTime) {
707        Key key = onDownKey(x, y, eventTime);
708        // Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding
709        // from modifier key, or 3) this pointer's KeyDetector always allows sliding input.
710        mIsAllowedSlidingKeyInput = sParams.mSlidingKeyInputEnabled
711                || (key != null && key.isModifier())
712                || mKeyDetector.alwaysAllowsSlidingInput();
713        mKeyboardLayoutHasBeenChanged = false;
714        mKeyAlreadyProcessed = false;
715        mIsInSlidingKeyInput = false;
716        mIgnoreModifierKey = false;
717        if (key != null) {
718            // This onPress call may have changed keyboard layout. Those cases are detected at
719            // {@link #setKeyboard}. In those cases, we should update key according to the new
720            // keyboard layout.
721            if (callListenerOnPressAndCheckKeyboardLayoutChange(key)) {
722                key = onDownKey(x, y, eventTime);
723            }
724
725            startRepeatKey(key);
726            startLongPressTimer(key);
727            setPressedKeyGraphics(key);
728        }
729    }
730
731    private void startSlidingKeyInput(final Key key) {
732        if (!mIsInSlidingKeyInput) {
733            mIgnoreModifierKey = key.isModifier();
734        }
735        mIsInSlidingKeyInput = true;
736    }
737
738    private void onGestureMoveEvent(final int x, final int y, final long eventTime,
739            final boolean isMajorEvent, final Key key) {
740        final int gestureTime = (int)(eventTime - sGestureFirstDownTime);
741        if (mIsDetectingGesture) {
742            mGestureStrokeWithPreviewPoints.addPoint(x, y, gestureTime, isMajorEvent);
743            mayStartBatchInput();
744            if (sInGesture && key != null) {
745                updateBatchInput(eventTime);
746            }
747        }
748    }
749
750    public void onMoveEvent(final int x, final int y, final long eventTime, final MotionEvent me) {
751        if (DEBUG_MOVE_EVENT) {
752            printTouchEvent("onMoveEvent:", x, y, eventTime);
753        }
754        if (mKeyAlreadyProcessed) {
755            return;
756        }
757
758        if (sShouldHandleGesture && me != null) {
759            // Add historical points to gesture path.
760            final int pointerIndex = me.findPointerIndex(mPointerId);
761            final int historicalSize = me.getHistorySize();
762            for (int h = 0; h < historicalSize; h++) {
763                final int historicalX = (int)me.getHistoricalX(pointerIndex, h);
764                final int historicalY = (int)me.getHistoricalY(pointerIndex, h);
765                final long historicalTime = me.getHistoricalEventTime(h);
766                onGestureMoveEvent(historicalX, historicalY, historicalTime,
767                        false /* isMajorEvent */, null);
768            }
769        }
770
771        onMoveEventInternal(x, y, eventTime);
772    }
773
774    private void onMoveEventInternal(final int x, final int y, final long eventTime) {
775        final int lastX = mLastX;
776        final int lastY = mLastY;
777        final Key oldKey = mCurrentKey;
778        Key key = onMoveKey(x, y);
779
780        if (sShouldHandleGesture) {
781            // Register move event on gesture tracker.
782            onGestureMoveEvent(x, y, eventTime, true /* isMajorEvent */, key);
783            if (sInGesture) {
784                mIgnoreModifierKey = true;
785                mTimerProxy.cancelLongPressTimer();
786                mIsInSlidingKeyInput = true;
787                mCurrentKey = null;
788                setReleasedKeyGraphics(oldKey);
789                return;
790            }
791        }
792
793        if (key != null) {
794            if (oldKey == null) {
795                // The pointer has been slid in to the new key, but the finger was not on any keys.
796                // In this case, we must call onPress() to notify that the new key is being pressed.
797                // This onPress call may have changed keyboard layout. Those cases are detected at
798                // {@link #setKeyboard}. In those cases, we should update key according to the
799                // new keyboard layout.
800                if (callListenerOnPressAndCheckKeyboardLayoutChange(key)) {
801                    key = onMoveKey(x, y);
802                }
803                onMoveToNewKey(key, x, y);
804                startLongPressTimer(key);
805                setPressedKeyGraphics(key);
806            } else if (isMajorEnoughMoveToBeOnNewKey(x, y, key)) {
807                // The pointer has been slid in to the new key from the previous key, we must call
808                // onRelease() first to notify that the previous key has been released, then call
809                // onPress() to notify that the new key is being pressed.
810                setReleasedKeyGraphics(oldKey);
811                callListenerOnRelease(oldKey, oldKey.mCode, true);
812                startSlidingKeyInput(oldKey);
813                mTimerProxy.cancelKeyTimers();
814                startRepeatKey(key);
815                if (mIsAllowedSlidingKeyInput) {
816                    // This onPress call may have changed keyboard layout. Those cases are detected
817                    // at {@link #setKeyboard}. In those cases, we should update key according
818                    // to the new keyboard layout.
819                    if (callListenerOnPressAndCheckKeyboardLayoutChange(key)) {
820                        key = onMoveKey(x, y);
821                    }
822                    onMoveToNewKey(key, x, y);
823                    startLongPressTimer(key);
824                    setPressedKeyGraphics(key);
825                } else {
826                    // HACK: On some devices, quick successive touches may be translated to sudden
827                    // move by touch panel firmware. This hack detects the case and translates the
828                    // move event to successive up and down events.
829                    final int dx = x - lastX;
830                    final int dy = y - lastY;
831                    final int lastMoveSquared = dx * dx + dy * dy;
832                    // TODO: Should find a way to balance gesture detection and this hack.
833                    if (sNeedsPhantomSuddenMoveEventHack
834                            && lastMoveSquared >= mKeyQuarterWidthSquared
835                            && !mIsDetectingGesture) {
836                        if (DEBUG_MODE) {
837                            Log.w(TAG, String.format("onMoveEvent:"
838                                    + " phantom sudden move event is translated to "
839                                    + "up[%d,%d]/down[%d,%d] events", lastX, lastY, x, y));
840                        }
841                        // TODO: This should be moved to outside of this nested if-clause?
842                        if (ProductionFlag.IS_EXPERIMENTAL) {
843                            ResearchLogger.pointerTracker_onMoveEvent(x, y, lastX, lastY);
844                        }
845                        onUpEventInternal();
846                        onDownEventInternal(x, y, eventTime);
847                    } else {
848                        // HACK: If there are currently multiple touches, register the key even if
849                        // the finger slides off the key. This defends against noise from some
850                        // touch panels when there are close multiple touches.
851                        // Caveat: When in chording input mode with a modifier key, we don't use
852                        // this hack.
853                        if (getActivePointerTrackerCount() > 1 && sPointerTrackerQueue != null
854                                && !sPointerTrackerQueue.hasModifierKeyOlderThan(this)) {
855                            onUpEventInternal();
856                        }
857                        if (!mIsDetectingGesture) {
858                            mKeyAlreadyProcessed = true;
859                        }
860                        setReleasedKeyGraphics(oldKey);
861                    }
862                }
863            }
864        } else {
865            if (oldKey != null && isMajorEnoughMoveToBeOnNewKey(x, y, key)) {
866                // The pointer has been slid out from the previous key, we must call onRelease() to
867                // notify that the previous key has been released.
868                setReleasedKeyGraphics(oldKey);
869                callListenerOnRelease(oldKey, oldKey.mCode, true);
870                startSlidingKeyInput(oldKey);
871                mTimerProxy.cancelLongPressTimer();
872                if (mIsAllowedSlidingKeyInput) {
873                    onMoveToNewKey(key, x, y);
874                } else {
875                    if (!mIsDetectingGesture) {
876                        mKeyAlreadyProcessed = true;
877                    }
878                }
879            }
880        }
881    }
882
883    public void onUpEvent(final int x, final int y, final long eventTime) {
884        if (DEBUG_EVENT) {
885            printTouchEvent("onUpEvent  :", x, y, eventTime);
886        }
887
888        final PointerTrackerQueue queue = sPointerTrackerQueue;
889        if (queue != null) {
890            if (!sInGesture) {
891                if (mCurrentKey != null && mCurrentKey.isModifier()) {
892                    // Before processing an up event of modifier key, all pointers already being
893                    // tracked should be released.
894                    queue.releaseAllPointersExcept(this, eventTime);
895                } else {
896                    queue.releaseAllPointersOlderThan(this, eventTime);
897                }
898            }
899        }
900        onUpEventInternal();
901        if (queue != null) {
902            queue.remove(this);
903        }
904    }
905
906    // Let this pointer tracker know that one of newer-than-this pointer trackers got an up event.
907    // This pointer tracker needs to keep the key top graphics "pressed", but needs to get a
908    // "virtual" up event.
909    @Override
910    public void onPhantomUpEvent(final long eventTime) {
911        if (DEBUG_EVENT) {
912            printTouchEvent("onPhntEvent:", getLastX(), getLastY(), eventTime);
913        }
914        onUpEventInternal();
915        mKeyAlreadyProcessed = true;
916    }
917
918    private void onUpEventInternal() {
919        mTimerProxy.cancelKeyTimers();
920        mIsInSlidingKeyInput = false;
921        mIsDetectingGesture = false;
922        final Key currentKey = mCurrentKey;
923        mCurrentKey = null;
924        // Release the last pressed key.
925        setReleasedKeyGraphics(currentKey);
926        if (mIsShowingMoreKeysPanel) {
927            mDrawingProxy.dismissMoreKeysPanel();
928            mIsShowingMoreKeysPanel = false;
929        }
930
931        if (sInGesture) {
932            if (currentKey != null) {
933                callListenerOnRelease(currentKey, currentKey.mCode, true);
934            }
935            mayEndBatchInput();
936            return;
937        }
938        // This event will be recognized as a regular code input. Clear unused possible batch points
939        // so they are not mistakenly displayed as preview.
940        clearBatchInputPointsOfAllPointerTrackers();
941        if (mKeyAlreadyProcessed) {
942            return;
943        }
944        if (currentKey != null && !currentKey.isRepeatable()) {
945            detectAndSendKey(currentKey, mKeyX, mKeyY);
946        }
947    }
948
949    public void onShowMoreKeysPanel(final int x, final int y, final KeyEventHandler handler) {
950        abortBatchInput();
951        onLongPressed();
952        mIsShowingMoreKeysPanel = true;
953        onDownEvent(x, y, SystemClock.uptimeMillis(), handler);
954    }
955
956    public void onLongPressed() {
957        mKeyAlreadyProcessed = true;
958        setReleasedKeyGraphics(mCurrentKey);
959        final PointerTrackerQueue queue = sPointerTrackerQueue;
960        if (queue != null) {
961            queue.remove(this);
962        }
963    }
964
965    public void onCancelEvent(final int x, final int y, final long eventTime) {
966        if (DEBUG_EVENT) {
967            printTouchEvent("onCancelEvt:", x, y, eventTime);
968        }
969
970        final PointerTrackerQueue queue = sPointerTrackerQueue;
971        if (queue != null) {
972            queue.releaseAllPointersExcept(this, eventTime);
973            queue.remove(this);
974        }
975        onCancelEventInternal();
976    }
977
978    private void onCancelEventInternal() {
979        mTimerProxy.cancelKeyTimers();
980        setReleasedKeyGraphics(mCurrentKey);
981        mIsInSlidingKeyInput = false;
982        if (mIsShowingMoreKeysPanel) {
983            mDrawingProxy.dismissMoreKeysPanel();
984            mIsShowingMoreKeysPanel = false;
985        }
986    }
987
988    private void startRepeatKey(final Key key) {
989        if (key != null && key.isRepeatable() && !sInGesture) {
990            onRegisterKey(key);
991            mTimerProxy.startKeyRepeatTimer(this);
992        }
993    }
994
995    public void onRegisterKey(final Key key) {
996        if (key != null) {
997            detectAndSendKey(key, key.mX, key.mY);
998            mTimerProxy.startTypingStateTimer(key);
999        }
1000    }
1001
1002    private boolean isMajorEnoughMoveToBeOnNewKey(final int x, final int y, final Key newKey) {
1003        if (mKeyDetector == null) {
1004            throw new NullPointerException("keyboard and/or key detector not set");
1005        }
1006        final Key curKey = mCurrentKey;
1007        if (newKey == curKey) {
1008            return false;
1009        } else if (curKey != null) {
1010            return curKey.squaredDistanceToEdge(x, y)
1011                    >= mKeyDetector.getKeyHysteresisDistanceSquared();
1012        } else {
1013            return true;
1014        }
1015    }
1016
1017    private void startLongPressTimer(final Key key) {
1018        if (key != null && key.isLongPressEnabled() && !sInGesture) {
1019            mTimerProxy.startLongPressTimer(this);
1020        }
1021    }
1022
1023    private void detectAndSendKey(final Key key, final int x, final int y) {
1024        if (key == null) {
1025            callListenerOnCancelInput();
1026            return;
1027        }
1028
1029        final int code = key.mCode;
1030        callListenerOnCodeInput(key, code, x, y);
1031        callListenerOnRelease(key, code, false);
1032    }
1033
1034    private void printTouchEvent(final String title, final int x, final int y,
1035            final long eventTime) {
1036        final Key key = mKeyDetector.detectHitKey(x, y);
1037        final String code = KeyDetector.printableCode(key);
1038        Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %s", title,
1039                (mKeyAlreadyProcessed ? "-" : " "), mPointerId, x, y, eventTime, code));
1040    }
1041}
1042