Skip to content

Commit 7c1542e

Browse files
committed
Added sdk instance to Devices, so runADB() does not need to be static
1 parent 21434b5 commit 7c1542e

File tree

8 files changed

+58
-41
lines changed

8 files changed

+58
-41
lines changed

src/processing/mode/android/AndroidEditor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ private Device selectFirstDevice(java.util.List<Device> deviceList) {
105105
@Override
106106
public void run() {
107107
if (androidMode == null || androidMode.getSDK() == null) return;
108+
109+
final Devices devices = Devices.getInstance();
108110

109111
if (appComponent == AndroidBuild.WATCHFACE) {
110-
Devices.enableBluetoothDebugging();
112+
devices.enableBluetoothDebugging();
111113
}
112-
113-
final Devices devices = Devices.getInstance();
114+
114115
java.util.List<Device> deviceList = devices.findMultiple(false);
115116
Device selectedDevice = devices.getSelectedDevice();
116117

@@ -633,7 +634,8 @@ public void actionPerformed(ActionEvent e) {
633634
item.addActionListener(new ActionListener() {
634635
public void actionPerformed(ActionEvent e) {
635636
// editor.statusNotice("Resetting the Android Debug Bridge server.");
636-
Devices.killAdbServer();
637+
final Devices devices = Devices.getInstance();
638+
devices.killAdbServer();
637639
}
638640
});
639641
androidMenu.add(item);

src/processing/mode/android/AndroidMode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ public void checkSDK(Editor editor) {
219219
Messages.showWarning("Bad news...",
220220
"The Android SDK could not be loaded.\n" +
221221
"The Android Mode will be disabled.", tr);
222+
} else {
223+
Devices devices = Devices.getInstance();
224+
devices.setSDK(sdk);
222225
}
223226
checkingSDK = false;
224227
}
@@ -282,7 +285,7 @@ public void handleRunEmulator(Sketch sketch, AndroidEditor editor,
282285
}
283286

284287
int comp = build.getAppComponent();
285-
Future<Device> emu = Devices.getInstance().getEmulator(sdk.getToolsFolder(), build.isWear());
288+
Future<Device> emu = Devices.getInstance().getEmulator(build.isWear());
286289
runner = new AndroidRunner(build, listener);
287290
runner.launch(emu, comp, true);
288291
}

src/processing/mode/android/AndroidRunner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public boolean launch(Future<Device> deviceFuture, int comp, boolean emu) {
6969
listener.statusError("Lost connection with " + devStr + " while launching. Try again.");
7070
// Reset the server, in case that's the problem. Sometimes when
7171
// launching the emulator times out, the device list refuses to update.
72-
Devices.killAdbServer();
72+
final Devices devices = Devices.getInstance();
73+
devices.killAdbServer();
7374
return false;
7475
}
7576

@@ -96,7 +97,8 @@ public boolean launch(Future<Device> deviceFuture, int comp, boolean emu) {
9697
// this stopped working with Android SDK tools revision 17
9798
if (!device.installApp(build, listener)) {
9899
listener.statusError("Lost connection with " + devStr + " while installing. Try again.");
99-
Devices.killAdbServer(); // see above
100+
final Devices devices = Devices.getInstance();
101+
devices.killAdbServer(); // see above
100102
return false;
101103
}
102104

src/processing/mode/android/AndroidSDK.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,23 +688,25 @@ static public File selectFolder(String prompt, File folder, Frame frame) {
688688
private static final String ADB_DAEMON_MSG_1 = "daemon not running";
689689
private static final String ADB_DAEMON_MSG_2 = "daemon started successfully";
690690

691-
public static ProcessResult runADB(final String... cmd)
691+
public ProcessResult runADB(final String... cmd)
692692
throws InterruptedException, IOException {
693693

694694
if (adbDisabled) {
695695
throw new IOException("adb is currently disabled");
696696
}
697697

698698
final String[] adbCmd;
699-
if (!cmd[0].equals("adb")) {
700-
adbCmd = PApplet.splice(cmd, "adb", 0);
699+
if (!cmd[0].contains("adb")) {
700+
File abdPath = Platform.isWindows() ? new File(platformTools, "adb.exe") :
701+
new File(platformTools, "adb");
702+
adbCmd = PApplet.splice(cmd, abdPath.getCanonicalPath(), 0);
701703
} else {
702704
adbCmd = cmd;
703705
}
704706
// printing this here to see if anyone else is killing the adb server
705-
if (processing.app.Base.DEBUG) {
707+
// if (processing.app.Base.DEBUG) {
706708
PApplet.printArray(adbCmd);
707-
}
709+
// }
708710
try {
709711
ProcessResult adbResult = new ProcessHelper(adbCmd).execute();
710712
// Ignore messages about starting up an adb daemon

src/processing/mode/android/Commander.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void execute() {
239239
if (task == RUN) {
240240
AndroidRunner runner = new AndroidRunner(build, this);
241241
runner.launch(runOnEmu ?
242-
Devices.getInstance().getEmulator(androidMode.getSDK().getToolsFolder(), build.isWear()) :
242+
Devices.getInstance().getEmulator(build.isWear()) :
243243
Devices.getInstance().getHardware(), build.getAppComponent(), runOnEmu);
244244
}
245245

src/processing/mode/android/Device.java

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

2424
import processing.app.Base;
25+
import processing.app.Platform;
2526
import processing.app.RunnerListener;
2627
import processing.app.exec.LineProcessor;
2728
import processing.app.exec.ProcessRegistry;
@@ -30,6 +31,7 @@
3031
import processing.core.PApplet;
3132
import processing.mode.android.LogEntry.Severity;
3233

34+
import java.io.File;
3335
import java.io.IOException;
3436
import java.util.*;
3537
import java.util.regex.Matcher;
@@ -86,12 +88,12 @@ public String getName() {
8688
String name = "";
8789

8890
try {
89-
ProcessResult result = AndroidSDK.runADB("-s", id, "shell", "getprop", "ro.product.brand");
91+
ProcessResult result = env.getSDK().runADB("-s", id, "shell", "getprop", "ro.product.brand");
9092
if (result.succeeded()) {
9193
name += result.getStdout() + " ";
9294
}
9395

94-
result = AndroidSDK.runADB("-s", id, "shell", "getprop", "ro.product.model");
96+
result = env.getSDK().runADB("-s", id, "shell", "getprop", "ro.product.model");
9597
if (result.succeeded()) {
9698
name += result.getStdout();
9799
}
@@ -407,17 +409,14 @@ public void removeListener(final DeviceListener listener) {
407409

408410
private ProcessResult adb(final String... cmd) throws InterruptedException, IOException {
409411
final String[] adbCmd = generateAdbCommand(cmd);
410-
return AndroidSDK.runADB(adbCmd);
412+
return env.getSDK().runADB(adbCmd);
411413
}
412414

413-
private String[] generateAdbCommand(final String... cmd) {
414-
// final String[] adbCmd = new String[3 + cmd.length];
415-
// adbCmd[0] = "adb";
416-
// adbCmd[1] = "-s";
417-
// adbCmd[2] = getId();
418-
// System.arraycopy(cmd, 0, adbCmd, 3, cmd.length);
419-
// return adbCmd;
420-
return PApplet.concat(new String[] { "adb", "-s", getId() }, cmd);
415+
private String[] generateAdbCommand(final String... cmd) throws IOException {
416+
File toolsPath = env.getSDK().getPlatformToolsFolder();
417+
File abdPath = Platform.isWindows() ? new File(toolsPath, "adb.exe") :
418+
new File(toolsPath, "adb");
419+
return PApplet.concat(new String[] { abdPath.getCanonicalPath(), "-s", getId() }, cmd);
421420
}
422421

423422
@Override

src/processing/mode/android/Devices.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import processing.app.exec.ProcessResult;
2525
import processing.mode.android.EmulatorController.State;
2626

27-
import java.io.File;
2827
import java.io.IOException;
2928
import java.util.ArrayList;
3029
import java.util.Collections;
@@ -50,6 +49,8 @@ class Devices {
5049

5150
private static final String BT_DEBUG_PORT = "4444";
5251

52+
private AndroidSDK sdk;
53+
5354
private Device selectedDevice;
5455

5556
public static Devices getInstance() {
@@ -61,6 +62,14 @@ public static Devices getInstance() {
6162
private final ExecutorService deviceLaunchThread =
6263
Executors.newSingleThreadExecutor();
6364

65+
public void setSDK(AndroidSDK sdk) {
66+
this.sdk = sdk;
67+
}
68+
69+
public AndroidSDK getSDK() {
70+
return sdk;
71+
}
72+
6473
public Device getSelectedDevice() {
6574
return selectedDevice;
6675
}
@@ -69,18 +78,18 @@ public void setSelectedDevice(Device selectedDevice) {
6978
this.selectedDevice = selectedDevice;
7079
}
7180

72-
public static void killAdbServer() {
81+
public void killAdbServer() {
7382
System.out.println("Shutting down any existing adb server...");
7483
System.out.flush();
7584
try {
76-
AndroidSDK.runADB("kill-server");
85+
sdk.runADB("kill-server");
7786
} catch (final Exception e) {
7887
System.err.println("Devices.killAdbServer() failed.");
7988
e.printStackTrace();
8089
}
8190
}
8291

83-
public static void enableBluetoothDebugging() {
92+
public void enableBluetoothDebugging() {
8493
final Devices devices = Devices.getInstance();
8594
java.util.List<Device> deviceList = devices.findMultiple(false);
8695

@@ -95,8 +104,8 @@ public static void enableBluetoothDebugging() {
95104
try {
96105
// Try Enable debugging over bluetooth
97106
// http://developer.android.com/training/wearables/apps/bt-debugging.html
98-
AndroidSDK.runADB("-s", device.getId(), "forward", "tcp:" + BT_DEBUG_PORT, "localabstract:/adb-hub");
99-
AndroidSDK.runADB("connect", "127.0.0.1:" + BT_DEBUG_PORT);
107+
sdk.runADB("-s", device.getId(), "forward", "tcp:" + BT_DEBUG_PORT, "localabstract:/adb-hub");
108+
sdk.runADB("connect", "127.0.0.1:" + BT_DEBUG_PORT);
100109
} catch (final Exception e) {
101110
e.printStackTrace();
102111
}
@@ -123,10 +132,10 @@ public void run() {
123132
}
124133

125134

126-
public Future<Device> getEmulator(final File sdkToolsPath, final boolean wear) {
135+
public Future<Device> getEmulator(final boolean wear) {
127136
final Callable<Device> androidFinder = new Callable<Device>() {
128137
public Device call() throws Exception {
129-
return blockingGetEmulator(sdkToolsPath, wear);
138+
return blockingGetEmulator( wear);
130139
}
131140
};
132141
final FutureTask<Device> task = new FutureTask<Device>(androidFinder);
@@ -135,7 +144,7 @@ public Device call() throws Exception {
135144
}
136145

137146

138-
private final Device blockingGetEmulator(final File sdkToolsPath, final boolean wear) {
147+
private final Device blockingGetEmulator(final boolean wear) {
139148
String port = AVD.getPreferredPort(wear);
140149
Device emu = find(true, port);
141150
if (emu != null) {
@@ -151,7 +160,7 @@ private final Device blockingGetEmulator(final File sdkToolsPath, final boolean
151160

152161
if (emuController.getState() == State.NOT_RUNNING) {
153162
try {
154-
emuController.launch(sdkToolsPath, wear); // this blocks until emulator boots
163+
emuController.launch(sdk, wear); // this blocks until emulator boots
155164
} catch (final IOException e) {
156165
System.err.println("Problem while launching emulator.");
157166
e.printStackTrace(System.err);
@@ -309,15 +318,15 @@ void deviceRemoved(final Device device) {
309318
* @return list of device identifiers
310319
* @throws IOException
311320
*/
312-
public static List<String> list() {
321+
public List<String> list() {
313322
if (AndroidSDK.adbDisabled) {
314323
return Collections.emptyList();
315324
}
316325

317326
ProcessResult result;
318327
try {
319328
// System.out.println("listing devices 00");
320-
result = AndroidSDK.runADB("devices");
329+
result = sdk.runADB("devices");
321330
// System.out.println("listing devices 05");
322331
} catch (InterruptedException e) {
323332
return Collections.emptyList();

src/processing/mode/android/EmulatorController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setState(final State state) {
5858
* Blocks until emulator is running, or some catastrophe happens.
5959
* @throws IOException
6060
*/
61-
synchronized public void launch(File sdkToolsPath, boolean wear)
61+
synchronized public void launch(final AndroidSDK sdk, final boolean wear)
6262
throws IOException {
6363
if (state != State.NOT_RUNNING) {
6464
String illegal = "You can't launch an emulator whose state is " + state;
@@ -75,8 +75,8 @@ synchronized public void launch(File sdkToolsPath, boolean wear)
7575
// https://developer.android.com/studio/run/emulator-acceleration.html#accel-graphics
7676
String gpuFlag = "auto";
7777

78-
File emulatorPath = Platform.isWindows() ? new File(sdkToolsPath, "emulator.exe") :
79-
new File(sdkToolsPath, "emulator");
78+
File emulatorPath = Platform.isWindows() ? new File(sdk.getToolsFolder(), "emulator.exe") :
79+
new File(sdk.getToolsFolder(), "emulator");
8080
final String[] cmd = new String[] {
8181
emulatorPath.getCanonicalPath(),
8282
"-avd", avdName,
@@ -143,8 +143,8 @@ public void run() {
143143
}
144144
Thread.sleep(2000);
145145
//System.out.println("done sleeping");
146-
ProcessResult result = AndroidSDK.runADB("-s", "emulator-" + portString,
147-
"shell", "getprop", "dev.bootcomplete");
146+
ProcessResult result = sdk.runADB("-s", "emulator-" + portString,
147+
"shell", "getprop", "dev.bootcomplete");
148148
if (result.getStdout().equals("1\n")) {
149149
setState(State.RUNNING);
150150
return;

0 commit comments

Comments
 (0)