Skip to content

Commit

Permalink
parses start and kill proc messages from logcat, fixes #277
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Jan 13, 2017
1 parent 8e3f993 commit e34ba12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/processing/mode/android/AndroidRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void launch(Future<Device> deviceFuture, boolean wear) {
// }

device.addListener(this);
device.setPackageName(build.getPackageName());

// if (listener.isHalted()) {
//// if (monitor.isCanceled()) {
Expand Down
43 changes: 42 additions & 1 deletion src/processing/mode/android/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
class Device {
private final Devices env;
private final String id;
private final String features;
private final String features;
private final Set<Integer> activeProcesses = new HashSet<Integer>();
private final Set<DeviceListener> listeners =
Collections.synchronizedSet(new HashSet<DeviceListener>());

// public static final String APP_STARTED = "android.device.app.started";
// public static final String APP_ENDED = "android.device.app.ended";

private String packageName = "";

// mutable state
private Process logcat;

Expand Down Expand Up @@ -196,6 +198,10 @@ public boolean isEmulator() {
return id.startsWith("emulator");
}

public void setPackageName(String pkgName) {
packageName = pkgName;
}

// I/Process ( 9213): Sending signal. PID: 9213 SIG: 9
private static final Pattern SIG = Pattern
.compile("PID:\\s+(\\d+)\\s+SIG:\\s+(\\d+)");
Expand All @@ -205,12 +211,47 @@ public boolean isEmulator() {
private class LogLineProcessor implements LineProcessor {
public void processLine(final String line) {
final LogEntry entry = new LogEntry(line);
// System.err.println("***************************************************");
// System.out.println(line);
// System.err.println(activeProcesses);
// System.err.println(entry.message);

if (entry.message.startsWith("PROCESSING")) {
// Old start/stop process detection, does not seem to work anymore.
// Should be ok to remove at some point.
if (entry.message.contains("onStart")) {
startProc(entry.source, entry.pid);
} else if (entry.message.contains("onStop")) {
endProc(entry.pid);
}
} else if (packageName != null && !packageName.equals("") &&
entry.message.contains("Start proc") &&
entry.message.contains(packageName)) {
// Sample message string from logcat when starting process:
// "Start proc 29318:processing.test.sketch001/u0a403 for activity processing.test.sketch001/.MainActivity"
try {
int idx0 = entry.message.indexOf("Start proc") + 11;
int idx1 = entry.message.indexOf(packageName) - 1;
String pidStr = entry.message.substring(idx0, idx1);
int pid = Integer.parseInt(pidStr);
startProc(entry.source, pid);
} catch (Exception ex) {
System.err.println("AndroidDevice: cannot find process id, console output will be disabled.");
}
} else if (packageName != null && !packageName.equals("") &&
entry.message.contains("Killing") &&
entry.message.contains(packageName)) {
// Sample message string from logcat when stopping process:
// "Killing 31360:processing.test.test1/u0a403 (adj 900): remove task"
try {
int idx0 = entry.message.indexOf("Killing") + 8;
int idx1 = entry.message.indexOf(packageName) - 1;
String pidStr = entry.message.substring(idx0, idx1);
int pid = Integer.parseInt(pidStr);
endProc(pid);
} catch (Exception ex) {
System.err.println("AndroidDevice: cannot find process id, console output will continue. " + packageName);
}
} else if (entry.source.equals("Process")) {
handleCrash(entry);
} else if (activeProcesses.contains(entry.pid)) {
Expand Down

0 comments on commit e34ba12

Please sign in to comment.