Skip to content

Commit

Permalink
feat: auth writeSecureSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Sep 11, 2024
1 parent f859fa1 commit 2ce08c8
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 57 deletions.
54 changes: 54 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.animation.core.AnimationConstants
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
Expand Down Expand Up @@ -40,9 +45,12 @@ import li.songe.gkd.ui.component.BuildDialog
import li.songe.gkd.ui.theme.AppTheme
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.UpgradeDialog
import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.initFolder
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.map
import li.songe.gkd.util.openApp
import li.songe.gkd.util.openUri
import li.songe.gkd.util.storeFlow

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -81,6 +89,7 @@ class MainActivity : ComponentActivity() {
navGraph = NavGraphs.root,
navController = navController
)
ShizukuErrorDialog(mainVm.shizukuErrorFlow)
AuthDialog(mainVm.authReasonFlow)
BuildDialog(mainVm.dialogFlow)
if (META.updateEnabled) {
Expand Down Expand Up @@ -183,3 +192,48 @@ private fun Activity.fixTopPadding() {
ViewCompat.onApplyWindowInsets(view, windowInsets)
}
}

@Composable
private fun ShizukuErrorDialog(stateFlow: MutableStateFlow<Boolean>) {
val state = stateFlow.collectAsState()
if (state.value) {
val appId = "moe.shizuku.privileged.api"
val appInfoCache = appInfoCacheFlow.collectAsState()
val installed = appInfoCache.value.contains(appId)
AlertDialog(
onDismissRequest = { stateFlow.value = false },
title = { Text(text = "授权错误") },
text = {
Text(
text = if (installed) {
"Shizuku 授权失败, 请检查是否运行"
} else {
"Shizuku 未安装, 请先下载后安装"
}
)
},
confirmButton = {
if (installed) {
TextButton(onClick = {
stateFlow.value = false
app.openApp(appId)
}) {
Text(text = "打开 Shizuku")
}
} else {
TextButton(onClick = {
stateFlow.value = false
app.openUri("https://shizuku.rikka.app/")
}) {
Text(text = "去下载")
}
}
},
dismissButton = {
TextButton(onClick = { stateFlow.value = false }) {
Text(text = "我知道了")
}
}
)
}
}
2 changes: 2 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class MainViewModel : ViewModel() {

val updateStatus = UpdateStatus()

val shizukuErrorFlow = MutableStateFlow(false)

init {
viewModelScope.launchTry(Dispatchers.IO) {
val subsItems = DbSet.subsItemDao.queryAll()
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.ActivityManager
import android.app.IActivityTaskManager
import android.content.ComponentName
import android.content.ServiceConnection
import android.content.pm.IPackageManager
import android.content.pm.PackageManager
import android.os.IBinder
import android.view.Display
Expand Down Expand Up @@ -56,6 +57,15 @@ fun newActivityTaskManager(): IActivityTaskManager? {
return service.let(::ShizukuBinderWrapper).let(IActivityTaskManager.Stub::asInterface)
}

fun newPackageManager(): IPackageManager? {
val service = SystemServiceHelper.getSystemService("package")
if (service == null) {
LogUtils.d("shizuku 无法获取 package")
return null
}
return service.let(::ShizukuBinderWrapper).let(IPackageManager.Stub::asInterface)
}

/**
* -1: invalid fc
* 1: (int) -> List<Task>
Expand Down
52 changes: 1 addition & 51 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.navigate
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import li.songe.gkd.MainActivity
import li.songe.gkd.app
import li.songe.gkd.appScope
import li.songe.gkd.debug.FloatingService
import li.songe.gkd.debug.HttpService
Expand All @@ -89,12 +87,10 @@ import li.songe.gkd.ui.style.itemPadding
import li.songe.gkd.ui.style.titleItemPadding
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.buildLogFile
import li.songe.gkd.util.json
import li.songe.gkd.util.launchAsFn
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.openApp
import li.songe.gkd.util.openUri
import li.songe.gkd.util.privacyStoreFlow
import li.songe.gkd.util.saveFileToDownloads
Expand All @@ -114,7 +110,6 @@ fun AdvancedPage() {
val store by storeFlow.collectAsState()
val snapshotCount by vm.snapshotCountFlow.collectAsState()

ShizukuErrorDialog(vm.shizukuErrorFlow)
vm.uploadOptions.ShowDialog()

var showEditPortDlg by remember {
Expand Down Expand Up @@ -321,7 +316,7 @@ fun AdvancedPage() {
Shizuku.requestPermission(Activity.RESULT_OK)
} catch (e: Exception) {
LogUtils.d("Shizuku授权错误", e.message)
vm.shizukuErrorFlow.value = true
context.mainVm.shizukuErrorFlow.value = true
}
})
ShizukuFragment(false)
Expand Down Expand Up @@ -666,48 +661,3 @@ private fun ShizukuFragment(enabled: Boolean = true) {
})

}

@Composable
private fun ShizukuErrorDialog(stateFlow: MutableStateFlow<Boolean>) {
val state = stateFlow.collectAsState()
if (state.value) {
val appId = "moe.shizuku.privileged.api"
val appInfoCache = appInfoCacheFlow.collectAsState()
val installed = appInfoCache.value.contains(appId)
AlertDialog(
onDismissRequest = { stateFlow.value = false },
title = { Text(text = "授权错误") },
text = {
Text(
text = if (installed) {
"Shizuku 授权失败, 请检查是否运行"
} else {
"Shizuku 未安装, 请先下载后安装"
}
)
},
confirmButton = {
if (installed) {
TextButton(onClick = {
stateFlow.value = false
app.openApp(appId)
}) {
Text(text = "打开 Shizuku")
}
} else {
TextButton(onClick = {
stateFlow.value = false
app.openUri("https://shizuku.rikka.app/")
}) {
Text(text = "去下载")
}
}
},
dismissButton = {
TextButton(onClick = { stateFlow.value = false }) {
Text(text = "我知道了")
}
}
)
}
}
3 changes: 0 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package li.songe.gkd.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.db.DbSet
Expand All @@ -12,7 +11,5 @@ class AdvancedVm : ViewModel() {
val snapshotCountFlow =
DbSet.snapshotDao.count().stateIn(viewModelScope, SharingStarted.Eagerly, 0)

val shizukuErrorFlow = MutableStateFlow(false)

val uploadOptions = UploadOptions(viewModelScope)
}
Loading

0 comments on commit 2ce08c8

Please sign in to comment.