Skip to content

Commit

Permalink
Add legacy actions for Android Marshmallow (6)
Browse files Browse the repository at this point in the history
  • Loading branch information
virresh committed Mar 6, 2021
1 parent cff3d4c commit 9267ae4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
applicationId "io.github.virresh.matvt"
minSdkVersion 24
minSdkVersion 23
targetSdkVersion 29
versionCode 101
versionName '1.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
Expand All @@ -15,6 +16,7 @@
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -98,6 +100,16 @@ public class MouseEmulationEngine {
colorSet = Collections.unmodifiableSet(integerSet);
}

private static final Map<Integer, Integer> legacyActionScrollMap;
static {
Map<Integer, Integer> integerMap = new HashMap<>();
integerMap.put(KeyEvent.KEYCODE_DPAD_DOWN, AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
integerMap.put(KeyEvent.KEYCODE_DPAD_UP, AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
integerMap.put(KeyEvent.KEYCODE_DPAD_RIGHT, AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_RIGHT.getId());
integerMap.put(KeyEvent.KEYCODE_DPAD_LEFT, AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_LEFT.getId());
legacyActionScrollMap = Collections.unmodifiableMap(integerMap);
}

public MouseEmulationEngine (Context c, OverlayView ov) {
momentumStack = 0;
// overlay view for drawing mouse
Expand Down Expand Up @@ -146,7 +158,9 @@ private void attachGesture (final PointF originPoint, final int direction) {
@Override
public void run() {
mPointerControl.reappear();
mService.dispatchGesture(createSwipe(originPoint, direction, 20 + momentumStack), null, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mService.dispatchGesture(createSwipe(originPoint, direction, 20 + momentumStack), null, null);
}
momentumStack += 1;
timerHandler.postDelayed(this, 30);
}
Expand All @@ -166,6 +180,7 @@ private void detachPreviousTimer () {
}
}

@RequiresApi(api = Build.VERSION_CODES.N)
private static GestureDescription createClick (PointF clickPoint) {
final int DURATION = 1;
Path clickPath = new Path();
Expand All @@ -177,6 +192,7 @@ private static GestureDescription createClick (PointF clickPoint) {
return clickBuilder.build();
}

@RequiresApi(api = Build.VERSION_CODES.N)
private static GestureDescription createSwipe (PointF originPoint, int direction, int momentum) {
final int DURATION = 10;
Path clickPath = new Path();
Expand All @@ -190,6 +206,27 @@ private static GestureDescription createSwipe (PointF originPoint, int direction
return clickBuilder.build();
}

private boolean legacyPerformAction (int actionSuggestion, boolean isScroll) {
int action = AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK.getId();
if (actionSuggestion == -2) {
action = AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK.getId();
}
if (isScroll && legacyActionScrollMap.containsKey(actionSuggestion)) {
action = legacyActionScrollMap.get(actionSuggestion);
} else if (isScroll) {
// scroll mode but no action
return false;
}
Point pInt = new Point((int) mPointerControl.getPointerLocation().x, (int) mPointerControl.getPointerLocation().y);
AccessibilityNodeInfo hitNode = findNode(null, action, pInt);
boolean consumed = false;
if (hitNode != null) {
hitNode.performAction(AccessibilityNodeInfo.FOCUS_INPUT);
consumed = hitNode.performAction(action);
}
return consumed;
}

public boolean perform (KeyEvent keyEvent) {

// toggle mouse mode if going via bossKey
Expand Down Expand Up @@ -235,7 +272,11 @@ public boolean perform (KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN){
if (scrollCodeMap.containsKey(keyEvent.getKeyCode())) {
if (isInScrollMode || colorSet.contains(keyEvent.getKeyCode())) {
attachGesture(mPointerControl.getPointerLocation(), scrollCodeMap.get(keyEvent.getKeyCode()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
attachGesture(mPointerControl.getPointerLocation(), scrollCodeMap.get(keyEvent.getKeyCode()));
} else {
legacyPerformAction(keyEvent.getKeyCode(), true);
}
}
else if (movementCodeMap.containsKey(keyEvent.getKeyCode())){
attachTimer(movementCodeMap.get(keyEvent.getKeyCode()));
Expand All @@ -259,18 +300,15 @@ else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
detachPreviousTimer();
if (keyEvent.getEventTime() - keyEvent.getDownTime() > 500) {
// unreliable long click event if button was pressed for more than 500 ms
int action = AccessibilityNodeInfo.ACTION_LONG_CLICK;
Point pInt = new Point((int) mPointerControl.getPointerLocation().x, (int) mPointerControl.getPointerLocation().y);
AccessibilityNodeInfo hitNode = findNode(null, action, pInt);

if (hitNode != null) {
hitNode.performAction(AccessibilityNodeInfo.FOCUS_INPUT);
consumed = hitNode.performAction(action);
}
legacyPerformAction(-2, false);
}
else {
mService.dispatchGesture(createClick(mPointerControl.getPointerLocation()), null, null);
return false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mService.dispatchGesture(createClick(mPointerControl.getPointerLocation()), null, null);
consumed = true;
} else {
consumed = legacyPerformAction(-1, false);
}
}

}
Expand Down Expand Up @@ -337,10 +375,10 @@ private AccessibilityNodeInfo findNodeHelper (AccessibilityNodeInfo node, int ac
}
AccessibilityNodeInfo result = null;
result = node;
// if ((node.getActions() & action) != 0) {
// // possible to use this one, but keep searching children as well
// result = node;
// }
if ((node.getActions() & action) != 0) {
// possible to use this one, but keep searching children as well
return node;
}
int childCount = node.getChildCount();
for (int i=0; i<childCount; i++) {
AccessibilityNodeInfo child = findNodeHelper(node.getChild(i), action, pInt);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.virresh.matvt.gui;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -11,6 +12,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -39,7 +41,16 @@ public IconStyleSpinnerAdapter(@NonNull Context context, int resource, int textV
}

public static List<String> getResourceList () {
return textToResourceIdMap.keySet().stream().collect(Collectors.toList());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return textToResourceIdMap.keySet().stream().collect(Collectors.toList());
}
else {
List<String> resList = new ArrayList<String>();
for (String x: textToResourceIdMap.keySet()) {
resList.add(x);
}
return resList;
}
}

@Override
Expand Down Expand Up @@ -74,7 +85,13 @@ public View getCustomView(int position, View convertView, ViewGroup parent, bool
String selection = objects.get(position);

label.setText(selection);
icon.setImageDrawable(context.getDrawable(textToResourceIdMap.getOrDefault(selection, R.drawable.pointer)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
icon.setImageDrawable(context.getDrawable(textToResourceIdMap.getOrDefault(selection, R.drawable.pointer)));
}
else {
// no default, can cause crashes
icon.setImageDrawable(context.getDrawable(textToResourceIdMap.get(selection)));
}

if (setBackcolor) {
label.setBackground(context.getDrawable(R.drawable.focus_selector));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.View;

import io.github.virresh.matvt.R;
Expand Down Expand Up @@ -42,7 +43,12 @@ public MouseCursorView(Context context) {

public void updateFromPreferences() {
Context ctx = getContext();
pointerDrawableReference = IconStyleSpinnerAdapter.textToResourceIdMap.getOrDefault(Helper.getMouseIconPref(ctx), R.drawable.pointer);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
pointerDrawableReference = IconStyleSpinnerAdapter.textToResourceIdMap.getOrDefault(Helper.getMouseIconPref(ctx), R.drawable.pointer);
}
else {
pointerDrawableReference = IconStyleSpinnerAdapter.textToResourceIdMap.get(Helper.getMouseIconPref(ctx));
}
pointerSizeReference = Helper.getMouseSizePref(ctx) + 1;
}

Expand Down

0 comments on commit 9267ae4

Please sign in to comment.