Skip to content

Commit 88bf84d

Browse files
author
andrewleo
committed
Added: NetEase#37 Add a new option in Settings: stop monitoring when app exited
1 parent b92a61d commit 88bf84d

File tree

8 files changed

+105
-26
lines changed

8 files changed

+105
-26
lines changed

res/layout/settings.xml

+27
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@
111111
android:paddingRight="@dimen/image_padding" />
112112
</RelativeLayout>
113113

114+
<RelativeLayout
115+
android:id="@+id/auto_stop_item"
116+
android:layout_width="match_parent"
117+
android:layout_height="wrap_content"
118+
android:gravity="center_vertical"
119+
android:orientation="horizontal"
120+
android:padding="@dimen/layout_vertical_margin_small" >
121+
122+
<TextView
123+
android:layout_width="wrap_content"
124+
android:layout_height="wrap_content"
125+
android:text="@string/stop_when_exited"
126+
android:textColor="@color/black"
127+
android:textSize="@dimen/text_size" />
128+
129+
<CheckBox
130+
android:id="@+id/auto_stop"
131+
android:layout_width="wrap_content"
132+
android:layout_height="wrap_content"
133+
android:layout_alignParentRight="true"
134+
android:background="@drawable/custom_checkbox"
135+
android:button="@null"
136+
android:checked="true"
137+
android:paddingLeft="@dimen/image_padding"
138+
android:paddingRight="@dimen/image_padding" />
139+
</RelativeLayout>
140+
114141
<LinearLayout
115142
android:id="@+id/heap_item"
116143
android:layout_width="match_parent"

res/values-zh-rCN/strings.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<string name="password">密码:</string>
1414
<string name="smtp">SMTP服务器:</string>
1515
<string name="receiver">收件人(多人以空格分割):</string>
16-
<string name="floating_window">是否显示浮窗</string>
16+
<string name="floating_window">显示浮窗</string>
1717
<string name="save">保存</string>
1818

1919
<string name="calculating">计算中...</string>
@@ -84,4 +84,5 @@
8484
<string name="root_notification">(需要root)</string>
8585

8686
<string name="root_failed_notification">无法获取root权限,请确认手机是否已经root过</string>
87+
<string name="stop_when_exited">应用退出后停止监听</string>
8788
</resources>

res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@
8383
<string name="collect_heap">Collect Heap</string>
8484
<string name="root_notification">(root is necessary)</string>
8585
<string name="root_failed_notification">Fail to get root permission, please check if this phone is rooted</string>
86+
<string name="stop_when_exited">Stop monitoring when app exited</string>
8687
</resources>

src/com/netease/qa/emmagee/activity/SettingsActivity.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
*/
1717
package com.netease.qa.emmagee.activity;
1818

19-
import java.io.BufferedReader;
2019
import java.io.DataOutputStream;
21-
import java.io.IOException;
22-
import java.io.InputStreamReader;
2320

2421
import android.app.Activity;
2522
import android.content.Intent;
@@ -52,6 +49,7 @@ public class SettingsActivity extends Activity {
5249

5350
private CheckBox chkFloat;
5451
private CheckBox chkRoot;
52+
private CheckBox chkAutoStop;
5553
private TextView tvTime;
5654
private LinearLayout about;
5755
private LinearLayout mailSettings;
@@ -67,25 +65,30 @@ public void onCreate(Bundle savedInstanceState) {
6765

6866
chkFloat = (CheckBox) findViewById(R.id.floating);
6967
chkRoot = (CheckBox) findViewById(R.id.is_root);
68+
chkAutoStop = (CheckBox) findViewById(R.id.auto_stop);
7069
tvTime = (TextView) findViewById(R.id.time);
7170
about = (LinearLayout) findViewById(R.id.about);
7271
mailSettings = (LinearLayout) findViewById(R.id.mail_settings);
7372
SeekBar timeBar = (SeekBar) findViewById(R.id.timeline);
7473
ImageView btnSave = (ImageView) findViewById(R.id.btn_set);
7574
RelativeLayout floatingItem = (RelativeLayout) findViewById(R.id.floating_item);
75+
RelativeLayout autoStopItem = (RelativeLayout) findViewById(R.id.auto_stop_item);
7676
LinearLayout layGoBack = (LinearLayout) findViewById(R.id.lay_go_back);
7777
LinearLayout layHeapItem = (LinearLayout) findViewById(R.id.heap_item);
78+
7879

7980
btnSave.setVisibility(ImageView.INVISIBLE);
8081

8182
preferences = Settings.getDefaultSharedPreferences(getApplicationContext());
8283
int interval = preferences.getInt(Settings.KEY_INTERVAL, 5);
8384
boolean isfloat = preferences.getBoolean(Settings.KEY_ISFLOAT, true);
8485
boolean isRoot = preferences.getBoolean(Settings.KEY_ROOT, false);
85-
86+
boolean autoStop = preferences.getBoolean(Settings.KEY_AUTO_STOP, true);
87+
8688
tvTime.setText(String.valueOf(interval));
8789
chkFloat.setChecked(isfloat);
8890
chkRoot.setChecked(isRoot);
91+
chkAutoStop.setChecked(autoStop);
8992

9093
timeBar.setProgress(interval);
9194
timeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@@ -142,7 +145,16 @@ public void onClick(View arg0) {
142145
preferences.edit().putBoolean(Settings.KEY_ISFLOAT, !isChecked).commit();
143146
}
144147
});
145-
148+
149+
autoStopItem.setOnClickListener(new OnClickListener() {
150+
@Override
151+
public void onClick(View arg0) {
152+
boolean isChecked = chkAutoStop.isChecked();
153+
chkAutoStop.setChecked(!isChecked);
154+
preferences.edit().putBoolean(Settings.KEY_AUTO_STOP, !isChecked).commit();
155+
}
156+
});
157+
146158
// get root permission
147159
layHeapItem.setOnClickListener(new OnClickListener() {
148160
@Override

src/com/netease/qa/emmagee/service/EmmageeService.java

+37-18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
import com.netease.qa.emmagee.utils.MailSender;
6969
import com.netease.qa.emmagee.utils.MemoryInfo;
7070
import com.netease.qa.emmagee.utils.MyApplication;
71+
import com.netease.qa.emmagee.utils.ProcessInfo;
72+
import com.netease.qa.emmagee.utils.Programe;
7173
import 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)

src/com/netease/qa/emmagee/utils/CpuInfo.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ public void readCpuStat() {
103103
processCpu = Long.parseLong(tok[13]) + Long.parseLong(tok[14]);
104104
processCpuInfo.close();
105105
} catch (FileNotFoundException e) {
106-
Log.e(LOG_TAG, "FileNotFoundException: " + e.getMessage());
107-
e.printStackTrace();
106+
Log.w(LOG_TAG, "FileNotFoundException: " + e.getMessage());
108107
} catch (IOException e) {
109108
e.printStackTrace();
110109
}

src/com/netease/qa/emmagee/utils/ProcessInfo.java

+19
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ private List<ApplicationInfo> getPackagesInfo(Context context) {
9090
return appList;
9191
}
9292

93+
/**
94+
* get pid by package name
95+
*
96+
* @param context
97+
* context of activity
98+
* @param packageName
99+
* package name of monitoring app
100+
* @return pid
101+
*/
102+
public Programe getProgrameByPackageName(Context context, String packageName) {
103+
List<Programe> processList = getRunningProcess(context);
104+
for (Programe programe : processList) {
105+
if ((programe.getPackageName() != null) && (programe.getPackageName().equals(packageName))) {
106+
return programe;
107+
}
108+
}
109+
return null;
110+
}
111+
93112
/**
94113
* get top activity name
95114
*

src/com/netease/qa/emmagee/utils/Settings.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class Settings {
1919
public static final String KEY_ISFLOAT = "isfloat";
2020
public static final String KEY_INTERVAL = "interval";
2121
public static final String KEY_ROOT = "root";
22+
public static final String KEY_AUTO_STOP = "autoStop";
2223

2324
public static SharedPreferences getDefaultSharedPreferences(Context context) {
2425
return PreferenceManager.getDefaultSharedPreferences(context);

0 commit comments

Comments
 (0)