Skip to content

Commit 48a8afe

Browse files
committed
修复currentPackage()和currentActivity()无论如何都返回空值的问题
1 parent b1f4768 commit 48a8afe

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

autojs/src/main/java/com/stardust/autojs/core/activity/ActivityInfoProvider.kt

+21-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.stardust.autojs.core.util.Shell
1616
import com.stardust.autojs.engine.ScriptEngineManager
1717
import com.stardust.view.accessibility.AccessibilityDelegate
1818
import com.stardust.view.accessibility.AccessibilityService.Companion.instance
19+
import java.util.WeakHashMap
1920
import java.util.regex.Pattern
2021

2122
/**
@@ -38,7 +39,7 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
3839
private val checkedPackage: Set<String> = HashSet()
3940
private val existsPackage: Set<String> = HashSet()
4041

41-
private val windowIdActivityMap: HashMap<Int, String> = HashMap()
42+
private val windowIdActivityMap: WeakHashMap<Int, String> = WeakHashMap()
4243

4344
val latestPackage: String
4445
get() {
@@ -83,9 +84,20 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
8384
get() = AccessibilityDelegate.ALL_EVENT_TYPES
8485

8586
override fun onAccessibilityEvent(service: AccessibilityService, event: AccessibilityEvent): Boolean {
86-
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
87-
&& event.className != null) {
88-
windowIdActivityMap[event.windowId] = event.className as String
87+
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
88+
if (event.className != null) {
89+
windowIdActivityMap[event.windowId] = event.className as String
90+
}
91+
if (scriptEngineManager.engines.size == 0) {
92+
return false
93+
}
94+
val start: Long = System.currentTimeMillis()
95+
val window = service.getWindow(event.windowId)
96+
Log.d(LOG_TAG, "get window cost: " + (System.currentTimeMillis() - start) + "ms")
97+
if (window?.isFocused != false) {
98+
setLatestComponent(event.packageName, event.className, false)
99+
return false
100+
}
89101
}
90102
return false
91103
}
@@ -95,7 +107,7 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
95107
instance?.windows?.forEach { window ->
96108
run {
97109
if (window?.isFocused != false && window?.root != null) {
98-
setLatestComponent(window.root.packageName, windowIdActivityMap[window.id])
110+
setLatestComponent(window.root.packageName, windowIdActivityMap[window.id], true)
99111
}
100112
}
101113
}
@@ -166,14 +178,16 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
166178

167179
}
168180

169-
private fun setLatestComponent(latestPackage: CharSequence?, latestClass: CharSequence?) {
181+
private fun setLatestComponent(latestPackage: CharSequence?, latestClass: CharSequence?, notCoverA6y: Boolean) {
170182
if (latestPackage == null)
171183
return
172184
val latestPackageStr = latestPackage.toString()
173185
val latestClassStr = (latestClass ?: "").toString()
174186
if (isPackageExists(latestPackageStr)) {
175187
mLatestPackage = latestPackage.toString()
176-
mLatestActivity = latestClassStr
188+
if (latestClassStr != "" || !notCoverA6y) {
189+
mLatestActivity = latestClassStr
190+
}
177191
}
178192
Log.d(LOG_TAG, "setLatestComponent: $latestPackage/$latestClassStr $mLatestPackage/$mLatestActivity")
179193
}

0 commit comments

Comments
 (0)