Skip to content

Commit

Permalink
Merge pull request #364 from caofengbin/feature/add_android_touch_mode
Browse files Browse the repository at this point in the history
feat:Android端增加第三种触控模式,基于uiautomator2
  • Loading branch information
ZhouYixun authored Jun 19, 2023
2 parents 2870746 + 70226a4 commit 6cacc32
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.1.24</version>
<version>1.1.25</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.cloud.sonic.agent.bridge.android.AndroidDeviceBridgeTool;
import org.cloud.sonic.agent.common.enums.AndroidKey;
import org.cloud.sonic.agent.common.maps.AndroidDeviceManagerMap;
import org.cloud.sonic.agent.common.maps.HandlerMap;
import org.cloud.sonic.agent.tools.PortTool;
import org.cloud.sonic.driver.android.AndroidDriver;
import org.cloud.sonic.driver.common.tool.SonicRespException;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -45,7 +48,7 @@ public class AndroidTouchHandler {
public enum TouchMode {
SONIC_APK,
ADB,
APPIUM_SERVER;
APPIUM_UIAUTOMATOR2_SERVER;
}

public static void switchTouchMode(IDevice iDevice, TouchMode mode) {
Expand All @@ -56,7 +59,7 @@ public static TouchMode getTouchMode(IDevice iDevice) {
return touchModeMap.get(iDevice.getSerialNumber()) == null ? TouchMode.ADB : touchModeMap.get(iDevice.getSerialNumber());
}

public static void tap(IDevice iDevice, int x, int y) {
public static void tap(IDevice iDevice, int x, int y) throws SonicRespException {
switch (getTouchMode(iDevice)) {
case SONIC_APK -> {
int[] re = transferWithRotation(iDevice, x, y);
Expand All @@ -69,11 +72,17 @@ public static void tap(IDevice iDevice, int x, int y) {
writeToOutputStream(iDevice, "up\n");
}
case ADB -> AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input tap %d %d", x, y));
case APPIUM_UIAUTOMATOR2_SERVER -> {
AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) {
curStepHandler.getAndroidDriver().tap(x, y);
}
}
default -> throw new IllegalStateException("Unexpected value: " + getTouchMode(iDevice));
}
}

public static void longPress(IDevice iDevice, int x, int y, int time) {
public static void longPress(IDevice iDevice, int x, int y, int time) throws SonicRespException {
switch (getTouchMode(iDevice)) {
case SONIC_APK -> {
int[] re = transferWithRotation(iDevice, x, y);
Expand All @@ -85,13 +94,18 @@ public static void longPress(IDevice iDevice, int x, int y, int time) {
}
writeToOutputStream(iDevice, "up\n");
}
case ADB ->
AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d", x, y, x, y, time));
case ADB -> AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d", x, y, x, y, time));
case APPIUM_UIAUTOMATOR2_SERVER -> {
AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) {
curStepHandler.getAndroidDriver().longPress(x, y, time);
}
}
default -> throw new IllegalStateException("Unexpected value: " + getTouchMode(iDevice));
}
}

public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) {
public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) throws SonicRespException {
switch (getTouchMode(iDevice)) {
case SONIC_APK -> {
int[] re1 = transferWithRotation(iDevice, x1, y1);
Expand All @@ -110,8 +124,13 @@ public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) {
}
writeToOutputStream(iDevice, "up\n");
}
case ADB ->
AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d", x1, y1, x2, y2, 300));
case ADB -> AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d", x1, y1, x2, y2, 300));
case APPIUM_UIAUTOMATOR2_SERVER -> {
AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) {
curStepHandler.getAndroidDriver().swipe(x1, y1, x2, y2);
}
}
default -> throw new IllegalStateException("Unexpected value: " + getTouchMode(iDevice));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void onMessage(String s) {
}
if (session.isOpen()) {
if (ss.equals("IOSWSServer")) {
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(session.getUserProperties().get("id").toString());
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(udId);
if (iosStepHandler != null) {
try {
iosStepHandler.getIOSDriver().pressButton("home");
Expand Down Expand Up @@ -372,7 +372,7 @@ public void onError(Exception e) {
private void runAndroidStep(JSONObject jsonObject) {

AndroidPasswordMap.getMap().put(jsonObject.getString("udId"), jsonObject.getString("pwd"));
AndroidStepHandler androidStepHandler = HandlerMap.getAndroidMap().get(jsonObject.getString("sessionId"));
AndroidStepHandler androidStepHandler = HandlerMap.getAndroidMap().get(jsonObject.getString("udId"));
if (androidStepHandler == null) {
return;
}
Expand All @@ -394,7 +394,7 @@ public void run() {
* IOS步骤调试
*/
private void runIOSStep(JSONObject jsonObject) {
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(jsonObject.getString("sessionId"));
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(jsonObject.getString("udId"));
if (iosStepHandler == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void onMessage(String message, Session session) {
BytesTool.sendText(session, result.toJSONString());
}
case "debug" -> {
AndroidStepHandler androidStepHandler = HandlerMap.getAndroidMap().get(session.getUserProperties().get("id").toString());
AndroidStepHandler androidStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
switch (msg.getString("detail")) {
case "poco" -> AndroidDeviceThreadPool.cachedThreadPool.execute(() -> {
androidStepHandler.startPocoDriver(new HandleContext(), msg.getString("engine"), msg.getInteger("port"));
Expand Down Expand Up @@ -337,7 +337,7 @@ public void onMessage(String message, Session session) {
case "closeDriver" -> {
if (androidStepHandler != null && androidStepHandler.getAndroidDriver() != null) {
androidStepHandler.closeAndroidDriver();
HandlerMap.getAndroidMap().remove(session.getUserProperties().get("id").toString());
HandlerMap.getAndroidMap().remove(iDevice.getSerialNumber());
}
}
case "tree" -> {
Expand Down Expand Up @@ -408,7 +408,7 @@ private void openDriver(IDevice iDevice, Session session) {
androidStepHandler.startAndroidDriver(iDevice, port);
result.put("status", "success");
result.put("port", port);
HandlerMap.getAndroidMap().put(session.getUserProperties().get("id").toString(), androidStepHandler);
HandlerMap.getAndroidMap().put(iDevice.getSerialNumber(), androidStepHandler);
} catch (Exception e) {
log.error(e.getMessage());
result.put("status", "error");
Expand All @@ -428,14 +428,14 @@ private void exit(Session session) {
AndroidDeviceLocalStatus.finish(session.getUserProperties().get("udId") + "");
IDevice iDevice = udIdMap.get(session);
try {
AndroidStepHandler androidStepHandler = HandlerMap.getAndroidMap().get(session.getUserProperties().get("id").toString());
AndroidStepHandler androidStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (androidStepHandler != null) {
androidStepHandler.closeAndroidDriver();
}
} catch (Exception e) {
log.info("close driver failed.");
} finally {
HandlerMap.getAndroidMap().remove(session.getUserProperties().get("id").toString());
HandlerMap.getAndroidMap().remove(iDevice.getSerialNumber());
}
if (iDevice != null) {
AndroidDeviceBridgeTool.clearProxy(iDevice);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/cloud/sonic/agent/websockets/IOSWSServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void onOpen(Session session, @PathParam("key") String secretKey,
session.getUserProperties().put("schedule", ScheduleTool.schedule(() -> {
log.info("time up!");
if (session.isOpen()) {
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(session.getUserProperties().get("id").toString());
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(udId);
if (iosStepHandler != null) {
try {
iosStepHandler.getIOSDriver().pressButton("home");
Expand Down Expand Up @@ -149,7 +149,7 @@ public void onOpen(Session session, @PathParam("key") String secretKey,
appiumSettings.put("mjpegScalingFactor", 100);
appiumSettings.put("mjpegServerScreenshotQuality", 50);
iosStepHandler.appiumSettings(appiumSettings);
HandlerMap.getIOSMap().put(session.getUserProperties().get("id").toString(), iosStepHandler);
HandlerMap.getIOSMap().put(udId, iosStepHandler);
} catch (Exception e) {
log.error(e.getMessage());
result.put("status", "error");
Expand Down Expand Up @@ -190,7 +190,7 @@ public void onMessage(String message, Session session) {
String udId = udIdMap.get(session);
IOSDeviceThreadPool.cachedThreadPool.execute(() -> {
IOSDriver iosDriver = null;
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(session.getUserProperties().get("id").toString());
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(udId);
if (iosStepHandler != null && iosStepHandler.getIOSDriver() != null) {
iosDriver = iosStepHandler.getIOSDriver();
}
Expand Down Expand Up @@ -465,14 +465,14 @@ private void exit(Session session) {
screenMap.remove(udId);
SibTool.stopOrientationWatcher(udId);
try {
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(session.getUserProperties().get("id").toString());
IOSStepHandler iosStepHandler = HandlerMap.getIOSMap().get(udId);
if (iosStepHandler != null) {
iosStepHandler.closeIOSDriver();
}
} catch (Exception e) {
log.info("close driver failed.");
} finally {
HandlerMap.getIOSMap().remove(session.getUserProperties().get("id").toString());
HandlerMap.getIOSMap().remove(udId);
}
SibTool.stopWebInspector(udId);
SibTool.stopPerfmon(udId);
Expand Down

0 comments on commit 6cacc32

Please sign in to comment.