AdbHelper

public final class AdbHelper
extends Object

java.lang.Object
   ↳ com.android.tradefed.device.server.AdbHelper


用于处理请求和与 adb 的连接的辅助类。

AndroidDebugBridge 是用于连接到 adb 的公共 API,而 AdbHelper 则负责低级别事务。

目前使用自旋等待非阻塞 I/O。选择器会更高效,但对于我们在此处所做的事情来说,似乎有点过头了。

摘要

嵌套类

class AdbHelper.AdbResponse

来自 ADB 的响应。 

常量

String HOST_TRANSPORT

字段

public static final Charset DEFAULT_CHARSET

公共方法

static void createForward(InetSocketAddress adbSockAddr, IDevice device, String localPortSpec, String remotePortSpec)

在本地端口和远程端口之间创建端口转发。

static AdbCommandRejectedException createHiddenException(String message, boolean errorDuringDeviceSelection)
static AdbCommandRejectedException createHiddenException(String message)
static byte[] createJdwpForwardRequest(int pid)

创建向 JDWP 进程发送的端口转发请求。

static SocketChannel createPassThroughConnection(InetSocketAddress adbSockAddr, String deviceSerialNumber, int pid)

创建并连接一个新的直通套接字,从主机到设备上的端口。

static void createReverse(InetSocketAddress adbSockAddr, IDevice device, String remotePortSpec, String localPortSpec)

在远程端口和本地端口之间创建端口反向。

static void executeRemoteCommand(InetSocketAddress adbSockAddr, String command, IDevice device, IShellOutputReceiver rcvr, long maxTimeToOutputResponse, TimeUnit maxTimeUnits)

在设备上执行 shell 命令并检索输出。

static void executeRemoteCommand(InetSocketAddress adbSockAddr, AdbHelper.AdbService adbService, String command, IDevice device, IShellOutputReceiver rcvr, long maxTimeout, long maxTimeToOutputResponse, TimeUnit maxTimeUnits, InputStream is)

在设备上执行远程命令并检索输出。

static void executeRemoteCommand(InetSocketAddress adbSockAddr, String command, IDevice device, IShellOutputReceiver rcvr, long maxTimeout, long maxTimeToOutputResponse, TimeUnit maxTimeUnits)

在设备上执行 shell 命令并检索输出。

static byte[] formAdbRequest(String payloadString)

创建以四个十六进制数字开头的 ASCII 字符串。

static String getFeatures(IDevice device)

从设备查询一组受支持的功能。

static RawImage getFrameBuffer(InetSocketAddress adbSockAddr, IDevice device, long timeout, TimeUnit unit)

从设备检索帧缓冲区,并设置给定的超时时间。

static String getHostFeatures()

从 ADB 主机查询一组受支持的功能。

static SocketChannel open(InetSocketAddress adbSockAddr, IDevice device, int devicePort)

创建并连接新的直通套接字,从主机到设备上的端口。

static SocketChannel rawAdbService(InetSocketAddress socketAddress, IDevice device, String command, AdbHelper.AdbService service)

在远程设备上调用服务。

static SocketChannel rawExec(InetSocketAddress socketAddress, IDevice device, String executable, String[] parameters)

在远程设备上调用 host:exec 服务。

static AdbHelper.AdbResponse readAdbResponse(SocketChannel chan, boolean readDiagString)

在命令之后从 ADB 读取响应。

static void reboot(String into, InetSocketAddress adbSockAddr, IDevice device)

重新启动设备。

static void removeForward(InetSocketAddress adbSockAddr, IDevice device, String localPortSpec)

移除本地端口与远程端口之间的端口转发。

static void removeReverse(InetSocketAddress adbSockAddr, IDevice device, String remotePortSpec)

移除远程端口与本地端口之间的端口反转。

static void root(InetSocketAddress adbSockAddr, IDevice device)

要求 adb 守护进程在设备上成为 root。

static void setDevice(SocketChannel adbChan, IDevice device)

给定一个 IDevice,获取序列号并告知 adb 与该设备通信。

static void setDevice(SocketChannel adbChan, String deviceSerialNumber)

告知 adb 与特定设备通信

static void write(SocketChannel chan, byte[] data, int length, int timeout)

写入,直到“data”中的所有数据都已写入、达到可选长度、超时时间到期或连接失败为止。

static void write(SocketChannel chan, byte[] data)

写入,直到“data”中的所有数据都写入完毕,或者连接失败或超时。

常量

HOST_TRANSPORT

public static final String HOST_TRANSPORT

常量值: "host:transport:"

字段

DEFAULT_CHARSET

public static final Charset DEFAULT_CHARSET

公共方法

createForward

public static void createForward (InetSocketAddress adbSockAddr, 
                IDevice device, 
                String localPortSpec, 
                String remotePortSpec)

在本地端口和远程端口之间创建端口转发。

参数
adbSockAddr InetSocketAddress:要连接到 adb 的套接字地址

device IDevice:要进行端口转发的设备

localPortSpec String:要转发的本地端口的规范,应采用以下格式:tcp:

remotePortSpec String:要转发到的远程端口的规范,可以是以下值之一:tcp: localabstract: localreserved: localfilesystem: dev: jdwp: (仅限远程)

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则会发生这种情况。

createHiddenException

public static AdbCommandRejectedException createHiddenException (String message, 
                boolean errorDuringDeviceSelection)

参数
message String

errorDuringDeviceSelection boolean

返回
AdbCommandRejectedException

createHiddenException

public static AdbCommandRejectedException createHiddenException (String message)

参数
message String

返回
AdbCommandRejectedException

createJdwpForwardRequest

public static byte[] createJdwpForwardRequest (int pid)

创建向 JDWP 进程发送的端口转发请求。这将返回一个包含“####jwdp:{pid}”的数组。

参数
pid int:设备上的 jdwp 进程 pid。

返回
byte[]

createPassThroughConnection

public static SocketChannel createPassThroughConnection (InetSocketAddress adbSockAddr, 
                String deviceSerialNumber, 
                int pid)

创建并连接一个新的直通套接字,从主机到设备上的端口。

参数
deviceSerialNumber String:要连接的设备的序列号。可以为 null 或空,在这种情况下,连接将连接到第一个可用设备。

pid int:要连接的进程 PID。

返回
SocketChannel

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则会发生这种情况。

createReverse

public static void createReverse (InetSocketAddress adbSockAddr, 
                IDevice device, 
                String remotePortSpec, 
                String localPortSpec)

在远程端口和本地端口之间创建端口反向。

参数
adbSockAddr InetSocketAddress:要连接到 adb 的套接字地址

device IDevice:要进行端口反转的设备

remotePortSpec String:要反向连接的远程端口的规范,可以是以下值之一:tcp: localabstract: localreserved: localfilesystem: dev: jdwp: (仅限远程)

localPortSpec String:本地端口的规范,应采用 tcp:格式

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则为 true。

executeRemoteCommand

public static void executeRemoteCommand (InetSocketAddress adbSockAddr, 
                String command, 
                IDevice device, 
                IShellOutputReceiver rcvr, 
                long maxTimeToOutputResponse, 
                TimeUnit maxTimeUnits)

在设备上执行 shell 命令并检索输出。输出会随着 rcvr 的到达而传递给它。

参数
adbSockAddr InetSocketAddress:传递给 adb 的 ERROR(/InetSocketAddress)

command String:要执行的 shell 命令

device IDevice:要执行命令的 IDevice

rcvr IShellOutputReceiver:将接收 shell 命令输出的 IShellOutputReceiver

maxTimeToOutputResponse long:命令输出之间的最长时间。如果命令输出之间的时间间隔过长,该方法将抛出 ShellCommandUnresponsiveException。值为 0 表示该方法将永远等待命令输出,并且永远不会抛出异常。

maxTimeUnits TimeUnit:非零 maxTimeToOutputResponse 值的单位。

抛出
TimeoutException 在发送命令时连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
ShellCommandUnresponsiveException 以防 shell 命令在超过 maxTimeToOutputResponse 的时间内未发送任何输出。
如果连接出现 I/O 错误,则会发生这种情况。

executeRemoteCommand

public static void executeRemoteCommand (InetSocketAddress adbSockAddr, 
                AdbHelper.AdbService adbService, 
                String command, 
                IDevice device, 
                IShellOutputReceiver rcvr, 
                long maxTimeout, 
                long maxTimeToOutputResponse, 
                TimeUnit maxTimeUnits, 
                InputStream is)

在设备上执行远程命令并检索输出。输出会随着 rcvr 的到达而传递给它。该命令由 adbService 参数标识的远程服务执行。

参数
adbSockAddr InetSocketAddress:传递给 adb 的 ERROR(/InetSocketAddress)

adbService AdbHelper.AdbService:用于运行命令的 AdbHelper.AdbService

command String:要执行的 shell 命令

device IDevice:要执行命令的 IDevice

rcvr IShellOutputReceiver:将接收 shell 命令输出的 IShellOutputReceiver

maxTimeout long:整个命令执行的最长超时时间。值为 0 表示不设置超时。

maxTimeToOutputResponse long:命令输出之间的最长时间。如果命令输出之间的时间间隔过长,该方法将抛出 ShellCommandUnresponsiveException。值为 0 表示该方法将永远等待命令输出,并且永远不会抛出异常。

maxTimeUnits TimeUnit:非零 maxTimeoutmaxTimeToOutputResponse 值的单位。

is InputStream:一个可选的 ERROR(/InputStream),在调用命令后和检索响应之前进行流式传输。

抛出
TimeoutException 在发送命令时连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
ShellCommandUnresponsiveException 以防 shell 命令在超过 maxTimeToOutputResponse 的时间内未发送任何输出。
如果连接出现 I/O 错误,则会发生这种情况。

executeRemoteCommand

public static void executeRemoteCommand (InetSocketAddress adbSockAddr, 
                String command, 
                IDevice device, 
                IShellOutputReceiver rcvr, 
                long maxTimeout, 
                long maxTimeToOutputResponse, 
                TimeUnit maxTimeUnits)

在设备上执行 shell 命令并检索输出。输出会随着 rcvr 的到达而传递给它。

参数
adbSockAddr InetSocketAddress:传递给 adb 的 ERROR(/InetSocketAddress)

command String:要执行的 shell 命令

device IDevice:要执行命令的 IDevice

rcvr IShellOutputReceiver:将接收 shell 命令输出的 IShellOutputReceiver

maxTimeout long:命令返回的最长时间。值为 0 表示不会应用最大超时时间。

maxTimeToOutputResponse long:命令输出之间的最长时间。如果命令输出之间的时间间隔过长,该方法将抛出 ShellCommandUnresponsiveException。值为 0 表示该方法将永远等待命令输出,并且永远不会抛出异常。

maxTimeUnits TimeUnit:非零 maxTimeoutmaxTimeToOutputResponse 值的单位。

抛出
TimeoutException 在发送命令时连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
ShellCommandUnresponsiveException 以防 shell 命令在超过 maxTimeToOutputResponse 的时间内未发送任何输出。
如果连接出现 I/O 错误,则会发生这种情况。

formAdbRequest

public static byte[] formAdbRequest (String payloadString)

创建以四个十六进制数字开头的 ASCII 字符串。开头的“####”是字符串其余部分的长度,以 ASCII 十六进制(不区分大小写)编码。

参数
payloadString String

返回
byte[]

getFeatures

public static String getFeatures (IDevice device)

从设备查询一组受支持的功能。

参数
device IDevice:要进行端口转发的设备

返回
String

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则会发生这种情况。

getFrameBuffer

public static RawImage getFrameBuffer (InetSocketAddress adbSockAddr, 
                IDevice device, 
                long timeout, 
                TimeUnit unit)

在指定超时时间内从设备检索帧缓冲区。超时时间为 0 表示将无限期等待。

参数
adbSockAddr InetSocketAddress

device IDevice

timeout long

unit TimeUnit

返回
RawImage

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则为 true。

getHostFeatures

public static String getHostFeatures ()

从 ADB 主机查询一组受支持的功能。

返回
String

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则会发生这种情况。

展开

public static SocketChannel open (InetSocketAddress adbSockAddr, 
                IDevice device, 
                int devicePort)

创建并连接新的直通套接字,从主机到设备上的端口。

参数
device IDevice:要连接的设备。可以为 null,在这种情况下,连接将是与第一个可用设备的连接。

devicePort int:我们正在打开的端口

返回
SocketChannel

抛出
TimeoutException 在连接超时的情况下。
如果连接出现 I/O 错误,则为 true。
AdbCommandRejectedException 如果 adb 拒绝该命令

rawAdbService

public static SocketChannel rawAdbService (InetSocketAddress socketAddress, 
                IDevice device, 
                String command, 
                AdbHelper.AdbService service)

在远程设备上调用服务。返回一个已连接到执行进程的套接字渠道。

ddlmib 会放弃对返回的 SocketChannel 的所有权,并且必须在使用后显式关闭。

参数
device IDevice:要连接的设备。可以为 null,在这种情况下,连接将是与第一个可用设备的连接。

command String:要执行的命令

service AdbHelper.AdbService:用于运行命令的 AdbHelper.AdbService

返回
SocketChannel

抛出
IOException
com.android.ddmlib.TimeoutException
com.android.ddmlib.AdbCommandRejectedException
AdbCommandRejectedException
TimeoutException

rawExec

public static SocketChannel rawExec (InetSocketAddress socketAddress, 
                IDevice device, 
                String executable, 
                String[] parameters)

在远程设备上调用 host:exec 服务。返回已连接到执行进程的套接字渠道。请注意,exec 服务不会区分 stdout 和 stderr,因此从套接字读取的任何内容都可能来自任一输出,并且可能会交错显示。

ddlmib 会放弃对返回的 SocketChannel 的所有权,并且必须在使用后显式关闭。

参数
device IDevice:要连接的设备。可以为 null,在这种情况下,连接将是与第一个可用设备的连接。

executable String:要运行的可执行文件的绝对路径

parameters String:执行可执行文件时传递的参数

返回
SocketChannel

抛出
IOException
com.android.ddmlib.TimeoutException
com.android.ddmlib.AdbCommandRejectedException
AdbCommandRejectedException
TimeoutException

readAdbResponse

public static AdbHelper.AdbResponse readAdbResponse (SocketChannel chan, 
                boolean readDiagString)

在命令后从 ADB 读取响应。

参数
chan SocketChannel:已连接到 adb 的套接字渠道。

readDiagString boolean:如果为 true,则表示我们期望 OKAY 响应后跟一个诊断字符串。否则,我们只希望诊断字符串跟在 FAIL 后面。

返回
AdbHelper.AdbResponse

抛出
TimeoutException 在连接超时的情况下。
如果连接出现 I/O 错误,则为 true。

重新启动

public static void reboot (String into, 
                InetSocketAddress adbSockAddr, 
                IDevice device)

重新启动设备。

参数
into String:重新启动进入的模式(恢复模式、引导加载程序模式)。或者为 null,表示仅重新启动。

adbSockAddr InetSocketAddress

device IDevice

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则会发生这种情况。

removeForward

public static void removeForward (InetSocketAddress adbSockAddr, 
                IDevice device, 
                String localPortSpec)

移除本地端口与远程端口之间的端口转发。

参数
adbSockAddr InetSocketAddress:要连接到 adb 的套接字地址

device IDevice:要移除端口转发的设备

localPortSpec String:转发的本地端口的规范,应采用以下格式:tcp:

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则为 true。

removeReverse

public static void removeReverse (InetSocketAddress adbSockAddr, 
                IDevice device, 
                String remotePortSpec)

移除远程端口与本地端口之间的端口反转。

参数
adbSockAddr InetSocketAddress:要连接到 adb 的套接字地址

device IDevice:要移除端口反转的设备

remotePortSpec String:反向远程端口规范,可以是以下值之一:tcp: localabstract: localreserved: localfilesystem: dev: jdwp: (仅限远程)

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则会发生这种情况。

root

public static void root (InetSocketAddress adbSockAddr, 
                IDevice device)

要求 adb 守护程序在设备上成为 root 用户。此操作可能会静默失败,并且只能在开发者 build 上成功。如需了解详情,请参阅“adb root”。如果您需要知道是否成功,可以检查“echo \$USER_ID”的 executeRemoteCommand 结果,如果结果为 0,则 adbd 以 root 身份运行。

参数
adbSockAddr InetSocketAddress

device IDevice

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则为 true。

setDevice

public static void setDevice (SocketChannel adbChan, 
                IDevice device)

给定一个 IDevice,获取序列号并告知 adb 与该设备通信。

参数
adbChan SocketChannel:与 adb 的套接字连接。

device IDevice:要对话的设备。

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则为 true。

setDevice

public static void setDevice (SocketChannel adbChan, 
                String deviceSerialNumber)

告知 adb 与特定设备通信

参数
adbChan SocketChannel:与 adb 的套接字连接

deviceSerialNumber String:要与之通信的设备的序列号;如果为 null,则所选的默认设备是 adb 选择的设备。

抛出
TimeoutException 在连接超时的情况下。
AdbCommandRejectedException 如果 adb 拒绝该命令
如果连接出现 I/O 错误,则为 true。

写内容

public static void write (SocketChannel chan, 
                byte[] data, 
                int length, 
                int timeout)

写入,直到“data”中的所有数据都已写入、达到可选长度、超时时间已过或连接失败。如果所有数据都已写入,则返回“true”。

参数
chan SocketChannel:要写入的已打开的套接字。

data byte:要发送的缓冲区。

length int:要写入的长度,或 -1(表示发送整个缓冲区)。

timeout int:超时值。超时时间为零表示“无限期等待”。

抛出
TimeoutException 在连接超时的情况下。
如果连接出现 I/O 错误,则会发生这种情况。

写内容

public static void write (SocketChannel chan, 
                byte[] data)

写入,直到“data”中的所有数据都写入完毕,或者连接失败或超时。

此方法使用默认的超时时间值。

参数
chan SocketChannel:要写入的已打开的套接字。

data byte:要发送的缓冲区。

抛出
TimeoutException 在连接超时的情况下。
如果连接出现 I/O 错误,则会发生这种情况。