Skip to content

Commit

Permalink
added accessibility node info traversal function to describe the scre…
Browse files Browse the repository at this point in the history
…en content at a given time.
  • Loading branch information
noobexon1 committed May 31, 2024
1 parent 5aac062 commit f678521
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/src/main/java/com/noobexon/asleech/ASLeech.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.noobexon.asleech

import android.accessibilityservice.AccessibilityService
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo

class ASLeech : AccessibilityService() {

Expand All @@ -14,11 +16,36 @@ class ASLeech : AccessibilityService() {

override fun onAccessibilityEvent(event: AccessibilityEvent?) {
event?.let {
eventHandler.handleEvent(event)
// Example: Log the view hierarchy when the window state changes
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
val rootNode: AccessibilityNodeInfo? = rootInActiveWindow
rootNode?.let {
logViewHierarchy(it, 0)
}
}
}
}

override fun onInterrupt() {
// Handle service interruption
}

private fun logViewHierarchy(nodeInfo: AccessibilityNodeInfo, depth: Int) {
val prefix = " ".repeat(depth)
val description = buildString {
append(prefix)
append("Class: ${nodeInfo.className}, ")
append("Text: ${nodeInfo.text}, ")
append("ContentDescription: ${nodeInfo.contentDescription}, ")
append("ViewId: ${nodeInfo.viewIdResourceName}")
}
Log.d("ASLeech", description)

for (i in 0 until nodeInfo.childCount) {
val child = nodeInfo.getChild(i)
if (child != null) {
logViewHierarchy(child, depth + 1)
}
}
}
}

0 comments on commit f678521

Please sign in to comment.