@@ -16,6 +16,7 @@ import com.stardust.autojs.core.util.Shell
16
16
import com.stardust.autojs.engine.ScriptEngineManager
17
17
import com.stardust.view.accessibility.AccessibilityDelegate
18
18
import com.stardust.view.accessibility.AccessibilityService.Companion.instance
19
+ import java.util.WeakHashMap
19
20
import java.util.regex.Pattern
20
21
21
22
/* *
@@ -38,7 +39,7 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
38
39
private val checkedPackage: Set <String > = HashSet ()
39
40
private val existsPackage: Set <String > = HashSet ()
40
41
41
- private val windowIdActivityMap: HashMap <Int , String > = HashMap ()
42
+ private val windowIdActivityMap: WeakHashMap <Int , String > = WeakHashMap ()
42
43
43
44
val latestPackage: String
44
45
get() {
@@ -83,9 +84,20 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
83
84
get() = AccessibilityDelegate .ALL_EVENT_TYPES
84
85
85
86
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
+ }
89
101
}
90
102
return false
91
103
}
@@ -95,7 +107,7 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
95
107
instance?.windows?.forEach { window ->
96
108
run {
97
109
if (window?.isFocused != false && window?.root != null ) {
98
- setLatestComponent(window.root.packageName, windowIdActivityMap[window.id])
110
+ setLatestComponent(window.root.packageName, windowIdActivityMap[window.id], true )
99
111
}
100
112
}
101
113
}
@@ -166,14 +178,16 @@ class ActivityInfoProvider(private val context: Context, private val scriptEngin
166
178
167
179
}
168
180
169
- private fun setLatestComponent (latestPackage : CharSequence? , latestClass : CharSequence? ) {
181
+ private fun setLatestComponent (latestPackage : CharSequence? , latestClass : CharSequence? , notCoverA6y : Boolean ) {
170
182
if (latestPackage == null )
171
183
return
172
184
val latestPackageStr = latestPackage.toString()
173
185
val latestClassStr = (latestClass ? : " " ).toString()
174
186
if (isPackageExists(latestPackageStr)) {
175
187
mLatestPackage = latestPackage.toString()
176
- mLatestActivity = latestClassStr
188
+ if (latestClassStr != " " || ! notCoverA6y) {
189
+ mLatestActivity = latestClassStr
190
+ }
177
191
}
178
192
Log .d(LOG_TAG , " setLatestComponent: $latestPackage /$latestClassStr $mLatestPackage /$mLatestActivity " )
179
193
}
0 commit comments