Skip to content

Commit

Permalink
Merge pull request #49 from ZhouYixun/dev
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
ZhouYixun authored Nov 3, 2021
2 parents 8a257a2 + fc3f7b3 commit 8072e84
Show file tree
Hide file tree
Showing 18 changed files with 635 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
/test-output/
/target/
/.gradle/
/logs/
*.iml
*/.DS_Store
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</p>
<p align="center">
<a href="#">
<img src="https://img.shields.io/badge/release-v1.0.0-orange">
<img src="https://img.shields.io/badge/release-v1.1.0-orange">
</a>
<a href="#">
<img src="https://img.shields.io/badge/platform-windows|macosx|linux-success">
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</p>
<p align="center">
<a href="#">
<img src="https://img.shields.io/badge/release-v1.0.0-orange">
<img src="https://img.shields.io/badge/release-v1.1.0-orange">
</a>
<a href="#">
<img src="https://img.shields.io/badge/platform-windows|macosx|linux-success">
Expand Down
Binary file added plugins/yadb
Binary file not shown.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
</parent>

<dependencies>
<!-- 重定向 -->
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.12</version>
</dependency>
<!-- websocket client -->
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.2</version>
</dependency>
<!-- 监控模块 刷新配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/com/sonic/agent/automation/AndroidStepHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.sonic.agent.tools.DownImageTool;
import com.sonic.agent.tools.LogTool;
import com.sonic.agent.interfaces.PlatformType;
import com.sonic.agent.tools.PortTool;
import com.sonic.agent.tools.UploadTools;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MultiTouchAction;
Expand Down Expand Up @@ -128,11 +129,9 @@ public void startAndroidDriver(String udId) throws InterruptedException {
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
//指定设备序列号
desiredCapabilities.setCapability(MobileCapabilityType.UDID, udId);
//过滤logcat,注意!请将MyCrashTag替换为自己安卓崩溃时的tag,需要跟开发配合约定,有多个的话可以add()多几个
List<String> logcatFilter = new ArrayList<>();
logcatFilter.add("MyCrashTag:W");
logcatFilter.add("*:S");
desiredCapabilities.setCapability("logcatFilterSpecs", logcatFilter);
//随机systemPort
desiredCapabilities.setCapability(AndroidMobileCapabilityType.SYSTEM_PORT, PortTool.getPort());
desiredCapabilities.setCapability("skipLogcatCapture", true);
try {
androidDriver = new AndroidDriver(AppiumServer.service.getUrl(), desiredCapabilities);
log.sendStepLog(StepType.PASS, "连接设备驱动成功", "");
Expand Down Expand Up @@ -442,7 +441,7 @@ public void settingSonicPlugins(IDevice iDevice) {
}
}

public void install(HandleDes handleDes, String path, String packName) {
public void install(HandleDes handleDes, String path) {
handleDes.setStepDes("安装应用");
handleDes.setDetail("App安装路径: " + path);
IDevice iDevice = AndroidDeviceBridgeTool.getIDeviceByUdId(log.udId);
Expand All @@ -466,7 +465,9 @@ public void install(HandleDes handleDes, String path, String packName) {
//单独适配一下oppo
if (manufacturer.equals("OPPO")) {
try {
androidDriver.installApp(path, new AndroidInstallApplicationOptions().withGrantPermissionsEnabled().withTimeout(Duration.ofMillis(60000)));
androidDriver.installApp(path, new AndroidInstallApplicationOptions()
.withAllowTestPackagesEnabled().withReplaceEnabled()
.withGrantPermissionsEnabled().withTimeout(Duration.ofMillis(60000)));
} catch (Exception e) {
}
//单独再适配colorOs
Expand Down Expand Up @@ -536,17 +537,14 @@ public void install(HandleDes handleDes, String path, String packName) {
}
} else {
try {
androidDriver.installApp(path, new AndroidInstallApplicationOptions().withGrantPermissionsEnabled().withTimeout(Duration.ofMillis(60000)));
androidDriver.installApp(path, new AndroidInstallApplicationOptions()
.withAllowTestPackagesEnabled().withReplaceEnabled()
.withGrantPermissionsEnabled().withTimeout(Duration.ofMillis(60000)));
} catch (Exception e) {
handleDes.setE(e);
return;
}
}
try {
androidDriver.activateApp(packName);
} catch (Exception e) {
handleDes.setE(e);
}
}

public void uninstall(HandleDes handleDes, String appPackage) {
Expand Down Expand Up @@ -1511,7 +1509,7 @@ public void runStep(JSONObject stepJSON) throws Throwable {
terminate(handleDes, step.getString("text"));
break;
case "install":
install(handleDes, step.getString("text"), step.getString("content"));
install(handleDes, step.getString("text"));
break;
case "uninstall":
uninstall(handleDes, step.getString("text"));
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/sonic/agent/automation/RemoteDebugDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.sonic.agent.automation;

import com.google.common.collect.ImmutableMap;
import com.sonic.agent.tools.PortTool;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.PostConstruct;
import javax.servlet.Servlet;
import java.util.Map;

/**
* @author ZhouYiXun
* @des
* @date 2021/10/29 0:28
*/
@Configuration
public class RemoteDebugDriver {
private static String chromePath;
public static int port = 0;
public static WebDriver webDriver;
@Value("${sonic.chrome.path}")
private String path;

@Bean
public void setChromePath() {
chromePath = path;
}

@Bean
public Servlet baiduProxyServlet() {
return new ProxyServlet();
}

@Bean
@DependsOn(value = "startChromeDriver")
public ServletRegistrationBean proxyServletRegistration() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(baiduProxyServlet(), "/agent/*");
Map<String, String> params = ImmutableMap.of(
"targetUri", "http://localhost:" + port + "/devtools",
"log", "false");
registrationBean.setInitParameters(params);
return registrationBean;
}

@Bean
@DependsOn(value = "setChromePath")
public static void startChromeDriver() {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
ChromeOptions chromeOptions = new ChromeOptions();
System.setProperty("webdriver.chrome.driver", chromePath);
if (port == 0) {
int debugPort = PortTool.getPort();
port = debugPort;
chromeOptions.addArguments("--remote-debugging-port=" + debugPort);
} else {
chromeOptions.addArguments("--remote-debugging-port=" + port);
}
chromeOptions.addArguments("--headless");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
webDriver = new ChromeDriver(desiredCapabilities);
}

public static void close() {
if (webDriver != null) {
webDriver.quit();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.sonic.agent.bridge.android;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.CollectingOutputReceiver;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.IShellOutputReceiver;
import com.android.ddmlib.*;
import com.sonic.agent.exception.SonicException;
import com.sonic.agent.tools.DownImageTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
Expand All @@ -12,6 +11,7 @@
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author ZhouYiXun
Expand Down Expand Up @@ -164,8 +164,8 @@ public static String executeCommand(IDevice iDevice, String command) {
try {
iDevice.executeShellCommand(command, output, 0, TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.info(String.format("发送shell指令 {} 给设备 {} 异常!"
, command, iDevice.getSerialNumber()));
logger.info("发送shell指令 {} 给设备 {} 异常!"
, command, iDevice.getSerialNumber());
logger.error(e.getMessage());
}
return output.getOutput();
Expand Down Expand Up @@ -328,6 +328,43 @@ public static String matchMiniTouchFile(String sdk) {
return filePath;
}

public static void pushYadb(IDevice iDevice) {
String yadbFile = executeCommand(iDevice, " ls /data/local/tmp | grep yadb");
if (yadbFile != null && yadbFile.contains("yadb")) {
return;
} else {
File yadbLocalFile = new File("plugins" + File.separator + "yadb");
pushLocalFile(iDevice, yadbLocalFile.getPath(), "/data/local/tmp/yadb");
boolean yadbFileExist = false;
//轮训目录,直到推送文件结束
while (!yadbFileExist) {
String yadbDeviceFile = executeCommand(iDevice, " ls /data/local/tmp | grep yadb");
if (yadbDeviceFile != null && yadbDeviceFile.contains("yadb")) {
yadbFileExist = true;
}
}
executeCommand(iDevice, "chmod 777 /data/local/tmp/yadb");
}
}

public static void pushToCamera(IDevice iDevice, String url) {
try {
File image = DownImageTool.download(url);
pushLocalFile(iDevice, image.getPath(), "/sdcard/DCIM/Camera/" + image.getName());
boolean fileExist = false;
//轮训目录,直到推送文件结束
while (!fileExist) {
String files = executeCommand(iDevice, " ls /sdcard/DCIM/Camera | grep " + image.getName());
if (files != null && files.contains(image.getName())) {
fileExist = true;
}
}
executeCommand(iDevice, "am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/DCIM/Camera/" + image.getName());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @param iDevice
* @param quality
Expand All @@ -336,7 +373,7 @@ public static String matchMiniTouchFile(String sdk) {
* @des 开启miniCap服务
* @date 2021/8/16 20:04
*/
public static void startMiniCapServer(IDevice iDevice, int quality, int screen) {
public static void startMiniCapServer(IDevice iDevice, int quality, int screen) throws SonicException {
//先删除原有路径下的文件,防止上次出错后停止,再次打开会报错的情况
executeCommand(iDevice, "rm -rf /data/local/tmp/minicap*");
//获取cpu信息
Expand All @@ -362,13 +399,19 @@ public static void startMiniCapServer(IDevice iDevice, int quality, int screen)
//给文件权限
executeCommand(iDevice, "chmod 777 /data/local/tmp/" + miniCapFileName);
String size = getScreenSize(iDevice);
AtomicBoolean isSupport = new AtomicBoolean(true);
try {
//开始启动
iDevice.executeShellCommand(String.format("LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/%s -Q " + quality + " -P %s@%s/%d", miniCapFileName, size, size, screen), new IShellOutputReceiver() {
@Override
public void addOutput(byte[] bytes, int i, int i1) {
String res = new String(bytes, i, i1);
logger.info(res);
if (res.contains("Vector<> have different types")) {
isSupport.set(false);
logger.info(iDevice.getSerialNumber() + "设备不兼容投屏!");
return;
}
}

@Override
Expand All @@ -385,6 +428,9 @@ public boolean isCancelled() {
, iDevice.getSerialNumber());
logger.error(e.getMessage());
}
if (!isSupport.get()) {
throw new SonicException("该设备不兼容投屏!");
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/sonic/agent/exception/SonicException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sonic.agent.exception;

/**
* @author ZhouYiXun
* @des
* @date 2021/10/10 11:57
*/
public class SonicException extends Exception {
public SonicException(String message) {
super(message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/sonic/agent/receiver/MsgReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void process(JSONObject jsonObject, Channel channel, Message message) {
AndroidDeviceThreadPool.cachedThreadPool.execute(() -> {
switch (jsonObject.getString("msg")) {
case "auth":
logger.info("当前apex-agent版本为:" + version);
logger.info("当前sonic-agent版本为:" + version);
AgentTool.agentId = jsonObject.getInteger("id");
RabbitMQThread.isPass = true;
JSONObject agentInfo = new JSONObject();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/sonic/agent/tests/AndroidTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void run(Channel channel, long tag, JSONObject jsonObject) throws IOExcep
}
} else {
MiniCapTool miniCapTool = new MiniCapTool();
miniCapPro = miniCapTool.start(udId, banner, imgList, "high", null);
miniCapPro = miniCapTool.start(udId, banner, imgList, "high", -1, null);
}
//两分钟录一次
try {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/sonic/agent/tools/LaunchTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.alibaba.fastjson.JSONObject;
import com.sonic.agent.automation.AppiumServer;
import com.sonic.agent.automation.RemoteDebugDriver;
import com.sonic.agent.rabbitmq.RabbitMQThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -38,6 +39,7 @@ public void destroy() throws InterruptedException {
JSONObject agentOffLine = new JSONObject();
agentOffLine.put("msg", "offLine");
RabbitMQThread.send(agentOffLine);
RemoteDebugDriver.close();
AppiumServer.close();
Thread.sleep(3000);
while (true) {
Expand Down
Loading

0 comments on commit 8072e84

Please sign in to comment.