Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Xicheng Guo committed Aug 14, 2024
1 parent fb940bd commit 3f8ec39
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -136,7 +143,7 @@ class MyAccessibilityService : AccessibilityService() {

private fun bfsTraverse(root: AccessibilityNodeInfo) {
val queue: MutableList<MyNode> = mutableListOf(MyNode(root, 0))
val temp: MutableList<String> = mutableListOf()
val temp: MutableList<MyNodeChild> = mutableListOf()

while (queue.isNotEmpty()) {
val (node, depth) = queue.removeAt(0)
Expand All @@ -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<String>, depth: Int) {
temp.add(" ".repeat(depth) + "childCount: ${node.childCount}")
temp.add(" ".repeat(depth) + "depth: $depth")
private fun processNode(node: AccessibilityNodeInfo, temp: MutableList<MyNodeChild>, 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()
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 3f8ec39

Please sign in to comment.