Skip to content

Commit

Permalink
feat: allow installation from inputstream
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsmotto committed Dec 31, 2023
1 parent 447f9b4 commit 01323f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
33 changes: 25 additions & 8 deletions dadb/src/main/kotlin/dadb/Dadb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package dadb

import dadb.adbserver.AdbServer
import dadb.forwarding.TcpForwarder
import okio.*
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.nio.file.Files
import okio.*

interface Dadb : AutoCloseable {

Expand Down Expand Up @@ -77,22 +77,39 @@ interface Dadb : AutoCloseable {
@Throws(IOException::class)
fun install(file: File, vararg options: String) {
if (supportsFeature("cmd")) {
execCmd("package", "install", "-S", file.length().toString(), *options).use { stream ->
stream.sink.writeAll(file.source())
install(file.source(), file.length(), *options)
} else {
pmInstall(file, *options)
}
}

@Throws(IOException::class)
fun install(source: Source, size: Long, vararg options: String) {
if (supportsFeature("cmd")) {
execCmd("package", "install", "-S", size.toString(), *options).use { stream ->
stream.sink.writeAll(source)
stream.sink.flush()
val response = stream.source.readString(Charsets.UTF_8)
if (!response.startsWith("Success")) {
throw IOException("Install failed: $response")
}
}
} else {
val fileName = file.name
val remotePath = "/data/local/tmp/$fileName"
push(file, remotePath)
shell("pm install ${options.joinToString(" ")} \"$remotePath\"")
val tempFile = kotlin.io.path.createTempFile()
val fileSink = tempFile.sink().buffer()
fileSink.writeAll(source)
fileSink.flush()
pmInstall(tempFile.toFile(), *options)
}
}

private fun pmInstall(file: File, vararg options: String) {
val fileName = file.name
val remotePath = "/data/local/tmp/$fileName"
push(file, remotePath)
shell("pm install ${options.joinToString(" ")} \"$remotePath\"")
}

@Throws(IOException::class)
fun installMultiple(apks: List<File>, vararg options: String) {
// http://aospxref.com/android-12.0.0_r3/xref/packages/modules/adb/client/adb_install.cpp#538
Expand Down
12 changes: 12 additions & 0 deletions dadb/src/test/kotlin/dadb/DadbTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import okio.source
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import java.io.ByteArrayInputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.net.Socket
import java.nio.charset.StandardCharsets
import java.util.*
Expand Down Expand Up @@ -191,6 +193,16 @@ internal abstract class DadbTest : BaseConcurrencyTest() {
}
}

@Test
fun installStream() {
localEmulator { dadb ->
val inputStream = FileInputStream(TestApk.FILE).source()
dadb.install(inputStream, TestApk.FILE.length())
val response = dadb.shell("pm list packages ${TestApk.PACKAGE_NAME}")
assertShellResponse(response, 0, "package:${TestApk.PACKAGE_NAME}\n")
}
}

@Test
fun installMultiple() {
localEmulator { dadb ->
Expand Down

0 comments on commit 01323f0

Please sign in to comment.