diff --git a/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/DefaultTerminal.kt b/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/DefaultTerminal.kt index e8a37a09..b54b319f 100644 --- a/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/DefaultTerminal.kt +++ b/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/DefaultTerminal.kt @@ -13,7 +13,7 @@ import javax.inject.Singleton @Singleton open class DefaultTerminal @Inject constructor( @IODispatcher protected val ioDispatcher: CoroutineDispatcher, -): Terminal { +) : Terminal { companion object { const val INDEX = 0 @@ -21,16 +21,10 @@ open class DefaultTerminal @Inject constructor( override val title = R.string.terminal_default - protected open val commandPrefix: Array? = null - override suspend fun isSupported() = true - private fun createProcess(commands: Array) = (commandPrefix ?: emptyArray()) - .plus(commands) - .let(Runtime.getRuntime()::exec) - override suspend fun executeNow(vararg command: String) = withContext(ioDispatcher) { - val process = createProcess(command) + val process = Runtime.getRuntime().exec(command) val output = async { process.inputStream.readBytes().decodeToString() @@ -43,7 +37,7 @@ open class DefaultTerminal @Inject constructor( TerminalResult(exitCode, output.await(), error.await()) } - override fun execute(vararg command: String) = createProcess(command).run { + override fun execute(vararg command: String) = Runtime.getRuntime().exec(command).run { TerminalProcess( output = inputStream, error = errorStream, diff --git a/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/RootTerminal.kt b/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/RootTerminal.kt index 1fdbffa3..7be5716b 100644 --- a/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/RootTerminal.kt +++ b/core/core-terminals/src/main/kotlin/com/f0x1d/logfox/terminals/RootTerminal.kt @@ -1,7 +1,9 @@ package com.f0x1d.logfox.terminals import com.f0x1d.logfox.arch.di.IODispatcher +import com.f0x1d.logfox.model.terminal.TerminalProcess import com.f0x1d.logfox.model.terminal.TerminalResult +import com.f0x1d.logfox.terminals.base.Terminal import com.topjohnwu.superuser.Shell import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.withContext @@ -10,8 +12,8 @@ import javax.inject.Singleton @Singleton class RootTerminal @Inject constructor( - @IODispatcher ioDispatcher: CoroutineDispatcher, -): DefaultTerminal(ioDispatcher) { + @IODispatcher private val ioDispatcher: CoroutineDispatcher, +) : Terminal { companion object { const val INDEX = 1 @@ -19,8 +21,6 @@ class RootTerminal @Inject constructor( override val title = R.string.root - override val commandPrefix = arrayOf("su", "-c") - override suspend fun isSupported(): Boolean = withContext(ioDispatcher) { Shell.getShell().isRoot } @@ -35,7 +35,30 @@ class RootTerminal @Inject constructor( } } - // can't make currently execute method with libsu + // It seems not all devices support "su -c ...". + // I got feedback that LogFox does not read logs, while other apps do. + // After I implemented libsu permission could be got. So i took a look at libsu code + // and tried to do the same here + private fun createProcess(commands: Array): Process? = runCatching { + val process = Runtime.getRuntime().exec("su") + + process.outputStream.run { + write(commands.joinToString(" ").encodeToByteArray()) + write('\n'.code) + flush() + } + + process + }.getOrNull() + + override fun execute(vararg command: String): TerminalProcess? = createProcess(command)?.run { + TerminalProcess( + output = inputStream, + error = errorStream, + input = outputStream, + destroy = this::destroy, + ) + } override suspend fun exit() = withContext(ioDispatcher) { Shell.getShell().waitAndClose()