|
| 1 | +package com.yeetor.engine; |
| 2 | + |
| 3 | +import com.android.ddmlib.IDevice; |
| 4 | +import com.google.common.util.concurrent.ListenableFuture; |
| 5 | +import com.google.common.util.concurrent.SettableFuture; |
| 6 | +import com.sun.org.apache.xpath.internal.operations.Bool; |
| 7 | +import com.yeetor.adb.AdbServer; |
| 8 | +import com.yeetor.minitouch.Minitouch; |
| 9 | +import com.yeetor.minitouch.MinitouchListener; |
| 10 | + |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | + |
| 13 | +/** |
| 14 | + * Created by harry on 2017/5/11. |
| 15 | + */ |
| 16 | +public class EngineDevice { |
| 17 | + |
| 18 | + private IDevice iDevice; |
| 19 | + private Minitouch minitouch; |
| 20 | + private boolean minitouchOpen = false; |
| 21 | + |
| 22 | + public static EngineDevice getDevice(String serialNumber) { |
| 23 | + IDevice iDevice = AdbServer.server().getDevice(serialNumber); |
| 24 | + if (iDevice != null) { |
| 25 | + EngineDevice device = new EngineDevice(iDevice); |
| 26 | + if (device.isMinitouchOpen()) { |
| 27 | + return device; |
| 28 | + } |
| 29 | + } |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + public EngineDevice(IDevice iDevice) { |
| 34 | + this.iDevice = iDevice; |
| 35 | + minitouch = new Minitouch(iDevice); |
| 36 | + SettableFuture future = SettableFuture.create(); |
| 37 | + |
| 38 | + minitouch.addEventListener(new MinitouchListener() { |
| 39 | + @Override |
| 40 | + public void onStartup(Minitouch minitouch, boolean success) { |
| 41 | + future.set(success); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onClose(Minitouch minitouch) { |
| 46 | + minitouchOpen = false; |
| 47 | + } |
| 48 | + }); |
| 49 | + minitouch.start(); |
| 50 | + |
| 51 | + // 等待,指导Minitouch启动完毕 |
| 52 | + try { |
| 53 | + Boolean success = (Boolean)future.get(); |
| 54 | + if (success) { |
| 55 | + minitouchOpen = true; |
| 56 | + } |
| 57 | + } catch (InterruptedException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } catch (ExecutionException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected boolean isMinitouchOpen() { |
| 65 | + return minitouchOpen; |
| 66 | + } |
| 67 | + |
| 68 | + public void touchDown(int x, int y) { |
| 69 | + minitouch.sendEvent("d 0 " + x + " " + y + " 50\nc\n"); |
| 70 | + } |
| 71 | + |
| 72 | + public void touchMove(int x, int y) { |
| 73 | + minitouch.sendEvent("m 0 " + x + " " + y + " 50\nc\n"); |
| 74 | + } |
| 75 | + |
| 76 | + public void touchUp() { |
| 77 | + minitouch.sendEvent("u 0\nc\n"); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + public String executeShellAndGetString(String command) { |
| 82 | + return AdbServer.executeShellCommand(iDevice, command); |
| 83 | + } |
| 84 | + |
| 85 | + public void startApp(String str) { |
| 86 | + AdbServer.executeShellCommand(iDevice, "am start " + str); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments