Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform swipe on screen using AccessibilityService

I want to perform swipe on screen using accessibility service. i tried this but this only perform a single touch. i know it is possible because when enable my service on device it says this service can perform swipe,touch,pinch etc.

Point position=new Point(100,10);
GestureDescription.Builder builder = new GestureDescription.Builder();
Path p = new Path();
p.moveTo(position.x, position.y);
builder.addStroke(new GestureDescription.StrokeDescription(p, 100L, 50L));
GestureDescription gesture = builder.build();
boolean isDispatched =    dispatchGesture(gesture,gestureResultCallback,null);
like image 858
Ali Asjad Avatar asked Jun 07 '17 18:06

Ali Asjad


2 Answers

I think you have multiple problems. How you're building your gesture is a little off, and the number of pixels you have to move for it to be a swipe is bigger than you think! I would calculate this based on screen size, rather than a specific number of pixels. I think of a typical swipe gesture as about half the screen, originating from one side to the other, right in the middle height wise.

I set up a silly little "onAccessibilityEvent" listener, that on my Nexus 6 bounces back and forth between home screen 1 and home screen two. You have to have two home screens set up obviously to see it in action.

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    switch (event.getEventType()) {
        case AccessibilityEvent.TYPE_ANNOUNCEMENT:
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {

                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

                int middleYValue = displayMetrics.heightPixels / 2;
                final int leftSideOfScreen = displayMetrics.widthPixels / 4;
                final int rightSizeOfScreen = leftSideOfScreen * 3;
                GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
                Path path = new Path();

                if (event.getText() != null && event.getText().toString().contains("1")) {
                    //Swipe left
                    path.moveTo(rightSizeOfScreen, middleYValue);
                    path.lineTo(leftSideOfScreen, middleYValue);
                } else {
                    //Swipe right
                    path.moveTo(leftSideOfScreen, middleYValue);
                    path.lineTo(rightSizeOfScreen, middleYValue);
                }

                gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path, 100, 50));
                dispatchGesture(gestureBuilder.build(), new GestureResultCallback() {
                    @Override
                    public void onCompleted(GestureDescription gestureDescription) {
                        Log.w("Gesture Completed");
                        super.onCompleted(gestureDescription);
                    }
                }, null);
            }

        default: {
            break;
        }
    }
}

Also important is the accessibility configuration info, check out my config xml file

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagReportViewIds"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.moba11y.basicaccessibilityservice.SettingsActivity"
android:canPerformGestures="true"
/>

EDIT:

To support swiping up or down you just need to change your path arguments.

final int height = displayMetrics.heightPixels;
final int top = height * .25;
final int mid = height * .5;
final int bottom = height * .75;
final int midX = displayMetrics.widthPixels / 2;

if(swipeUp) {
    path.moveTo(midX, bottom);
    path.lineTo(midX, top);
} else {
    path.moveTo(midX, top);
    path.lineTo(midX, bottom);
}
like image 168
ChrisCM Avatar answered Nov 15 '22 19:11

ChrisCM


Make sure you configured your service to perform gestures:

android:canPerformGestures="true"

Setup your path correctly:

p.moveTo(position.x, position.y);
p.lineTo(position.x + 300, position.y); 

It only works on android 24 and above.

like image 34
user8010354 Avatar answered Nov 15 '22 19:11

user8010354