PointerTracker.java revision 694ccb5ba1bb61caea379487c6bdfae7c64c2643
16a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka/*
28632bff2d5a8e1160989008dea6eff4b94b065ddTadashi G. Takaoka * Copyright (C) 2010 The Android Open Source Project
36a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka *
46a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License"); you may not
56a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * use this file except in compliance with the License. You may obtain a copy of
66a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * the License at
76a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka *
86a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * http://www.apache.org/licenses/LICENSE-2.0
96a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka *
106a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software
116a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
126a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
136a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * License for the specific language governing permissions and limitations under
146a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka * the License.
156a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka */
166a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
175a309f57155fb95667c2ccdda730eaf175de8876Tadashi G. Takaokapackage com.android.inputmethod.keyboard;
186a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
194daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyangimport android.graphics.Canvas;
204daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyangimport android.graphics.Paint;
21e51d164482c7896892d6eccb80f1e1e6fe6d50dbTadashi G. Takaokaimport android.os.SystemClock;
22c2a21786e526cc32e48a577a55b1b7e72ae1a6ddTadashi G. Takaokaimport android.util.Log;
238ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaokaimport android.view.MotionEvent;
24c2a21786e526cc32e48a577a55b1b7e72ae1a6ddTadashi G. Takaoka
25918e420d1becc1389b9895538eceff85fe882c99Tadashi G. Takaokaimport com.android.inputmethod.accessibility.AccessibilityUtils;
26f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.GestureStroke;
2772934bd5967d0127f71fd4d66158b18b4e6ceefeTadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.PointerTrackerQueue;
285f282ea9e4a4590fcbab6e27d5fca7dacbb40a6aTadashi G. Takaokaimport com.android.inputmethod.latin.CollectionUtils;
29f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaokaimport com.android.inputmethod.latin.InputPointers;
30faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaokaimport com.android.inputmethod.latin.LatinImeLogger;
319bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridgeimport com.android.inputmethod.latin.define.ProductionFlag;
326b966160ac8570271547bf63217efa5e228d4accKurt Partridgeimport com.android.inputmethod.research.ResearchLogger;
336a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
345c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaokaimport java.util.ArrayList;
35dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka
367ae1fd02d40c8c026a411f1037753725868c611eTadashi G. Takaokapublic class PointerTracker implements PointerTrackerQueue.Element {
37dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    private static final String TAG = PointerTracker.class.getSimpleName();
38dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    private static final boolean DEBUG_EVENT = false;
39dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    private static final boolean DEBUG_MOVE_EVENT = false;
40dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    private static final boolean DEBUG_LISTENER = false;
41faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka    private static boolean DEBUG_MODE = LatinImeLogger.sDBG;
4240a05f62edc6cdedb4365a722b48a72826ef2bf6Tadashi G. Takaoka
430657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka    /** True if {@link PointerTracker}s should handle gesture events. */
440657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka    private static boolean sShouldHandleGesture = false;
458335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    private static boolean sMainDictionaryAvailable = false;
468335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    private static boolean sGestureHandlingEnabledByInputField = false;
478335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    private static boolean sGestureHandlingEnabledByUser = false;
48918e420d1becc1389b9895538eceff85fe882c99Tadashi G. Takaoka
491e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka    private static final int MIN_GESTURE_RECOGNITION_TIME = 100; // msec
509580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
51f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka    public interface KeyEventHandler {
52f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        /**
53f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * Get KeyDetector object that is used for this PointerTracker.
54f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * @return the KeyDetector object that is used for this PointerTracker
55f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         */
56f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        public KeyDetector getKeyDetector();
57f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka
58f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        /**
59f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * Get KeyboardActionListener object that is used to register key code and so on.
60f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * @return the KeyboardActionListner for this PointerTracker
61f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         */
62f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        public KeyboardActionListener getKeyboardActionListener();
63f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka
64f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        /**
65f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * Get DrawingProxy object that is used for this PointerTracker.
66f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * @return the DrawingProxy object that is used for this PointerTracker
67f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         */
68f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        public DrawingProxy getDrawingProxy();
69f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka
70f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        /**
71f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * Get TimerProxy object that handles key repeat and long press timer event for this
72f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * PointerTracker.
73f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         * @return the TimerProxy object that handles key repeat and long press timer event.
74f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka         */
75f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        public TimerProxy getTimerProxy();
76f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka    }
77f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka
789d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka    public interface DrawingProxy extends MoreKeysPanel.Controller {
796a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        public void invalidateKey(Key key);
80e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        public void showKeyPreview(PointerTracker tracker);
81d9786ce2e389c8c02af7773b53b5c44fe4fa0b0cTadashi G. Takaoka        public void dismissKeyPreview(PointerTracker tracker);
824daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang        public void showGestureTrail(PointerTracker tracker);
836a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
846a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
852321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka    public interface TimerProxy {
86d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka        public void startTypingStateTimer(Key typedKey);
8773a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        public boolean isTypingState();
88a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka        public void startKeyRepeatTimer(PointerTracker tracker);
89a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka        public void startLongPressTimer(PointerTracker tracker);
90a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka        public void startLongPressTimer(int code);
9198b5c982b93cbfc74b221af30079ecb69dd4e0a1Tadashi G. Takaoka        public void cancelLongPressTimer();
920ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        public void startDoubleTapTimer();
93beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka        public void cancelDoubleTapTimer();
940ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        public boolean isInDoubleTapTimeout();
952321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        public void cancelKeyTimers();
9629e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka
9729e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka        public static class Adapter implements TimerProxy {
9829e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka            @Override
99d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            public void startTypingStateTimer(Key typedKey) {}
10093246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka            @Override
10173a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            public boolean isTypingState() { return false; }
10293246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka            @Override
103a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            public void startKeyRepeatTimer(PointerTracker tracker) {}
10429e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka            @Override
105a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            public void startLongPressTimer(PointerTracker tracker) {}
106a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            @Override
107a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            public void startLongPressTimer(int code) {}
10829e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka            @Override
10929e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka            public void cancelLongPressTimer() {}
11029e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka            @Override
1110ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            public void startDoubleTapTimer() {}
1120ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            @Override
113beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka            public void cancelDoubleTapTimer() {}
114beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka            @Override
1150ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            public boolean isInDoubleTapTimeout() { return false; }
1160ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            @Override
11729e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka            public void cancelKeyTimers() {}
11829e7b7ed6ef88c3e10cc6469801fef87241c9cb5Tadashi G. Takaoka        }
1192321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka    }
1202321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka
121160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka    // Parameters for pointer handling.
122c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka    private static MainKeyboardView.PointerTrackerParams sParams;
1235c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    private static int sTouchNoiseThresholdDistanceSquared;
124d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka    private static boolean sNeedsPhantomSuddenMoveEventHack;
1255c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
1265f282ea9e4a4590fcbab6e27d5fca7dacbb40a6aTadashi G. Takaoka    private static final ArrayList<PointerTracker> sTrackers = CollectionUtils.newArrayList();
1270c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang    private static final InputPointers sAggregratedPointers = new InputPointers(
1280c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang            GestureStroke.DEFAULT_CAPACITY);
1295c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    private static PointerTrackerQueue sPointerTrackerQueue;
1305c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
1315c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    public final int mPointerId;
1326a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
1330efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    private DrawingProxy mDrawingProxy;
1340efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    private TimerProxy mTimerProxy;
135a19b84dcf65bd70caa0fc72089cfe043b023a898Tadashi G. Takaoka    private KeyDetector mKeyDetector;
136dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    private KeyboardActionListener mListener = EMPTY_LISTENER;
137baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka
1385a309f57155fb95667c2ccdda730eaf175de8876Tadashi G. Takaoka    private Keyboard mKeyboard;
139faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka    private int mKeyQuarterWidthSquared;
1406a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
1419580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private boolean mIsAlphabetKeyboard;
1429580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private boolean mIsPossibleGesture = false;
1439580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private boolean mInGesture = false;
1449580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
1451e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka    // TODO: Remove these variables
1469580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private int mLastRecognitionPointSize = 0;
1479580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private long mLastRecognitionTime = 0;
1489580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
1498a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    // The position and time at which first down event occurred.
1508a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    private long mDownTime;
1518a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    private long mUpTime;
1528a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
153e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka    // The current key where this pointer is.
154e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka    private Key mCurrentKey = null;
155e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka    // The position where the current key was recognized for the first time.
1568a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    private int mKeyX;
1578a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    private int mKeyY;
1588a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
1598a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    // Last pointer position.
1608a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    private int mLastX;
1618a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    private int mLastY;
1626a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
1631a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka    // true if keyboard layout has been changed.
1641a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka    private boolean mKeyboardLayoutHasBeenChanged;
1651a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka
1669ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka    // true if event is already translated to a key action.
167c5d33b16521de56ad01b0b6308217efb009078b7Tadashi G. Takaoka    private boolean mKeyAlreadyProcessed;
168c5d33b16521de56ad01b0b6308217efb009078b7Tadashi G. Takaoka
1699d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka    // true if this pointer has been long-pressed and is showing a more keys panel.
1709d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka    private boolean mIsShowingMoreKeysPanel;
1719ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka
172cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka    // true if this pointer is in sliding key input
1735c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    boolean mIsInSlidingKeyInput;
174cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka
17567a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka    // true if sliding key is allowed.
17667a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka    private boolean mIsAllowedSlidingKeyInput;
17767a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka
178996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka    // ignore modifier key if true
179996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka    private boolean mIgnoreModifierKey;
180996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka
181dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    // Empty {@link KeyboardActionListener}
182e3be3bcebc11892b536fdf650f74bba21af13383Tadashi G. Takaoka    private static final KeyboardActionListener EMPTY_LISTENER =
183e3be3bcebc11892b536fdf650f74bba21af13383Tadashi G. Takaoka            new KeyboardActionListener.Adapter();
1846e5a3986854549a45c95770b5a88ae5577e93299Tadashi G. Takaoka
185f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka    private final GestureStroke mGestureStroke;
186f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka
187d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka    public static void init(boolean hasDistinctMultitouch,
18862b8dddb6ddb057555a1665759f9cf197e480c9fTadashi G. Takaoka            boolean needsPhantomSuddenMoveEventHack) {
1895c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        if (hasDistinctMultitouch) {
1905c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            sPointerTrackerQueue = new PointerTrackerQueue();
1915c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        } else {
1925c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            sPointerTrackerQueue = null;
1935c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        }
194d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka        sNeedsPhantomSuddenMoveEventHack = needsPhantomSuddenMoveEventHack;
1955c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
196c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka        setParameters(MainKeyboardView.PointerTrackerParams.DEFAULT);
197160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka    }
19893246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka
199694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public static void setParameters(final MainKeyboardView.PointerTrackerParams params) {
200160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka        sParams = params;
2015c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        sTouchNoiseThresholdDistanceSquared = (int)(
202160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka                params.mTouchNoiseThresholdDistance * params.mTouchNoiseThresholdDistance);
2036a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
2046a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
2058335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    private static void updateGestureHandlingMode() {
2068335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        sShouldHandleGesture = sMainDictionaryAvailable
2078335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka                && sGestureHandlingEnabledByInputField
2088335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka                && sGestureHandlingEnabledByUser
2098335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka                && !AccessibilityUtils.getInstance().isTouchExplorationEnabled();
2108335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    }
2118335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka
2128335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    // Note that this method is called from a non-UI thread.
213694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public static void setMainDictionaryAvailability(final boolean mainDictionaryAvailable) {
2148335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        sMainDictionaryAvailable = mainDictionaryAvailable;
2158335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        updateGestureHandlingMode();
2168335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    }
2178335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka
218694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public static void setGestureHandlingEnabledByUser(final boolean gestureHandlingEnabledByUser) {
2198335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        sGestureHandlingEnabledByUser = gestureHandlingEnabledByUser;
2208335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        updateGestureHandlingMode();
221918e420d1becc1389b9895538eceff85fe882c99Tadashi G. Takaoka    }
222918e420d1becc1389b9895538eceff85fe882c99Tadashi G. Takaoka
223694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public static PointerTracker getPointerTracker(final int id, final KeyEventHandler handler) {
224b4fbbe57f574ce6e6a5827156f875fe7d3eb5089Tadashi G. Takaoka        final ArrayList<PointerTracker> trackers = sTrackers;
2255c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
2265c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        // Create pointer trackers until we can get 'id+1'-th tracker, if needed.
2275c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        for (int i = trackers.size(); i <= id; i++) {
2285c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            final PointerTracker tracker = new PointerTracker(i, handler);
2295c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            trackers.add(tracker);
2305c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        }
2315c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
2325c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        return trackers.get(id);
2335c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    }
2345c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
2355c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    public static boolean isAnyInSlidingKeyInput() {
2365c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        return sPointerTrackerQueue != null ? sPointerTrackerQueue.isAnyInSlidingKeyInput() : false;
2375c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    }
2385c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
239694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public static void setKeyboardActionListener(final KeyboardActionListener listener) {
240afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        final int trackersSize = sTrackers.size();
241afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        for (int i = 0; i < trackersSize; ++i) {
242afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa            final PointerTracker tracker = sTrackers.get(i);
2435c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            tracker.mListener = listener;
2445c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        }
2455c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    }
2465c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
247694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public static void setKeyDetector(final KeyDetector keyDetector) {
248afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        final int trackersSize = sTrackers.size();
249afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        for (int i = 0; i < trackersSize; ++i) {
250afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa            final PointerTracker tracker = sTrackers.get(i);
2515c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            tracker.setKeyDetectorInner(keyDetector);
2525c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            // Mark that keyboard layout has been changed.
2535c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            tracker.mKeyboardLayoutHasBeenChanged = true;
2545c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        }
255918e420d1becc1389b9895538eceff85fe882c99Tadashi G. Takaoka        final Keyboard keyboard = keyDetector.getKeyboard();
2568335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        sGestureHandlingEnabledByInputField = !keyboard.mId.passwordInput();
2578335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        updateGestureHandlingMode();
2585c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    }
2595c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
260dde36ef34329164cf8b8a3985c578dab0343b3ebTadashi G. Takaoka    public static void setReleasedKeyGraphicsToAllKeys() {
261afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        final int trackersSize = sTrackers.size();
262afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        for (int i = 0; i < trackersSize; ++i) {
263afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa            final PointerTracker tracker = sTrackers.get(i);
264e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            tracker.setReleasedKeyGraphics(tracker.mCurrentKey);
2655c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        }
2665c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka    }
2675c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka
2689580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    // TODO: To handle multi-touch gestures we may want to move this method to
2699580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    // {@link PointerTrackerQueue}.
2709580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private static InputPointers getIncrementalBatchPoints() {
271afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        final int trackersSize = sTrackers.size();
272afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        for (int i = 0; i < trackersSize; ++i) {
273afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa            final PointerTracker tracker = sTrackers.get(i);
2740c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang            tracker.mGestureStroke.appendIncrementalBatchPoints(sAggregratedPointers);
275f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka        }
2760c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang        return sAggregratedPointers;
277f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka    }
278f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka
2799580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    // TODO: To handle multi-touch gestures we may want to move this method to
2809580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    // {@link PointerTrackerQueue}.
2819580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private static InputPointers getAllBatchPoints() {
282afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        final int trackersSize = sTrackers.size();
283afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        for (int i = 0; i < trackersSize; ++i) {
284afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa            final PointerTracker tracker = sTrackers.get(i);
2850c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang            tracker.mGestureStroke.appendAllBatchPoints(sAggregratedPointers);
286f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka        }
2870c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang        return sAggregratedPointers;
288f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka    }
289f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka
2909580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    // TODO: To handle multi-touch gestures we may want to move this method to
2919580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    // {@link PointerTrackerQueue}.
2921e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka    public static void clearBatchInputPointsOfAllPointerTrackers() {
293afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        final int trackersSize = sTrackers.size();
294afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa        for (int i = 0; i < trackersSize; ++i) {
295afed0567e91b9411fa61b03f5ac17812db56fd18Ken Wakasa            final PointerTracker tracker = sTrackers.get(i);
2969580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            tracker.mGestureStroke.reset();
297f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka        }
2980c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang        sAggregratedPointers.reset();
299f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka    }
300f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka
301694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private PointerTracker(final int id, final KeyEventHandler handler) {
302694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (handler == null) {
3035c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            throw new NullPointerException();
304694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
3055c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        mPointerId = id;
306f39fccbd0fd63647c52e8eabcb60df69f97492b5Tadashi G. Takaoka        mGestureStroke = new GestureStroke(id);
3075c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        setKeyDetectorInner(handler.getKeyDetector());
3085c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        mListener = handler.getKeyboardActionListener();
3095c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        mDrawingProxy = handler.getDrawingProxy();
3105c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        mTimerProxy = handler.getTimerProxy();
3116a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
3126a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
3131a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka    // Returns true if keyboard has been changed by this callback.
314694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private boolean callListenerOnPressAndCheckKeyboardLayoutChange(final Key key) {
3159580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (mInGesture) {
316eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            return false;
317eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        }
318e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
319e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka        if (DEBUG_LISTENER) {
3207dfd5a3e833e14d5bf90d728d5a50b40c8a927d2Tadashi G. Takaoka            Log.d(TAG, "onPress    : " + KeyDetector.printableCode(key)
3212a88440419f49d100c73e067a823390f64aba3b1Tadashi G. Takaoka                    + " ignoreModifier=" + ignoreModifierKey
322e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka                    + " enabled=" + key.isEnabled());
323e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka        }
32493246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        if (ignoreModifierKey) {
325996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka            return false;
32693246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        }
327e7759091ddb5ec18268945d70d9212195bf6497bTadashi G. Takaoka        if (key.isEnabled()) {
3282a88440419f49d100c73e067a823390f64aba3b1Tadashi G. Takaoka            mListener.onPressKey(key.mCode);
329690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka            final boolean keyboardLayoutHasBeenChanged = mKeyboardLayoutHasBeenChanged;
330690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka            mKeyboardLayoutHasBeenChanged = false;
331d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            mTimerProxy.startTypingStateTimer(key);
332690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka            return keyboardLayoutHasBeenChanged;
333690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka        }
334690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka        return false;
335dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    }
336dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka
337690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka    // Note that we need primaryCode argument because the keyboard may in shifted state and the
338690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka    // primaryCode is different from {@link Key#mCode}.
339694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void callListenerOnCodeInput(final Key key, final int primaryCode, final int x,
340694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka            final int y) {
341e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
3426bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        final boolean altersCode = key.altCodeWhileTyping() && mTimerProxy.isTypingState();
3436bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        final int code = altersCode ? key.mAltCode : primaryCode;
3442013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka        if (DEBUG_LISTENER) {
3458cab0b56eb8db311f158b18a361d9ceb85cff482Tadashi G. Takaoka            Log.d(TAG, "onCodeInput: " + Keyboard.printableCode(code) + " text=" + key.mOutputText
346ca7ec2097ca6af1505c1e6aa8b81b6068ba46daesatok                    + " x=" + x + " y=" + y
3476bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka                    + " ignoreModifier=" + ignoreModifierKey + " altersCode=" + altersCode
348e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka                    + " enabled=" + key.isEnabled());
3492013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka        }
3509bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        if (ProductionFlag.IS_EXPERIMENTAL) {
3519bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge            ResearchLogger.pointerTracker_callListenerOnCodeInput(key, x, y, ignoreModifierKey,
3529bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                    altersCode, code);
3539bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        }
354e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        if (ignoreModifierKey) {
355996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka            return;
356e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        }
3576bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        // Even if the key is disabled, it should respond if it is in the altCodeWhileTyping state.
3586bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        if (key.isEnabled() || altersCode) {
3598cab0b56eb8db311f158b18a361d9ceb85cff482Tadashi G. Takaoka            if (code == Keyboard.CODE_OUTPUT_TEXT) {
3608cab0b56eb8db311f158b18a361d9ceb85cff482Tadashi G. Takaoka                mListener.onTextInput(key.mOutputText);
3618cab0b56eb8db311f158b18a361d9ceb85cff482Tadashi G. Takaoka            } else if (code != Keyboard.CODE_UNSPECIFIED) {
362ca7ec2097ca6af1505c1e6aa8b81b6068ba46daesatok                mListener.onCodeInput(code, x, y);
3632013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka            }
36493246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        }
365dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    }
366dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka
367690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka    // Note that we need primaryCode argument because the keyboard may in shifted state and the
368690b1360bfda3cbaae896de65dcc3cd347dc8329Tadashi G. Takaoka    // primaryCode is different from {@link Key#mCode}.
369694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void callListenerOnRelease(final Key key, final int primaryCode,
370694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka            final boolean withSliding) {
3719580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (mInGesture) {
372eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            return;
373eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        }
374e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
375e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka        if (DEBUG_LISTENER) {
3767dfd5a3e833e14d5bf90d728d5a50b40c8a927d2Tadashi G. Takaoka            Log.d(TAG, "onRelease  : " + Keyboard.printableCode(primaryCode)
377e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka                    + " sliding=" + withSliding + " ignoreModifier=" + ignoreModifierKey
378e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka                    + " enabled="+ key.isEnabled());
379e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka        }
3809bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        if (ProductionFlag.IS_EXPERIMENTAL) {
3819bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge            ResearchLogger.pointerTracker_callListenerOnRelease(key, primaryCode, withSliding,
3829bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                    ignoreModifierKey);
3839bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        }
384e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        if (ignoreModifierKey) {
385996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka            return;
386e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        }
38793246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        if (key.isEnabled()) {
3882a88440419f49d100c73e067a823390f64aba3b1Tadashi G. Takaoka            mListener.onReleaseKey(primaryCode, withSliding);
38993246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        }
390dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    }
391dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka
3928aa3f5a3ad6095a3355841ce30bce4877319d0a0Tadashi G. Takaoka    private void callListenerOnCancelInput() {
393694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (DEBUG_LISTENER) {
3948aa3f5a3ad6095a3355841ce30bce4877319d0a0Tadashi G. Takaoka            Log.d(TAG, "onCancelInput");
395694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
3969bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        if (ProductionFlag.IS_EXPERIMENTAL) {
3979bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge            ResearchLogger.pointerTracker_callListenerOnCancelInput();
3989bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        }
3998aa3f5a3ad6095a3355841ce30bce4877319d0a0Tadashi G. Takaoka        mListener.onCancelInput();
400dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    }
401dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka
402694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void setKeyDetectorInner(final KeyDetector keyDetector) {
403a19b84dcf65bd70caa0fc72089cfe043b023a898Tadashi G. Takaoka        mKeyDetector = keyDetector;
4045a7a696aff6718d4e0250c394a9d01cbf2a16916Tadashi G. Takaoka        mKeyboard = keyDetector.getKeyboard();
4059580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mIsAlphabetKeyboard = mKeyboard.mId.isAlphabetKeyboard();
40674d0bb09c700aec91afd120688c56498d93e3110Tadashi G. Takaoka        mGestureStroke.setGestureSampleLength(mKeyboard.mMostCommonKeyWidth);
4078a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka        final Key newKey = mKeyDetector.detectHitKey(mKeyX, mKeyY);
4088a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka        if (newKey != mCurrentKey) {
4098a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka            if (mDrawingProxy != null) {
4108a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka                setReleasedKeyGraphics(mCurrentKey);
4118a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka            }
41244972bcdb6f6dd0e4f02a26f31c7effaf0525403Tadashi G. Takaoka            // Keep {@link #mCurrentKey} that comes from previous keyboard.
4138a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka        }
4148da9a13760896cd78235b60d0ea680ea13620532Tadashi G. Takaoka        final int keyQuarterWidth = mKeyboard.mMostCommonKeyWidth / 4;
415faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka        mKeyQuarterWidthSquared = keyQuarterWidth * keyQuarterWidth;
4165a7a696aff6718d4e0250c394a9d01cbf2a16916Tadashi G. Takaoka    }
4175a7a696aff6718d4e0250c394a9d01cbf2a16916Tadashi G. Takaoka
4180cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka    @Override
419cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka    public boolean isInSlidingKeyInput() {
420cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka        return mIsInSlidingKeyInput;
421cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka    }
422cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka
423e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka    public Key getKey() {
424e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        return mCurrentKey;
425dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    }
426dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka
4270cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka    @Override
4282aa8078df86029dab394d8dd616f4f6decb39035Tadashi G. Takaoka    public boolean isModifier() {
429e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        return mCurrentKey != null && mCurrentKey.isModifier();
4302aa8078df86029dab394d8dd616f4f6decb39035Tadashi G. Takaoka    }
4312aa8078df86029dab394d8dd616f4f6decb39035Tadashi G. Takaoka
432694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public Key getKeyOn(final int x, final int y) {
433723aaa2eebcfea0d285f11fc265941057332664dTadashi G. Takaoka        return mKeyDetector.detectHitKey(x, y);
4342aa8078df86029dab394d8dd616f4f6decb39035Tadashi G. Takaoka    }
4352aa8078df86029dab394d8dd616f4f6decb39035Tadashi G. Takaoka
436694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void setReleasedKeyGraphics(final Key key) {
437d3002aa8cd5339d59123e0c96174f6701e2c72ccTadashi G. Takaoka        mDrawingProxy.dismissKeyPreview(this);
4386bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        if (key == null) {
439faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            return;
440faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        }
441faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka
4426bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        // Even if the key is disabled, update the key release graphics just in case.
443faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        updateReleaseKeyGraphics(key);
444faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka
445faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        if (key.isShift()) {
446faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            for (final Key shiftKey : mKeyboard.mShiftKeys) {
447faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                if (shiftKey != key) {
448faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                    updateReleaseKeyGraphics(shiftKey);
4492013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka                }
4502013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka            }
451faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        }
4522013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka
453faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        if (key.altCodeWhileTyping()) {
454faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            final int altCode = key.mAltCode;
455faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            final Key altKey = mKeyboard.getKey(altCode);
456faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            if (altKey != null) {
457faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                updateReleaseKeyGraphics(altKey);
458faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            }
459faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            for (final Key k : mKeyboard.mAltCodeKeysWhileTyping) {
460faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                if (k != key && k.mAltCode == altCode) {
461faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                    updateReleaseKeyGraphics(k);
4622013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka                }
4632013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka            }
4646a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        }
4656a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
4666a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
467694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void setPressedKeyGraphics(final Key key) {
4686bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        if (key == null) {
4696bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka            return;
4706bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        }
4716bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka
4726bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        // Even if the key is disabled, it should respond if it is in the altCodeWhileTyping state.
4736bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        final boolean altersCode = key.altCodeWhileTyping() && mTimerProxy.isTypingState();
4746bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        final boolean needsToUpdateGraphics = key.isEnabled() || altersCode;
4756bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        if (!needsToUpdateGraphics) {
476faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            return;
477faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        }
478faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka
4799580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (!key.noKeyPreview() && !mInGesture) {
480faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            mDrawingProxy.showKeyPreview(this);
481faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        }
482faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        updatePressKeyGraphics(key);
483faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka
484faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        if (key.isShift()) {
485faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            for (final Key shiftKey : mKeyboard.mShiftKeys) {
486faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                if (shiftKey != key) {
487faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                    updatePressKeyGraphics(shiftKey);
4882013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka                }
4892013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka            }
490faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        }
4912013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka
49273a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        if (key.altCodeWhileTyping() && mTimerProxy.isTypingState()) {
493faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            final int altCode = key.mAltCode;
494faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            final Key altKey = mKeyboard.getKey(altCode);
495faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            if (altKey != null) {
496faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                updatePressKeyGraphics(altKey);
497faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            }
498faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka            for (final Key k : mKeyboard.mAltCodeKeysWhileTyping) {
499faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                if (k != key && k.mAltCode == altCode) {
500faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka                    updatePressKeyGraphics(k);
5012013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka                }
5022013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka            }
503d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka        }
504c5d33b16521de56ad01b0b6308217efb009078b7Tadashi G. Takaoka    }
505c5d33b16521de56ad01b0b6308217efb009078b7Tadashi G. Takaoka
506694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void updateReleaseKeyGraphics(final Key key) {
507faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        key.onReleased();
508faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        mDrawingProxy.invalidateKey(key);
509faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka    }
510faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka
511694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void updatePressKeyGraphics(final Key key) {
512faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        key.onPressed();
513faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka        mDrawingProxy.invalidateKey(key);
514faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka    }
515faad353feabef17e3bea6d5d9b4cb8bacf94788cTadashi G. Takaoka
516694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void drawGestureTrail(final Canvas canvas, final Paint paint) {
5172f81757c3a5eb50d41ce19fb534f72cbf607a997Tom Ouyang        if (mInGesture) {
518fc5e3f973c043e88e1859f5dafe8df9b417dc3caTadashi G. Takaoka            mGestureStroke.drawGestureTrail(canvas, paint);
5192f81757c3a5eb50d41ce19fb534f72cbf607a997Tom Ouyang        }
5202f81757c3a5eb50d41ce19fb534f72cbf607a997Tom Ouyang    }
5212f81757c3a5eb50d41ce19fb534f72cbf607a997Tom Ouyang
5228a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    public int getLastX() {
5238a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        return mLastX;
5248a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5258a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
5268a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    public int getLastY() {
5278a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        return mLastY;
5288a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5298a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
5308a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    public long getDownTime() {
5318a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        return mDownTime;
5328a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5338a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
534694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private Key onDownKey(final int x, final int y, final long eventTime) {
5358a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        mDownTime = eventTime;
5368a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        return onMoveToNewKey(onMoveKeyInternal(x, y), x, y);
5378a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5388a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
539694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private Key onMoveKeyInternal(final int x, final int y) {
5408a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        mLastX = x;
5418a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        mLastY = y;
542723aaa2eebcfea0d285f11fc265941057332664dTadashi G. Takaoka        return mKeyDetector.detectHitKey(x, y);
5438a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5448a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
545694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private Key onMoveKey(final int x, final int y) {
5468a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        return onMoveKeyInternal(x, y);
5478a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5488a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
549694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private Key onMoveToNewKey(final Key newKey, final int x, final int y) {
550e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        mCurrentKey = newKey;
5518a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        mKeyX = x;
5528a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        mKeyY = y;
553e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        return newKey;
5548a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka    }
5558a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka
5569580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private void startBatchInput() {
5579580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (DEBUG_LISTENER) {
5589580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            Log.d(TAG, "onStartBatchInput");
5599580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        }
5609580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mInGesture = true;
5619580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mListener.onStartBatchInput();
5629580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    }
5639580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
564694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void updateBatchInput(final InputPointers batchPoints) {
5659580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (DEBUG_LISTENER) {
5669580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            Log.d(TAG, "onUpdateBatchInput: batchPoints=" + batchPoints.getPointerSize());
5679580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        }
5689580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mListener.onUpdateBatchInput(batchPoints);
5699580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    }
5709580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
571694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void endBatchInput(final InputPointers batchPoints) {
5729580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (DEBUG_LISTENER) {
5739580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            Log.d(TAG, "onEndBatchInput: batchPoints=" + batchPoints.getPointerSize());
5749580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        }
5759580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mListener.onEndBatchInput(batchPoints);
5761e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        clearBatchInputRecognitionStateOfThisPointerTracker();
5771e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        clearBatchInputPointsOfAllPointerTrackers();
5789580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    }
5799580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
5809580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    private void abortBatchInput() {
5811e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        clearBatchInputRecognitionStateOfThisPointerTracker();
5821e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        clearBatchInputPointsOfAllPointerTrackers();
5831e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka    }
5841e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka
5851e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka    private void clearBatchInputRecognitionStateOfThisPointerTracker() {
5869580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mIsPossibleGesture = false;
5879580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        mInGesture = false;
5881e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        mLastRecognitionPointSize = 0;
5891e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        mLastRecognitionTime = 0;
5909580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    }
5919580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
592694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private boolean updateBatchInputRecognitionState(final long eventTime, final int size) {
5939580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (size > mLastRecognitionPointSize
5941e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka                && eventTime > mLastRecognitionTime + MIN_GESTURE_RECOGNITION_TIME) {
5959580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            mLastRecognitionPointSize = size;
5969580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            mLastRecognitionTime = eventTime;
5979580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            return true;
5989580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        }
5999580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        return false;
6009580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    }
6019580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
602694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void processMotionEvent(final int action, final int x, final int y, final long eventTime,
603694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka            final KeyEventHandler handler) {
6048ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        switch (action) {
6058ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        case MotionEvent.ACTION_DOWN:
6068ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        case MotionEvent.ACTION_POINTER_DOWN:
6078ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            onDownEvent(x, y, eventTime, handler);
6088ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            break;
6098ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        case MotionEvent.ACTION_UP:
6108ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        case MotionEvent.ACTION_POINTER_UP:
6118ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            onUpEvent(x, y, eventTime);
6128ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            break;
6138ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        case MotionEvent.ACTION_MOVE:
6143314d38dafc0b9670e695a194c74950c4ebf2b3dTadashi G. Takaoka            onMoveEvent(x, y, eventTime, null);
6158ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            break;
6168ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        case MotionEvent.ACTION_CANCEL:
6178ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            onCancelEvent(x, y, eventTime);
6188ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka            break;
6198ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka        }
6208ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka    }
6218ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka
622694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onDownEvent(final int x, final int y, final long eventTime,
623694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka            final KeyEventHandler handler) {
624694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (DEBUG_EVENT) {
625dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka            printTouchEvent("onDownEvent:", x, y, eventTime);
626694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
6271d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
628f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        mDrawingProxy = handler.getDrawingProxy();
62963c233ab9f50d844be6e52e382c6664475606760Tadashi G. Takaoka        mTimerProxy = handler.getTimerProxy();
630f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        setKeyboardActionListener(handler.getKeyboardActionListener());
631f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        setKeyDetectorInner(handler.getKeyDetector());
632baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka        // Naive up-to-down noise filter.
6338a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        final long deltaT = eventTime - mUpTime;
634160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka        if (deltaT < sParams.mTouchNoiseThresholdTime) {
6358a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka            final int dx = x - mLastX;
6368a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka            final int dy = y - mLastY;
637baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka            final int distanceSquared = (dx * dx + dy * dy);
6385c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka            if (distanceSquared < sTouchNoiseThresholdDistanceSquared) {
639faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                if (DEBUG_MODE)
640faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    Log.w(TAG, "onDownEvent: ignore potential noise: time=" + deltaT
641faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                            + " distance=" + distanceSquared);
6429bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                if (ProductionFlag.IS_EXPERIMENTAL) {
6439bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                    ResearchLogger.pointerTracker_onDownEvent(deltaT, distanceSquared);
6449bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                }
645d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka                mKeyAlreadyProcessed = true;
646baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka                return;
647baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka            }
648baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka        }
649baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka
6505c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        final PointerTrackerQueue queue = sPointerTrackerQueue;
651eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        final Key key = getKeyOn(x, y);
6521d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka        if (queue != null) {
653e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            if (key != null && key.isModifier()) {
6541d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka                // Before processing a down event of modifier key, all pointers already being
6551d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka                // tracked should be released.
6561d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka                queue.releaseAllPointers(eventTime);
6571d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka            }
6581d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka            queue.add(this);
6591d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka        }
6601d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka        onDownEventInternal(x, y, eventTime);
661eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        if (queue != null && queue.size() == 1) {
6629580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            mIsPossibleGesture = false;
6639580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            // A gesture should start only from the letter key.
6640657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka            if (sShouldHandleGesture && mIsAlphabetKeyboard && !mIsShowingMoreKeysPanel
6650657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka                    && key != null && Keyboard.isLetterCode(key.mCode)) {
6669580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka                mIsPossibleGesture = true;
6670c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang                // TODO: pointer times should be relative to first down even in entire batch input
6680c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang                // instead of resetting to 0 for each new down event.
6699580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka                mGestureStroke.addPoint(x, y, 0, false);
6709580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            }
671eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        }
6721d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka    }
6731d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
674694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void onDownEventInternal(final int x, final int y, final long eventTime) {
675e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        Key key = onDownKey(x, y, eventTime);
67667a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka        // Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding
67732572948d7e3956efebcbd69d7c7d8403bb659e6Tadashi G. Takaoka        // from modifier key, or 3) this pointer's KeyDetector always allows sliding input.
678160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka        mIsAllowedSlidingKeyInput = sParams.mSlidingKeyInputEnabled
679c9f203805ca23276fcdcdc79b9298bc1d413ad98Tadashi G. Takaoka                || (key != null && key.isModifier())
68032572948d7e3956efebcbd69d7c7d8403bb659e6Tadashi G. Takaoka                || mKeyDetector.alwaysAllowsSlidingInput();
6811a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka        mKeyboardLayoutHasBeenChanged = false;
682c5d33b16521de56ad01b0b6308217efb009078b7Tadashi G. Takaoka        mKeyAlreadyProcessed = false;
683cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka        mIsInSlidingKeyInput = false;
684996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka        mIgnoreModifierKey = false;
685e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        if (key != null) {
6861a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka            // This onPress call may have changed keyboard layout. Those cases are detected at
687e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            // {@link #setKeyboard}. In those cases, we should update key according to the new
6881a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka            // keyboard layout.
6892a88440419f49d100c73e067a823390f64aba3b1Tadashi G. Takaoka            if (callListenerOnPressAndCheckKeyboardLayoutChange(key)) {
690e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                key = onDownKey(x, y, eventTime);
691e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            }
692996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka
693e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            startRepeatKey(key);
694e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            startLongPressTimer(key);
695e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            setPressedKeyGraphics(key);
6966a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        }
6976a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
6986a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
699694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void startSlidingKeyInput(final Key key) {
700e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        if (!mIsInSlidingKeyInput) {
701e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            mIgnoreModifierKey = key.isModifier();
702e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        }
703996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka        mIsInSlidingKeyInput = true;
704996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka    }
705996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka
706694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void onGestureMoveEvent(final PointerTracker tracker, final int x, final int y,
707694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka            final long eventTime, final boolean isHistorical, final Key key) {
7089580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        final int gestureTime = (int)(eventTime - tracker.getDownTime());
7090657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka        if (sShouldHandleGesture && mIsPossibleGesture) {
7109580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            final GestureStroke stroke = mGestureStroke;
7119580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            stroke.addPoint(x, y, gestureTime, isHistorical);
71274d0bb09c700aec91afd120688c56498d93e3110Tadashi G. Takaoka            if (!mInGesture && stroke.isStartOfAGesture()) {
7139580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka                startBatchInput();
7149580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            }
7159580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        }
7169580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
7179580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (key != null && mInGesture) {
7189580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            final InputPointers batchPoints = getIncrementalBatchPoints();
7194daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang            mDrawingProxy.showGestureTrail(this);
7209580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            if (updateBatchInputRecognitionState(eventTime, batchPoints.getPointerSize())) {
7219580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka                updateBatchInput(batchPoints);
7229580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            }
7239580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        }
7249580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka    }
7259580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka
726694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onMoveEvent(final int x, final int y, final long eventTime, final MotionEvent me) {
727694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (DEBUG_MOVE_EVENT) {
728dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka            printTouchEvent("onMoveEvent:", x, y, eventTime);
729694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
730694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (mKeyAlreadyProcessed) {
731e8f45ab56f3e6f358953dede794a63fc5901961dTadashi G. Takaoka            return;
732694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
733baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka
734eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        if (me != null) {
735eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            // Add historical points to gesture path.
736eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            final int pointerIndex = me.findPointerIndex(mPointerId);
737eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            final int historicalSize = me.getHistorySize();
738eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            for (int h = 0; h < historicalSize; h++) {
739eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                final int historicalX = (int)me.getHistoricalX(pointerIndex, h);
740eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                final int historicalY = (int)me.getHistoricalY(pointerIndex, h);
741eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                final long historicalTime = me.getHistoricalEventTime(h);
7429580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka                onGestureMoveEvent(this, historicalX, historicalY, historicalTime,
743eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                        true /* isHistorical */, null);
744eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            }
745eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        }
746eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang
7478a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        final int lastX = mLastX;
7488a995157f37365cf79b893e9106d1830d70c39dcTadashi G. Takaoka        final int lastY = mLastY;
749e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        final Key oldKey = mCurrentKey;
750e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        Key key = onMoveKey(x, y);
751eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang
752eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        // Register move event on gesture tracker.
7539580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        onGestureMoveEvent(this, x, y, eventTime, false /* isHistorical */, key);
7549580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (mInGesture) {
755eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            mIgnoreModifierKey = true;
756eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            mTimerProxy.cancelLongPressTimer();
757eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            mIsInSlidingKeyInput = true;
758eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            mCurrentKey = null;
759eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            setReleasedKeyGraphics(oldKey);
760eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        }
761eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang
762e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        if (key != null) {
763c0b5c9c43e5eb7a6ed768d56f462ca9ed5c5f913Tadashi G. Takaoka            if (oldKey == null) {
7649e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // The pointer has been slid in to the new key, but the finger was not on any keys.
7659e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // In this case, we must call onPress() to notify that the new key is being pressed.
7661a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka                // This onPress call may have changed keyboard layout. Those cases are detected at
767e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                // {@link #setKeyboard}. In those cases, we should update key according to the
7681a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka                // new keyboard layout.
7692a88440419f49d100c73e067a823390f64aba3b1Tadashi G. Takaoka                if (callListenerOnPressAndCheckKeyboardLayoutChange(key)) {
770e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    key = onMoveKey(x, y);
771e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                }
772e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                onMoveToNewKey(key, x, y);
773e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                startLongPressTimer(key);
774e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                setPressedKeyGraphics(key);
775e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            } else if (isMajorEnoughMoveToBeOnNewKey(x, y, key)) {
7769e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // The pointer has been slid in to the new key from the previous key, we must call
7779e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // onRelease() first to notify that the previous key has been released, then call
7789e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // onPress() to notify that the new key is being pressed.
779e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                setReleasedKeyGraphics(oldKey);
780e59491460b0411bed430a5ca6eca0c56c5bf18d9Tadashi G. Takaoka                callListenerOnRelease(oldKey, oldKey.mCode, true);
781996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka                startSlidingKeyInput(oldKey);
7822321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka                mTimerProxy.cancelKeyTimers();
783e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                startRepeatKey(key);
78467a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka                if (mIsAllowedSlidingKeyInput) {
7851a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka                    // This onPress call may have changed keyboard layout. Those cases are detected
786e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    // at {@link #setKeyboard}. In those cases, we should update key according
7871a6fba570260ca9f837e5a6874274f39a3c0a734Tadashi G. Takaoka                    // to the new keyboard layout.
7882a88440419f49d100c73e067a823390f64aba3b1Tadashi G. Takaoka                    if (callListenerOnPressAndCheckKeyboardLayoutChange(key)) {
789e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                        key = onMoveKey(x, y);
790e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    }
791e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    onMoveToNewKey(key, x, y);
792e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    startLongPressTimer(key);
793e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    setPressedKeyGraphics(key);
79467a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka                } else {
795faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    // HACK: On some devices, quick successive touches may be translated to sudden
796faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    // move by touch panel firmware. This hack detects the case and translates the
797faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    // move event to successive up and down events.
798faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    final int dx = x - lastX;
799faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    final int dy = y - lastY;
800faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    final int lastMoveSquared = dx * dx + dy * dy;
801c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                    // TODO: Should find a way to balance gesture detection and this hack.
802d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka                    if (sNeedsPhantomSuddenMoveEventHack
803c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                            && lastMoveSquared >= mKeyQuarterWidthSquared
804c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                            && !mIsPossibleGesture) {
805d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka                        if (DEBUG_MODE) {
806d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka                            Log.w(TAG, String.format("onMoveEvent:"
807d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka                                    + " phantom sudden move event is translated to "
808faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                                    + "up[%d,%d]/down[%d,%d] events", lastX, lastY, x, y));
809d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka                        }
810c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                        // TODO: This should be moved to outside of this nested if-clause?
8119bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                        if (ProductionFlag.IS_EXPERIMENTAL) {
8129bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                            ResearchLogger.pointerTracker_onMoveEvent(x, y, lastX, lastY);
8139bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge                        }
8140cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka                        onUpEventInternal();
815faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                        onDownEventInternal(x, y, eventTime);
816faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    } else {
8175e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        // HACK: If there are currently multiple touches, register the key even if
8185e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        // the finger slides off the key. This defends against noise from some
8195e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        // touch panels when there are close multiple touches.
8205e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        // Caveat: When in chording input mode with a modifier key, we don't use
8215e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        // this hack.
8225e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        if (me != null && me.getPointerCount() > 1
8235e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                                && !sPointerTrackerQueue.hasModifierKeyOlderThan(this)) {
8240cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka                            onUpEventInternal();
8255e06b8534ffdf5099d2ef4551b113a103cdf7061Tadashi G. Takaoka                        }
826c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                        if (!mIsPossibleGesture) {
827c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                            mKeyAlreadyProcessed = true;
828c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                        }
829e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                        setReleasedKeyGraphics(oldKey);
830faf437b5078e882b630706cd315c335f204ab861Tadashi G. Takaoka                    }
83167a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka                }
832c5c57b506e97b334a394d23ed73c9597cb55707aTadashi G. Takaoka            }
8336a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        } else {
834e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            if (oldKey != null && isMajorEnoughMoveToBeOnNewKey(x, y, key)) {
8359e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // The pointer has been slid out from the previous key, we must call onRelease() to
8369e91472285a1b903631f1e3c998f5aa1efd3e98eTadashi G. Takaoka                // notify that the previous key has been released.
837e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                setReleasedKeyGraphics(oldKey);
838e59491460b0411bed430a5ca6eca0c56c5bf18d9Tadashi G. Takaoka                callListenerOnRelease(oldKey, oldKey.mCode, true);
839996db15d3c018ed2a7b4eee96ea94b9f80d8e379Tadashi G. Takaoka                startSlidingKeyInput(oldKey);
84098b5c982b93cbfc74b221af30079ecb69dd4e0a1Tadashi G. Takaoka                mTimerProxy.cancelLongPressTimer();
84167a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka                if (mIsAllowedSlidingKeyInput) {
842e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                    onMoveToNewKey(key, x, y);
84367a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka                } else {
844c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                    if (!mIsPossibleGesture) {
845c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                        mKeyAlreadyProcessed = true;
846c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka                    }
84767a4ecacc7525c9343cded13fc93e9a2381ea2d8Tadashi G. Takaoka                }
84807221a4ad11fa5ae6275c107f1f86260691bd505Tadashi G. Takaoka            }
8496a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        }
8506a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
8516a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
852694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onUpEvent(final int x, final int y, final long eventTime) {
853694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (DEBUG_EVENT) {
854dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka            printTouchEvent("onUpEvent  :", x, y, eventTime);
855694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
8561d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
8575c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        final PointerTrackerQueue queue = sPointerTrackerQueue;
8581d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka        if (queue != null) {
8599580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            if (!mInGesture) {
860eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                if (mCurrentKey != null && mCurrentKey.isModifier()) {
861eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                    // Before processing an up event of modifier key, all pointers already being
862eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                    // tracked should be released.
863eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                    queue.releaseAllPointersExcept(this, eventTime);
864eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                } else {
865eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                    queue.releaseAllPointersOlderThan(this, eventTime);
866eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                }
8671d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka            }
8681d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka            queue.remove(this);
8691d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka        }
8700cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka        onUpEventInternal();
8711d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka    }
8721d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
873d9786ce2e389c8c02af7773b53b5c44fe4fa0b0cTadashi G. Takaoka    // Let this pointer tracker know that one of newer-than-this pointer trackers got an up event.
874d9786ce2e389c8c02af7773b53b5c44fe4fa0b0cTadashi G. Takaoka    // This pointer tracker needs to keep the key top graphics "pressed", but needs to get a
875d9786ce2e389c8c02af7773b53b5c44fe4fa0b0cTadashi G. Takaoka    // "virtual" up event.
8760cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka    @Override
877694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onPhantomUpEvent(final long eventTime) {
878694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (DEBUG_EVENT) {
8790cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka            printTouchEvent("onPhntEvent:", getLastX(), getLastY(), eventTime);
880694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
8810cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka        onUpEventInternal();
882d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka        mKeyAlreadyProcessed = true;
8831d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka    }
8841d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
8850cc425bd9c476d3cb6708554282a3242019eb317Tadashi G. Takaoka    private void onUpEventInternal() {
8862321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        mTimerProxy.cancelKeyTimers();
887cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka        mIsInSlidingKeyInput = false;
888c902dfeb8f8b889e885565a000556419e0d8307fTadashi G. Takaoka        mIsPossibleGesture = false;
8895a40dcaf8b6250eeea241471e54e8fe856cdf19bTadashi G. Takaoka        // Release the last pressed key.
8905a40dcaf8b6250eeea241471e54e8fe856cdf19bTadashi G. Takaoka        setReleasedKeyGraphics(mCurrentKey);
8919d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka        if (mIsShowingMoreKeysPanel) {
8929d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka            mDrawingProxy.dismissMoreKeysPanel();
8939d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka            mIsShowingMoreKeysPanel = false;
8949ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka        }
895eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang
8969580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (mInGesture) {
897eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            // Register up event on gesture tracker.
8989580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            // TODO: Figure out how to deal with multiple fingers that are in gesture, sliding,
8999580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            // and/or tapping mode?
9009580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka            endBatchInput(getAllBatchPoints());
901eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            if (mCurrentKey != null) {
902eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang                callListenerOnRelease(mCurrentKey, mCurrentKey.mCode, true);
9039580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka                mCurrentKey = null;
904eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            }
9054daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang            mDrawingProxy.showGestureTrail(this);
906eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang            return;
907eea34598bf63f670f47d7b3f37b6436921e5fe02Tom Ouyang        }
9080c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang        // This event will be recognized as a regular code input. Clear unused batch points so they
9090c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang        // are not mistakenly included in the next batch event.
9100c5f72e2bf22df48af051827f97ab6052026d531Tom Ouyang        clearBatchInputPointsOfAllPointerTrackers();
911694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (mKeyAlreadyProcessed) {
912d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka            return;
913694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
9140d9d37cec2b3c4b4c3747baeb529bd2a70cbafb8Tadashi G. Takaoka        if (mCurrentKey != null && !mCurrentKey.isRepeatable()) {
9155a40dcaf8b6250eeea241471e54e8fe856cdf19bTadashi G. Takaoka            detectAndSendKey(mCurrentKey, mKeyX, mKeyY);
9166a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        }
9176a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
9186a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
919694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onShowMoreKeysPanel(final int x, final int y, final KeyEventHandler handler) {
9201e6f39a9f994e21b749a1cbae55a3adbfb5640e9Tadashi G. Takaoka        abortBatchInput();
9219ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka        onLongPressed();
9229d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka        mIsShowingMoreKeysPanel = true;
923cc3500b0c8f11e8de1ad7376dda466d30637e462Tadashi G. Takaoka        onDownEvent(x, y, SystemClock.uptimeMillis(), handler);
9249ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka    }
9259ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka
926906f03121b6c6a795f35dbc24d2eceac0665f35fTadashi G. Takaoka    public void onLongPressed() {
927d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka        mKeyAlreadyProcessed = true;
928e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        setReleasedKeyGraphics(mCurrentKey);
9295c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        final PointerTrackerQueue queue = sPointerTrackerQueue;
9301ddb4897fee79ec00c68e4a255e653568477a995Tadashi G. Takaoka        if (queue != null) {
931d00d963b9d47c1bba6f65534033a33fe7c30dde5Tadashi G. Takaoka            queue.remove(this);
9321ddb4897fee79ec00c68e4a255e653568477a995Tadashi G. Takaoka        }
933d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka    }
934d2c2b4d112ee17750c1a49ff223b9410aa9e4ec6Tadashi G. Takaoka
935694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onCancelEvent(final int x, final int y, final long eventTime) {
936694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (DEBUG_EVENT) {
937dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka            printTouchEvent("onCancelEvt:", x, y, eventTime);
938694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
9391d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
9405c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        final PointerTrackerQueue queue = sPointerTrackerQueue;
941c5c57b506e97b334a394d23ed73c9597cb55707aTadashi G. Takaoka        if (queue != null) {
942d3002aa8cd5339d59123e0c96174f6701e2c72ccTadashi G. Takaoka            queue.releaseAllPointersExcept(this, eventTime);
9431d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka            queue.remove(this);
944c5c57b506e97b334a394d23ed73c9597cb55707aTadashi G. Takaoka        }
945baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka        onCancelEventInternal();
9461d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka    }
9471d7d9664a9850a7c8043651e4b7a055ec034f571Tadashi G. Takaoka
948baf83886be975d804eda3e1519b7255026e5163eTadashi G. Takaoka    private void onCancelEventInternal() {
9492321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        mTimerProxy.cancelKeyTimers();
950e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        setReleasedKeyGraphics(mCurrentKey);
951cb2469ae17e0ca8a94767008fef3945cb2a3b406Tadashi G. Takaoka        mIsInSlidingKeyInput = false;
9529d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka        if (mIsShowingMoreKeysPanel) {
9539d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka            mDrawingProxy.dismissMoreKeysPanel();
9549d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka            mIsShowingMoreKeysPanel = false;
9559ec80d9d89eb599329c354451acdc482cc3de836Tadashi G. Takaoka        }
9566a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
9576a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
958694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void startRepeatKey(final Key key) {
9599580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (key != null && key.isRepeatable() && !mInGesture) {
9606662e2a40dc764d5b6a55c0e30ce650fd834afb6alanv            onRegisterKey(key);
961a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            mTimerProxy.startKeyRepeatTimer(this);
962a0537fb4c73dff8beecc328720830af9719d0277Tadashi G. Takaoka        }
963a0537fb4c73dff8beecc328720830af9719d0277Tadashi G. Takaoka    }
964a0537fb4c73dff8beecc328720830af9719d0277Tadashi G. Takaoka
965694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    public void onRegisterKey(final Key key) {
966c6df09182cac288c9a4de2cc05628dac6b6db41eTadashi G. Takaoka        if (key != null) {
967e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            detectAndSendKey(key, key.mX, key.mY);
968d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            mTimerProxy.startTypingStateTimer(key);
969c6df09182cac288c9a4de2cc05628dac6b6db41eTadashi G. Takaoka        }
9706a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
9716a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
972694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private boolean isMajorEnoughMoveToBeOnNewKey(final int x, final int y, final Key newKey) {
973694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        if (mKeyDetector == null) {
974a19b84dcf65bd70caa0fc72089cfe043b023a898Tadashi G. Takaoka            throw new NullPointerException("keyboard and/or key detector not set");
975694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        }
976694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        final Key curKey = mCurrentKey;
9776a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        if (newKey == curKey) {
978e6cb8fc234940700ae97af787e62962a98d332e5Tadashi G. Takaoka            return false;
979e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        } else if (curKey != null) {
980e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka            return curKey.squaredDistanceToEdge(x, y)
981a19b84dcf65bd70caa0fc72089cfe043b023a898Tadashi G. Takaoka                    >= mKeyDetector.getKeyHysteresisDistanceSquared();
9826a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        } else {
983e6cb8fc234940700ae97af787e62962a98d332e5Tadashi G. Takaoka            return true;
9846a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka        }
9856a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
9866a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
987694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void startLongPressTimer(final Key key) {
9889580c467f96c542c66af86a2c376612ba4d91434Tadashi G. Takaoka        if (key != null && key.isLongPressEnabled() && !mInGesture) {
989a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            mTimerProxy.startLongPressTimer(this);
9904189eb23082fcd4bf8cfb2085d18e226e0e7ce13Tadashi G. Takaoka        }
99166e306d01c6820d4f4d8b2209438ec086b48ac51Tadashi G. Takaoka    }
99266e306d01c6820d4f4d8b2209438ec086b48ac51Tadashi G. Takaoka
993694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void detectAndSendKey(final Key key, final int x, final int y) {
99483e63ace2a1bd5b3c27d26d914456c2b0def17c5Tadashi G. Takaoka        if (key == null) {
9958aa3f5a3ad6095a3355841ce30bce4877319d0a0Tadashi G. Takaoka            callListenerOnCancelInput();
996dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka            return;
997dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka        }
99866e306d01c6820d4f4d8b2209438ec086b48ac51Tadashi G. Takaoka
999694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka        final int code = key.mCode;
1000ca7ec2097ca6af1505c1e6aa8b81b6068ba46daesatok        callListenerOnCodeInput(key, code, x, y);
10012013bab89ca2f82589f99d98d9cf3b41ea5aac65Tadashi G. Takaoka        callListenerOnRelease(key, code, false);
10026a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka    }
10036a1514a0deac7f3d8ec33430403b2caea05bc8b9Tadashi G. Takaoka
1004694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka    private void printTouchEvent(final String title, final int x, final int y,
1005694ccb5ba1bb61caea379487c6bdfae7c64c2643Tadashi G. Takaoka            final long eventTime) {
1006723aaa2eebcfea0d285f11fc265941057332664dTadashi G. Takaoka        final Key key = mKeyDetector.detectHitKey(x, y);
1007e742436a29f46764441e83aaebf3ec25283bff1bTadashi G. Takaoka        final String code = KeyDetector.printableCode(key);
1008e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka        Log.d(TAG, String.format("%s%s[%d] %4d %4d %5d %s", title,
1009d7edd1cbb5dfacacdd39766e379fb60d2b7c6b73Tadashi G. Takaoka                (mKeyAlreadyProcessed ? "-" : " "), mPointerId, x, y, eventTime, code));
1010dbc44989a5be68679c889ae45cde17002b748fdaTadashi G. Takaoka    }
10116e5a3986854549a45c95770b5a88ae5577e93299Tadashi G. Takaoka}
1012