1
1
package io .github .virresh .matvt .engine .impl ;
2
2
3
+ import static io .github .virresh .matvt .helper .Helper .helperContext ;
4
+
3
5
import android .accessibilityservice .AccessibilityService ;
4
6
import android .accessibilityservice .GestureDescription ;
5
7
import android .content .Context ;
11
13
import android .os .Handler ;
12
14
import android .util .Log ;
13
15
import android .view .KeyEvent ;
16
+ import android .view .View ;
14
17
import android .view .accessibility .AccessibilityNodeInfo ;
15
18
import android .view .accessibility .AccessibilityWindowInfo ;
19
+ import android .view .inputmethod .InputMethodManager ;
16
20
import android .widget .Toast ;
17
21
18
22
import androidx .annotation .NonNull ;
25
29
import java .util .Map ;
26
30
import java .util .Set ;
27
31
32
+ import io .github .virresh .matvt .helper .Helper ;
28
33
import io .github .virresh .matvt .view .MouseCursorView ;
29
34
import io .github .virresh .matvt .view .OverlayView ;
30
35
31
36
public class MouseEmulationEngine {
32
37
38
+ private static boolean DPAD_SELECT_PRESSED = false ;
33
39
private static String LOG_TAG = "MOUSE_EMULATION" ;
34
40
35
41
CountDownTimer waitToChange ;
@@ -43,6 +49,8 @@ public class MouseEmulationEngine {
43
49
44
50
private final PointerControl mPointerControl ;
45
51
52
+ public static int stuckAtSide = 0 ;
53
+
46
54
private int momentumStack ;
47
55
48
56
private boolean isEnabled ;
@@ -57,6 +65,8 @@ public class MouseEmulationEngine {
57
65
58
66
private Handler timerHandler ;
59
67
68
+ private Point DPAD_Center_Init_Point = new Point ();
69
+
60
70
private Runnable previousRunnable ;
61
71
62
72
// tells which keycodes correspond to which pointer movement in scroll and movement mode
@@ -165,6 +175,23 @@ public void run() {
165
175
timerHandler .postDelayed (previousRunnable , 0 );
166
176
}
167
177
178
+ private void createSwipeForSingle (final PointF originPoint , final int direction ) {
179
+ if (previousRunnable != null ) {
180
+ detachPreviousTimer ();
181
+ }
182
+ previousRunnable = new Runnable () {
183
+ @ Override
184
+ public void run () {
185
+ mPointerControl .reappear ();
186
+ mService .dispatchGesture (createSwipe (originPoint , direction , 20 + momentumStack ), null , null );
187
+ momentumStack += 1 ;
188
+ timerHandler .postDelayed (this , 30 );
189
+ }
190
+ };
191
+ timerHandler .postDelayed (previousRunnable , 0 );
192
+ }
193
+
194
+
168
195
/**
169
196
* Auto Disappear mouse after some duration and reset momentum
170
197
*/
@@ -282,16 +309,18 @@ else if (keyEvent.getKeyCode() == bossKey && isBossKeyDisabled) {
282
309
boolean consumed = false ;
283
310
if (keyEvent .getAction () == KeyEvent .ACTION_DOWN ){
284
311
if (scrollCodeMap .containsKey (keyEvent .getKeyCode ())) {
285
- if (isInScrollMode || colorSet .contains (keyEvent .getKeyCode ())) {
312
+ if (isInScrollMode || colorSet .contains (keyEvent .getKeyCode ()))
286
313
attachGesture (mPointerControl .getPointerLocation (), scrollCodeMap .get (keyEvent .getKeyCode ()));
287
- }
288
- else if (movementCodeMap .containsKey (keyEvent .getKeyCode ())){
314
+ else if (!isInScrollMode && stuckAtSide != 0 && keyEvent .getKeyCode () == stuckAtSide )
315
+ createSwipeForSingle (mPointerControl .getCenterPointOfView (), scrollCodeMap .get (keyEvent .getKeyCode ()));
316
+ else if (movementCodeMap .containsKey (keyEvent .getKeyCode ()))
289
317
attachTimer (movementCodeMap .get (keyEvent .getKeyCode ()));
290
- }
291
318
consumed = true ;
292
319
}
293
320
else if (keyEvent .getKeyCode () == KeyEvent .KEYCODE_DPAD_CENTER ) {
294
321
// just consume this event to prevent propagation
322
+ DPAD_Center_Init_Point = new Point ((int ) mPointerControl .getPointerLocation ().x , (int ) mPointerControl .getPointerLocation ().y );
323
+ DPAD_SELECT_PRESSED = true ;
295
324
consumed = true ;
296
325
}
297
326
}
@@ -304,50 +333,63 @@ else if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
304
333
consumed = true ;
305
334
}
306
335
else if (keyEvent .getKeyCode () == KeyEvent .KEYCODE_DPAD_CENTER ) {
336
+ DPAD_SELECT_PRESSED = false ;
307
337
detachPreviousTimer ();
308
338
// if (keyEvent.getEventTime() - keyEvent.getDownTime() > 500) {
309
339
// unreliable long click event if button was pressed for more than 500 ms
310
340
int action = AccessibilityNodeInfo .ACTION_CLICK ;
311
341
Point pInt = new Point ((int ) mPointerControl .getPointerLocation ().x , (int ) mPointerControl .getPointerLocation ().y );
312
- List <AccessibilityWindowInfo > windowList = mService .getWindows ();
313
- boolean wasIME = false , focused = false ;
314
- for (AccessibilityWindowInfo window : windowList ) {
315
- if (consumed || wasIME ) {
316
- break ;
317
- }
318
- List <AccessibilityNodeInfo > nodeHierarchy = findNode (window .getRoot (), action , pInt );
319
- for (int i =nodeHierarchy .size ()-1 ; i >=0 ; i --){
320
- if (consumed || focused ) {
321
- break ;
322
- };
323
- AccessibilityNodeInfo hitNode = nodeHierarchy .get (i );
324
- List <AccessibilityNodeInfo .AccessibilityAction > availableActions = hitNode .getActionList ();
325
- if (availableActions .contains (AccessibilityNodeInfo .AccessibilityAction .ACTION_ACCESSIBILITY_FOCUS )){
326
- focused = hitNode .performAction (AccessibilityNodeInfo .ACTION_ACCESSIBILITY_FOCUS );
327
- }
328
- // if (hitNode.isFocused() && availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_SELECT)){
329
- // hitNode.performAction(AccessibilityNodeInfo.ACTION_SELECT);
330
- // }
331
- // if (hitNode.isFocused() && availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK)){
332
- // consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
333
- // }
334
- if (window .getType () == AccessibilityWindowInfo .TYPE_INPUT_METHOD ) {
335
- wasIME = true ;
336
- consumed = hitNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
342
+ if (DPAD_Center_Init_Point .equals (pInt )) {
343
+ List <AccessibilityWindowInfo > windowList = mService .getWindows ();
344
+ boolean wasIME = false , focused = false ;
345
+ for (AccessibilityWindowInfo window : windowList ) {
346
+ if (consumed || wasIME ) {
337
347
break ;
338
348
}
349
+ List <AccessibilityNodeInfo > nodeHierarchy = findNode (window .getRoot (), action , pInt );
350
+ for (int i = nodeHierarchy .size () - 1 ; i >= 0 ; i --) {
351
+ if (consumed || focused ) {
352
+ break ;
353
+ }
354
+ ;
355
+ AccessibilityNodeInfo hitNode = nodeHierarchy .get (i );
356
+ List <AccessibilityNodeInfo .AccessibilityAction > availableActions = hitNode .getActionList ();
357
+ if (availableActions .contains (AccessibilityNodeInfo .AccessibilityAction .ACTION_ACCESSIBILITY_FOCUS )) {
358
+ focused = hitNode .performAction (AccessibilityNodeInfo .ACTION_ACCESSIBILITY_FOCUS );
359
+ }
360
+ if (hitNode .isFocused () && availableActions .contains (AccessibilityNodeInfo .AccessibilityAction .ACTION_SELECT )) {
361
+ hitNode .performAction (AccessibilityNodeInfo .ACTION_SELECT );
362
+ }
363
+ if (hitNode .isFocused () && availableActions .contains (AccessibilityNodeInfo .AccessibilityAction .ACTION_CLICK )) {
364
+ consumed = hitNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
365
+ }
366
+ if (window .getType () == AccessibilityWindowInfo .TYPE_INPUT_METHOD && !(hitNode .getPackageName ()).toString ().contains ("leankeyboard" )) {
367
+ if (hitNode .getPackageName ().equals ("com.amazon.tv.ime" ) && keyEvent .getKeyCode () == KeyEvent .KEYCODE_BACK && helperContext != null ) {
368
+ InputMethodManager imm = (InputMethodManager ) helperContext .getSystemService (Context .INPUT_METHOD_SERVICE );
369
+ imm .toggleSoftInput (InputMethodManager .HIDE_IMPLICIT_ONLY , 0 );
370
+ consumed = wasIME = true ;
371
+ } else {
372
+ wasIME = true ;
373
+ consumed = hitNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
374
+ }
375
+ break ;
376
+ }
339
377
340
- if ((hitNode .getPackageName ().equals ("com.google.android.tvlauncher" )
341
- && availableActions .contains (AccessibilityNodeInfo .AccessibilityAction .ACTION_CLICK ))) {
342
- if (hitNode .isFocusable ()) {
343
- focused = hitNode .performAction (AccessibilityNodeInfo .FOCUS_INPUT );
378
+ if ((hitNode .getPackageName ().equals ("com.google.android.tvlauncher" )
379
+ && availableActions .contains (AccessibilityNodeInfo .AccessibilityAction .ACTION_CLICK ))) {
380
+ if (hitNode .isFocusable ()) {
381
+ focused = hitNode .performAction (AccessibilityNodeInfo .FOCUS_INPUT );
382
+ }
383
+ consumed = hitNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
344
384
}
345
- consumed = hitNode .performAction (AccessibilityNodeInfo .ACTION_CLICK );
346
385
}
347
386
}
387
+ if (!consumed && !wasIME ) {
388
+ mService .dispatchGesture (createClick (mPointerControl .getPointerLocation (), keyEvent .getEventTime () - keyEvent .getDownTime ()), null , null );
389
+ }
348
390
}
349
- if (! consumed && ! wasIME ) {
350
- mService . dispatchGesture ( createClick ( mPointerControl . getPointerLocation (), keyEvent . getEventTime () - keyEvent . getDownTime ()), null , null );
391
+ else {
392
+ //Implement Drag Function here
351
393
}
352
394
}
353
395
}
0 commit comments