Skip to content

Instantly share code, notes, and snippets.

@sergeykomlach
Last active November 19, 2021 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergeykomlach/d3a7e21023d27f0651265941b56e66a0 to your computer and use it in GitHub Desktop.
Save sergeykomlach/d3a7e21023d27f0651265941b56e66a0 to your computer and use it in GitHub Desktop.
private AccessibilityNodeInfoCompat getActiveWindows(String pkg) {
List<AccessibilityWindowInfo> windows = serviceA11y.getWindows();
if (windows != null) {
for (AccessibilityWindowInfo windowInfo : windows) {
if (windowInfo != null) {
try {
if (windowInfo.getType() == AccessibilityWindowInfo.TYPE_APPLICATION) {
AccessibilityNodeInfo nodeInfo = windowInfo.getRoot();
if (nodeInfo != null) {
if (TextUtils.equals(pkg, nodeInfo.getPackageName()))
return AccessibilityNodeInfoCompat.wrap(nodeInfo);
else
nodeInfo.recycle();
}
}
} catch (Throwable e) {
if (!(e instanceof ClassCastException))//sometimes happens that Object casted to AccessibilityWindowInfo
{ logException(e); }
} finally {
windowInfo.recycle();
}
}
}
}
AccessibilityNodeInfo nodeInfo = serviceA11y.getRootInActiveWindow();//hangs very often
if (nodeInfo != null) {
AccessibilityWindowInfo windowInfo = nodeInfo.getWindow();
try {
if (windowInfo != null && windowInfo.getType() == AccessibilityWindowInfo.TYPE_APPLICATION) {
if (TextUtils.equals(pkg, nodeInfo.getPackageName()))
return AccessibilityNodeInfoCompat.wrap(nodeInfo);
} else {
nodeInfo.recycle();
}
} finally {
if (windowInfo != null) {
windowInfo.recycle();
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment