Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sweenwolf/matvt into sweenwolf-ma…
Browse files Browse the repository at this point in the history
…ster
  • Loading branch information
virresh committed Oct 3, 2021
2 parents 51a2335 + b2b1c00 commit c29c9fa
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 46 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "io.github.virresh.matvt"
minSdkVersion 24
targetSdkVersion 29
versionCode 106
versionName '1.0.6'
versionCode 107
versionName '1.0.7'

}

Expand All @@ -30,5 +30,5 @@ android {
dependencies {

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.virresh.matvt.engine.impl;

import static io.github.virresh.matvt.helper.Helper.helperContext;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.Context;
Expand All @@ -11,8 +13,10 @@
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -25,11 +29,13 @@
import java.util.Map;
import java.util.Set;

import io.github.virresh.matvt.helper.Helper;
import io.github.virresh.matvt.view.MouseCursorView;
import io.github.virresh.matvt.view.OverlayView;

public class MouseEmulationEngine {

private static boolean DPAD_SELECT_PRESSED = false;
private static String LOG_TAG = "MOUSE_EMULATION";

CountDownTimer waitToChange;
Expand All @@ -43,6 +49,8 @@ public class MouseEmulationEngine {

private final PointerControl mPointerControl;

public static int stuckAtSide = 0;

private int momentumStack;

private boolean isEnabled;
Expand All @@ -57,6 +65,8 @@ public class MouseEmulationEngine {

private Handler timerHandler;

private Point DPAD_Center_Init_Point = new Point();

private Runnable previousRunnable;

// tells which keycodes correspond to which pointer movement in scroll and movement mode
Expand Down Expand Up @@ -165,6 +175,23 @@ public void run() {
timerHandler.postDelayed(previousRunnable, 0);
}

private void createSwipeForSingle (final PointF originPoint, final int direction) {
if (previousRunnable != null) {
detachPreviousTimer();
}
previousRunnable = new Runnable() {
@Override
public void run() {
mPointerControl.reappear();
mService.dispatchGesture(createSwipe(originPoint, direction, 20 + momentumStack), null, null);
momentumStack += 1;
timerHandler.postDelayed(this, 30);
}
};
timerHandler.postDelayed(previousRunnable, 0);
}


/**
* Auto Disappear mouse after some duration and reset momentum
*/
Expand Down Expand Up @@ -282,16 +309,18 @@ else if (keyEvent.getKeyCode() == bossKey && isBossKeyDisabled) {
boolean consumed = false;
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN){
if (scrollCodeMap.containsKey(keyEvent.getKeyCode())) {
if (isInScrollMode || colorSet.contains(keyEvent.getKeyCode())) {
if (isInScrollMode || colorSet.contains(keyEvent.getKeyCode()))
attachGesture(mPointerControl.getPointerLocation(), scrollCodeMap.get(keyEvent.getKeyCode()));
}
else if (movementCodeMap.containsKey(keyEvent.getKeyCode())){
else if (!isInScrollMode && stuckAtSide != 0 && keyEvent.getKeyCode() == stuckAtSide)
createSwipeForSingle(mPointerControl.getCenterPointOfView(), scrollCodeMap.get(keyEvent.getKeyCode()));
else if (movementCodeMap.containsKey(keyEvent.getKeyCode()))
attachTimer(movementCodeMap.get(keyEvent.getKeyCode()));
}
consumed = true;
}
else if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
// just consume this event to prevent propagation
DPAD_Center_Init_Point = new Point((int) mPointerControl.getPointerLocation().x, (int) mPointerControl.getPointerLocation().y);
DPAD_SELECT_PRESSED = true;
consumed = true;
}
}
Expand All @@ -304,50 +333,63 @@ else if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
consumed = true;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
DPAD_SELECT_PRESSED = false;
detachPreviousTimer();
// if (keyEvent.getEventTime() - keyEvent.getDownTime() > 500) {
// unreliable long click event if button was pressed for more than 500 ms
int action = AccessibilityNodeInfo.ACTION_CLICK;
Point pInt = new Point((int) mPointerControl.getPointerLocation().x, (int) mPointerControl.getPointerLocation().y);
List<AccessibilityWindowInfo> windowList= mService.getWindows();
boolean wasIME = false, focused = false;
for (AccessibilityWindowInfo window : windowList) {
if (consumed || wasIME) {
break;
}
List<AccessibilityNodeInfo> nodeHierarchy = findNode(window.getRoot(), action, pInt);
for (int i=nodeHierarchy.size()-1; i>=0; i--){
if (consumed || focused) {
break;
};
AccessibilityNodeInfo hitNode = nodeHierarchy.get(i);
List<AccessibilityNodeInfo.AccessibilityAction> availableActions = hitNode.getActionList();
if (availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_ACCESSIBILITY_FOCUS)){
focused = hitNode.performAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
}
// if (hitNode.isFocused() && availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_SELECT)){
// hitNode.performAction(AccessibilityNodeInfo.ACTION_SELECT);
// }
// if (hitNode.isFocused() && availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK)){
// consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
// }
if (window.getType() == AccessibilityWindowInfo.TYPE_INPUT_METHOD) {
wasIME = true;
consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
if (DPAD_Center_Init_Point.equals(pInt)) {
List<AccessibilityWindowInfo> windowList = mService.getWindows();
boolean wasIME = false, focused = false;
for (AccessibilityWindowInfo window : windowList) {
if (consumed || wasIME) {
break;
}
List<AccessibilityNodeInfo> nodeHierarchy = findNode(window.getRoot(), action, pInt);
for (int i = nodeHierarchy.size() - 1; i >= 0; i--) {
if (consumed || focused) {
break;
}
;
AccessibilityNodeInfo hitNode = nodeHierarchy.get(i);
List<AccessibilityNodeInfo.AccessibilityAction> availableActions = hitNode.getActionList();
if (availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_ACCESSIBILITY_FOCUS)) {
focused = hitNode.performAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
}
if (hitNode.isFocused() && availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_SELECT)) {
hitNode.performAction(AccessibilityNodeInfo.ACTION_SELECT);
}
if (hitNode.isFocused() && availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK)) {
consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
if (window.getType() == AccessibilityWindowInfo.TYPE_INPUT_METHOD && !(hitNode.getPackageName()).toString().contains("leankeyboard")) {
if (hitNode.getPackageName().equals("com.amazon.tv.ime") && keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && helperContext != null) {
InputMethodManager imm = (InputMethodManager) helperContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
consumed = wasIME = true;
} else {
wasIME = true;
consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
break;
}

if ((hitNode.getPackageName().equals("com.google.android.tvlauncher")
&& availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK))) {
if (hitNode.isFocusable()) {
focused = hitNode.performAction(AccessibilityNodeInfo.FOCUS_INPUT);
if ((hitNode.getPackageName().equals("com.google.android.tvlauncher")
&& availableActions.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK))) {
if (hitNode.isFocusable()) {
focused = hitNode.performAction(AccessibilityNodeInfo.FOCUS_INPUT);
}
consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
consumed = hitNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
if (!consumed && !wasIME) {
mService.dispatchGesture(createClick(mPointerControl.getPointerLocation(), keyEvent.getEventTime() - keyEvent.getDownTime()), null, null);
}
}
if (!consumed && !wasIME) {
mService.dispatchGesture(createClick(mPointerControl.getPointerLocation(), keyEvent.getEventTime() - keyEvent.getDownTime()), null, null);
else{
//Implement Drag Function here
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import android.graphics.PointF;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;

import io.github.virresh.matvt.helper.Helper;
import io.github.virresh.matvt.view.MouseCursorView;
import io.github.virresh.matvt.view.OverlayView;

Expand Down Expand Up @@ -66,6 +66,7 @@ public void reappear () {
public static boolean isBordered = false;

public void move (int direction, int momentum) {

int movementX = (int) (dirX[direction] * ((momentum)));
int movementY = (int) (dirY[direction] * ((momentum)));

Expand All @@ -87,21 +88,30 @@ public void move (int direction, int momentum) {
}

else {
MouseEmulationEngine.stuckAtSide = 0;

if (mPointerLocation.x + movementX >= 0 && mPointerLocation.x + movementX <= mPointerLayerView.getWidth())
mPointerLocation.x += movementX;
else {
if (mPointerLocation.x < (mPointerLayerView.getWidth() / 2f))
if (mPointerLocation.x < (mPointerLayerView.getWidth() / 2f)) {
MouseEmulationEngine.stuckAtSide = KeyEvent.KEYCODE_DPAD_LEFT;
mPointerLocation.x = 0;
else mPointerLocation.x = mPointerLayerView.getWidth();
}
else {
MouseEmulationEngine.stuckAtSide = KeyEvent.KEYCODE_DPAD_RIGHT;
mPointerLocation.x = mPointerLayerView.getWidth();
}
}

if (mPointerLocation.y + movementY >= 0 && mPointerLocation.y + movementY <= mPointerLayerView.getHeight())
mPointerLocation.y += movementY;
else {
if (mPointerLocation.y < (mPointerLayerView.getHeight() / 2f)) {
MouseEmulationEngine.stuckAtSide = KeyEvent.KEYCODE_DPAD_UP;
mPointerLocation.y = 0;
} else mPointerLocation.y = mPointerLayerView.getHeight();
} else {
MouseEmulationEngine.stuckAtSide = KeyEvent.KEYCODE_DPAD_DOWN;
mPointerLocation.y = mPointerLayerView.getHeight();
}
}
}

Expand All @@ -112,4 +122,8 @@ public void move (int direction, int momentum) {
PointF getPointerLocation() {
return mPointerLocation;
}

PointF getCenterPointOfView() {
return new PointF(mPointerLayerView.getWidth() / 2f,mPointerLayerView.getWidth() / 2f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class GuiActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Helper.helperContext = this;
setContentView(R.layout.activity_main_gui);
gui_acc_perm = findViewById(R.id.gui_acc_perm);
gui_acc_serv = findViewById(R.id.gui_acc_serv);
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/io/github/virresh/matvt/helper/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

public class Helper {

@SuppressLint("StaticFieldLeak")
public static Context helperContext;

static final String PREFS_ID = "MATVT";
static final String PREF_KEY_CB_OVERRIDE_STAT = "CB_OVERRIDE_STAT";
static final String PREF_KEY_CB_OVERRIDE_VAL = "CB_OVERRIDE_VAL";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
Expand All @@ -22,7 +23,6 @@ public class KeyDetection extends AppCompatActivity{

@SuppressLint("StaticFieldLeak")
private static Activity activity;
private static TextView textView;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void changeBossKey(int keyCode){
});
builder.setNegativeButton("NO", (dialog, whichButton) -> dialog.dismiss());
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected void onServiceConnected() {
}

private void init() {
if (Helper.helperContext != null) Helper.helperContext = this;
OverlayView mOverlayView = new OverlayView(this);
AccessibilityServiceInfo asi = this.getServiceInfo();
if (asi != null) {
Expand Down

0 comments on commit c29c9fa

Please sign in to comment.