diff --git a/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java b/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java index 1a830a738..1b0358f90 100644 --- a/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java +++ b/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java @@ -675,7 +675,7 @@ public String getWifiRssi(String serial) throws MobileHarnessException, Interrup * @throws InterruptedException if current thread is interrupted during this method */ public double pingSuccessRate(String serial, String host, int count) throws InterruptedException { - String shell = String.format(ADB_SHELL_TEMPLATE_PING, SHORT_PING_TIMEOUT.getSeconds(), host); + String shell = String.format(ADB_SHELL_TEMPLATE_PING, SHORT_PING_TIMEOUT.toSeconds(), host); int successCount = 0; for (int i = 0; i < count; i++) { String output = null; @@ -734,6 +734,76 @@ public boolean pingSuccessfully(String serial) throws InterruptedException { return false; } + /** + * Checks if the device is reachable. + * + *
This function first tries to connect to the device via adb. If it fails, it then tries to
+ * ping the device. If the ping command returns "0% packet loss", then the device is reachable.
+ *
+ * @param deviceId The device ID of the device to check.
+ * @return True if the device is reachable, false otherwise.
+ */
+ public boolean canPingDevice(String deviceId)
+ throws MobileHarnessException, InterruptedException {
+ try {
+ adbUtil.connect(deviceId);
+ } catch (MobileHarnessException e) {
+ // Log the original connection error for debugging
+ logger.atWarning().withCause(e).log(
+ "Failed to connect to device %s. Trying to ping instead.", deviceId);
+
+ try {
+ String output = adb.runShell(deviceId, "ping -c 1"); // Ping the device
+ // If the ping command returns "0% packet loss", then the device is reachable.
+ if (!output.contains("0% packet loss")) {
+ logger.atWarning().log("Packet loss detected when pinging device %s.", deviceId);
+ return false;
+ }
+ return true;
+ } catch (MobileHarnessException pingException) {
+ logger.atWarning().withCause(e).log("Failed to ping device %s", deviceId);
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Gets the ping response time value for the given device.
+ *
+ * @param deviceId The device ID of the device to check.
+ * @param metric The metric to get the value for.
+ * @return The ping response time value for the given device.
+ */
+ public String getPingResponseValue(String deviceId, String metric) throws InterruptedException {
+ try {
+ String pingOutput = adb.runShell(deviceId, "ping -c 5");
+
+ // Extract the last line of the ping output.
+ List