@@ -18,6 +18,7 @@ import com.ramcosta.composedestinations.spec.Direction
18
18
import io.ktor.client.request.get
19
19
import io.ktor.client.statement.bodyAsText
20
20
import kotlinx.coroutines.Dispatchers
21
+ import kotlinx.coroutines.delay
21
22
import kotlinx.coroutines.flow.MutableStateFlow
22
23
import kotlinx.coroutines.flow.update
23
24
import kotlinx.coroutines.launch
@@ -29,8 +30,10 @@ import li.songe.gkd.data.importData
29
30
import li.songe.gkd.db.DbSet
30
31
import li.songe.gkd.permission.AuthReason
31
32
import li.songe.gkd.permission.shizukuOkState
32
- import li.songe.gkd.shizuku.execCommandForResult
33
+ import li.songe.gkd.shizuku.shizukuContextFlow
34
+ import li.songe.gkd.shizuku.updateBinderMutex
33
35
import li.songe.gkd.store.createTextFlow
36
+ import li.songe.gkd.store.storeFlow
34
37
import li.songe.gkd.ui.component.AlertDialogOptions
35
38
import li.songe.gkd.ui.component.InputSubsLinkOption
36
39
import li.songe.gkd.ui.component.RuleGroupState
@@ -95,7 +98,7 @@ class MainViewModel : ViewModel() {
95
98
oldItem : SubsItem ? = null,
96
99
) = viewModelScope.launchTry(Dispatchers .IO ) {
97
100
if (updateSubsMutex.mutex.isLocked) return @launchTry
98
- updateSubsMutex.withLock {
101
+ updateSubsMutex.withStateLock {
99
102
val subItems = subsItemsFlow.value
100
103
val text = try {
101
104
client.get(url).bodyAsText()
@@ -256,24 +259,33 @@ class MainViewModel : ViewModel() {
256
259
)
257
260
}
258
261
262
+
263
+ fun requestShizuku () = try {
264
+ Shizuku .requestPermission(Activity .RESULT_OK )
265
+ } catch (e: Throwable ) {
266
+ shizukuErrorFlow.value = e
267
+ }
268
+
259
269
suspend fun grantPermissionByShizuku (command : String ) {
260
- if (shizukuOkState.stateFlow.value) {
261
- try {
262
- execCommandForResult(command)
263
- return
264
- } catch (e: Exception ) {
265
- toast(" 运行失败:${e.message} " )
266
- LogUtils .d(e)
267
- }
268
- } else {
269
- try {
270
- Shizuku .requestPermission(Activity .RESULT_OK )
271
- } catch (e: Throwable ) {
272
- LogUtils .d(" Shizuku授权错误" , e.message)
273
- shizukuErrorFlow.value = e
270
+ if (updateBinderMutex.mutex.isLocked) {
271
+ toast(" 正在连接 Shizuku 服务,请稍后" )
272
+ stopCoroutine()
273
+ }
274
+ if (! shizukuOkState.stateFlow.value) {
275
+ requestShizuku()
276
+ stopCoroutine()
277
+ }
278
+ if (! storeFlow.value.enableShizuku) {
279
+ storeFlow.update { it.copy(enableShizuku = true ) }
280
+ delay(500 )
281
+ while (updateBinderMutex.mutex.isLocked) {
282
+ delay(100 )
274
283
}
275
284
}
276
- stopCoroutine()
285
+ val service = shizukuContextFlow.value.serviceWrapper ? : stopCoroutine()
286
+ if (! service.execCommandForResult(command).ok) {
287
+ stopCoroutine()
288
+ }
277
289
}
278
290
279
291
val a11yServiceEnabledFlow = useA11yServiceEnabledFlow()
0 commit comments