Skip to content

Commit 17ac016

Browse files
committed
some refactoring of abd calls
1 parent c0a89a9 commit 17ac016

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

processing/mode/src/processing/mode/android/Device.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
package processing.mode.android;
2323

2424
import processing.app.Base;
25-
import processing.app.Platform;
2625
import processing.app.RunnerListener;
2726
import processing.app.exec.LineProcessor;
2827
import processing.app.exec.ProcessRegistry;
@@ -31,7 +30,6 @@
3130
import processing.core.PApplet;
3231
import processing.mode.android.LogEntry.Severity;
3332

34-
import java.io.File;
3533
import java.io.IOException;
3634
import java.util.*;
3735
import java.util.regex.Matcher;
@@ -73,8 +71,8 @@ public Device(final Devices env, final String id) {
7371
public void bringLauncherToFront() {
7472
try {
7573
adb("shell", "am", "start",
76-
"-a", "android.intent.action.MAIN",
77-
"-c", "android.intent.category.HOME");
74+
"-a", "android.intent.action.MAIN",
75+
"-c", "android.intent.category.HOME");
7876
} catch (final Exception e) {
7977
e.printStackTrace(System.err);
8078
}
@@ -88,12 +86,12 @@ public String getName() {
8886
String name = "";
8987

9088
try {
91-
ProcessResult result = env.getSDK().runADB("-s", id, "shell", "getprop", "ro.product.brand");
89+
ProcessResult result = adb("shell", "getprop", "ro.product.brand");
9290
if (result.succeeded()) {
9391
name += result.getStdout() + " ";
9492
}
9593

96-
result = env.getSDK().runADB("-s", id, "shell", "getprop", "ro.product.model");
94+
result = adb("shell", "getprop", "ro.product.model");
9795
if (result.succeeded()) {
9896
name += result.getStdout();
9997
}
@@ -208,7 +206,6 @@ public boolean launchApp(final String packageName, boolean isDebuggerEnabled)
208206
};
209207
pr = adb(cmd);
210208
}
211-
// PApplet.println(cmd);
212209

213210
if (Base.DEBUG) {
214211
System.out.println(pr.toString());
@@ -226,8 +223,9 @@ public boolean launchApp(final String packageName, boolean isDebuggerEnabled)
226223
public void forwardPort(int tcpPort) throws IOException, InterruptedException {
227224
// Start ADB Server
228225
adb("start-server");
229-
final String[] jdwpcmd = generateAdbCommand("jdwp");
230-
Process deviceId = Runtime.getRuntime().exec(jdwpcmd);
226+
227+
Process deviceId = adbProc("jdwp");
228+
231229
// Get Process ID from ADB command `adb jdwp`
232230
JDWPProcessor pIDProcessor = new JDWPProcessor();
233231
new StreamPump(deviceId.getInputStream(), "jdwp: ").addTarget(
@@ -236,7 +234,8 @@ public void forwardPort(int tcpPort) throws IOException, InterruptedException {
236234
System.err).start();
237235

238236
Thread.sleep(1000);
239-
// forward to tcp port
237+
238+
// Forward to tcp port
240239
adb("forward", "tcp:" + tcpPort, "jdwp:" + pIDProcessor.getId());
241240
}
242241

@@ -389,9 +388,11 @@ private void reportStackTrace(final LogEntry entry) {
389388

390389
void initialize() throws IOException, InterruptedException {
391390
adb("logcat", "-c");
392-
final String[] cmd = generateAdbCommand("logcat", "-v", "brief");
391+
392+
final String[] cmd = {"-s", id, "logcat", "-v", "brief"};
393393
final String title = PApplet.join(cmd, ' ');
394-
logcat = Runtime.getRuntime().exec(cmd);
394+
logcat = adbProc(cmd);
395+
395396
ProcessRegistry.watch(logcat);
396397
new StreamPump(logcat.getInputStream(), "log: " + title).addTarget(
397398
new LogLineProcessor()).start();
@@ -465,15 +466,17 @@ public void removeListener(final DeviceListener listener) {
465466
}
466467

467468
private ProcessResult adb(final String... cmd) throws InterruptedException, IOException {
468-
final String[] adbCmd = generateAdbCommand(cmd);
469-
return env.getSDK().runADB(adbCmd);
469+
final String[] adbCmd = genAdbCommand(cmd);
470+
return env.getSDK().runAdb(adbCmd);
471+
}
472+
473+
private Process adbProc(final String... cmd) throws IOException {
474+
final String[] adbCmd = genAdbCommand(cmd);
475+
return env.getSDK().getAdbProcess(adbCmd);
470476
}
471477

472-
private String[] generateAdbCommand(final String... cmd) throws IOException {
473-
File toolsPath = env.getSDK().getPlatformToolsFolder();
474-
File abdPath = Platform.isWindows() ? new File(toolsPath, "adb.exe") :
475-
new File(toolsPath, "adb");
476-
return PApplet.concat(new String[] { abdPath.getCanonicalPath(), "-s", getId() }, cmd);
478+
private String[] genAdbCommand(final String... cmd) {
479+
return PApplet.concat(new String[] { "-s", getId() }, cmd);
477480
}
478481

479482
@Override

processing/mode/src/processing/mode/android/Devices.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void killAdbServer() {
8383
System.out.print("Shutting down any existing adb server...");
8484
System.out.flush();
8585
try {
86-
sdk.runADB("kill-server");
86+
sdk.runAdb("kill-server");
8787
System.out.println(" Done.");
8888
} catch (final Exception e) {
8989
System.err.println("/nDevices.killAdbServer() failed.");
@@ -95,7 +95,7 @@ public void startAdbServer() {
9595
System.out.print("Starting a new adb server...");
9696
System.out.flush();
9797
try {
98-
sdk.runADB("start-server");
98+
sdk.runAdb("start-server");
9999
System.out.println(" Done.");
100100
} catch (final Exception e) {
101101
System.err.println("/nDevices.startAdbServer() failed.");
@@ -118,9 +118,9 @@ public void enableBluetoothDebugging() {
118118
try {
119119
// Try Enable debugging over bluetooth
120120
// http://developer.android.com/training/wearables/apps/bt-debugging.html
121-
sdk.runADB("-s", device.getId(), "forward", "tcp:" + BT_DEBUG_PORT,
121+
sdk.runAdb("-s", device.getId(), "forward", "tcp:" + BT_DEBUG_PORT,
122122
"localabstract:/adb-hub");
123-
sdk.runADB("connect", "127.0.0.1:" + BT_DEBUG_PORT);
123+
sdk.runAdb("connect", "127.0.0.1:" + BT_DEBUG_PORT);
124124
} catch (final Exception e) {
125125
e.printStackTrace();
126126
}
@@ -349,7 +349,7 @@ public List<String> list() {
349349
ProcessResult result;
350350
try {
351351
// System.out.println("listing devices 00");
352-
result = sdk.runADB("devices");
352+
result = sdk.runAdb("devices");
353353
// System.out.println("listing devices 05");
354354
} catch (InterruptedException e) {
355355
return Collections.emptyList();

0 commit comments

Comments
 (0)