Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal change #3036

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public enum AndroidErrorId implements ErrorId {
ANDROID_ADB_UTIL_REVERSE_TCP_PORT_ERROR(100_679, ErrorType.INFRA_ISSUE),
ANDROID_ADB_UTIL_WAIT_FOR_SIGNAL_IN_LOG_ERROR(100_680, ErrorType.INFRA_ISSUE),
ANDROID_ADB_UTIL_NETSTAT_ERROR(100_681, ErrorType.INFRA_ISSUE),
ANDROID_ADB_UTIL_RESTART_SERVER_ERROR(100_682, ErrorType.INFRA_ISSUE),

// Android File Util: 100_851 ~ 101_000
ANDROID_FILE_UTIL_LIST_FILE_ERROR(100_851, ErrorType.INFRA_ISSUE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,17 @@ public class AndroidAdbUtil {
/** ADB args for port reverse. Should be followed by the "tcp:device_port" and "tcp:host_port". */
@VisibleForTesting static final String ADB_ARG_REVERSE_TCP = "reverse";

/** ADB arg to wait for the device to be ready. */
@VisibleForTesting static final String ADB_ARG_WAIT_FOR_DEVICE = "wait-for-device";

/** ADB arg for killing the ADB server. */
@VisibleForTesting static final String ADB_ARG_KILL_SERVER = "kill-server";

/** ADB arg for starting the ADB server. */
@VisibleForTesting static final String ADB_ARG_START_SERVER = "start-server";

/** ADB args for clearing the entire log of a device. */
@VisibleForTesting static final String[] ADB_ARGS_CLEAR_LOG = new String[] {"logcat", "-c"};
@VisibleForTesting static final String[] adbArgsClearLog = new String[] {"logcat", "-c"};

/** ADB Shell command for "cmd". */
@VisibleForTesting static final String ADB_SHELL_CMD = "cmd";
Expand Down Expand Up @@ -295,7 +304,7 @@ public String bugreportDirectory(String serial)
*/
public void clearLog(String serial) throws MobileHarnessException, InterruptedException {
try {
String unused = adb.runWithRetry(serial, ADB_ARGS_CLEAR_LOG);
String unused = adb.runWithRetry(serial, adbArgsClearLog);
} catch (MobileHarnessException e) {
throw new MobileHarnessException(
AndroidErrorId.ANDROID_ADB_UTIL_CLEAR_LOG_ERROR, e.getMessage(), e);
Expand Down Expand Up @@ -532,7 +541,7 @@ public String dumpSys(
if (sdkVersion >= AndroidVersion.NOUGAT.getStartSdkVersion()) {
baseCommand =
new String[] {
"dumpsys", "-t", String.valueOf(timeout.getSeconds()), dumpSysType.getTypeValue()
"dumpsys", "-t", String.valueOf(timeout.toSeconds()), dumpSysType.getTypeValue()
};
} else {
baseCommand = new String[] {"dumpsys", dumpSysType.getTypeValue()};
Expand Down Expand Up @@ -564,7 +573,7 @@ public String dumpSys(
*/
public void forwardTcpPort(String serial, int hostPort, int devicePort)
throws MobileHarnessException, InterruptedException {
forwardTcpPort(serial, hostPort, "tcp:" + devicePort);
forwardTcpPort(serial, hostPort, "tcp:" + devicePort, /* shouldWait= */ false);
}

/**
Expand All @@ -580,10 +589,31 @@ public void forwardTcpPort(String serial, int hostPort, int devicePort)
*/
public void forwardTcpPort(String serial, int hostPort, String devicePortDescription)
throws MobileHarnessException, InterruptedException {
forwardTcpPort(serial, hostPort, devicePortDescription, /* shouldWait= */ false);
}

/**
* Sets tcp port forwarding on an android device with specific port description on device.
*
* @param serial serial number of the device
* @param hostPort the port on local machine
* @param devicePortDescription the description of port on the android device. See <a
* href="https://developer.android.com/studio/command-line/adb.html#forwardports" /> for more
* details.
* @param shouldWait whether to wait for the device to be ready before forwarding the port
* @throws MobileHarnessException if fails to set tcp forward
* @throws InterruptedException if the thread executing the commands is interrupted
*/
public void forwardTcpPort(
String serial, int hostPort, String devicePortDescription, boolean shouldWait)
throws MobileHarnessException, InterruptedException {
String[] args = new String[] {ADB_ARG_FORWARD_TCP, "tcp:" + hostPort, devicePortDescription};
try {
String unused =
adb.runWithRetry(
serial, new String[] {ADB_ARG_FORWARD_TCP, "tcp:" + hostPort, devicePortDescription});
String unused;
if (shouldWait) {
unused = adb.runWithRetry(serial, new String[] {ADB_ARG_WAIT_FOR_DEVICE});
}
unused = adb.runWithRetry(serial, args);
} catch (MobileHarnessException e) {
throw new MobileHarnessException(
AndroidErrorId.ANDROID_ADB_UTIL_FORWARD_TCP_PORT_ERROR, e.getMessage(), e);
Expand Down Expand Up @@ -646,9 +676,7 @@ public String getProperty(String serial, ImmutableList<String> propertyKeys)
String output =
adb.runShellWithRetry(serial, ADB_SHELL_GET_PROPERTY + " " + key, SHORT_COMMAND_TIMEOUT)
.trim();
if (output.isEmpty() || output.contains(OUTPUT_KEY_NOT_FOUND)) {
continue;
} else {
if (!output.isEmpty() && !output.contains(OUTPUT_KEY_NOT_FOUND)) {
return output;
}
}
Expand Down Expand Up @@ -838,16 +866,53 @@ public void removeTcpPortForward(String serial, int hostPort)
*/
public void reverseTcpPort(String serial, int devicePort, int hostPort)
throws MobileHarnessException, InterruptedException {
reverseTcpPort(serial, devicePort, hostPort, /* shouldWait= */ false);
}

/**
* Sets tcp port reversing on an android device. Only works with API level >= 21.
*
* @param serial serial number of the device
* @param devicePort the port on the android device
* @param hostPort the port on local machine
* @param shouldWait whether to wait for the device to be ready before reversing the port
* @throws MobileHarnessException if fails to set tcp reverse
* @throws InterruptedException if the thread executing the commands is interrupted
*/
public void reverseTcpPort(String serial, int devicePort, int hostPort, boolean shouldWait)
throws MobileHarnessException, InterruptedException {
String[] args = new String[] {ADB_ARG_REVERSE_TCP, "tcp:" + devicePort, "tcp:" + hostPort};
try {
String unused =
adb.runWithRetry(
serial, new String[] {ADB_ARG_REVERSE_TCP, "tcp:" + devicePort, "tcp:" + hostPort});
String unused;
if (shouldWait) {
unused = adb.runWithRetry(serial, new String[] {ADB_ARG_WAIT_FOR_DEVICE});
}
unused = adb.runWithRetry(serial, args);
} catch (MobileHarnessException e) {
throw new MobileHarnessException(
AndroidErrorId.ANDROID_ADB_UTIL_REVERSE_TCP_PORT_ERROR, e.getMessage(), e);
}
}

/**
* Restarts the ADB server.
*
* <p>See https://developer.android.com/tools/adb#stopping.
*
* @param serial serial number of the device
* @throws MobileHarnessException if fails to restart the ADB server
* @throws InterruptedException if the thread executing the commands is interrupted
*/
public void restartServer(String serial) throws MobileHarnessException, InterruptedException {
try {
String unused = adb.runWithRetry(serial, new String[] {ADB_ARG_KILL_SERVER});
unused = adb.runWithRetry(serial, new String[] {ADB_ARG_START_SERVER});
} catch (MobileHarnessException e) {
throw new MobileHarnessException(
AndroidErrorId.ANDROID_ADB_UTIL_RESTART_SERVER_ERROR, e.getMessage(), e);
}
}

/**
* Forks a new thread to dump the log from the device using logcat asynchronously.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ java_library(
"//src/java/com/google/devtools/mobileharness/shared/util/time:sleeper",
"//src/java/com/google/wireless/qa/mobileharness/shared/util:base",
"@maven//:com_google_code_findbugs_jsr305",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)
Expand Down
Loading