1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* Copyright (c) 2013 Menny Even-Danan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.anysoftkeyboard.keyboards.views;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.MotionEvent;
import com.anysoftkeyboard.keyboards.AnyKeyboard.AnyKey;
import com.anysoftkeyboard.keyboards.Keyboard.Key;
import com.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.KeyPressTimingHandler;
import java.util.Locale;
class PointerTracker {
static class SharedPointerTrackersData {
int lastSentKeyIndex = NOT_A_KEY;
int delayBeforeKeyRepeatStart;
int longPressKeyTimeout;
int multiTapKeyTimeout;
}
interface UIProxy {
boolean isAtTwoFingersState();
void invalidateKey(Key key);
void showPreview(int keyIndex, PointerTracker tracker);
void hidePreview(int keyIndex, PointerTracker tracker);
}
final int mPointerId;
// Miscellaneous constants
private static final int NOT_A_KEY = AnyKeyboardViewBase.NOT_A_KEY;
private final UIProxy mProxy;
private final KeyPressTimingHandler mHandler;
private final KeyDetector mKeyDetector;
private OnKeyboardActionListener mListener;
private Key[] mKeys;
private int mKeyHysteresisDistanceSquared = -1;
private final KeyState mKeyState;
private int mKeyCodesInPathLength = -1;
// true if keyboard layout has been changed.
private boolean mKeyboardLayoutHasBeenChanged;
// true if event is already translated to a key action (long press or mini-keyboard)
private boolean mKeyAlreadyProcessed;
// true if this pointer is repeatable key
private boolean mIsRepeatableKey;
// For multi-tap
private final SharedPointerTrackersData mSharedPointerTrackersData;
private int mTapCount;
private long mLastTapTime;
private boolean mInMultiTap;
// pressed key
private int mPreviousKey = NOT_A_KEY;
// This class keeps track of a key index and a position where this pointer is.
private static class KeyState {
private final KeyDetector mKeyDetector;
// The current key index where this pointer is.
private int mKeyIndex = NOT_A_KEY;
// The position where mKeyIndex was recognized for the first time.
private int mKeyX;
private int mKeyY;
// Last pointer position.
private int mLastX;
private int mLastY;
KeyState(KeyDetector keyDetector) {
mKeyDetector = keyDetector;
}
int getKeyIndex() {
return mKeyIndex;
}
int getKeyX() {
return mKeyX;
}
int getKeyY() {
return mKeyY;
}
int getLastX() {
return mLastX;
}
int getLastY() {
return mLastY;
}
int onDownKey(int x, int y) {
return onMoveToNewKey(onMoveKeyInternal(x, y), x, y);
}
private int onMoveKeyInternal(int x, int y) {
mLastX = x;
mLastY = y;
return mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
}
int onMoveKey(int x, int y) {
return onMoveKeyInternal(x, y);
}
int onMoveToNewKey(int keyIndex, int x, int y) {
mKeyIndex = keyIndex;
mKeyX = x;
mKeyY = y;
return keyIndex;
}
int onUpKey(int x, int y) {
return onMoveKeyInternal(x, y);
}
}
PointerTracker(
int id,
KeyPressTimingHandler handler,
KeyDetector keyDetector,
UIProxy proxy,
@NonNull SharedPointerTrackersData sharedPointerTrackersData) {
if (proxy == null || handler == null || keyDetector == null) {
throw new NullPointerException();
}
mSharedPointerTrackersData = sharedPointerTrackersData;
mPointerId = id;
mProxy = proxy;
mHandler = handler;
mKeyDetector = keyDetector;
mKeyState = new KeyState(keyDetector);
resetMultiTap();
}
void setOnKeyboardActionListener(OnKeyboardActionListener listener) {
mListener = listener;
}
public void setKeyboard(Key[] keys, float keyHysteresisDistance) {
if (keys == null || keyHysteresisDistance < 0) throw new IllegalArgumentException();
mKeys = keys;
mKeyHysteresisDistanceSquared = (int) (keyHysteresisDistance * keyHysteresisDistance);
// Mark that keyboard layout has been changed.
mKeyboardLayoutHasBeenChanged = true;
}
private boolean isValidKeyIndex(int keyIndex) {
return keyIndex >= 0 && keyIndex < mKeys.length;
}
public Key getKey(int keyIndex) {
return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null;
}
private boolean isModifierInternal(int keyIndex) {
Key key = getKey(keyIndex);
return key != null && key.modifier;
}
public boolean isModifier() {
return isModifierInternal(mKeyState.getKeyIndex());
}
boolean isOnModifierKey(int x, int y) {
return isModifierInternal(mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null));
}
private void updateKey(int keyIndex) {
if (mKeyAlreadyProcessed) {
return;
}
int oldKeyIndex = mPreviousKey;
mPreviousKey = keyIndex;
if (keyIndex != oldKeyIndex) {
if (isValidKeyIndex(oldKeyIndex)) {
// if new key index is not a key, old key was just released inside of the key.
mKeys[oldKeyIndex].onReleased();
mProxy.invalidateKey(mKeys[oldKeyIndex]);
}
if (isValidKeyIndex(keyIndex)) {
mKeys[keyIndex].onPressed();
mProxy.invalidateKey(mKeys[keyIndex]);
}
}
}
void setAlreadyProcessed() {
mKeyAlreadyProcessed = true;
}
public void onTouchEvent(int action, int x, int y, long eventTime) {
switch (action) {
case MotionEvent.ACTION_MOVE:
onMoveEvent(x, y, eventTime);
break;
case MotionEvent.ACTION_DOWN:
case 0x00000005: // MotionEvent.ACTION_POINTER_DOWN:
onDownEvent(x, y, eventTime);
break;
case MotionEvent.ACTION_UP:
case 0x00000006: // MotionEvent.ACTION_POINTER_UP:
onUpEvent(x, y, eventTime);
break;
case MotionEvent.ACTION_CANCEL:
onCancelEvent();
break;
default:
break;
}
}
void onDownEvent(int x, int y, long eventTime) {
int keyIndex = mKeyState.onDownKey(x, y);
mKeyboardLayoutHasBeenChanged = false;
mKeyAlreadyProcessed = false;
mIsRepeatableKey = false;
mKeyCodesInPathLength = -1;
checkMultiTap(eventTime, keyIndex);
if (mListener != null && isValidKeyIndex(keyIndex)) {
AnyKey key = (AnyKey) mKeys[keyIndex];
final int codeAtIndex = key.getCodeAtIndex(0, mKeyDetector.isKeyShifted(key));
if (!mProxy.isAtTwoFingersState()
&& mListener.onGestureTypingInputStart(x, y, key, eventTime)) {
mKeyCodesInPathLength = 1;
}
if (codeAtIndex != 0) {
mListener.onPress(codeAtIndex);
// also notifying about first down
mListener.onFirstDownKey(codeAtIndex);
}
// This onPress call may have changed keyboard layout. Those cases are detected at
// {@link #setKeyboard}. In those cases, we should update keyIndex according to the
// new keyboard layout.
if (mKeyboardLayoutHasBeenChanged) {
mKeyboardLayoutHasBeenChanged = false;
keyIndex = mKeyState.onDownKey(x, y);
}
}
if (isValidKeyIndex(keyIndex)) {
if (mKeys[keyIndex].repeatable) {
repeatKey(keyIndex);
mHandler.startKeyRepeatTimer(
mSharedPointerTrackersData.delayBeforeKeyRepeatStart, keyIndex, this);
mIsRepeatableKey = true;
}
startLongPressTimer(keyIndex);
}
showKeyPreviewAndUpdateKey(keyIndex);
}
void onMoveEvent(int x, int y, long eventTime) {
if (canDoGestureTyping()) {
mListener.onGestureTypingInput(x, y, eventTime);
}
if (mKeyAlreadyProcessed) {
return;
}
final KeyState keyState = mKeyState;
final int oldKeyIndex = keyState.getKeyIndex();
int keyIndex = keyState.onMoveKey(x, y);
final Key oldKey = getKey(oldKeyIndex);
if (isValidKeyIndex(keyIndex)) {
if (oldKey == null) {
// The pointer has been slid in to the new key, but the finger was not on any keys.
// In this case, we must call onPress() to notify that the new key is being pressed.
if (mListener != null) {
Key key = getKey(keyIndex);
mListener.onPress(key.getCodeAtIndex(0, mKeyDetector.isKeyShifted(key)));
// This onPress call may have changed keyboard layout. Those cases are detected
// at {@link #setKeyboard}. In those cases, we should update keyIndex according
// to the new keyboard layout.
if (mKeyboardLayoutHasBeenChanged) {
mKeyboardLayoutHasBeenChanged = false;
keyIndex = keyState.onMoveKey(x, y);
}
}
keyState.onMoveToNewKey(keyIndex, x, y);
startLongPressTimer(keyIndex);
} else if (!isMinorMoveBounce(x, y, keyIndex)) {
// The pointer has been slid in to the new key from the previous key, we must call
// onRelease() first to notify that the previous key has been released, then call
// onPress() to notify that the new key is being pressed.
if (mListener != null && !isInGestureTyping()) {
mListener.onRelease(
oldKey.getCodeAtIndex(0, mKeyDetector.isKeyShifted(oldKey)));
}
resetMultiTap();
if (mListener != null) {
Key key = getKey(keyIndex);
if (canDoGestureTyping()) {
mKeyCodesInPathLength++;
} else {
mListener.onPress(key.getCodeAtIndex(0, mKeyDetector.isKeyShifted(key)));
}
// This onPress call may have changed keyboard layout. Those cases are detected
// at {@link #setKeyboard}. In those cases, we should update keyIndex according
// to the new keyboard layout.
if (mKeyboardLayoutHasBeenChanged) {
mKeyboardLayoutHasBeenChanged = false;
keyIndex = keyState.onMoveKey(x, y);
}
}
keyState.onMoveToNewKey(keyIndex, x, y);
startLongPressTimer(keyIndex);
if (oldKeyIndex != keyIndex) {
mProxy.hidePreview(oldKeyIndex, this);
}
}
} else {
if (oldKey != null && !isMinorMoveBounce(x, y, keyIndex)) {
// The pointer has been slid out from the previous key, we must call onRelease() to
// notify that the previous key has been released.
if (mListener != null) {
mListener.onRelease(
oldKey.getCodeAtIndex(0, mKeyDetector.isKeyShifted(oldKey)));
}
resetMultiTap();
keyState.onMoveToNewKey(keyIndex, x, y);
mHandler.cancelLongPressTimer();
if (oldKeyIndex != keyIndex) {
mProxy.hidePreview(oldKeyIndex, this);
}
}
}
showKeyPreviewAndUpdateKey(keyState.getKeyIndex());
}
void onUpEvent(int x, int y, long eventTime) {
mHandler.cancelAllMessages();
mProxy.hidePreview(mKeyState.getKeyIndex(), this);
showKeyPreviewAndUpdateKey(NOT_A_KEY);
if (mKeyAlreadyProcessed) {
return;
}
int keyIndex = mKeyState.onUpKey(x, y);
if (isMinorMoveBounce(x, y, keyIndex)) {
// Use previous fixed key index and coordinates.
keyIndex = mKeyState.getKeyIndex();
x = mKeyState.getKeyX();
y = mKeyState.getKeyY();
}
if (!mIsRepeatableKey) {
detectAndSendKey(keyIndex, x, y, eventTime);
}
if (isValidKeyIndex(keyIndex)) {
mProxy.invalidateKey(mKeys[keyIndex]);
}
}
void onCancelEvent() {
mKeyCodesInPathLength = -1;
mHandler.cancelAllMessages();
int keyIndex = mKeyState.getKeyIndex();
mProxy.hidePreview(keyIndex, this);
showKeyPreviewAndUpdateKey(NOT_A_KEY);
if (isValidKeyIndex(keyIndex)) mProxy.invalidateKey(mKeys[keyIndex]);
setAlreadyProcessed();
}
void repeatKey(int keyIndex) {
Key key = getKey(keyIndex);
if (key != null) {
// While key is repeating, because there is no need to handle multi-tap key, we can
// pass -1 as eventTime argument.
detectAndSendKey(keyIndex, key.x, key.y, -1);
}
}
int getLastX() {
return mKeyState.getLastX();
}
int getLastY() {
return mKeyState.getLastY();
}
private boolean isMinorMoveBounce(int x, int y, int newKey) {
if (mKeys == null || mKeyHysteresisDistanceSquared < 0) {
throw new IllegalStateException("keyboard and/or hysteresis not set");
}
int curKey = mKeyState.getKeyIndex();
if (newKey == curKey) {
return true;
} else if (isValidKeyIndex(curKey)) {
return getSquareDistanceToKeyEdge(x, y, mKeys[curKey]) < mKeyHysteresisDistanceSquared;
} else {
return false;
}
}
private static int getSquareDistanceToKeyEdge(int x, int y, Key key) {
final int left = key.x;
final int right = key.x + key.width;
final int top = key.y;
final int bottom = key.y + key.height;
final int edgeX = x < left ? left : (x > right ? right : x);
final int edgeY = y < top ? top : (y > bottom ? bottom : y);
final int dx = x - edgeX;
final int dy = y - edgeY;
return dx * dx + dy * dy;
}
private void showKeyPreviewAndUpdateKey(int keyIndex) {
updateKey(keyIndex);
if (!isInGestureTyping()) mProxy.showPreview(keyIndex, this);
}
private void startLongPressTimer(int keyIndex) {
// in gesture typing we do not do long-pressing.
if (isInGestureTyping()) {
mHandler.cancelLongPressTimer();
} else {
Key key = mKeys[keyIndex];
final int delay =
shouldLongPressQuickly(key)
? 1
: mSharedPointerTrackersData.longPressKeyTimeout;
mHandler.startLongPressTimer(delay, keyIndex, this);
}
}
private boolean shouldLongPressQuickly(Key key) {
return key.getCodesCount() == 0 && key.popupResId != 0 && TextUtils.isEmpty(key.text);
}
private void detectAndSendKey(int index, int x, int y, long eventTime) {
final OnKeyboardActionListener listener = mListener;
final Key key = getKey(index);
if (key == null) {
if (listener != null) {
listener.onCancel();
}
} else {
if (key.text != null) {
if (listener != null) {
listener.onText(key, key.text);
listener.onRelease(0); // dummy key code
}
} else {
int code = key.getCodeAtIndex(0, mKeyDetector.isKeyShifted(key));
int[] nearByKeyCodes = mKeyDetector.newCodeArray();
mKeyDetector.getKeyIndexAndNearbyCodes(x, y, nearByKeyCodes);
boolean multiTapStarted = false;
// Multi-tap
if (mInMultiTap) {
if (mTapCount != -1) {
multiTapStarted = true;
mListener.onMultiTapStarted();
} else {
mTapCount = 0;
}
code = key.getMultiTapCode(mTapCount);
}
/*
* Swap the first and second values in the mCodes array if the primary code is not
* the first value but the second value in the array. This happens when key
* debouncing is in effect.
*/
if (nearByKeyCodes.length >= 2
&& nearByKeyCodes[0] != code
&& nearByKeyCodes[1] == code) {
nearByKeyCodes[1] = nearByKeyCodes[0];
nearByKeyCodes[0] = code;
}
if (listener != null) {
if (isInGestureTyping()) {
listener.onGestureTypingInputDone();
} else {
listener.onKey(code, key, mTapCount, nearByKeyCodes, x >= 0 || y >= 0);
}
mKeyCodesInPathLength = -1;
listener.onRelease(code);
if (multiTapStarted) {
mListener.onMultiTapEnded();
}
}
}
mSharedPointerTrackersData.lastSentKeyIndex = index;
mLastTapTime = eventTime;
}
}
/** Handle multi-tap keys by producing the key label for the current multi-tap state. */
CharSequence getPreviewText(Key key) {
boolean isShifted = mKeyDetector.isKeyShifted(key);
AnyKey anyKey = (AnyKey) key;
if (isShifted && !TextUtils.isEmpty(anyKey.shiftedKeyLabel)) {
return anyKey.shiftedKeyLabel;
} else if (!TextUtils.isEmpty(anyKey.label)) {
return isShifted
? anyKey.label.toString().toUpperCase(Locale.getDefault())
: anyKey.label;
} else {
int multiTapCode = key.getMultiTapCode(mTapCount);
// The following line became necessary when we stopped casting multiTapCode to char
if (multiTapCode < 32) {
multiTapCode = 32;
}
// because, if multiTapCode happened to be negative, this would fail:
return new String(new int[] {multiTapCode}, 0, 1);
}
}
private void resetMultiTap() {
mSharedPointerTrackersData.lastSentKeyIndex = NOT_A_KEY;
mTapCount = 0;
mLastTapTime = -1;
mInMultiTap = false;
}
private void checkMultiTap(long eventTime, int keyIndex) {
Key key = getKey(keyIndex);
if (key == null) {
return;
}
final boolean isMultiTap =
(eventTime < mLastTapTime + mSharedPointerTrackersData.multiTapKeyTimeout
&& keyIndex == mSharedPointerTrackersData.lastSentKeyIndex);
if (key.getCodesCount() > 1) {
mInMultiTap = true;
if (isMultiTap) {
mTapCount++;
return;
} else {
mTapCount = -1;
return;
}
}
if (!isMultiTap) {
resetMultiTap();
}
}
boolean isInGestureTyping() {
return mKeyCodesInPathLength > 1;
}
private boolean canDoGestureTyping() {
return mKeyCodesInPathLength >= 1;
}
}
|