Skip to content

Commit

Permalink
Revert "manager: support choose kmi manually. close tiann#1496"
Browse files Browse the repository at this point in the history
This reverts commit a177589.
  • Loading branch information
Coconutat committed Mar 30, 2024
1 parent 9be2a4b commit 1f3ea38
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 121 deletions.
11 changes: 4 additions & 7 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Flash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize
import me.weishu.kernelsu.R
import me.weishu.kernelsu.ui.component.KeyEventBlocker
import me.weishu.kernelsu.ui.util.LkmSelection
import me.weishu.kernelsu.ui.util.LocalSnackbarHost
import me.weishu.kernelsu.ui.util.installBoot
import me.weishu.kernelsu.ui.util.installModule
Expand Down Expand Up @@ -142,8 +140,7 @@ fun FlashScreen(navigator: DestinationsNavigator, flashIt: FlashIt) {

@Parcelize
sealed class FlashIt : Parcelable {
data class FlashBoot(val boot: Uri? = null, val lkm: LkmSelection, val ota: Boolean) :
FlashIt()
data class FlashBoot(val bootUri: Uri? = null, val lkmUri: Uri? = null, val ota: Boolean) : FlashIt()

data class FlashModule(val uri: Uri) : FlashIt()
}
Expand All @@ -155,8 +152,8 @@ fun flashIt(
) {
when (flashIt) {
is FlashIt.FlashBoot -> installBoot(
flashIt.boot,
flashIt.lkm,
flashIt.bootUri,
flashIt.lkmUri,
flashIt.ota,
onFinish,
onStdout,
Expand Down Expand Up @@ -191,5 +188,5 @@ private fun TopBar(onBack: () -> Unit = {}, onSave: () -> Unit = {}) {
@Preview
@Composable
fun InstallPreview() {
InstallScreen(EmptyDestinationsNavigator)
// InstallScreen(DestinationsNavigator(), uri = Uri.EMPTY)
}
82 changes: 9 additions & 73 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 @@ -33,22 +33,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.maxkeppeker.sheets.core.models.base.Header
import com.maxkeppeker.sheets.core.models.base.rememberUseCaseState
import com.maxkeppeler.sheets.list.ListDialog
import com.maxkeppeler.sheets.list.models.ListOption
import com.maxkeppeler.sheets.list.models.ListSelection
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import me.weishu.kernelsu.R
import me.weishu.kernelsu.ui.component.DialogHandle
import me.weishu.kernelsu.ui.component.rememberConfirmDialog
import me.weishu.kernelsu.ui.component.rememberCustomDialog
import me.weishu.kernelsu.ui.screen.destinations.FlashScreenDestination
import me.weishu.kernelsu.ui.util.LkmSelection
import me.weishu.kernelsu.ui.util.getCurrentKmi
import me.weishu.kernelsu.ui.util.getSupportedKmis
import me.weishu.kernelsu.ui.util.isAbDevice
import me.weishu.kernelsu.ui.util.isInitBoot
import me.weishu.kernelsu.ui.util.rootAvailable
Expand All @@ -64,44 +53,24 @@ fun InstallScreen(navigator: DestinationsNavigator) {
mutableStateOf<InstallMethod?>(null)
}

var lkmSelection by remember {
mutableStateOf<LkmSelection>(LkmSelection.KmiNone)
}
var lkmFileUri = null as Uri?

val onInstall = {
val onClickInstall = {
installMethod?.let { method ->
val flashIt = FlashIt.FlashBoot(
boot = if (method is InstallMethod.SelectFile) method.uri else null,
lkm = lkmSelection,
bootUri = if (method is InstallMethod.SelectFile) method.uri else null,
lkmUri = lkmFileUri,
ota = method is InstallMethod.DirectInstallToInactiveSlot
)
navigator.navigate(FlashScreenDestination(flashIt))
}
}

val currentKmi = remember { getCurrentKmi() }

val selectKmiDialog = rememberSelectKmiDialog { kmi ->
kmi?.let {
lkmSelection = LkmSelection.KmiString(it)
onInstall()
}
}

val onClickNext = {
if (lkmSelection == LkmSelection.KmiNone && currentKmi.isBlank()) {
// no lkm file selected and cannot get current kmi
selectKmiDialog.show()
} else {
onInstall()
}
}

val selectLkmLauncher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
it.data?.data?.let { uri ->
lkmSelection = LkmSelection.LkmUri(uri)
lkmFileUri = uri
}
}
}
Expand Down Expand Up @@ -134,7 +103,7 @@ fun InstallScreen(navigator: DestinationsNavigator) {
modifier = Modifier.fillMaxWidth(),
enabled = installMethod != null,
onClick = {
onClickNext()
onClickInstall()
}) {
Text(
stringResource(id = R.string.install_next),
Expand All @@ -153,12 +122,12 @@ sealed class InstallMethod {
override val summary: String?
) : InstallMethod()

data object DirectInstall : InstallMethod() {
object DirectInstall : InstallMethod() {
override val label: Int
get() = R.string.direct_install
}

data object DirectInstallToInactiveSlot : InstallMethod() {
object DirectInstallToInactiveSlot : InstallMethod() {
override val label: Int
get() = R.string.install_inactive_slot
}
Expand Down Expand Up @@ -259,39 +228,6 @@ private fun SelectInstallMethod(onSelected: (InstallMethod) -> Unit = {}) {
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun rememberSelectKmiDialog(onSelected: (String?) -> Unit): DialogHandle {
return rememberCustomDialog { dismiss ->
val kmis = remember {
getSupportedKmis()
}
val options = kmis.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
}
)
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun TopBar(onBack: () -> Unit = {}, onLkmUpload: () -> Unit = {}) {
Expand All @@ -313,5 +249,5 @@ private fun TopBar(onBack: () -> Unit = {}, onLkmUpload: () -> Unit = {}) {
@Composable
@Preview
fun SelectInstall_Preview() {
InstallScreen(EmptyDestinationsNavigator)
// InstallScreen(DestinationsNavigator())
}
72 changes: 32 additions & 40 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 @@ -3,14 +3,12 @@ package me.weishu.kernelsu.ui.util
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.Parcelable
import android.os.SystemClock
import android.util.Log
import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import com.topjohnwu.superuser.io.SuFile
import kotlinx.parcelize.Parcelize
import me.weishu.kernelsu.BuildConfig
import me.weishu.kernelsu.Natives
import me.weishu.kernelsu.ksuApp
Expand Down Expand Up @@ -104,7 +102,10 @@ fun uninstallModule(id: String): Boolean {
}

fun installModule(
uri: Uri, onFinish: (Boolean) -> Unit, onStdout: (String) -> Unit, onStderr: (String) -> Unit
uri: Uri,
onFinish: (Boolean) -> Unit,
onStdout: (String) -> Unit,
onStderr: (String) -> Unit
): Boolean {
val resolver = ksuApp.contentResolver
with(resolver.openInputStream(uri)) {
Expand Down Expand Up @@ -140,16 +141,9 @@ fun installModule(
}
}

@Parcelize
sealed class LkmSelection: Parcelable {
data class LkmUri(val uri: Uri) : LkmSelection()
data class KmiString(val value: String) : LkmSelection()
data object KmiNone : LkmSelection()
}

fun installBoot(
bootUri: Uri?,
lkm: LkmSelection,
lkmUri: Uri?,
ota: Boolean,
onFinish: (Boolean) -> Unit,
onStdout: (String) -> Unit,
Expand All @@ -168,6 +162,17 @@ fun installBoot(
}
}

val lkmFile = lkmUri?.let { uri ->
with(resolver.openInputStream(uri)) {
val lkmFile = File(ksuApp.cacheDir, "kernelsu-tmp-lkm.ko")
lkmFile.outputStream().use { output ->
this?.copyTo(output)
}

lkmFile
}
}

val magiskboot = File(ksuApp.applicationInfo.nativeLibraryDir, "libmagiskboot.so")
var cmd = "boot-patch --magiskboot ${magiskboot.absolutePath}"

Expand All @@ -182,27 +187,8 @@ fun installBoot(
cmd += " -u"
}

var lkmFile: File? = null
when (lkm) {
is LkmSelection.LkmUri -> {
lkmFile = with(resolver.openInputStream(lkm.uri)) {
val file = File(ksuApp.cacheDir, "kernelsu-tmp-lkm.ko")
file.outputStream().use { output ->
this?.copyTo(output)
}

file
}
cmd += " -m ${lkmFile.absolutePath}"
}

is LkmSelection.KmiString -> {
cmd += " --kmi ${lkm.value}"
}

LkmSelection.KmiNone -> {
// do nothing
}
lkmFile?.let {
cmd += " -m ${it.absolutePath}"
}

// output dir
Expand All @@ -225,7 +211,8 @@ fun installBoot(
}

val result =
shell.newJob().add("${getKsuDaemonPath()} $cmd").to(stdoutCallback, stderrCallback).exec()
shell.newJob().add("${getKsuDaemonPath()} $cmd").to(stdoutCallback, stderrCallback)
.exec()
Log.i("KernelSU", "install boot result: ${result.isSuccess}")

bootFile?.delete()
Expand Down Expand Up @@ -272,14 +259,15 @@ fun isInitBoot(): Boolean {

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

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

Expand Down Expand Up @@ -318,8 +306,9 @@ fun getSepolicy(pkg: String): String {

fun setSepolicy(pkg: String, rules: String): Boolean {
val shell = getRootShell()
val result = shell.newJob().add("${getKsuDaemonPath()} profile set-sepolicy $pkg '$rules'")
.to(ArrayList(), null).exec()
val result =
shell.newJob().add("${getKsuDaemonPath()} profile set-sepolicy $pkg '$rules'")
.to(ArrayList(), null).exec()
Log.i(TAG, "set sepolicy result: ${result.code}")
return result.isSuccess
}
Expand All @@ -333,19 +322,22 @@ fun listAppProfileTemplates(): List<String> {
fun getAppProfileTemplate(id: String): String {
val shell = getRootShell()
return shell.newJob().add("${getKsuDaemonPath()} profile get-template '${id}'")
.to(ArrayList(), null).exec().out.joinToString("\n")
.to(ArrayList(), null)
.exec().out.joinToString("\n")
}

fun setAppProfileTemplate(id: String, template: String): Boolean {
val shell = getRootShell()
return shell.newJob().add("${getKsuDaemonPath()} profile set-template '${id}' '${template}'")
.to(ArrayList(), null).exec().isSuccess
.to(ArrayList(), null)
.exec().isSuccess
}

fun deleteAppProfileTemplate(id: String): Boolean {
val shell = getRootShell()
return shell.newJob().add("${getKsuDaemonPath()} profile delete-template '${id}'")
.to(ArrayList(), null).exec().isSuccess
.to(ArrayList(), null)
.exec().isSuccess
}

fun forceStopApp(packageName: String) {
Expand Down
1 change: 0 additions & 1 deletion manager/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,4 @@
<string name="install_inactive_slot_warning">Your device will be **FORCED** to boot to the current inactive slot after a reboot!\nOnly use this option after OTA is done.\nContinue?</string>
<string name="install_next">Next</string>
<string name="select_file_tip">%1$s partition image is recommended</string>
<string name="select_kmi">Select KMI</string>
</resources>

0 comments on commit 1f3ea38

Please sign in to comment.