Skip to content

Commit 72faecb

Browse files
committed
#661 refactor shell adapters to return a ShellResult class and tweak the ShellCommandActionScreen
1 parent 86cd243 commit 72faecb

11 files changed

Lines changed: 212 additions & 73 deletions

File tree

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionData.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,13 @@ sealed class ActionData : Comparable<ActionData> {
937937
val description: String,
938938
val command: String,
939939
val useRoot: Boolean,
940-
val timeoutMs: Int = 10000, // milliseconds (default 10 seconds)
940+
val timeoutMillis: Int = 10000, // milliseconds (default 10 seconds)
941941
) : ActionData() {
942942
override val id: ActionId = ActionId.SHELL_COMMAND
943943

944944
override fun toString(): String {
945945
// Do not leak sensitive command info to logs.
946-
return "ShellCommand(description=$description, useRoot=$useRoot, timeoutMs=$timeoutMs)"
946+
return "ShellCommand(description=$description, useRoot=$useRoot, timeoutMs=$timeoutMillis)"
947947
}
948948
}
949949

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionDataEntityMapper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ object ActionDataEntityMapper {
674674
description = description,
675675
command = command,
676676
useRoot = useRoot,
677-
timeoutMs = timeoutMs,
677+
timeoutMillis = timeoutMs,
678678
)
679679
}
680680

@@ -1026,7 +1026,7 @@ object ActionDataEntityMapper {
10261026

10271027
is ActionData.ShellCommand -> listOf(
10281028
EntityExtra(ActionEntity.EXTRA_SHELL_COMMAND_DESCRIPTION, data.description),
1029-
EntityExtra(ActionEntity.EXTRA_SHELL_COMMAND_TIMEOUT, data.timeoutMs.toString()),
1029+
EntityExtra(ActionEntity.EXTRA_SHELL_COMMAND_TIMEOUT, data.timeoutMillis.toString()),
10301030
)
10311031

10321032
else -> emptyList()

base/src/main/java/io/github/sds100/keymapper/base/actions/ConfigShellCommandViewModel.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ConfigShellCommandViewModel @Inject constructor(
3232
description = action.description,
3333
command = action.command,
3434
useRoot = action.useRoot,
35-
timeoutSeconds = action.timeoutMs / 1000,
35+
timeoutSeconds = action.timeoutMillis / 1000,
3636
)
3737
}
3838

@@ -53,7 +53,6 @@ class ConfigShellCommandViewModel @Inject constructor(
5353
}
5454

5555
fun onTestClick() {
56-
// Cancel any existing test
5756
testJob?.cancel()
5857

5958
state = state.copy(
@@ -102,7 +101,7 @@ class ConfigShellCommandViewModel @Inject constructor(
102101
description = state.description,
103102
command = state.command,
104103
useRoot = state.useRoot,
105-
timeoutMs = state.timeoutSeconds * 1000,
104+
timeoutMillis = state.timeoutSeconds * 1000,
106105
)
107106

108107
viewModelScope.launch {

base/src/main/java/io/github/sds100/keymapper/base/actions/ExecuteShellCommandUseCase.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.github.sds100.keymapper.common.utils.KMError
44
import io.github.sds100.keymapper.common.utils.KMResult
55
import io.github.sds100.keymapper.system.root.SuAdapter
66
import io.github.sds100.keymapper.system.shell.ShellAdapter
7+
import io.github.sds100.keymapper.system.shell.ShellResult
78
import kotlinx.coroutines.TimeoutCancellationException
89
import kotlinx.coroutines.flow.Flow
910
import kotlinx.coroutines.withTimeout
@@ -16,25 +17,25 @@ class ExecuteShellCommandUseCase @Inject constructor(
1617
suspend fun execute(
1718
command: String,
1819
useRoot: Boolean,
19-
timeoutMs: Long,
20+
timeoutMillis: Long,
2021
): KMResult<Unit> {
2122
return try {
22-
withTimeout(timeoutMs) {
23+
withTimeout(timeoutMillis) {
2324
if (useRoot) {
2425
suAdapter.execute(command)
2526
} else {
2627
shellAdapter.execute(command)
2728
}
2829
}
2930
} catch (e: TimeoutCancellationException) {
30-
KMError.ShellCommandTimeout(timeoutMs.toInt())
31+
KMError.ShellCommandTimeout(timeoutMillis.toInt())
3132
}
3233
}
3334

3435
fun executeWithStreamingOutput(
3536
command: String,
3637
useRoot: Boolean,
37-
): Flow<KMResult<String>> {
38+
): Flow<KMResult<ShellResult>> {
3839
return if (useRoot) {
3940
suAdapter.executeWithStreamingOutput(command)
4041
} else {

base/src/main/java/io/github/sds100/keymapper/base/actions/PerformActionsUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ class PerformActionsUseCaseImpl @AssistedInject constructor(
911911
result = executeShellCommandUseCase.execute(
912912
command = action.command,
913913
useRoot = action.useRoot,
914-
timeoutMs = action.timeoutMs.toLong(),
914+
timeoutMillis = action.timeoutMillis.toLong(),
915915
)
916916
}
917917

0 commit comments

Comments
 (0)