6868import com .netease .qa .emmagee .utils .MailSender ;
6969import com .netease .qa .emmagee .utils .MemoryInfo ;
7070import com .netease .qa .emmagee .utils .MyApplication ;
71+ import com .netease .qa .emmagee .utils .ProcessInfo ;
72+ import com .netease .qa .emmagee .utils .Programe ;
7173import com .netease .qa .emmagee .utils .Settings ;
7274
7375/**
@@ -101,12 +103,14 @@ public class EmmageeService extends Service {
101103 private CpuInfo cpuInfo ;
102104 private boolean isFloating ;
103105 private boolean isRoot ;
106+ private boolean isAutoStop = false ;
104107 private String processName , packageName , startActivity ;
105108 private int pid , uid ;
106109 private boolean isServiceStop = false ;
107110 private String sender , password , recipients , smtp ;
108111 private String [] receivers ;
109112 private EncryptData des ;
113+ private ProcessInfo procInfo ;
110114
111115 public static BufferedWriter bw ;
112116 public static FileOutputStream out ;
@@ -127,6 +131,7 @@ public class EmmageeService extends Service {
127131 private boolean isGetStartTime = true ;
128132 private String startTime = "" ;
129133 public static final String SERVICE_ACTION = "com.netease.action.emmageeService" ;
134+ private static final String BATTERY_CHANGED = "android.intent.action.BATTERY_CHANGED" ;
130135
131136 @ Override
132137 public void onCreate () {
@@ -135,6 +140,7 @@ public void onCreate() {
135140 isServiceStop = false ;
136141 isStop = false ;
137142 memoryInfo = new MemoryInfo ();
143+ procInfo = new ProcessInfo ();
138144 fomart = new DecimalFormat ();
139145 fomart .setDecimalFormatSymbols (new DecimalFormatSymbols (Locale .US ));
140146 fomart .setGroupingUsed (false );
@@ -143,7 +149,7 @@ public void onCreate() {
143149 des = new EncryptData ("emmagee" );
144150 currentInfo = new CurrentInfo ();
145151 batteryBroadcast = new BatteryInfoBroadcastReceiver ();
146- registerReceiver (batteryBroadcast , new IntentFilter ("android.intent.action. BATTERY_CHANGED" ));
152+ registerReceiver (batteryBroadcast , new IntentFilter (BATTERY_CHANGED ));
147153 }
148154
149155 /**
@@ -237,6 +243,7 @@ private void readSettingInfo() {
237243 receivers = recipients .split ("\\ s+" );
238244 smtp = preferences .getString (Settings .KEY_SMTP , BLANK_STRING );
239245 isRoot = preferences .getBoolean (Settings .KEY_ROOT , false );
246+ isAutoStop = preferences .getBoolean (Settings .KEY_AUTO_STOP , true );
240247 }
241248
242249 /**
@@ -274,20 +281,21 @@ private void createResultCsv() {
274281 for (int i = 0 ; i < cpuList .size (); i ++) {
275282 multiCpuTitle += Constants .COMMA + cpuList .get (i ) + getString (R .string .total_usage );
276283 }
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
282290 + uid + Constants .LINE_END );
283291
284292 if (isGrantedReadLogsPermission ()) {
285293 bw .write (START_TIME );
286294 }
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 ;
289297 }
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
291299 + getString (R .string .used_mem_PSS ) + Constants .COMMA + getString (R .string .used_mem_ratio ) + Constants .COMMA
292300 + getString (R .string .mobile_free_mem ) + Constants .COMMA + getString (R .string .app_used_cpu_ratio ) + Constants .COMMA
293301 + getString (R .string .total_used_cpu_ratio ) + multiCpuTitle + Constants .COMMA + getString (R .string .traffic ) + Constants .COMMA
@@ -449,7 +457,7 @@ private void dataRefresh() {
449457 } catch (Exception e ) {
450458 currentBatt = Constants .NA ;
451459 }
452- ArrayList <String > processInfo = cpuInfo .getCpuRatioInfo (totalBatt , currentBatt , temperature , voltage ,isRoot );
460+ ArrayList <String > processInfo = cpuInfo .getCpuRatioInfo (totalBatt , currentBatt , temperature , voltage , isRoot );
453461 if (isFloating ) {
454462 String processCpuRatio = "0.00" ;
455463 String totalCpuRatio = "0.00" ;
@@ -474,17 +482,28 @@ private void dataRefresh() {
474482 txtTotalMem .setText (getString (R .string .process_overall_cpu ) + processCpuRatio + "%/" + totalCpuRatio + "%" );
475483 String batt = getString (R .string .current ) + currentBatt ;
476484 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 );
478486 } 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" );
480488 else
481- txtTraffic .setText (batt + "," + getString (R .string .traffic ) + trafficSize + "KB" );
489+ txtTraffic .setText (batt + Constants . COMMA + getString (R .string .traffic ) + trafficSize + "KB" );
482490 }
483491 // 当内存为0切cpu使用率为0时则是被测应用退出
484492 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+ }
488507 }
489508 }
490509
@@ -508,8 +527,8 @@ private void updateViewPosition() {
508527 public void closeOpenedStream () {
509528 try {
510529 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 );
513532 bw .close ();
514533 }
515534 if (osw != null )
0 commit comments