3939class Device {
4040 private final Devices env ;
4141 private final String id ;
42- private final String features ;
42+ private final String features ;
4343 private final Set <Integer > activeProcesses = new HashSet <Integer >();
4444 private final Set <DeviceListener > listeners =
4545 Collections .synchronizedSet (new HashSet <DeviceListener >());
4646
4747// public static final String APP_STARTED = "android.device.app.started";
4848// public static final String APP_ENDED = "android.device.app.ended";
4949
50+ private String packageName = "" ;
51+
5052 // mutable state
5153 private Process logcat ;
5254
@@ -196,6 +198,10 @@ public boolean isEmulator() {
196198 return id .startsWith ("emulator" );
197199 }
198200
201+ public void setPackageName (String pkgName ) {
202+ packageName = pkgName ;
203+ }
204+
199205 // I/Process ( 9213): Sending signal. PID: 9213 SIG: 9
200206 private static final Pattern SIG = Pattern
201207 .compile ("PID:\\ s+(\\ d+)\\ s+SIG:\\ s+(\\ d+)" );
@@ -205,12 +211,47 @@ public boolean isEmulator() {
205211 private class LogLineProcessor implements LineProcessor {
206212 public void processLine (final String line ) {
207213 final LogEntry entry = new LogEntry (line );
214+ // System.err.println("***************************************************");
215+ // System.out.println(line);
216+ // System.err.println(activeProcesses);
217+ // System.err.println(entry.message);
218+
208219 if (entry .message .startsWith ("PROCESSING" )) {
220+ // Old start/stop process detection, does not seem to work anymore.
221+ // Should be ok to remove at some point.
209222 if (entry .message .contains ("onStart" )) {
210223 startProc (entry .source , entry .pid );
211224 } else if (entry .message .contains ("onStop" )) {
212225 endProc (entry .pid );
213226 }
227+ } else if (packageName != null && !packageName .equals ("" ) &&
228+ entry .message .contains ("Start proc" ) &&
229+ entry .message .contains (packageName )) {
230+ // Sample message string from logcat when starting process:
231+ // "Start proc 29318:processing.test.sketch001/u0a403 for activity processing.test.sketch001/.MainActivity"
232+ try {
233+ int idx0 = entry .message .indexOf ("Start proc" ) + 11 ;
234+ int idx1 = entry .message .indexOf (packageName ) - 1 ;
235+ String pidStr = entry .message .substring (idx0 , idx1 );
236+ int pid = Integer .parseInt (pidStr );
237+ startProc (entry .source , pid );
238+ } catch (Exception ex ) {
239+ System .err .println ("AndroidDevice: cannot find process id, console output will be disabled." );
240+ }
241+ } else if (packageName != null && !packageName .equals ("" ) &&
242+ entry .message .contains ("Killing" ) &&
243+ entry .message .contains (packageName )) {
244+ // Sample message string from logcat when stopping process:
245+ // "Killing 31360:processing.test.test1/u0a403 (adj 900): remove task"
246+ try {
247+ int idx0 = entry .message .indexOf ("Killing" ) + 8 ;
248+ int idx1 = entry .message .indexOf (packageName ) - 1 ;
249+ String pidStr = entry .message .substring (idx0 , idx1 );
250+ int pid = Integer .parseInt (pidStr );
251+ endProc (pid );
252+ } catch (Exception ex ) {
253+ System .err .println ("AndroidDevice: cannot find process id, console output will continue. " + packageName );
254+ }
214255 } else if (entry .source .equals ("Process" )) {
215256 handleCrash (entry );
216257 } else if (activeProcesses .contains (entry .pid )) {
0 commit comments