Skip to content

Instantly share code, notes, and snippets.

@ataulm
Last active June 16, 2018 21:23
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 ataulm/6b1d749be33c5ff56f667c96473bd0b5 to your computer and use it in GitHub Desktop.
Save ataulm/6b1d749be33c5ff56f667c96473bd0b5 to your computer and use it in GitHub Desktop.
with onAccessibilityEvent for clickable words
class SkipperAccessibilityService : AccessibilityService() {
...
override fun onAccessibilityEvent(event: AccessibilityEvent) {
val clickableWords = appsToWordsMap[AppPackageName(event.packageName.toString())].orEmpty()
clickableWords.forEach {
val matchingNodes = event.source?.findAccessibilityNodeInfosByText(it.word).orEmpty()
matchingNodes.forEach { node ->
// we want at most one successful click from any of the nodes, matching any of the words
if (node.clickClosestAncestor()) {
return
}
}
}
}
/**
* @return true if something was clicked
*/
private fun AccessibilityNodeInfo?.clickClosestAncestor(): Boolean {
if (this == null) {
return false
}
if (hasClickAction()) {
performAction(AccessibilityNodeInfo.ACTION_CLICK)
return true
} else {
return parent.clickClosestAncestor()
}
}
private fun AccessibilityNodeInfo.hasClickAction(): Boolean {
actionList?.forEach({
if (it.id == AccessibilityNodeInfo.ACTION_CLICK) {
return true
}
})
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment