39
39
class Device {
40
40
private final Devices env ;
41
41
private final String id ;
42
- private final String features ;
42
+ private final String features ;
43
43
private final Set <Integer > activeProcesses = new HashSet <Integer >();
44
44
private final Set <DeviceListener > listeners =
45
45
Collections .synchronizedSet (new HashSet <DeviceListener >());
46
46
47
47
// public static final String APP_STARTED = "android.device.app.started";
48
48
// public static final String APP_ENDED = "android.device.app.ended";
49
49
50
+ private String packageName = "" ;
51
+
50
52
// mutable state
51
53
private Process logcat ;
52
54
@@ -196,6 +198,10 @@ public boolean isEmulator() {
196
198
return id .startsWith ("emulator" );
197
199
}
198
200
201
+ public void setPackageName (String pkgName ) {
202
+ packageName = pkgName ;
203
+ }
204
+
199
205
// I/Process ( 9213): Sending signal. PID: 9213 SIG: 9
200
206
private static final Pattern SIG = Pattern
201
207
.compile ("PID:\\ s+(\\ d+)\\ s+SIG:\\ s+(\\ d+)" );
@@ -205,12 +211,47 @@ public boolean isEmulator() {
205
211
private class LogLineProcessor implements LineProcessor {
206
212
public void processLine (final String line ) {
207
213
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
+
208
219
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.
209
222
if (entry .message .contains ("onStart" )) {
210
223
startProc (entry .source , entry .pid );
211
224
} else if (entry .message .contains ("onStop" )) {
212
225
endProc (entry .pid );
213
226
}
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
+ }
214
255
} else if (entry .source .equals ("Process" )) {
215
256
handleCrash (entry );
216
257
} else if (activeProcesses .contains (entry .pid )) {
0 commit comments