6

With API 24 we got a way to dispatch a gesture to the device, however there is no solid documentation or examples out there yet. I am trying to get it to work but currently the gesture is hitting the "onCancelled" callback every time.

Here is my code that calls the method:

@TargetApi(24)
private void pressLocation(Point position){
    GestureDescription.Builder builder = new GestureDescription.Builder();
    Path p = new Path();
    p.lineTo(position.x, position.y);
    p.lineTo(position.x+10, position.y+10);
    builder.addStroke(new GestureDescription.StrokeDescription(p, 10L, 200L));
    GestureDescription gesture = builder.build();
    boolean isDispatched = dispatchGesture(gesture, new GestureResultCallback() {
        @Override
        public void onCompleted(GestureDescription gestureDescription) {
            super.onCompleted(gestureDescription);
        }

        @Override
        public void onCancelled(GestureDescription gestureDescription) {
            super.onCancelled(gestureDescription);
        }
    }, null);

    Toast.makeText(FingerprintService.this, "Was it dispatched? " + isDispatched, Toast.LENGTH_SHORT).show();
}`

Has anyone used this new method yet or know of an example of how to get it functioning?

Nick Yelito
  • 77
  • 1
  • 10
  • I assume you've also added the [canPerformGestures](https://developer.android.com/reference/android/R.attr.html#canPerformGestures) option to your [SERVICE_META_DATA](https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html#SERVICE_META_DATA) as per the [documentation](https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html#dispatchGesture(android.accessibilityservice.GestureDescription, android.accessibilityservice.AccessibilityService.GestureResultCallback, android.os.Handler))? – ianhanniballake Sep 02 '16 at 03:00
  • Yup. The method is being called correctly it seems, but it is resulting in the "onCancelled" callback every time – Nick Yelito Sep 02 '16 at 03:14

1 Answers1

5

Your path is just lineTos, which doesn't specify a starting point. Try changing the first one to a moveTo.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Phil Weaver
  • 738
  • 3
  • 7
  • Hey Phil, thanks for the response. I tried changing the first lineTo to a moveTo but it is still being cancelled every time. What I am really trying to do is to create a gesture that will basically act like a tap at a given position. So I was thinking of making a Path that barely moves positions and occurs really quickly. Do you think this is possible? – Nick Yelito Sep 15 '16 at 02:04
  • 1
    I tried your code in a service and I'm seeing the gesture complete and taps happen. As long as you have canPerformGestures set to true, the gesture should only be canceled if other gestures happen while the gesture is being dispatched. – Phil Weaver Sep 16 '16 at 16:16
  • 1
    Ah ha! I've figured it out. You were right in that my code should have worked, but the problem was that I was trying to have 2 separate AccessibilityServices in one application, and Android seemed to be getting confused. Not sure if that isn't allowed as I didn't see it anywhere, but I moved the functionality to one service and now I am able to dispatch gestures! – Nick Yelito Sep 18 '16 at 19:22