Skip to content

Commit

Permalink
disconnect connect work
Browse files Browse the repository at this point in the history
  • Loading branch information
ckesc committed Nov 21, 2014
1 parent 3af37b0 commit 0dcadcd
Showing 1 changed file with 36 additions and 2 deletions.
Expand Up @@ -8,9 +8,16 @@ public class MonkeyDeviceConnection implements DeviceConnection {
private ConnectionListener connectionListener;

private IChimpDevice device;
private AdbBackend adbBackend;

@Override
public void sendEventToDevice(int eventType, KeyAction keyAction) {
if (!isAlive()) {
disconnect();
connect();
return;
}

switch (keyAction) {
case UP:
device.press(String.valueOf(eventType), TouchPressType.UP);
Expand All @@ -19,10 +26,20 @@ public void sendEventToDevice(int eventType, KeyAction keyAction) {
device.press(String.valueOf(eventType), TouchPressType.DOWN);
break;
}
if (!isAlive()) {
disconnect();
connect();
}
}

@Override
public void sendTextToDevice(String text, KeyAction keyAction) {
if (!isAlive()) {
disconnect();
connect();
return;
}

switch (keyAction) {
case UP:
device.press(text, TouchPressType.UP);
Expand All @@ -31,6 +48,11 @@ public void sendTextToDevice(String text, KeyAction keyAction) {
device.press(text, TouchPressType.DOWN);
break;
}
if (!isAlive()) {
disconnect();
connect();
}

}

@Override
Expand All @@ -41,8 +63,14 @@ public void setConnectionListener(ConnectionListener connectionListener) {
@Override
public void connect() {
try {
if (adbBackend != null) {
adbBackend.shutdown();
adbBackend = null;
}

// sdk/platform-tools has to be in PATH env variable in order to find adb
device = new AdbBackend().waitForConnection();
adbBackend = new AdbBackend();
device = adbBackend.waitForConnection(3000, ".*");

// Print Device Name
System.out.println("Connected to: " + device.getProperty("build.model"));
Expand All @@ -57,6 +85,12 @@ public void connect() {
@Override
public void disconnect() {
connectionListener.onConnectionLost();
device.dispose();
if (device != null) {
device.dispose();
}
}

public boolean isAlive() {
return device != null && device.getPropertyList() != null;
}
}

0 comments on commit 0dcadcd

Please sign in to comment.