working poc
This commit is contained in:
54
app/src/main/java/sh/sar/isodroid/root/RootManager.kt
Normal file
54
app/src/main/java/sh/sar/isodroid/root/RootManager.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package sh.sar.isodroid.root
|
||||
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
object RootManager {
|
||||
|
||||
init {
|
||||
Shell.enableVerboseLogging = true
|
||||
Shell.setDefaultBuilder(
|
||||
Shell.Builder.create()
|
||||
.setFlags(Shell.FLAG_MOUNT_MASTER or Shell.FLAG_REDIRECT_STDERR)
|
||||
.setTimeout(10)
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun hasRoot(): Boolean = withContext(Dispatchers.IO) {
|
||||
Shell.isAppGrantedRoot() == true
|
||||
}
|
||||
|
||||
suspend fun requestRoot(): Boolean = withContext(Dispatchers.IO) {
|
||||
Shell.getShell().isRoot
|
||||
}
|
||||
|
||||
suspend fun executeCommand(command: String): CommandResult = withContext(Dispatchers.IO) {
|
||||
val result = Shell.cmd(command).exec()
|
||||
CommandResult(
|
||||
success = result.isSuccess,
|
||||
output = result.out.joinToString("\n"),
|
||||
error = result.err.joinToString("\n"),
|
||||
exitCode = result.code
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun executeCommands(vararg commands: String): CommandResult = withContext(Dispatchers.IO) {
|
||||
val result = Shell.cmd(*commands).exec()
|
||||
CommandResult(
|
||||
success = result.isSuccess,
|
||||
output = result.out.joinToString("\n"),
|
||||
error = result.err.joinToString("\n"),
|
||||
exitCode = result.code
|
||||
)
|
||||
}
|
||||
|
||||
fun getShell(): Shell = Shell.getShell()
|
||||
}
|
||||
|
||||
data class CommandResult(
|
||||
val success: Boolean,
|
||||
val output: String,
|
||||
val error: String,
|
||||
val exitCode: Int
|
||||
)
|
||||
Reference in New Issue
Block a user