Skip to content

Commit

Permalink
perf: shizuku bindServiceMutex
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 4, 2024
1 parent 709b6b3 commit b82355c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.updateAndGet
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withTimeoutOrNull
import li.songe.gkd.META
import li.songe.gkd.appScope
Expand Down Expand Up @@ -177,6 +179,7 @@ data class UserServiceWrapper(
}
}

private val bindServiceMutex by lazy { Mutex() }
private suspend fun buildServiceWrapper(): UserServiceWrapper? {
val serviceArgs = Shizuku
.UserServiceArgs(UserService::class.componentName)
Expand Down Expand Up @@ -208,15 +211,17 @@ private suspend fun buildServiceWrapper(): UserServiceWrapper? {
LogUtils.d("onServiceDisconnected", componentName)
}
}
return withTimeoutOrNull(1000) {
suspendCoroutine { continuation ->
resumeCallback = { continuation.resume(it) }
Shizuku.bindUserService(serviceArgs, connection)
}
}.apply {
if (this == null) {
toast("Shizuku获取绑定服务超时失败")
unbindUserService(serviceArgs, connection)
bindServiceMutex.withLock {
return withTimeoutOrNull(3000) {
suspendCoroutine { continuation ->
resumeCallback = { continuation.resume(it) }
Shizuku.bindUserService(serviceArgs, connection)
}
}.apply {
if (this == null) {
toast("Shizuku获取绑定服务超时失败")
unbindUserService(serviceArgs, connection)
}
}
}
}
Expand Down

0 comments on commit b82355c

Please sign in to comment.