Skip to content

Commit

Permalink
manager: make some shell op suspend
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Mar 23, 2024
1 parent c924c65 commit 7451d0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
82 changes: 34 additions & 48 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Install.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -79,7 +80,7 @@ fun InstallScreen(navigator: DestinationsNavigator) {
}
}

val currentKmi = remember { getCurrentKmi() }
val currentKmi by produceState(initialValue = "") { value = getCurrentKmi() }

val selectKmiDialog = rememberSelectKmiDialog { kmi ->
kmi?.let {
Expand Down Expand Up @@ -107,17 +108,14 @@ fun InstallScreen(navigator: DestinationsNavigator) {
}

val onLkmUpload = {
selectLkmLauncher.launch(
Intent(Intent.ACTION_GET_CONTENT).apply {
type = "application/octet-stream"
}
)
selectLkmLauncher.launch(Intent(Intent.ACTION_GET_CONTENT).apply {
type = "application/octet-stream"
})
}

Scaffold(topBar = {
TopBar(
onBack = { navigator.popBackStack() },
onLkmUpload = onLkmUpload
onBack = { navigator.popBackStack() }, onLkmUpload = onLkmUpload
)
}) {
Column(modifier = Modifier.padding(it)) {
Expand All @@ -130,8 +128,7 @@ fun InstallScreen(navigator: DestinationsNavigator) {
.fillMaxWidth()
.padding(16.dp)
) {
Button(
modifier = Modifier.fillMaxWidth(),
Button(modifier = Modifier.fillMaxWidth(),
enabled = installMethod != null,
onClick = {
onClickNext()
Expand Down Expand Up @@ -172,8 +169,7 @@ private fun SelectInstallMethod(onSelected: (InstallMethod) -> Unit = {}) {
val rootAvailable = rootAvailable()
val isAbDevice = isAbDevice()
val selectFileTip = stringResource(
id = R.string.select_file_tip,
if (isInitBoot()) "init_boot" else "boot"
id = R.string.select_file_tip, if (isInitBoot()) "init_boot" else "boot"
)
val radioOptions =
mutableListOf<InstallMethod>(InstallMethod.SelectFile(summary = selectFileTip))
Expand Down Expand Up @@ -209,11 +205,9 @@ private fun SelectInstallMethod(onSelected: (InstallMethod) -> Unit = {}) {

when (option) {
is InstallMethod.SelectFile -> {
selectImageLauncher.launch(
Intent(Intent.ACTION_GET_CONTENT).apply {
type = "application/octet-stream"
}
)
selectImageLauncher.launch(Intent(Intent.ACTION_GET_CONTENT).apply {
type = "application/octet-stream"
})
}

is InstallMethod.DirectInstall -> {
Expand Down Expand Up @@ -263,51 +257,43 @@ private fun SelectInstallMethod(onSelected: (InstallMethod) -> Unit = {}) {
@Composable
fun rememberSelectKmiDialog(onSelected: (String?) -> Unit): DialogHandle {
return rememberCustomDialog { dismiss ->
val kmis = remember {
getSupportedKmis()
val supportedKmi by produceState(initialValue = emptyList<String>()) {
value = getSupportedKmis()
}
val options = kmis.map { value ->
val options = supportedKmi.map { value ->
ListOption(
titleText = value
)
}

var selection: String? = null
ListDialog(
state = rememberUseCaseState(visible = true, onFinishedRequest = {
onSelected(selection)
}, onCloseRequest = {
dismiss()
}),
header = Header.Default(
title = stringResource(R.string.select_kmi),
),
selection = ListSelection.Single(
showRadioButtons = true,
options = options,
) { _, option ->
selection = option.titleText
}
)
ListDialog(state = rememberUseCaseState(visible = true, onFinishedRequest = {
onSelected(selection)
}, onCloseRequest = {
dismiss()
}), header = Header.Default(
title = stringResource(R.string.select_kmi),
), selection = ListSelection.Single(
showRadioButtons = true,
options = options,
) { _, option ->
selection = option.titleText
})
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun TopBar(onBack: () -> Unit = {}, onLkmUpload: () -> Unit = {}) {
TopAppBar(
title = { Text(stringResource(R.string.install)) },
navigationIcon = {
IconButton(
onClick = onBack
) { Icon(Icons.Filled.ArrowBack, contentDescription = null) }
},
actions = {
IconButton(onClick = onLkmUpload) {
Icon(Icons.Filled.FileUpload, contentDescription = null)
}
TopAppBar(title = { Text(stringResource(R.string.install)) }, navigationIcon = {
IconButton(
onClick = onBack
) { Icon(Icons.Filled.ArrowBack, contentDescription = null) }
}, actions = {
IconButton(onClick = onLkmUpload) {
Icon(Icons.Filled.FileUpload, contentDescription = null)
}
)
})
}

@Composable
Expand Down
10 changes: 6 additions & 4 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import com.topjohnwu.superuser.io.SuFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize
import me.weishu.kernelsu.BuildConfig
import me.weishu.kernelsu.Natives
Expand Down Expand Up @@ -270,17 +272,17 @@ fun isInitBoot(): Boolean {
.toInt() >= Build.VERSION_CODES.TIRAMISU
}

fun getCurrentKmi(): String {
suspend fun getCurrentKmi(): String = withContext(Dispatchers.IO) {
val shell = getRootShell()
val cmd = "boot-info current-kmi"
return ShellUtils.fastCmd(shell, "${getKsuDaemonPath()} $cmd")
ShellUtils.fastCmd(shell, "${getKsuDaemonPath()} $cmd")
}

fun getSupportedKmis(): List<String> {
suspend fun getSupportedKmis(): List<String> = withContext(Dispatchers.IO) {
val shell = getRootShell()
val cmd = "boot-info supported-kmi"
val out = shell.newJob().add("${getKsuDaemonPath()} $cmd").to(ArrayList(), null).exec().out
return out.filter { it.isNotBlank() }.map { it.trim() }
out.filter { it.isNotBlank() }.map { it.trim() }
}

fun overlayFsAvailable(): Boolean {
Expand Down

0 comments on commit 7451d0f

Please sign in to comment.