From 3f8ec39a6b1ac8edc0e573bf4da1d393dd013820 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Wed, 14 Aug 2024 16:50:50 +0800 Subject: [PATCH] update --- .../skip/service/MyAccessibilityService.kt | 43 +++++++++++-------- .../android/skip/utils/AccessibilityUtils.kt | 15 ++++++- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt index e42a1997..c16821e1 100644 --- a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt +++ b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt @@ -13,6 +13,7 @@ import android.view.KeyEvent import android.view.accessibility.AccessibilityEvent import android.view.accessibility.AccessibilityNodeInfo import androidx.annotation.RequiresApi +import com.android.skip.SKIPApp import com.android.skip.SKIP_LAYOUT_INSPECT import com.android.skip.SKIP_PERMIT_NOTICE import com.android.skip.handler.BoundsHandler @@ -21,13 +22,19 @@ import com.android.skip.handler.TextNodeHandler import com.android.skip.manager.AnalyticsManager import com.android.skip.manager.ToastManager import com.android.skip.manager.WhitelistManager +import com.android.skip.utils.AccessibilityUtils import com.android.skip.utils.Constants import com.android.skip.utils.DataStoreUtils import com.blankj.utilcode.util.LogUtils import com.blankj.utilcode.util.ServiceUtils +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import java.io.File data class MyNode(val node: AccessibilityNodeInfo, val depth: Int) +data class MyNodeChild(val depth: Int, val childCount: Int, var className: String? = null, var text: String?=null, var viewIdResourceName: String?=null) + class MyAccessibilityService : AccessibilityService() { private val textNodeHandler = TextNodeHandler() private val idNodeHandler = IdNodeHandler() @@ -59,7 +66,7 @@ class MyAccessibilityService : AccessibilityService() { val className = event.className if (className != null) { - if (!isSystemClass(className.toString())) { + if (!AccessibilityUtils.isSystemClass(className.toString())) { layoutInspectClassName = className.toString() } if (isLayoutInspect) { @@ -136,7 +143,7 @@ class MyAccessibilityService : AccessibilityService() { private fun bfsTraverse(root: AccessibilityNodeInfo) { val queue: MutableList = mutableListOf(MyNode(root, 0)) - val temp: MutableList = mutableListOf() + val temp: MutableList = mutableListOf() while (queue.isNotEmpty()) { val (node, depth) = queue.removeAt(0) @@ -146,35 +153,33 @@ class MyAccessibilityService : AccessibilityService() { node.getChild(i)?.let { queue.add(MyNode(it, depth + 1)) } } } - LogUtils.d(temp.joinToString(separator = "\n", prefix = "\n")) + + val gson = Gson() + val jsonStr = gson.toJson(temp) + val file = File(SKIPApp.context.filesDir, "temp.json") + file.writeText(jsonStr) } - private fun processNode(node: AccessibilityNodeInfo, temp: MutableList, depth: Int) { - temp.add(" ".repeat(depth) + "childCount: ${node.childCount}") - temp.add(" ".repeat(depth) + "depth: $depth") + private fun processNode(node: AccessibilityNodeInfo, temp: MutableList, depth: Int) { + val myNodeChild = MyNodeChild(depth, node.childCount) - node.text?.let { - temp.add(" ".repeat(depth) + "text: $it") + node.className?.let { + myNodeChild.className = it.toString() } - node.className?.let { - temp.add(" ".repeat(depth) + "className: $it") + node.text?.let { + myNodeChild.text = it.toString() } node.viewIdResourceName?.let { - temp.add(" ".repeat(depth) + "viewIdResourceName: $it") + myNodeChild.viewIdResourceName = it } - } - private fun isSystemClass(className: String): Boolean { - return try { - val clazz = Class.forName(className) - clazz.getPackage()?.name?.startsWith("android") == true - } catch(e: ClassNotFoundException) { - false - } + temp.add(myNodeChild) } + + @RequiresApi(Build.VERSION_CODES.TIRAMISU) override fun onServiceConnected() { super.onServiceConnected() diff --git a/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt b/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt index afdf932c..aff325f7 100644 --- a/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt +++ b/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt @@ -16,7 +16,8 @@ object AccessibilityUtils { */ fun getAccessibilityState(): AccessibilityState { val isSettingsOn = isAccessibilitySettingsOn(SKIPApp.context) - val isRunning = isAccessibilityServiceRunning(SKIPApp.context, MyAccessibilityService::class.java) + val isRunning = + isAccessibilityServiceRunning(SKIPApp.context, MyAccessibilityService::class.java) return when { isSettingsOn && isRunning -> AccessibilityState.STARTED @@ -75,4 +76,16 @@ object AccessibilityUtils { } return false } + + /** + * 判断className是不是Android自带 + */ + fun isSystemClass(className: String): Boolean { + return try { + val clazz = Class.forName(className) + clazz.getPackage()?.name?.startsWith("android") == true + } catch (e: ClassNotFoundException) { + false + } + } } \ No newline at end of file