package com.example.modifier.service import android.accessibilityservice.AccessibilityService import android.util.Log import android.view.accessibility.AccessibilityNodeInfo import com.example.modifier.TraverseResult import com.example.modifier.baseTag import com.example.modifier.enums.RcsConnectionStatus import java.util.Optional class ScreenInspector(val context: AccessibilityService) { companion object { private const val TAG = "$baseTag/Inspector" } private fun traverse( node: AccessibilityNodeInfo?, result: TraverseResult, log: Boolean = false ) { if (node == null) { return } val packageInfo = context.packageManager.getPackageInfo( node.packageName.toString(), 0 ) val className = node.className?.toString() val name = node.viewIdResourceName val text = Optional.ofNullable(node.text).map { obj: CharSequence -> obj.toString() } .orElse(null) val id = node.viewIdResourceName if (log) { Log.d(TAG, "Node: class=$className, text=$text, name=$name, id=$id") } if ("Compose:Draft:Send" == name) { result.sendBtn = node } if ("com.google.android.apps.messaging:id/send_message_button_icon" == id) { result.sendBtn = node } if (text != null && (text.contains("RCS 聊天") || text.contains("RCS chat") || text.contains( "Chat with" )) ) { result.isRcsCapable = true } if ("com.google.android.apps.messaging:id/tombstone_message" == id) { result.isRcsCapable = text.contains("聊天") || text.contains("Chatting with") } if (text != null && (text.contains("Turn on RCS chats") || text.contains("开启 RCS 聊天功能") || text.contains("Enable chat features") || text.contains("启用聊天功能")) ) { fun findSwitch(node: AccessibilityNodeInfo): Boolean { if ("com.google.android.apps.messaging:id/switchWidget" == node.viewIdResourceName) { result.rcsSwitch = node return true } for (i in 0 until node.childCount) { val child = node.getChild(i) if (findSwitch(child)) { return true } } return false } findSwitch(node.parent.parent) } if ("com.google.android.apps.messaging:id/rcs_sim_status_status_text" == id) { if (text.lowercase().contains("connected") || text.lowercase().contains("已连接")) { result.rcsConnectionStatus = RcsConnectionStatus.CONNECTED } } if ("android:id/title" == id) { when (text) { "状态:已连接" -> result.rcsConnectionStatus = RcsConnectionStatus.CONNECTED "Status: Connected" -> result.rcsConnectionStatus = RcsConnectionStatus.CONNECTED } } if (text != null && (text.contains("end-to-end encrypted") || text.contains("已经过端到端加密"))) { result.encrypted = true } if ("com.google.android.apps.messaging:id/conversation_list_google_tos_popup_positive_button" == id) { result.tosAgreeBtn = node } if ("Turn Off" == text || "关闭" == text) { result.closeBtn = node } if (node.childCount != 0) { for (i in 0 until node.childCount) { traverse(node.getChild(i), result, log) } } } fun traverseNode(result: TraverseResult, log: Boolean = false) { traverse(context.rootInActiveWindow, result, log) } }