Skip to content

Commit

Permalink
Added: NetEase#37 Add a new option in Settings: stop monitoring when …
Browse files Browse the repository at this point in the history
…app exited
  • Loading branch information
andrewleo committed Mar 13, 2015
1 parent b92a61d commit 88bf84d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 26 deletions.
27 changes: 27 additions & 0 deletions res/layout/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@
android:paddingRight="@dimen/image_padding" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/auto_stop_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/layout_vertical_margin_small" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop_when_exited"
android:textColor="@color/black"
android:textSize="@dimen/text_size" />

<CheckBox
android:id="@+id/auto_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/custom_checkbox"
android:button="@null"
android:checked="true"
android:paddingLeft="@dimen/image_padding"
android:paddingRight="@dimen/image_padding" />
</RelativeLayout>

<LinearLayout
android:id="@+id/heap_item"
android:layout_width="match_parent"
Expand Down
3 changes: 2 additions & 1 deletion res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<string name="password">密码:</string>
<string name="smtp">SMTP服务器:</string>
<string name="receiver">收件人(多人以空格分割):</string>
<string name="floating_window">是否显示浮窗</string>
<string name="floating_window">显示浮窗</string>
<string name="save">保存</string>

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

<string name="root_failed_notification">无法获取root权限,请确认手机是否已经root过</string>
<string name="stop_when_exited">应用退出后停止监听</string>
</resources>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@
<string name="collect_heap">Collect Heap</string>
<string name="root_notification">(root is necessary)</string>
<string name="root_failed_notification">Fail to get root permission, please check if this phone is rooted</string>
<string name="stop_when_exited">Stop monitoring when app exited</string>
</resources>
22 changes: 17 additions & 5 deletions src/com/netease/qa/emmagee/activity/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package com.netease.qa.emmagee.activity;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
Expand Down Expand Up @@ -52,6 +49,7 @@ public class SettingsActivity extends Activity {

private CheckBox chkFloat;
private CheckBox chkRoot;
private CheckBox chkAutoStop;
private TextView tvTime;
private LinearLayout about;
private LinearLayout mailSettings;
Expand All @@ -67,25 +65,30 @@ public void onCreate(Bundle savedInstanceState) {

chkFloat = (CheckBox) findViewById(R.id.floating);
chkRoot = (CheckBox) findViewById(R.id.is_root);
chkAutoStop = (CheckBox) findViewById(R.id.auto_stop);
tvTime = (TextView) findViewById(R.id.time);
about = (LinearLayout) findViewById(R.id.about);
mailSettings = (LinearLayout) findViewById(R.id.mail_settings);
SeekBar timeBar = (SeekBar) findViewById(R.id.timeline);
ImageView btnSave = (ImageView) findViewById(R.id.btn_set);
RelativeLayout floatingItem = (RelativeLayout) findViewById(R.id.floating_item);
RelativeLayout autoStopItem = (RelativeLayout) findViewById(R.id.auto_stop_item);
LinearLayout layGoBack = (LinearLayout) findViewById(R.id.lay_go_back);
LinearLayout layHeapItem = (LinearLayout) findViewById(R.id.heap_item);


btnSave.setVisibility(ImageView.INVISIBLE);

preferences = Settings.getDefaultSharedPreferences(getApplicationContext());
int interval = preferences.getInt(Settings.KEY_INTERVAL, 5);
boolean isfloat = preferences.getBoolean(Settings.KEY_ISFLOAT, true);
boolean isRoot = preferences.getBoolean(Settings.KEY_ROOT, false);

boolean autoStop = preferences.getBoolean(Settings.KEY_AUTO_STOP, true);

tvTime.setText(String.valueOf(interval));
chkFloat.setChecked(isfloat);
chkRoot.setChecked(isRoot);
chkAutoStop.setChecked(autoStop);

timeBar.setProgress(interval);
timeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
Expand Down Expand Up @@ -142,7 +145,16 @@ public void onClick(View arg0) {
preferences.edit().putBoolean(Settings.KEY_ISFLOAT, !isChecked).commit();
}
});


autoStopItem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
boolean isChecked = chkAutoStop.isChecked();
chkAutoStop.setChecked(!isChecked);
preferences.edit().putBoolean(Settings.KEY_AUTO_STOP, !isChecked).commit();
}
});

// get root permission
layHeapItem.setOnClickListener(new OnClickListener() {
@Override
Expand Down
55 changes: 37 additions & 18 deletions src/com/netease/qa/emmagee/service/EmmageeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
import com.netease.qa.emmagee.utils.MailSender;
import com.netease.qa.emmagee.utils.MemoryInfo;
import com.netease.qa.emmagee.utils.MyApplication;
import com.netease.qa.emmagee.utils.ProcessInfo;
import com.netease.qa.emmagee.utils.Programe;
import com.netease.qa.emmagee.utils.Settings;

/**
Expand Down Expand Up @@ -101,12 +103,14 @@ public class EmmageeService extends Service {
private CpuInfo cpuInfo;
private boolean isFloating;
private boolean isRoot;
private boolean isAutoStop = false;
private String processName, packageName, startActivity;
private int pid, uid;
private boolean isServiceStop = false;
private String sender, password, recipients, smtp;
private String[] receivers;
private EncryptData des;
private ProcessInfo procInfo;

public static BufferedWriter bw;
public static FileOutputStream out;
Expand All @@ -127,6 +131,7 @@ public class EmmageeService extends Service {
private boolean isGetStartTime = true;
private String startTime = "";
public static final String SERVICE_ACTION = "com.netease.action.emmageeService";
private static final String BATTERY_CHANGED = "android.intent.action.BATTERY_CHANGED";

@Override
public void onCreate() {
Expand All @@ -135,6 +140,7 @@ public void onCreate() {
isServiceStop = false;
isStop = false;
memoryInfo = new MemoryInfo();
procInfo = new ProcessInfo();
fomart = new DecimalFormat();
fomart.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
fomart.setGroupingUsed(false);
Expand All @@ -143,7 +149,7 @@ public void onCreate() {
des = new EncryptData("emmagee");
currentInfo = new CurrentInfo();
batteryBroadcast = new BatteryInfoBroadcastReceiver();
registerReceiver(batteryBroadcast, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
registerReceiver(batteryBroadcast, new IntentFilter(BATTERY_CHANGED));
}

/**
Expand Down Expand Up @@ -237,6 +243,7 @@ private void readSettingInfo() {
receivers = recipients.split("\\s+");
smtp = preferences.getString(Settings.KEY_SMTP, BLANK_STRING);
isRoot = preferences.getBoolean(Settings.KEY_ROOT, false);
isAutoStop = preferences.getBoolean(Settings.KEY_AUTO_STOP, true);
}

/**
Expand Down Expand Up @@ -274,20 +281,21 @@ private void createResultCsv() {
for (int i = 0; i < cpuList.size(); i++) {
multiCpuTitle += Constants.COMMA + cpuList.get(i) + getString(R.string.total_usage);
}
bw.write(getString(R.string.process_package) + Constants.COMMA + packageName + Constants.LINE_END + getString(R.string.process_name) + Constants.COMMA
+ processName + Constants.LINE_END + getString(R.string.process_pid) + Constants.COMMA + pid + Constants.LINE_END
+ getString(R.string.mem_size) + Constants.COMMA + totalMemory + "MB" + Constants.LINE_END + getString(R.string.cpu_type) + Constants.COMMA
+ cpuInfo.getCpuName() + Constants.LINE_END + getString(R.string.android_system_version) + Constants.COMMA + memoryInfo.getSDKVersion()
+ Constants.LINE_END + getString(R.string.mobile_type) + Constants.COMMA + memoryInfo.getPhoneType() + Constants.LINE_END + "UID" + Constants.COMMA
bw.write(getString(R.string.process_package) + Constants.COMMA + packageName + Constants.LINE_END + getString(R.string.process_name)
+ Constants.COMMA + processName + Constants.LINE_END + getString(R.string.process_pid) + Constants.COMMA + pid
+ Constants.LINE_END + getString(R.string.mem_size) + Constants.COMMA + totalMemory + "MB" + Constants.LINE_END
+ getString(R.string.cpu_type) + Constants.COMMA + cpuInfo.getCpuName() + Constants.LINE_END
+ getString(R.string.android_system_version) + Constants.COMMA + memoryInfo.getSDKVersion() + Constants.LINE_END
+ getString(R.string.mobile_type) + Constants.COMMA + memoryInfo.getPhoneType() + Constants.LINE_END + "UID" + Constants.COMMA
+ uid + Constants.LINE_END);

if (isGrantedReadLogsPermission()) {
bw.write(START_TIME);
}
if(isRoot){
heapData = getString(R.string.native_heap) + Constants.COMMA+getString(R.string.dalvik_heap) + Constants.COMMA;
if (isRoot) {
heapData = getString(R.string.native_heap) + Constants.COMMA + getString(R.string.dalvik_heap) + Constants.COMMA;
}
bw.write(getString(R.string.timestamp) + Constants.COMMA + getString(R.string.top_activity) + Constants.COMMA+heapData
bw.write(getString(R.string.timestamp) + Constants.COMMA + getString(R.string.top_activity) + Constants.COMMA + heapData
+ getString(R.string.used_mem_PSS) + Constants.COMMA + getString(R.string.used_mem_ratio) + Constants.COMMA
+ getString(R.string.mobile_free_mem) + Constants.COMMA + getString(R.string.app_used_cpu_ratio) + Constants.COMMA
+ getString(R.string.total_used_cpu_ratio) + multiCpuTitle + Constants.COMMA + getString(R.string.traffic) + Constants.COMMA
Expand Down Expand Up @@ -449,7 +457,7 @@ private void dataRefresh() {
} catch (Exception e) {
currentBatt = Constants.NA;
}
ArrayList<String> processInfo = cpuInfo.getCpuRatioInfo(totalBatt, currentBatt, temperature, voltage,isRoot);
ArrayList<String> processInfo = cpuInfo.getCpuRatioInfo(totalBatt, currentBatt, temperature, voltage, isRoot);
if (isFloating) {
String processCpuRatio = "0.00";
String totalCpuRatio = "0.00";
Expand All @@ -474,17 +482,28 @@ private void dataRefresh() {
txtTotalMem.setText(getString(R.string.process_overall_cpu) + processCpuRatio + "%/" + totalCpuRatio + "%");
String batt = getString(R.string.current) + currentBatt;
if ("-1".equals(trafficSize)) {
txtTraffic.setText(batt + "," + getString(R.string.traffic) + Constants.NA);
txtTraffic.setText(batt + Constants.COMMA + getString(R.string.traffic) + Constants.NA);
} else if (isMb)
txtTraffic.setText(batt + "," + getString(R.string.traffic) + fomart.format(trafficMb) + "MB");
txtTraffic.setText(batt + Constants.COMMA + getString(R.string.traffic) + fomart.format(trafficMb) + "MB");
else
txtTraffic.setText(batt + "," + getString(R.string.traffic) + trafficSize + "KB");
txtTraffic.setText(batt + Constants.COMMA + getString(R.string.traffic) + trafficSize + "KB");
}
// 当内存为0切cpu使用率为0时则是被测应用退出
if ("0".equals(processMemory)) {
closeOpenedStream();
isServiceStop = true;
return;
if (isAutoStop) {
closeOpenedStream();
isServiceStop = true;
return;
} else {
Log.i(LOG_TAG, "未设置自动停止测试,继续监听");
// 如果设置应用退出后不自动停止,则需要每次监听时重新获取pid
Programe programe = procInfo.getProgrameByPackageName(this, packageName);
if (programe != null && programe.getPid() > 0) {
pid = programe.getPid();
uid = programe.getUid();
cpuInfo = new CpuInfo(getBaseContext(), pid, Integer.toString(uid));
}
}
}
}

Expand All @@ -508,8 +527,8 @@ private void updateViewPosition() {
public void closeOpenedStream() {
try {
if (bw != null) {
bw.write(getString(R.string.comment1) + Constants.LINE_END + getString(R.string.comment2) + Constants.LINE_END + getString(R.string.comment3) + Constants.LINE_END
+ getString(R.string.comment4) + Constants.LINE_END);
bw.write(getString(R.string.comment1) + Constants.LINE_END + getString(R.string.comment2) + Constants.LINE_END
+ getString(R.string.comment3) + Constants.LINE_END + getString(R.string.comment4) + Constants.LINE_END);
bw.close();
}
if (osw != null)
Expand Down
3 changes: 1 addition & 2 deletions src/com/netease/qa/emmagee/utils/CpuInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public void readCpuStat() {
processCpu = Long.parseLong(tok[13]) + Long.parseLong(tok[14]);
processCpuInfo.close();
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "FileNotFoundException: " + e.getMessage());
e.printStackTrace();
Log.w(LOG_TAG, "FileNotFoundException: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
19 changes: 19 additions & 0 deletions src/com/netease/qa/emmagee/utils/ProcessInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ private List<ApplicationInfo> getPackagesInfo(Context context) {
return appList;
}

/**
* get pid by package name
*
* @param context
* context of activity
* @param packageName
* package name of monitoring app
* @return pid
*/
public Programe getProgrameByPackageName(Context context, String packageName) {
List<Programe> processList = getRunningProcess(context);
for (Programe programe : processList) {
if ((programe.getPackageName() != null) && (programe.getPackageName().equals(packageName))) {
return programe;
}
}
return null;
}

/**
* get top activity name
*
Expand Down
1 change: 1 addition & 0 deletions src/com/netease/qa/emmagee/utils/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class Settings {
public static final String KEY_ISFLOAT = "isfloat";
public static final String KEY_INTERVAL = "interval";
public static final String KEY_ROOT = "root";
public static final String KEY_AUTO_STOP = "autoStop";

public static SharedPreferences getDefaultSharedPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
Expand Down

0 comments on commit 88bf84d

Please sign in to comment.