Skip to content

Commit

Permalink
feat(shizuku): 手动控制在获取授权后是否使用
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 6, 2023
1 parent 97eef8e commit b3f7126
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class GkdAbService : CompositionAbService({
ManageService.stop(context)
}


val safeGetTasksFc = useSafeGetTasksFc(scope)
fun getActivityIdByShizuku(): String? {
if (!storeFlow.value.enableShizuku) return null
return safeGetTasksFc()?.lastOrNull()?.topActivity?.className
}

Expand Down
20 changes: 8 additions & 12 deletions app/src/main/java/li/songe/gkd/shizuku/ShizukuApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package li.songe.gkd.shizuku

import android.app.ActivityManager
import android.app.IActivityTaskManager
import android.os.Build
import android.view.Display
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RomUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -16,10 +14,12 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.composition.CanOnDestroy
import li.songe.gkd.data.DeviceInfo
import li.songe.gkd.util.launchWhile
import li.songe.gkd.util.storeFlow
import rikka.shizuku.Shizuku
import rikka.shizuku.ShizukuBinderWrapper
import rikka.shizuku.SystemServiceHelper
Expand All @@ -30,9 +30,6 @@ import kotlin.reflect.typeOf
* https://github.com/gkd-kit/gkd/issues/44
*/

val skipShizuku by lazy {
Build.VERSION.SDK_INT == Build.VERSION_CODES.P && RomUtils.isVivo()
}

fun newActivityTaskManager(): IActivityTaskManager? {
return SystemServiceHelper.getSystemService("activity_task").let(::ShizukuBinderWrapper)
Expand Down Expand Up @@ -89,19 +86,18 @@ fun CanOnDestroy.useShizukuAliveState(): StateFlow<Boolean> {
}

fun CanOnDestroy.useSafeGetTasksFc(scope: CoroutineScope): () -> List<ActivityManager.RunningTaskInfo>? {
if (skipShizuku) {
return { null }
}
val shizukuAliveFlow = useShizukuAliveState()
val shizukuGrantFlow = MutableStateFlow(false)
scope.launchWhile(Dispatchers.IO) {
shizukuGrantFlow.value = if (shizukuAliveFlow.value) shizukuIsSafeOK() else false
delay(3000)
}
val activityTaskManagerFlow =
combine(shizukuAliveFlow, shizukuGrantFlow) { shizukuAlive, shizukuGrant ->
if (shizukuAlive && shizukuGrant) newActivityTaskManager() else null
}.flowOn(Dispatchers.IO).stateIn(scope, SharingStarted.Lazily, null)
val activityTaskManagerFlow = combine(
shizukuAliveFlow,
shizukuGrantFlow,
storeFlow.map { s -> s.enableShizuku }) { shizukuAlive, shizukuGrant, enableShizuku ->
if (enableShizuku && shizukuAlive && shizukuGrant) newActivityTaskManager() else null
}.flowOn(Dispatchers.IO).stateIn(scope, SharingStarted.Lazily, null)
return {
activityTaskManagerFlow.value?.safeGetTasks()
}
Expand Down
36 changes: 27 additions & 9 deletions app/src/main/java/li/songe/gkd/ui/DebugPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.material.TextButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -91,14 +90,6 @@ fun DebugPage() {
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
val shizukuIsOk by usePollState { shizukuIsSafeOK() }
LaunchedEffect(key1 = shizukuIsOk, block = {
if (shizukuIsOk) {
// check method type
appScope.launchTry(Dispatchers.IO) {
newActivityTaskManager()?.safeGetTasks()
}
}
})
if (!shizukuIsOk) {
AuthCard(title = "Shizuku授权",
desc = "高级运行模式,能更准确识别界面活动ID",
Expand All @@ -110,6 +101,33 @@ fun DebugPage() {
}
})
Divider()
} else {
TextSwitch(name = "Shizuku模式",
desc = "高级运行模式,能更准确识别界面活动ID",
checked = store.enableShizuku,
onCheckedChange = { enableShizuku ->
if (enableShizuku) {
appScope.launchTry(Dispatchers.IO) {
// 检验方法是否适配, 再允许使用 shizuku
val tasks = newActivityTaskManager()?.safeGetTasks()?.firstOrNull()
if (tasks != null) {
updateStorage(
storeFlow, store.copy(
enableShizuku = true
)
)
}
}
} else {
updateStorage(
storeFlow, store.copy(
enableShizuku = false
)
)
}

})
Divider()
}

val httpServerRunning by usePollState { HttpService.isRunning() }
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/li/songe/gkd/util/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ data class Store(
val clickToast: String = "跳过",
val autoClearMemorySubs: Boolean = true,
val hideSnapshotStatusBar: Boolean = false,
val enableShizuku: Boolean = false,
) : Parcelable

val storeFlow by lazy {
Expand Down

0 comments on commit b3f7126

Please sign in to comment.