Perform swipe on screen using AccessibilityService

后端 未结 2 1310
南旧
南旧 2020-12-31 14:34

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 dev

相关标签:
2条回答
  • 2020-12-31 15:01

    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);
    }
    
    0 讨论(0)
  • 2020-12-31 15:10

    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.

    0 讨论(0)
提交回复
热议问题