From f4e4cc7126521732cee443b6eed7f177c67a7385 Mon Sep 17 00:00:00 2001 From: Simon Chan <1330321+yume-chan@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:42:49 +0800 Subject: [PATCH] feat(usb): expose adb endpoints for external use --- libraries/adb-daemon-webusb/src/device.ts | 24 ++++++++++++++++++----- libraries/android-bin/src/logcat.ts | 11 ++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/libraries/adb-daemon-webusb/src/device.ts b/libraries/adb-daemon-webusb/src/device.ts index ddac0dbe2..14f5d42c9 100644 --- a/libraries/adb-daemon-webusb/src/device.ts +++ b/libraries/adb-daemon-webusb/src/device.ts @@ -105,6 +105,14 @@ export class AdbDaemonWebUsbConnection } #inEndpoint: USBEndpoint; + get inEndpoint() { + return this.#inEndpoint; + } + + #outEndpoint: USBEndpoint; + get outEndpoint() { + return this.#outEndpoint; + } #readable: ReadableStream; get readable() { @@ -124,6 +132,7 @@ export class AdbDaemonWebUsbConnection ) { this.#device = device; this.#inEndpoint = inEndpoint; + this.#outEndpoint = outEndpoint; let closed = false; @@ -313,11 +322,7 @@ export class AdbDaemonWebUsbDevice implements AdbDaemonDevice { this.#usbManager = usbManager; } - /** - * Claim the device and create a pair of `AdbPacket` streams to the ADB interface. - * @returns The pair of `AdbPacket` streams. - */ - async connect(): Promise { + async #claimInterface(): Promise<[USBEndpoint, USBEndpoint]> { if (!this.#raw.opened) { await this.#raw.open(); } @@ -352,6 +357,15 @@ export class AdbDaemonWebUsbDevice implements AdbDaemonDevice { const { inEndpoint, outEndpoint } = findUsbEndpoints( alternate.endpoints, ); + return [inEndpoint, outEndpoint]; + } + + /** + * Claim the device and create a pair of `AdbPacket` streams to the ADB interface. + * @returns The pair of `AdbPacket` streams. + */ + async connect(): Promise { + const [inEndpoint, outEndpoint] = await this.#claimInterface(); return new AdbDaemonWebUsbConnection( this, inEndpoint, diff --git a/libraries/android-bin/src/logcat.ts b/libraries/android-bin/src/logcat.ts index b5d0aea70..9f7ced451 100644 --- a/libraries/android-bin/src/logcat.ts +++ b/libraries/android-bin/src/logcat.ts @@ -471,11 +471,12 @@ export class Logcat extends AdbCommandBase { } async clear(ids?: LogId[]) { - await this.adb.subprocess.spawnAndWait([ - "logcat", - "-c", - ...(ids ? ["-b", Logcat.joinLogId(ids)] : []), - ]); + const args = ["logcat", "-c"]; + if (ids && ids.length > 0) { + args.push("-b", Logcat.joinLogId(ids)); + } + + await this.adb.subprocess.spawnAndWait(args); } binary(options?: LogcatOptions): ReadableStream {