manager: Fix crash when no root

This commit is contained in:
tiann
2023-01-01 23:11:13 +08:00
parent 4fc8a62374
commit 6d2762b1e1
3 changed files with 41 additions and 39 deletions

View File

@@ -1,14 +1,11 @@
package me.weishu.kernelsu package me.weishu.kernelsu
import android.app.Application import android.app.Application
import android.util.Log
import coil.Coil import coil.Coil
import coil.ImageLoader import coil.ImageLoader
import com.topjohnwu.superuser.Shell import me.weishu.kernelsu.ui.util.install
import com.topjohnwu.superuser.ShellUtils
import me.zhanghai.android.appiconloader.coil.AppIconFetcher import me.zhanghai.android.appiconloader.coil.AppIconFetcher
import me.zhanghai.android.appiconloader.coil.AppIconKeyer import me.zhanghai.android.appiconloader.coil.AppIconKeyer
import java.io.File
lateinit var ksuApp: KernelSUApplication lateinit var ksuApp: KernelSUApplication
@@ -32,17 +29,5 @@ class KernelSUApplication : Application() {
install() install()
} }
fun createRootShell(): Shell {
Shell.enableVerboseLogging = BuildConfig.DEBUG
val su = applicationInfo.nativeLibraryDir + File.separator + "libksu.so"
val builder = Shell.Builder.create()
return builder.build(su)
}
fun install() {
val shell = createRootShell()
val ksduLib = ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksud.so"
val result = ShellUtils.fastCmdResult(shell, "$ksduLib install")
Log.w("KernelSU", "install ksud result: $result")
}
} }

View File

@@ -1,12 +1,11 @@
package me.weishu.kernelsu.ui.util package me.weishu.kernelsu.ui.util
import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.util.Log import android.util.Log
import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils import com.topjohnwu.superuser.ShellUtils
import me.weishu.kernelsu.BuildConfig
import me.weishu.kernelsu.ksuApp import me.weishu.kernelsu.ksuApp
import java.io.File import java.io.File
@@ -17,10 +16,36 @@ import java.io.File
*/ */
private const val TAG = "KsuCli" private const val TAG = "KsuCli"
private fun getKsuDaemonPath(): String {
return ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksud.so"
}
fun createRootShell(): Shell {
Shell.enableVerboseLogging = BuildConfig.DEBUG
val su = ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksu.so"
val builder = Shell.Builder.create()
return try {
builder.build(su)
} catch (e: Throwable) {
builder.build("sh")
}
}
fun execKsud(args: String): Boolean { fun execKsud(args: String): Boolean {
val shell = ksuApp.createRootShell() val shell = createRootShell()
val ksduLib = ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksud.so" return ShellUtils.fastCmdResult(shell, "${getKsuDaemonPath()} $args")
return ShellUtils.fastCmdResult(shell, "$ksduLib $args") }
fun install() {
val result = execKsud("install")
Log.w("KernelSU", "install ksud result: $result")
}
fun listModules(): String {
val shell = createRootShell()
val out = shell.newJob().add("${getKsuDaemonPath()} module list").to(ArrayList(), null).exec().out
return out.joinToString("\n")
} }
fun toggleModule(id: String, enable: Boolean): Boolean { fun toggleModule(id: String, enable: Boolean): Boolean {
@@ -50,8 +75,7 @@ fun installModule(uri: Uri, onFinish: (Boolean)->Unit, onOutput: (String) -> Uni
} }
val cmd = "module install ${file.absolutePath}" val cmd = "module install ${file.absolutePath}"
val shell = ksuApp.createRootShell() val shell = createRootShell()
val ksduLib = ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksud.so"
val callbackList: CallbackList<String?> = object : CallbackList<String?>() { val callbackList: CallbackList<String?> = object : CallbackList<String?>() {
override fun onAddElement(s: String?) { override fun onAddElement(s: String?) {
@@ -59,7 +83,7 @@ fun installModule(uri: Uri, onFinish: (Boolean)->Unit, onOutput: (String) -> Uni
} }
} }
val result = shell.newJob().add("$ksduLib $cmd").to(callbackList, callbackList).exec() val result = shell.newJob().add("${getKsuDaemonPath()} $cmd").to(callbackList, callbackList).exec()
Log.i("KernelSU", "install module $uri result: $result") Log.i("KernelSU", "install module $uri result: $result")
file.delete() file.delete()
@@ -70,7 +94,7 @@ fun installModule(uri: Uri, onFinish: (Boolean)->Unit, onOutput: (String) -> Uni
} }
fun reboot(reason: String = "") { fun reboot(reason: String = "") {
val shell = ksuApp.createRootShell() val shell = createRootShell()
if (reason == "recovery") { if (reason == "recovery") {
// KEYCODE_POWER = 26, hide incorrect "Factory data reset" message // KEYCODE_POWER = 26, hide incorrect "Factory data reset" message
ShellUtils.fastCmd(shell, "/system/bin/input keyevent 26") ShellUtils.fastCmd(shell, "/system/bin/input keyevent 26")

View File

@@ -1,6 +1,5 @@
package me.weishu.kernelsu.ui.viewmodel package me.weishu.kernelsu.ui.viewmodel
import android.net.Uri
import android.os.SystemClock import android.os.SystemClock
import android.util.Log import android.util.Log
import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.derivedStateOf
@@ -8,13 +7,10 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import me.weishu.kernelsu.ksuApp import me.weishu.kernelsu.ui.util.listModules
import org.json.JSONArray import org.json.JSONArray
import java.io.File
import java.text.Collator import java.text.Collator
import java.util.* import java.util.*
@@ -50,15 +46,12 @@ class ModuleViewModel : ViewModel() {
isRefreshing = true isRefreshing = true
val start = SystemClock.elapsedRealtime() val start = SystemClock.elapsedRealtime()
val shell = ksuApp.createRootShell() kotlin.runCatching {
val ksduLib = ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksud.so"
val out = shell.newJob().add("$ksduLib module list").to(ArrayList(), null).exec().out val result = listModules()
val result = out.joinToString("\n")
Log.i(TAG, "result: $result") Log.i(TAG, "result: $result")
kotlin.runCatching {
val array = JSONArray(result) val array = JSONArray(result)
modules = (0 until array.length()) modules = (0 until array.length())
.asSequence() .asSequence()