What is the use of setTouchExplorationPassthroughRegion() in android accessibility API?

Viewed 120

I am developing an Android application that uses accessibility-service, and I want to disable explore-by-touch when the user opens specific applications like Piano, Games, etc.

I read API documentation of setTouchExplorationPassthroughRegion()

public void setTouchExplorationPassthroughRegion (int displayId, 
                Region region)

When AccessibilityServiceInfo#FLAG_REQUEST_TOUCH_EXPLORATION_MODE is enabled, this function requests that touch interactions starting in the specified region of the screen bypass the touch explorer and go straight to the view hierarchy. There can only be one-touch exploration passthrough region per display. Requesting a new touch explorationpassthrough region clears the existing one. To disable this passthrough and return to the original behaviour, pass in an empty region. When AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE is disabled this function has no effect.

So I tried the above function in Android-11 but explore-by-touch is not getting disabled. What can I do to disable explore-by-touch in some applications.

Note: The user might be running his own Accessibility-Service like TalkBack screen reader which also uses the Explore-By-Touch feature.

1 Answers

This function is working in Android 11 and above with proper context and region

//Disabling explore by touch
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    setTouchExplorationPassthroughRegion(
            Display.DEFAULT_DISPLAY, getRegionOfFullScreen(name_of_the_view.getContext()));
}

private Region getRegionOfFullScreen(Context context) {
    final DisplayMetrics metrics = new DisplayMetrics();
    final Display display;
    DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
    display = dm.getDisplay(Display.DEFAULT_DISPLAY);
    if (display == null) {
        return new Region();
    } else {
        display.getRealMetrics(metrics);
        return new Region(0, 0, metrics.widthPixels, metrics.heightPixels);
    }
}

Don't forget to enable Explore-By-Touch if your view is not active or removed using following code

//Enabling explore by touch
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
   setTouchExplorationPassthroughRegion(Display.DEFAULT_DISPLAY, new Region());
}
Related