Skip to content

Commit

Permalink
TECH: adbserver's backward compatibility was fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ersanin committed Sep 4, 2024
1 parent 9c94128 commit 1ae2a67
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ package com.kaspersky.adbserver.commandtypes
import com.kaspersky.adbserver.common.api.Command

data class AdbCommand(
override val command: String,
override val arguments: List<String> = emptyList()
) : Command(command, arguments)
override val body: String,
) : Command(body)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.kaspersky.adbserver.commandtypes

import com.kaspersky.adbserver.common.api.Command
import com.kaspersky.adbserver.common.api.ComplexCommand

data class CmdCommand(
override val command: String,
override val body: String,
override val arguments: List<String> = emptyList()
) : Command(command, arguments)
) : ComplexCommand(body, arguments)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kaspersky.adbserver.commandtypes

import com.kaspersky.adbserver.common.api.ComplexCommand

data class ComplexAdbCommand(
override val body: String,
override val arguments: List<String> = emptyList()
) : ComplexCommand(body, arguments)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import java.io.Serializable
/**
* Command to execute by AdbServer
*/
abstract class Command(open val command: String, open val arguments: List<String> = emptyList()) : Serializable
abstract class Command(open val body: String) : Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.kaspersky.adbserver.common.api

abstract class ComplexCommand(override val body: String, open val arguments: List<String> = emptyList()) : Command(body)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kaspersky.adbserver.device

import com.kaspersky.adbserver.commandtypes.AdbCommand
import com.kaspersky.adbserver.commandtypes.CmdCommand
import com.kaspersky.adbserver.commandtypes.ComplexAdbCommand
import com.kaspersky.adbserver.common.api.CommandResult
import com.kaspersky.adbserver.common.log.LoggerFactory
import com.kaspersky.adbserver.common.log.logger.LogLevel
Expand Down Expand Up @@ -35,7 +36,7 @@ object AdbTerminal {
* Please first of all call [connect] method to establish a connection
*/
fun executeAdb(command: String, arguments: List<String>): CommandResult = device?.fulfill(
AdbCommand(command, arguments)
ComplexAdbCommand(command, arguments)
) ?: throw IllegalStateException("Please first of all call [connect] method to establish a connection")

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.kaspersky.adbserver.common.api.CommandExecutor
import com.kaspersky.adbserver.common.api.CommandResult
import com.kaspersky.adbserver.commandtypes.AdbCommand
import com.kaspersky.adbserver.commandtypes.CmdCommand
import com.kaspersky.adbserver.commandtypes.ComplexAdbCommand
import com.kaspersky.adbserver.common.log.logger.Logger
import java.lang.UnsupportedOperationException

Expand All @@ -16,16 +17,24 @@ internal class CommandExecutorImpl(
private val adbPath: String
) : CommandExecutor {

private fun getSimpleAdbCommand(command: Command): String = "$adbPath ${adbServerPort?.let { "-P $adbServerPort " } ?: ""}-s $deviceName ${command.body}"

override fun execute(command: Command): CommandResult {
return when (command) {
is CmdCommand -> cmdCommandPerformer.perform(command.command, command.arguments)
is CmdCommand -> cmdCommandPerformer.perform(command.body, command.arguments)

is AdbCommand -> {
val adbCommand = getSimpleAdbCommand(command)
logger.d("The created adbCommand=$adbCommand")
cmdCommandPerformer.perform(adbCommand, emptyList())
}

is ComplexAdbCommand -> {
val adbCommand: String
val adbArguments: List<String>

if (command.arguments.isEmpty()) {
adbCommand = "$adbPath ${adbServerPort?.let { "-P $adbServerPort " } ?: ""}-s $deviceName ${command.command}"
adbCommand = getSimpleAdbCommand(command)
adbArguments = emptyList()
} else {
adbCommand = adbPath
Expand All @@ -36,7 +45,7 @@ internal class CommandExecutorImpl(
}
add("-s")
add(deviceName)
add(command.command)
add(command.body)
addAll(command.arguments)
}
}
Expand Down
Binary file modified artifacts/adbserver-desktop.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class AdbServerTest : TestCase() {

@Test
fun singleCommandTest() = run {
val devices = adbServer.performAdb("devices")
assert(devices.isNotEmpty())
}

@Test
fun singleComplexCommandTest() = run {
val devices = adbServer.performAdb("devices", arguments = emptyList())
assert(devices.isNotEmpty())
}
Expand Down

0 comments on commit 1ae2a67

Please sign in to comment.