Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access html content of AccessibilityNodeInfo of a WebView element using Accessibility Service in Android?

I'm trying to access the textual content of another app which is probably built using a non-native(js+html) based framework.

Hence, I figured I'll try to access the data from an accessibility node corresponding to a WebView element. However, I'm unable to grab textual/html data using the usual methods since methods like getText() work only if it is a native android element such as a TextView, Button etc.

public class MyAccessibilityService extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    AccessibilityNodeInfo accessibilityNodeInfo = accessibilityEvent.getSource();
    if (accessibilityNodeInfo == null) {
        return;
    }
    int childCount = accessibilityNodeInfo.getChildCount();
    for (int i = 0; i < childCount; i++) {
        AccessibilityNodeInfo accessibilityNodeInfoChild = accessibilityNodeInfo.getChild(i);
        myRecursiveFunc(accessibilityNodeInfoChild);
    }
}

@Override
public void onInterrupt() {

}


private void myRecursiveFunc(AccessibilityNodeInfo accessibilityNodeInfoChild) {
    if (accessibilityNodeInfoChild.getChildCount() > 0) {
        for (int j = 0; j < accessibilityNodeInfoChild.getChildCount(); j++) {
            AccessibilityNodeInfo child = accessibilityNodeInfoChild.getChild(j);
            if (child != null) {
                myRecursiveFunc(child);
            }
        }
    } else {
        if ("android.webkit.WebView".equals(accessibilityNodeInfoChild.getClassName())) {

            //===========This is a WebView's AccessibilityNodeInfo !!!!
            //===========How to get HTML data from nodeinfo object here ??

        }
    }
}

}

like image 475
Umang Mathur Avatar asked Nov 10 '16 07:11

Umang Mathur


People also ask

What is Accessibilitynodeinfo?

This class represents a node of the window content as well as actions that can be requested from its source. From the point of view of an AccessibilityService a window's content is presented as a tree of accessibility node infos, which may or may not map one-to-one to the view hierarchy.

What is AccessibilityDelegate?

android.view.View.AccessibilityDelegate. This class represents a delegate that can be registered in a View to enhance accessibility support via composition rather via inheritance. It is specifically targeted to widget developers that extend basic View classes i.e. classes in package android.

What are Accessibility services in Android?

What is an Accessibility Service? An Accessibility Service assists users with disabilities in using Android devices and apps. It is a long-running privileged service that helps users process information on the screen and lets them to interact meaningfully with a device.

What is accessibility node in Android view?

Android. Views. Accessibility Android. Views. Accessibility This class represents a node of the window content as well as actions that can be requested from its source. Java documentation for android.view.accessibility.AccessibilityNodeInfo.

How do I create an accessibility service in Android?

An accessibility service can be bundled with a normal application or created as a standalone Android project. The steps to creating the service are the same in either situation. Within your project, create a class that extends AccessibilityService. ... override fun onAccessibilityEvent(event: AccessibilityEvent?) {} ...

How do I request all views in accessibilityserviceinfo?

If your service requires all views, it can request them by setting the flags member of the service's AccessibilityServiceInfo instance to FLAG_INCLUDE_NOT_IMPORTANT_VIEWS. Android 9 (API level 28) and higher allow apps to keep track of window updates when an app redraws multiple windows simultaneously.

What is an example of accessibility in Android?

For example, users who are driving, taking care of a young child or attending a very loud party might need additional or alternative interface feedback. Android provides standard accessibility services, including TalkBack, and developers can create and distribute their own services.


1 Answers

Does your service have FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY?

final AccessibilityServiceInfo info = getServiceInfo();
info.flags |= AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY;
setServiceInfo(info);

Android's default AccessibilityService (TalkBack) requires this to be able to screen read the text in WebViews. From examining the source, it appears that there's some script injection and some slightly hackiness in order to be able to get the textual content of the HTML elements

You should check out their source:

TalkBackService

WebInterfaceUtils

ProcessorWebContent

like image 125
Reuben Tanner Avatar answered Oct 16 '22 08:10

Reuben Tanner