提交 88bf84d7 编写于 作者: A andrewleo

Added: #37 Add a new option in Settings: stop monitoring when app exited

上级 b92a61df
......@@ -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"
......
......@@ -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>
......@@ -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>
......@@ -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>
......@@ -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;
......@@ -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;
......@@ -67,14 +65,17 @@ public class SettingsActivity extends Activity {
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);
......@@ -82,10 +83,12 @@ public class SettingsActivity extends Activity {
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() {
......@@ -142,7 +145,16 @@ public class SettingsActivity extends Activity {
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
......
......@@ -68,6 +68,8 @@ import com.netease.qa.emmagee.utils.EncryptData;
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;
/**
......@@ -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;
......@@ -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() {
......@@ -135,6 +140,7 @@ public class EmmageeService extends Service {
isServiceStop = false;
isStop = false;
memoryInfo = new MemoryInfo();
procInfo = new ProcessInfo();
fomart = new DecimalFormat();
fomart.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
fomart.setGroupingUsed(false);
......@@ -143,7 +149,7 @@ public class EmmageeService extends Service {
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));
}
/**
......@@ -237,6 +243,7 @@ public class EmmageeService extends Service {
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);
}
/**
......@@ -274,20 +281,21 @@ public class EmmageeService extends Service {
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
......@@ -449,7 +457,7 @@ public class EmmageeService extends Service {
} 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";
......@@ -474,17 +482,28 @@ public class EmmageeService extends Service {
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));
}
}
}
}
......@@ -508,8 +527,8 @@ public class EmmageeService extends Service {
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)
......
......@@ -103,8 +103,7 @@ public class CpuInfo {
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();
}
......
......@@ -90,6 +90,25 @@ public class ProcessInfo {
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
*
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册