-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdbBridge.java
More file actions
80 lines (68 loc) · 2.29 KB
/
AdbBridge.java
File metadata and controls
80 lines (68 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.libKudos254.vision;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* AdbBridge interfaces to an Android Debug Bridge (adb) binary, which is needed
* to communicate to Android devices over USB.
*
* adb binary provided by https://github.com/Spectrum3847/RIOdroid
*/
public class AdbBridge {
Path bin_location_;
public final static Path DEFAULT_LOCATION = Paths.get("/usr/bin/adb");
public AdbBridge() {
Path adb_location;
String env_val = System.getenv("FRC_ADB_LOCATION");
if (env_val == null || "".equals(env_val)) {
adb_location = DEFAULT_LOCATION;
} else {
adb_location = Paths.get(env_val);
}
bin_location_ = adb_location;
}
public AdbBridge(Path location) {
bin_location_ = location;
}
private boolean runCommand(String args) {
Runtime r = Runtime.getRuntime();
String cmd = bin_location_.toString() + " " + args;
try {
Process p = r.exec(cmd);
p.waitFor();
} catch (IOException e) {
System.err.println("AdbBridge: Could not run command " + cmd);
e.printStackTrace();
return false;
} catch (InterruptedException e) {
System.err.println("AdbBridge: Could not run command " + cmd);
e.printStackTrace();
return false;
}
return true;
}
public void start() {
System.out.println("Starting adb");
runCommand("start");
}
public void stop() {
System.out.println("Stopping adb");
runCommand("kill-server");
}
public void restartAdb() {
System.out.println("Restarting adb");
stop();
start();
}
public void portForward(int local_port, int remote_port) {
runCommand("forward tcp:" + local_port + " tcp:" + remote_port);
}
public void reversePortForward(int remote_port, int local_port) {
runCommand("reverse tcp:" + remote_port + " tcp:" + local_port);
}
public void restartApp() {
System.out.println("Restarting app");
runCommand("shell am force-stop com.team254.cheezdroid \\; "
+ "am start com.team254.cheezdroid/com.team254.cheezdroid.VisionTrackerActivity");
}
}