diff --git a/src/com/netease/qa/emmagee/activity/MainPageActivity.java b/src/com/netease/qa/emmagee/activity/MainPageActivity.java index 2b9109e28b55d1ca1cfb2e2b7e81b90c973f4881..0398da4ad0499d37316eb50fc9134a9d1c6fddf6 100644 --- a/src/com/netease/qa/emmagee/activity/MainPageActivity.java +++ b/src/com/netease/qa/emmagee/activity/MainPageActivity.java @@ -42,6 +42,8 @@ public class MainPageActivity extends Activity { private final String LOG_TAG = "Emmagee-" + MainPageActivity.class.getSimpleName(); + private final int TIMEOUT = 20000; + private List processList; private ProcessInfo processInfo; private Intent MonitorService; @@ -49,7 +51,6 @@ public class MainPageActivity extends Activity { private Button btnTest; private boolean isTesting = true; private boolean isRadioChecked = false; - private int pid, uid; private String processName, packageName, settingTempFile; @@ -61,7 +62,6 @@ public class MainPageActivity extends Activity { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.mainpage); - createNewFile(); processInfo = new ProcessInfo(); lstViProgramme = (ListView) findViewById(R.id.processList); @@ -71,8 +71,8 @@ public class MainPageActivity extends Activity { @Override public void onClick(View v) { MonitorService = new Intent(); - MonitorService - .setClass(MainPageActivity.this, EmmageeService.class); + MonitorService.setClass(MainPageActivity.this, + EmmageeService.class); if (isTesting) { if (isRadioChecked == true) { Intent intent = getPackageManager() @@ -112,7 +112,7 @@ public class MainPageActivity extends Activity { settingTempFile = getBaseContext().getFilesDir().getPath() + "\\Emmagee_Settings.txt"; File settingFile = new File(settingTempFile); - if (!settingFile.exists()) // 若文件不存在 + if (!settingFile.exists()) try { settingFile.createNewFile(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( @@ -125,23 +125,31 @@ public class MainPageActivity extends Activity { } /** - * wait for test app start + * wait for test app start , timeout is 20s * * @param packageName * the package name of test app */ private void waitForAppStart(String packageName) { Log.d(LOG_TAG, "wait for app start"); - processList = processInfo.getRunningProcess(getBaseContext()); - for (Programe programe : processList) { - if ((programe.getPackageName() != null) - && (programe.getPackageName().equals(packageName))) { - pid = programe.getPid(); - uid = programe.getUid(); - if (pid != 0) { - break; + boolean isProcessStarted = false; + long startTime = System.currentTimeMillis(); + while (System.currentTimeMillis() < startTime + TIMEOUT) { + processList = processInfo.getRunningProcess(getBaseContext()); + for (Programe programe : processList) { + if ((programe.getPackageName() != null) + && (programe.getPackageName().equals(packageName))) { + pid = programe.getPid(); + Log.d(LOG_TAG, "pid:" + pid); + uid = programe.getUid(); + if (pid != 0) { + isProcessStarted = true; + break; + } } } + if (isProcessStarted) + break; } } @@ -163,7 +171,6 @@ public class MainPageActivity extends Activity { android.R.drawable.ic_menu_delete); menu.add(0, Menu.FIRST, 1, "设置").setIcon( android.R.drawable.ic_menu_directions); - return true; } @@ -291,7 +298,7 @@ public class MainPageActivity extends Activity { return convertView; } } - + @Override public void finish() { super.finish(); diff --git a/src/com/netease/qa/emmagee/service/EmmageeService.java b/src/com/netease/qa/emmagee/service/EmmageeService.java index 0805792a3d9e228b49a7a8070f40c9085b44778d..2fb6bd3d204f43fa5d1ceabdd2c30d1da41fefe4 100644 --- a/src/com/netease/qa/emmagee/service/EmmageeService.java +++ b/src/com/netease/qa/emmagee/service/EmmageeService.java @@ -39,6 +39,7 @@ import com.netease.qa.emmagee.utils.MyApplication; import com.netease.qa.emmagee.R; public class EmmageeService extends Service { + private final static String LOG_TAG = "Emmagee-" + EmmageeService.class.getSimpleName(); @@ -51,7 +52,6 @@ public class EmmageeService extends Service { private float startY; private float x; private float y; - private TextView txtTotalMem; private TextView txtUnusedMem; private TextView txtTraffic; @@ -98,7 +98,6 @@ public class EmmageeService extends Service { cpuInfo = new CpuInfo(getBaseContext(), pid,Integer.toString(uid)); readSettingInfo(intent); delaytime = Integer.parseInt(time) * 1000; - // 如果设置选项是显示浮窗的这里才展示出来 if (isFloating) { viFloatingWindow = LayoutInflater.from(this).inflate( R.layout.floating, null); @@ -213,7 +212,8 @@ public class EmmageeService extends Service { public boolean onTouch(View v, MotionEvent event) { // 获取相对屏幕的坐标,即以屏幕左上角为原点 x = event.getRawX(); - y = event.getRawY() - 25; // 25是系统状态栏的高度 + // 25是系统状态栏的高度 + y = event.getRawY() - 25; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // state = MotionEvent.ACTION_DOWN; @@ -230,7 +230,7 @@ public class EmmageeService extends Service { break; case MotionEvent.ACTION_UP: -// state = MotionEvent.ACTION_UP; + //state = MotionEvent.ACTION_UP; updateViewPosition(); showImg(); mTouchStartX = mTouchStartY = 0; @@ -285,7 +285,7 @@ public class EmmageeService extends Service { }; /** - * refresh data + * refresh the data displayed in floating window * * @throws FileNotFoundException * diff --git a/src/com/netease/qa/emmagee/utils/CpuInfo.java b/src/com/netease/qa/emmagee/utils/CpuInfo.java index 2a11e9e54ed706162c3c00a5ff6dd2c0de88a72f..1e1d2b441084b7b3093e1c6719c2fee3dd991861 100644 --- a/src/com/netease/qa/emmagee/utils/CpuInfo.java +++ b/src/com/netease/qa/emmagee/utils/CpuInfo.java @@ -38,7 +38,7 @@ public class CpuInfo { private String totalCpuRatio = ""; private int pid; - public CpuInfo(Context context,int pid,String uid) { + public CpuInfo(Context context, int pid, String uid) { this.pid = pid; this.context = context; trafficInfo = new TrafficInfo(uid); @@ -143,8 +143,7 @@ public class CpuInfo { .format(100 * ((double) (processCpu - processCpu2) / (double) (totalCpu - totalCpu2))); totalCpuRatio = fomart .format(100 * ((double) ((totalCpu - idleCpu) - (totalCpu2 - idleCpu2)) / (double) (totalCpu - totalCpu2))); - long pidMemory = mi.getPidMemorySize(pid, - context); + long pidMemory = mi.getPidMemorySize(pid, context); String pMemory = fomart.format((double) pidMemory / 1024); long freeMemory = mi.getFreeMemorySize(context); String fMemory = fomart.format((double) freeMemory / 1024); diff --git a/src/com/netease/qa/emmagee/utils/MemoryInfo.java b/src/com/netease/qa/emmagee/utils/MemoryInfo.java index beb15cf5d77ec5787dfabeb59da87753882d4d1b..a1a5121c0c9b1db295a2a3d50f1828e70bda91b2 100644 --- a/src/com/netease/qa/emmagee/utils/MemoryInfo.java +++ b/src/com/netease/qa/emmagee/utils/MemoryInfo.java @@ -15,7 +15,9 @@ public class MemoryInfo { + MemoryInfo.class.getSimpleName(); /** - * read the total memory of certain device 读取当前机器的内存总数,返回值为KB + * read the total memory of certain device + * + * @return total memory of device */ public long getTotalMemory() { String memInfoPath = "/proc/meminfo"; @@ -43,6 +45,9 @@ public class MemoryInfo { /** * get free memory + * + * @return free memory of device + * */ public long getFreeMemorySize(Context context) { ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo(); @@ -54,30 +59,31 @@ public class MemoryInfo { } /** - * get the memory of process with certain pid 获取指定pid的进程,当前占用的内存,返回值为KB + * get the memory of process with certain pid * * @param pid - * 进程pid + * pid of process * @param context - * activity上下文 - * @return + * context of certain activity + * @return memory usage of certain process */ public int getPidMemorySize(int pid, Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); int[] myMempid = new int[] { pid }; - // 此MemoryInfo位于android.os.Debug.MemoryInfo包中,用来统计进程的内存信息 Debug.MemoryInfo[] memoryInfo = am.getProcessMemoryInfo(myMempid); memoryInfo[0].getTotalSharedDirty(); // int memSize = memoryInfo[0].dalvikPrivateDirty; // TODO 不一定是PSS,可能是其他的 int memSize = memoryInfo[0].getTotalPss(); -// int memSize = memoryInfo[0].getTotalPrivateDirty(); + // int memSize = memoryInfo[0].getTotalPrivateDirty(); return memSize; } /** * get the sdk version of phone + * + * @return sdk version */ public String getSDKVersion() { return android.os.Build.VERSION.RELEASE; @@ -85,6 +91,8 @@ public class MemoryInfo { /** * get phone type + * + * @return phone type */ public String getPhoneType() { return android.os.Build.MODEL; diff --git a/src/com/netease/qa/emmagee/utils/MyApplication.java b/src/com/netease/qa/emmagee/utils/MyApplication.java index 49eec7a3751caf0a26af56a30fff9c84c9b9df27..3d85e96cf6aea77ca33adb81e6bf4433f032374f 100644 --- a/src/com/netease/qa/emmagee/utils/MyApplication.java +++ b/src/com/netease/qa/emmagee/utils/MyApplication.java @@ -8,11 +8,6 @@ public class MyApplication extends Application { private static final String LOG_TAG = "Emmagee-" + MyApplication.class.getSimpleName(); - /** - * 创建全局变量 全局变量一般都比较倾向于创建一个单独的数据类文件,并使用static静态变量 - * 这里使用了在Application中添加数据的方法实现全局变量 - * 注意在AndroidManifest.xml中的Application节点添加android:name=".MyApplication"属性 - */ private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams(); public WindowManager.LayoutParams getMywmParams() { diff --git a/src/com/netease/qa/emmagee/utils/ProcessInfo.java b/src/com/netease/qa/emmagee/utils/ProcessInfo.java index 845172807d385cc8d7d5df97dbd01d1799041fb5..6e8578ee2cedf7b42e6f52f54c04719e78b852f3 100644 --- a/src/com/netease/qa/emmagee/utils/ProcessInfo.java +++ b/src/com/netease/qa/emmagee/utils/ProcessInfo.java @@ -10,6 +10,7 @@ import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.util.Log; public class ProcessInfo { @@ -17,13 +18,15 @@ public class ProcessInfo { + ProcessInfo.class.getSimpleName(); private final String PACKAGE_NAME = "com.netease.qa.emmagee"; + /** * get information of all running processes * - * @param context - * @return + * @param context context of activity + * @return running process lists */ public List getRunningProcess(Context context) { + Log.i(LOG_TAG, "get running processes"); ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); @@ -61,10 +64,10 @@ public class ProcessInfo { } /** - * get information of all apps + * get information of all applications * - * @param context - * @return + * @param context context of activity + * @return packages information of all applications */ private List getPackagesInfo(Context context) { PackageManager pm = context.getApplicationContext().getPackageManager(); diff --git a/src/com/netease/qa/emmagee/utils/TrafficInfo.java b/src/com/netease/qa/emmagee/utils/TrafficInfo.java index 2cc9a8e7bc7066910fd221b3e75afa9cca55d712..06be9eaf0cc692a0f636e3ca9b6bcc0c4af85de9 100644 --- a/src/com/netease/qa/emmagee/utils/TrafficInfo.java +++ b/src/com/netease/qa/emmagee/utils/TrafficInfo.java @@ -17,7 +17,13 @@ public class TrafficInfo { this.uid = uid; } + /** + * get traffic information include received and send traffic + * + * @return total traffic include received and send traffic + */ public long getTrafficInfo() { + Log.i(LOG_TAG,"get traffic information"); String rcvPath = "/proc/uid_stat/" + uid + "/tcp_rcv"; String sndPath = "/proc/uid_stat/" + uid + "/tcp_snd"; long rcvTraffic = -1;