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 ??
}
}
}
}
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.
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 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.
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.
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?) {} ...
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.
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.
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 WebView
s. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With