diff --git a/res/layout/settings.xml b/res/layout/settings.xml index 0d6d62fe1faf5c68d0bc51bcdc8debe167a27276..305169fb4f49b427df275b01ba830fd231af6f27 100644 --- a/res/layout/settings.xml +++ b/res/layout/settings.xml @@ -111,6 +111,33 @@ android:paddingRight="@dimen/image_padding" /> + + + + + + + 密码: SMTP服务器: 收件人(多人以空格分割): - 是否显示浮窗 + 显示浮窗 保存 计算中... @@ -84,4 +84,5 @@ (需要root) 无法获取root权限,请确认手机是否已经root过 + 应用退出后停止监听 diff --git a/res/values/strings.xml b/res/values/strings.xml index fd47d40699aa16a41db9a886a356522d71e8275f..cafe0b9675f8652f92180dc2b0a11c115b52e5c6 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -83,4 +83,5 @@ Collect Heap (root is necessary) Fail to get root permission, please check if this phone is rooted + Stop monitoring when app exited diff --git a/src/com/netease/qa/emmagee/activity/SettingsActivity.java b/src/com/netease/qa/emmagee/activity/SettingsActivity.java index cc0fe0174fd1fe6ebdc1e5daf8e09c4ae4fa0a65..f51535afe0620c6bac907557d85e8718eceb2d6b 100644 --- a/src/com/netease/qa/emmagee/activity/SettingsActivity.java +++ b/src/com/netease/qa/emmagee/activity/SettingsActivity.java @@ -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 diff --git a/src/com/netease/qa/emmagee/service/EmmageeService.java b/src/com/netease/qa/emmagee/service/EmmageeService.java index d2bee786bad62bf69c2bf1002376067e92936da5..c6d9c7aa48e9314b50afc2aecfc16a8a3ab0b792 100644 --- a/src/com/netease/qa/emmagee/service/EmmageeService.java +++ b/src/com/netease/qa/emmagee/service/EmmageeService.java @@ -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 processInfo = cpuInfo.getCpuRatioInfo(totalBatt, currentBatt, temperature, voltage,isRoot); + ArrayList 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) diff --git a/src/com/netease/qa/emmagee/utils/CpuInfo.java b/src/com/netease/qa/emmagee/utils/CpuInfo.java index ea9627b341047fc77737428e1a973b3ccd09b22e..7ce27f431c5ddde2030b8e6438bcf8b4131a2884 100644 --- a/src/com/netease/qa/emmagee/utils/CpuInfo.java +++ b/src/com/netease/qa/emmagee/utils/CpuInfo.java @@ -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(); } diff --git a/src/com/netease/qa/emmagee/utils/ProcessInfo.java b/src/com/netease/qa/emmagee/utils/ProcessInfo.java index f4fdd7bdb77965a20a88144c4ce532e77dbf311e..bc7a2d7a6f6549c823f659ace46e933534db3e3a 100644 --- a/src/com/netease/qa/emmagee/utils/ProcessInfo.java +++ b/src/com/netease/qa/emmagee/utils/ProcessInfo.java @@ -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 processList = getRunningProcess(context); + for (Programe programe : processList) { + if ((programe.getPackageName() != null) && (programe.getPackageName().equals(packageName))) { + return programe; + } + } + return null; + } + /** * get top activity name * diff --git a/src/com/netease/qa/emmagee/utils/Settings.java b/src/com/netease/qa/emmagee/utils/Settings.java index 1e9e50c6a15075ac8e81303588d98eb6c5083465..344a17c269682f395debeb641633ccb63d5ce780 100644 --- a/src/com/netease/qa/emmagee/utils/Settings.java +++ b/src/com/netease/qa/emmagee/utils/Settings.java @@ -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);