Skip to content

Commit 9723f43

Browse files
committed
handle permission denied to run adb
1 parent 8eaccf1 commit 9723f43

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

src/processing/mode/android/AndroidKeyStore.java

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

2424
import processing.app.Messages;
25+
import processing.app.exec.ProcessHelper;
26+
import processing.app.exec.ProcessResult;
27+
import processing.core.PApplet;
2528

2629
import java.io.File;
2730

@@ -63,10 +66,8 @@ public static void generateKeyStore(String password,
6366
parseDnameField(commonName), parseDnameField(organizationalUnit), parseDnameField(organizationName),
6467
parseDnameField(locality), parseDnameField(state), parseDnameField(country));
6568

66-
String[] args = {
67-
System.getProperty("java.home")
68-
+ System.getProperty("file.separator") + "bin"
69-
+ System.getProperty("file.separator") + "keytool", "-genkey",
69+
ProcessHelper ph = new ProcessHelper(new String[] {
70+
"keytool", "-genkey",
7071
"-keystore", getKeyStoreLocation().getAbsolutePath(),
7172
"-alias", ALIAS_STRING,
7273
"-keyalg", "RSA",
@@ -75,12 +76,45 @@ public static void generateKeyStore(String password,
7576
"-keypass", password,
7677
"-storepass", password,
7778
"-dname", dname
78-
};
79-
80-
Process generation = Runtime.getRuntime().exec(args);
81-
generation.waitFor();
82-
83-
if (getKeyStore() == null) throw new Exception();
79+
});
80+
81+
try {
82+
ProcessResult result = ph.execute();
83+
if (result.succeeded()) {
84+
if (getKeyStore() == null) {
85+
Messages.showWarning("Well, this is unexpected...",
86+
"The keystore was succesfully cretated but cannot be found.\n" +
87+
"Perhaps was it deleted accidentally?");
88+
}
89+
} else {
90+
String[] lines = PApplet.split(result.getStderr(), '\n');
91+
System.err.println("The keystore could not be created, due to the following error:");
92+
for (String line: lines) {
93+
System.err.println(line);
94+
}
95+
}
96+
} catch (Exception e) {
97+
e.printStackTrace();
98+
}
99+
100+
// String[] args = {
101+
// System.getProperty("java.home")
102+
// + System.getProperty("file.separator") + "bin"
103+
// + System.getProperty("file.separator") + "keytool", "-genkey",
104+
// "-keystore", getKeyStoreLocation().getAbsolutePath(),
105+
// "-alias", ALIAS_STRING,
106+
// "-keyalg", "RSA",
107+
// "-keysize", "2048",
108+
// "-validity", "10000",
109+
// "-keypass", password,
110+
// "-storepass", password,
111+
// "-dname", dname
112+
// };
113+
//
114+
// Process generation = Runtime.getRuntime().exec(args);
115+
// generation.waitFor();
116+
117+
// if (getKeyStore() == null) throw new Exception();
84118
}
85119

86120
public static boolean resetKeyStore() {

src/processing/mode/android/AndroidSDK.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package processing.mode.android;
2323

24+
import processing.app.Messages;
2425
import processing.app.Platform;
2526
import processing.app.Preferences;
2627
import processing.app.exec.ProcessHelper;
@@ -429,8 +430,15 @@ static public File selectFolder(String prompt, File folder, Frame frame) {
429430
private static final String ADB_DAEMON_MSG_1 = "daemon not running";
430431
private static final String ADB_DAEMON_MSG_2 = "daemon started successfully";
431432

433+
public static boolean adbDisabled = false;
434+
432435
public static ProcessResult runADB(final String... cmd)
433-
throws InterruptedException, IOException {
436+
throws InterruptedException, IOException {
437+
438+
if (adbDisabled) {
439+
throw new IOException("adb is currently disabled");
440+
}
441+
434442
final String[] adbCmd;
435443
if (!cmd[0].equals("adb")) {
436444
adbCmd = PApplet.splice(cmd, "adb", 0);
@@ -441,29 +449,37 @@ public static ProcessResult runADB(final String... cmd)
441449
if (processing.app.Base.DEBUG) {
442450
PApplet.printArray(adbCmd);
443451
}
444-
// try {
445-
ProcessResult adbResult = new ProcessHelper(adbCmd).execute();
446-
// Ignore messages about starting up an adb daemon
447-
String out = adbResult.getStdout();
448-
if (out.contains(ADB_DAEMON_MSG_1) && out.contains(ADB_DAEMON_MSG_2)) {
449-
StringBuilder sb = new StringBuilder();
450-
for (String line : out.split("\n")) {
451-
if (!out.contains(ADB_DAEMON_MSG_1) &&
452-
!out.contains(ADB_DAEMON_MSG_2)) {
453-
sb.append(line).append("\n");
452+
try {
453+
ProcessResult adbResult = new ProcessHelper(adbCmd).execute();
454+
// Ignore messages about starting up an adb daemon
455+
String out = adbResult.getStdout();
456+
if (out.contains(ADB_DAEMON_MSG_1) && out.contains(ADB_DAEMON_MSG_2)) {
457+
StringBuilder sb = new StringBuilder();
458+
for (String line : out.split("\n")) {
459+
if (!out.contains(ADB_DAEMON_MSG_1) &&
460+
!out.contains(ADB_DAEMON_MSG_2)) {
461+
sb.append(line).append("\n");
462+
}
454463
}
464+
return new ProcessResult(adbResult.getCmd(),
465+
adbResult.getResult(),
466+
sb.toString(),
467+
adbResult.getStderr(),
468+
adbResult.getTime());
469+
}
470+
return adbResult;
471+
} catch (IOException ioe) {
472+
if (-1 < ioe.getMessage().indexOf("Permission denied")) {
473+
Messages.showWarning("Trouble with adb!",
474+
"Could not run the adb tool from the Android SDK.\n" +
475+
"One possibility is that its executable permission\n" +
476+
"is not properly set. You can try setting this\n" +
477+
"permission manually, or re-installing the SDK.\n\n" +
478+
"The mode will be disabled until this problem is fixed.\n");
479+
adbDisabled = true;
455480
}
456-
return new ProcessResult(adbResult.getCmd(),
457-
adbResult.getResult(),
458-
sb.toString(),
459-
adbResult.getStderr(),
460-
adbResult.getTime());
481+
throw ioe;
461482
}
462-
return adbResult;
463-
// } catch (IOException ioe) {
464-
// ioe.printStackTrace();
465-
// throw ioe;
466-
// }
467483
}
468484

469485
static class SDKTarget {

src/processing/mode/android/Devices.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ void deviceRemoved(final Device device) {
286286
* @throws IOException
287287
*/
288288
public static List<String> list() {
289+
if (AndroidSDK.adbDisabled) {
290+
return Collections.emptyList();
291+
}
289292
ProcessResult result;
290293
try {
291294
// System.out.println("listing devices 00");

0 commit comments

Comments
 (0)