-
-
Save ataulm/6b1d749be33c5ff56f667c96473bd0b5 to your computer and use it in GitHub Desktop.
with onAccessibilityEvent for clickable words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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