Skip to content

Commit a5c2796

Browse files
committed
fix: killRelaunchApp when uiAutomation connected
1 parent 9bc386e commit a5c2796

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

app/src/main/kotlin/li/songe/gkd/shizuku/AutomationService.kt

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,21 @@ class AutomationService private constructor() : A11yCommonImpl {
3939
}
4040

4141
override suspend fun screenshot(): Bitmap? = withContext(Dispatchers.IO) {
42-
val tempDir = createGkdTempDir()
43-
val fp = tempDir.resolve("screenshot.png")
44-
val ok = shizukuContextFlow.value.serviceWrapper?.screencapFile(fp.absolutePath)
45-
if (ok == true && fp.exists()) {
46-
BitmapFactory.decodeFile(fp.absolutePath).apply {
47-
tempDir.deleteRecursively()
42+
try {
43+
uiAutomation.takeScreenshot()
44+
} catch (e: Throwable) {
45+
LogUtils.d("takeScreenshot failed, rollback to screencapFile", e)
46+
val tempDir = createGkdTempDir()
47+
val fp = tempDir.resolve("screenshot.png")
48+
val ok = shizukuContextFlow.value.serviceWrapper?.screencapFile(fp.absolutePath)
49+
if (ok == true && fp.exists()) {
50+
BitmapFactory.decodeFile(fp.absolutePath).apply {
51+
tempDir.deleteRecursively()
52+
}
53+
} else {
54+
null
4855
}
49-
} else {
50-
null
5156
}
52-
// try {
53-
// uiAutomation.takeScreenshot()
54-
// } catch (e: Throwable) {
55-
// LogUtils.d("takeScreenshot failed, rollback to screencapFile", e)
56-
// val tempDir = createGkdTempDir()
57-
// val fp = tempDir.resolve("screenshot.png")
58-
// val ok = shizukuContextFlow.value.serviceWrapper?.screencapFile(fp.absolutePath)
59-
// if (ok == true && fp.exists()) {
60-
// BitmapFactory.decodeFile(fp.absolutePath).apply {
61-
// tempDir.deleteRecursively()
62-
// }
63-
// } else {
64-
// null
65-
// }
66-
// }
6757
}
6858

6959
override val windowNodeInfo: AccessibilityNodeInfo? get() = uiAutomation.rootInActiveWindow

app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package li.songe.gkd.shizuku
22

33

44
import android.content.ComponentName
5+
import android.content.Intent
56
import android.content.pm.PackageManager
67
import android.os.Build
78
import androidx.annotation.RequiresApi
89
import androidx.annotation.WorkerThread
910
import kotlinx.coroutines.Dispatchers
11+
import kotlinx.coroutines.delay
1012
import kotlinx.coroutines.flow.MutableStateFlow
1113
import kotlinx.coroutines.flow.SharingStarted
1214
import kotlinx.coroutines.flow.combine
1315
import kotlinx.coroutines.flow.map
1416
import kotlinx.coroutines.flow.stateIn
17+
import li.songe.gkd.META
1518
import li.songe.gkd.app
1619
import li.songe.gkd.appScope
20+
import li.songe.gkd.isActivityVisible
1721
import li.songe.gkd.permission.shizukuGrantedState
1822
import li.songe.gkd.permission.updatePermissionState
1923
import li.songe.gkd.service.ExposeService
@@ -31,6 +35,7 @@ import rikka.shizuku.Shizuku
3135
import rikka.shizuku.ShizukuBinderWrapper
3236
import rikka.shizuku.SystemServiceHelper
3337
import java.lang.reflect.Method
38+
import kotlin.system.exitProcess
3439

3540
inline fun <T> safeInvokeShizuku(
3641
block: () -> T
@@ -224,11 +229,36 @@ private fun updateShizukuBinder() = updateBinderMutex.launchTry(appScope, Dispat
224229
toast("Shizuku 服务连接成功", delayMillis = delayMillis)
225230
}
226231
} else if (shizukuContextFlow.value.ok) {
227-
uiAutomationFlow.value?.shutdown()
228-
shizukuContextFlow.value.destroy()
229-
shizukuContextFlow.value = defaultShizukuContext
230-
toast("Shizuku 服务已断开")
232+
val willRelaunch = uiAutomationFlow.value != null && !shizukuGrantedState.updateAndGet()
233+
if (willRelaunch) {
234+
// 需要重启应用让系统释放 UiAutomation
235+
killRelaunchApp()
236+
} else {
237+
uiAutomationFlow.value?.shutdown(true)
238+
shizukuContextFlow.value.destroy()
239+
shizukuContextFlow.value = defaultShizukuContext
240+
toast("Shizuku 服务已断开")
241+
}
242+
}
243+
}
244+
245+
private suspend fun killRelaunchApp() {
246+
if (isActivityVisible) {
247+
toast("Shizuku 断开,重启应用以释放自动化服务", forced = true)
248+
delay(1500)
249+
val intent = app.packageManager.getLaunchIntentForPackage(META.appId)!!
250+
intent.addFlags(
251+
Intent.FLAG_ACTIVITY_NEW_TASK
252+
or Intent.FLAG_ACTIVITY_CLEAR_TOP
253+
or Intent.FLAG_ACTIVITY_CLEAR_TASK
254+
)
255+
app.startActivity(intent)
256+
} else {
257+
toast("Shizuku 断开,结束应用以释放自动化服务", forced = true)
258+
delay(1500)
231259
}
260+
android.os.Process.killProcess(android.os.Process.myPid())
261+
exitProcess(0)
232262
}
233263

234264
fun initShizuku() {

0 commit comments

Comments
 (0)