68
68
import com .netease .qa .emmagee .utils .MailSender ;
69
69
import com .netease .qa .emmagee .utils .MemoryInfo ;
70
70
import com .netease .qa .emmagee .utils .MyApplication ;
71
+ import com .netease .qa .emmagee .utils .ProcessInfo ;
72
+ import com .netease .qa .emmagee .utils .Programe ;
71
73
import com .netease .qa .emmagee .utils .Settings ;
72
74
73
75
/**
@@ -101,12 +103,14 @@ public class EmmageeService extends Service {
101
103
private CpuInfo cpuInfo ;
102
104
private boolean isFloating ;
103
105
private boolean isRoot ;
106
+ private boolean isAutoStop = false ;
104
107
private String processName , packageName , startActivity ;
105
108
private int pid , uid ;
106
109
private boolean isServiceStop = false ;
107
110
private String sender , password , recipients , smtp ;
108
111
private String [] receivers ;
109
112
private EncryptData des ;
113
+ private ProcessInfo procInfo ;
110
114
111
115
public static BufferedWriter bw ;
112
116
public static FileOutputStream out ;
@@ -127,6 +131,7 @@ public class EmmageeService extends Service {
127
131
private boolean isGetStartTime = true ;
128
132
private String startTime = "" ;
129
133
public static final String SERVICE_ACTION = "com.netease.action.emmageeService" ;
134
+ private static final String BATTERY_CHANGED = "android.intent.action.BATTERY_CHANGED" ;
130
135
131
136
@ Override
132
137
public void onCreate () {
@@ -135,6 +140,7 @@ public void onCreate() {
135
140
isServiceStop = false ;
136
141
isStop = false ;
137
142
memoryInfo = new MemoryInfo ();
143
+ procInfo = new ProcessInfo ();
138
144
fomart = new DecimalFormat ();
139
145
fomart .setDecimalFormatSymbols (new DecimalFormatSymbols (Locale .US ));
140
146
fomart .setGroupingUsed (false );
@@ -143,7 +149,7 @@ public void onCreate() {
143
149
des = new EncryptData ("emmagee" );
144
150
currentInfo = new CurrentInfo ();
145
151
batteryBroadcast = new BatteryInfoBroadcastReceiver ();
146
- registerReceiver (batteryBroadcast , new IntentFilter ("android.intent.action. BATTERY_CHANGED" ));
152
+ registerReceiver (batteryBroadcast , new IntentFilter (BATTERY_CHANGED ));
147
153
}
148
154
149
155
/**
@@ -237,6 +243,7 @@ private void readSettingInfo() {
237
243
receivers = recipients .split ("\\ s+" );
238
244
smtp = preferences .getString (Settings .KEY_SMTP , BLANK_STRING );
239
245
isRoot = preferences .getBoolean (Settings .KEY_ROOT , false );
246
+ isAutoStop = preferences .getBoolean (Settings .KEY_AUTO_STOP , true );
240
247
}
241
248
242
249
/**
@@ -274,20 +281,21 @@ private void createResultCsv() {
274
281
for (int i = 0 ; i < cpuList .size (); i ++) {
275
282
multiCpuTitle += Constants .COMMA + cpuList .get (i ) + getString (R .string .total_usage );
276
283
}
277
- bw .write (getString (R .string .process_package ) + Constants .COMMA + packageName + Constants .LINE_END + getString (R .string .process_name ) + Constants .COMMA
278
- + processName + Constants .LINE_END + getString (R .string .process_pid ) + Constants .COMMA + pid + Constants .LINE_END
279
- + getString (R .string .mem_size ) + Constants .COMMA + totalMemory + "MB" + Constants .LINE_END + getString (R .string .cpu_type ) + Constants .COMMA
280
- + cpuInfo .getCpuName () + Constants .LINE_END + getString (R .string .android_system_version ) + Constants .COMMA + memoryInfo .getSDKVersion ()
281
- + Constants .LINE_END + getString (R .string .mobile_type ) + Constants .COMMA + memoryInfo .getPhoneType () + Constants .LINE_END + "UID" + Constants .COMMA
284
+ bw .write (getString (R .string .process_package ) + Constants .COMMA + packageName + Constants .LINE_END + getString (R .string .process_name )
285
+ + Constants .COMMA + processName + Constants .LINE_END + getString (R .string .process_pid ) + Constants .COMMA + pid
286
+ + Constants .LINE_END + getString (R .string .mem_size ) + Constants .COMMA + totalMemory + "MB" + Constants .LINE_END
287
+ + getString (R .string .cpu_type ) + Constants .COMMA + cpuInfo .getCpuName () + Constants .LINE_END
288
+ + getString (R .string .android_system_version ) + Constants .COMMA + memoryInfo .getSDKVersion () + Constants .LINE_END
289
+ + getString (R .string .mobile_type ) + Constants .COMMA + memoryInfo .getPhoneType () + Constants .LINE_END + "UID" + Constants .COMMA
282
290
+ uid + Constants .LINE_END );
283
291
284
292
if (isGrantedReadLogsPermission ()) {
285
293
bw .write (START_TIME );
286
294
}
287
- if (isRoot ){
288
- heapData = getString (R .string .native_heap ) + Constants .COMMA + getString (R .string .dalvik_heap ) + Constants .COMMA ;
295
+ if (isRoot ) {
296
+ heapData = getString (R .string .native_heap ) + Constants .COMMA + getString (R .string .dalvik_heap ) + Constants .COMMA ;
289
297
}
290
- bw .write (getString (R .string .timestamp ) + Constants .COMMA + getString (R .string .top_activity ) + Constants .COMMA + heapData
298
+ bw .write (getString (R .string .timestamp ) + Constants .COMMA + getString (R .string .top_activity ) + Constants .COMMA + heapData
291
299
+ getString (R .string .used_mem_PSS ) + Constants .COMMA + getString (R .string .used_mem_ratio ) + Constants .COMMA
292
300
+ getString (R .string .mobile_free_mem ) + Constants .COMMA + getString (R .string .app_used_cpu_ratio ) + Constants .COMMA
293
301
+ getString (R .string .total_used_cpu_ratio ) + multiCpuTitle + Constants .COMMA + getString (R .string .traffic ) + Constants .COMMA
@@ -449,7 +457,7 @@ private void dataRefresh() {
449
457
} catch (Exception e ) {
450
458
currentBatt = Constants .NA ;
451
459
}
452
- ArrayList <String > processInfo = cpuInfo .getCpuRatioInfo (totalBatt , currentBatt , temperature , voltage ,isRoot );
460
+ ArrayList <String > processInfo = cpuInfo .getCpuRatioInfo (totalBatt , currentBatt , temperature , voltage , isRoot );
453
461
if (isFloating ) {
454
462
String processCpuRatio = "0.00" ;
455
463
String totalCpuRatio = "0.00" ;
@@ -474,17 +482,28 @@ private void dataRefresh() {
474
482
txtTotalMem .setText (getString (R .string .process_overall_cpu ) + processCpuRatio + "%/" + totalCpuRatio + "%" );
475
483
String batt = getString (R .string .current ) + currentBatt ;
476
484
if ("-1" .equals (trafficSize )) {
477
- txtTraffic .setText (batt + "," + getString (R .string .traffic ) + Constants .NA );
485
+ txtTraffic .setText (batt + Constants . COMMA + getString (R .string .traffic ) + Constants .NA );
478
486
} else if (isMb )
479
- txtTraffic .setText (batt + "," + getString (R .string .traffic ) + fomart .format (trafficMb ) + "MB" );
487
+ txtTraffic .setText (batt + Constants . COMMA + getString (R .string .traffic ) + fomart .format (trafficMb ) + "MB" );
480
488
else
481
- txtTraffic .setText (batt + "," + getString (R .string .traffic ) + trafficSize + "KB" );
489
+ txtTraffic .setText (batt + Constants . COMMA + getString (R .string .traffic ) + trafficSize + "KB" );
482
490
}
483
491
// 当内存为0切cpu使用率为0时则是被测应用退出
484
492
if ("0" .equals (processMemory )) {
485
- closeOpenedStream ();
486
- isServiceStop = true ;
487
- return ;
493
+ if (isAutoStop ) {
494
+ closeOpenedStream ();
495
+ isServiceStop = true ;
496
+ return ;
497
+ } else {
498
+ Log .i (LOG_TAG , "未设置自动停止测试,继续监听" );
499
+ // 如果设置应用退出后不自动停止,则需要每次监听时重新获取pid
500
+ Programe programe = procInfo .getProgrameByPackageName (this , packageName );
501
+ if (programe != null && programe .getPid () > 0 ) {
502
+ pid = programe .getPid ();
503
+ uid = programe .getUid ();
504
+ cpuInfo = new CpuInfo (getBaseContext (), pid , Integer .toString (uid ));
505
+ }
506
+ }
488
507
}
489
508
}
490
509
@@ -508,8 +527,8 @@ private void updateViewPosition() {
508
527
public void closeOpenedStream () {
509
528
try {
510
529
if (bw != null ) {
511
- bw .write (getString (R .string .comment1 ) + Constants .LINE_END + getString (R .string .comment2 ) + Constants .LINE_END + getString ( R . string . comment3 ) + Constants . LINE_END
512
- + getString (R .string .comment4 ) + Constants .LINE_END );
530
+ bw .write (getString (R .string .comment1 ) + Constants .LINE_END + getString (R .string .comment2 ) + Constants .LINE_END
531
+ + getString (R .string .comment3 ) + Constants . LINE_END + getString ( R . string . comment4 ) + Constants .LINE_END );
513
532
bw .close ();
514
533
}
515
534
if (osw != null )
0 commit comments