提交 630c877f 编写于 作者: A andrewleo

issue#11 过滤异常的数据;issue#13 保留应用的选中状态,优化界面

......@@ -42,36 +42,38 @@
android:id="@+id/memtotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_weight="0.4"
android:gravity="right"
android:paddingRight="10.0dip"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/batt"
android:id="@+id/traffic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_weight="0.4"
android:gravity="right"
android:paddingRight="10.0dip"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/traffic"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_weight="0.4"
android:gravity="right"
android:paddingRight="10.0dip"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/wifi"
android:layout_width="80dp"
android:layout_height="40dp" />
android:orientation="horizontal"
android:layout_weight="0.4">
<Button
android:id="@+id/wifi"
android:textSize = "12dp"
android:layout_width="80dp"
android:layout_height="40dp" />
<Button
android:id="@+id/stop"
android:text="停止测试"
android:textSize = "12dp"
android:layout_width="80dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
......
......@@ -23,5 +23,6 @@
<string name="ok">保存</string>
<string name="openwifi">开启wifi</string>
<string name="closewifi">关闭wifi</string>
<string name="stopTest">停止测试</string>
</resources>
\ No newline at end of file
......@@ -60,8 +60,7 @@ import com.netease.qa.emmagee.R;
*/
public class MainPageActivity extends Activity {
private static final String LOG_TAG = "Emmagee-"
+ MainPageActivity.class.getSimpleName();
private static final String LOG_TAG = "Emmagee-" + MainPageActivity.class.getSimpleName();
private static final int TIMEOUT = 20000;
......@@ -89,12 +88,10 @@ 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 ("开始测试".equals(btnTest.getText().toString())) {
if (isRadioChecked) {
Intent intent = getPackageManager()
.getLaunchIntentForPackage(packageName);
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
Log.d(LOG_TAG, packageName);
//clear logcat
try {
......@@ -105,8 +102,7 @@ public class MainPageActivity extends Activity {
try {
startActivity(intent);
} catch (NullPointerException e) {
Toast.makeText(MainPageActivity.this, "该程序无法启动",
Toast.LENGTH_LONG).show();
Toast.makeText(MainPageActivity.this, "该程序无法启动", Toast.LENGTH_LONG).show();
return;
}
waitForAppStart(packageName);
......@@ -114,23 +110,20 @@ public class MainPageActivity extends Activity {
monitorService.putExtra("pid", pid);
monitorService.putExtra("uid", uid);
monitorService.putExtra("packageName", packageName);
monitorService.putExtra("settingTempFile",
settingTempFile);
monitorService.putExtra("settingTempFile", settingTempFile);
startService(monitorService);
btnTest.setText("停止测试");
} else {
Toast.makeText(MainPageActivity.this, "请选择需要测试的应用程序",
Toast.LENGTH_LONG).show();
Toast.makeText(MainPageActivity.this, "请选择需要测试的应用程序", Toast.LENGTH_LONG).show();
}
} else {
btnTest.setText("开始测试");
Toast.makeText(MainPageActivity.this,
"测试结果文件:" + EmmageeService.resultFilePath,
Toast.LENGTH_LONG).show();
Toast.makeText(MainPageActivity.this, "测试结果文件:" + EmmageeService.resultFilePath, Toast.LENGTH_LONG).show();
stopService(monitorService);
}
}
});
lstViProgramme.setAdapter(new ListAdapter());
}
/**
......@@ -166,7 +159,6 @@ public class MainPageActivity extends Activity {
if (EmmageeService.isStop) {
btnTest.setText("开始测试");
}
lstViProgramme.setAdapter(new ListAdapter());
}
/**
......@@ -174,8 +166,7 @@ public class MainPageActivity extends Activity {
*/
private void createNewFile() {
Log.i(LOG_TAG, "create new file to save setting data");
settingTempFile = getBaseContext().getFilesDir().getPath()
+ "\\EmmageeSettings.properties";
settingTempFile = getBaseContext().getFilesDir().getPath() + "\\EmmageeSettings.properties";
Log.i(LOG_TAG, "settingFile = " + settingTempFile);
File settingFile = new File(settingTempFile);
if (!settingFile.exists()) {
......@@ -210,8 +201,7 @@ public class MainPageActivity extends Activity {
while (System.currentTimeMillis() < startTime + TIMEOUT) {
processList = processInfo.getRunningProcess(getBaseContext());
for (Programe programe : processList) {
if ((programe.getPackageName() != null)
&& (programe.getPackageName().equals(packageName))) {
if ((programe.getPackageName() != null) && (programe.getPackageName().equals(packageName))) {
pid = programe.getPid();
Log.d(LOG_TAG, "pid:" + pid);
uid = programe.getUid();
......@@ -247,10 +237,8 @@ public class MainPageActivity extends Activity {
* @return true
*/
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, Menu.FIRST, 0, "退出").setIcon(
android.R.drawable.ic_menu_delete);
menu.add(0, Menu.FIRST, 1, "设置").setIcon(
android.R.drawable.ic_menu_directions);
menu.add(0, Menu.FIRST, 0, "退出").setIcon(android.R.drawable.ic_menu_delete);
menu.add(0, Menu.FIRST, 1, "设置").setIcon(android.R.drawable.ic_menu_directions);
return true;
}
......@@ -284,24 +272,19 @@ public class MainPageActivity extends Activity {
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("确定退出程序?")
.setPositiveButton(
"确定",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (monitorService != null) {
Log.d(LOG_TAG, "stop service");
stopService(monitorService);
}
Log.d(LOG_TAG, "exit Emmagee");
EmmageeService.closeOpenedStream();
finish();
System.exit(0);
}
}).setNegativeButton("取消", null).create();
return new AlertDialog.Builder(this).setTitle("确定退出程序?").setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (monitorService != null) {
Log.d(LOG_TAG, "stop service");
stopService(monitorService);
}
Log.d(LOG_TAG, "exit Emmagee");
EmmageeService.closeOpenedStream();
finish();
System.exit(0);
}
}).setNegativeButton("取消", null).create();
default:
return null;
}
......@@ -350,37 +333,30 @@ public class MainPageActivity extends Activity {
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder holder = new Viewholder();
final int i = position;
convertView = MainPageActivity.this.getLayoutInflater().inflate(
R.layout.list_item, null);
holder.imgViAppIcon = (ImageView) convertView
.findViewById(R.id.image);
convertView = MainPageActivity.this.getLayoutInflater().inflate(R.layout.list_item, null);
holder.imgViAppIcon = (ImageView) convertView.findViewById(R.id.image);
holder.txtAppName = (TextView) convertView.findViewById(R.id.text);
holder.rdoBtnApp = (RadioButton) convertView.findViewById(R.id.rb);
holder.rdoBtnApp.setId(position);
holder.rdoBtnApp
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
isRadioChecked = true;
// Radio function
if (tempPosition != -1) {
RadioButton tempButton = (RadioButton) findViewById(tempPosition);
if ((tempButton != null)
&& (tempPosition != i)) {
tempButton.setChecked(false);
}
}
tempPosition = buttonView.getId();
packageName = programe.get(tempPosition)
.getPackageName();
processName = programe.get(tempPosition)
.getProcessName();
holder.rdoBtnApp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
isRadioChecked = true;
// Radio function
if (tempPosition != -1) {
RadioButton tempButton = (RadioButton) findViewById(tempPosition);
if ((tempButton != null) && (tempPosition != i)) {
tempButton.setChecked(false);
}
}
});
tempPosition = buttonView.getId();
packageName = programe.get(tempPosition).getPackageName();
processName = programe.get(tempPosition).getProcessName();
}
}
});
if (tempPosition == position) {
if (!holder.rdoBtnApp.isChecked())
holder.rdoBtnApp.setChecked(true);
......
......@@ -90,7 +90,6 @@ public class EmmageeService extends Service {
private TextView txtTotalMem;
private TextView txtUnusedMem;
private TextView txtTraffic;
private TextView txtBatt;
private ImageView imgViIcon;
private Button btnWifi;
private int delaytime;
......@@ -192,7 +191,6 @@ public class EmmageeService extends Service {
viFloatingWindow = LayoutInflater.from(this).inflate(R.layout.floating, null);
txtUnusedMem = (TextView) viFloatingWindow.findViewById(R.id.memunused);
txtTotalMem = (TextView) viFloatingWindow.findViewById(R.id.memtotal);
txtBatt = (TextView) viFloatingWindow.findViewById(R.id.batt);
txtTraffic = (TextView) viFloatingWindow.findViewById(R.id.traffic);
btnWifi = (Button) viFloatingWindow.findViewById(R.id.wifi);
......@@ -206,15 +204,28 @@ public class EmmageeService extends Service {
txtUnusedMem.setTextColor(android.graphics.Color.RED);
txtTotalMem.setTextColor(android.graphics.Color.RED);
txtTraffic.setTextColor(android.graphics.Color.RED);
txtBatt.setTextColor(android.graphics.Color.RED);
imgViIcon = (ImageView) viFloatingWindow.findViewById(R.id.img2);
imgViIcon.setVisibility(View.GONE);
imgViIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(EmmageeService.this, "测试结果文件:" + resultFilePath, Toast.LENGTH_LONG).show();
stopSelf();
}
});
createFloatingWindow();
}
createResultCsv();
handler.postDelayed(task, 1000);
}
/**
* 点击左上的关闭按钮可以停止测试
*/
private void closeTest() {
}
/**
* read configuration file.
*
......@@ -436,7 +447,7 @@ public class EmmageeService extends Service {
processCpuRatio = processInfo.get(0);
totalCpuRatio = processInfo.get(1);
trafficSize = processInfo.get(2);
if ("".equals(trafficSize) && !("-1".equals(trafficSize))) {
if (!("".equals(trafficSize)) && !("-1".equals(trafficSize))) {
tempTraffic = Integer.parseInt(trafficSize);
if (tempTraffic > 1024) {
isMb = true;
......@@ -444,21 +455,23 @@ public class EmmageeService extends Service {
}
}
}
if ("0".equals(processMemory) && "0.00".equals(processCpuRatio)) {
closeOpenedStream();
isServiceStop = true;
return;
}
// 如果cpu使用率存在且都不小于0,则输出
if (processCpuRatio != null && totalCpuRatio != null) {
txtUnusedMem.setText("占用内存:" + processMemory + "MB" + ",机器剩余:" + freeMemoryKb + "MB");
txtTotalMem.setText("占用CPU:" + processCpuRatio + "%" + ",总体CPU:" + totalCpuRatio + "%");
txtBatt.setText("电量:" + totalBatt + ",电流:" + currentBatt + "mA");
txtUnusedMem.setText("应用/剩余内存:" + processMemory + "/" + freeMemoryKb + "MB");
txtTotalMem.setText("应用/总体CPU:" + processCpuRatio + "%/" + totalCpuRatio + "%");
String batt = "电流:" + currentBatt + "mA,";
if ("-1".equals(trafficSize)) {
txtTraffic.setText("本程序或本设备不支持流量统计");
txtTraffic.setText(batt + "本程序或本设备不支持流量统计");
} else if (isMb)
txtTraffic.setText("消耗流量:" + fomart.format(trafficMb) + "MB");
txtTraffic.setText(batt + "流量:" + fomart.format(trafficMb) + "MB");
else
txtTraffic.setText("消耗流量:" + trafficSize + "KB");
txtTraffic.setText(batt + "流量:" + trafficSize + "KB");
}
// 当内存为0切cpu使用率为0时则是被测应用退出
if ("0".equals(processMemory) && "0.00".equals(processCpuRatio)) {
closeOpenedStream();
isServiceStop = true;
return;
}
}
}
......@@ -477,8 +490,13 @@ public class EmmageeService extends Service {
*/
public static void closeOpenedStream() {
try {
if (bw != null)
if (bw != null) {
// TODO 补充一些注释
bw.write("注释:已知部分不支持的机型可在此查阅:https://github.com/NetEase/Emmagee/wiki/Some-devices-are-not-supported \r\n");
bw.write("电流:小于0是放电,大于0是充电,部分机型可能无数据\r\n");
bw.write("N/A意味不支持或者数据异常\r\n");
bw.close();
}
if (osw != null)
osw.close();
if (out != null)
......@@ -544,4 +562,8 @@ public class EmmageeService extends Service {
public IBinder onBind(Intent intent) {
return null;
}
private void is_double() {
}
}
\ No newline at end of file
......@@ -37,8 +37,7 @@ import android.util.Log;
*/
public class CpuInfo {
private static final String LOG_TAG = "Emmagee-"
+ CpuInfo.class.getSimpleName();
private static final String LOG_TAG = "Emmagee-" + CpuInfo.class.getSimpleName();
private Context context;
private long processCpu;
......@@ -80,8 +79,7 @@ public class CpuInfo {
String cpuStatPath = "/proc/" + processPid + "/stat";
try {
// monitor cpu stat of certain process
RandomAccessFile processCpuInfo = new RandomAccessFile(cpuStatPath,
"r");
RandomAccessFile processCpuInfo = new RandomAccessFile(cpuStatPath, "r");
String line = "";
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.setLength(0);
......@@ -103,10 +101,8 @@ public class CpuInfo {
RandomAccessFile cpuInfo = new RandomAccessFile("/proc/stat", "r");
String[] toks = cpuInfo.readLine().split("\\s+");
idleCpu = Long.parseLong(toks[4]);
totalCpu = Long.parseLong(toks[1]) + Long.parseLong(toks[2])
+ Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[5])
+ Long.parseLong(toks[7]);
totalCpu = Long.parseLong(toks[1]) + Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[5]) + Long.parseLong(toks[7]);
cpuInfo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
......@@ -122,8 +118,7 @@ public class CpuInfo {
*/
public String getCpuName() {
try {
RandomAccessFile cpuStat = new RandomAccessFile("/proc/cpuinfo",
"r");
RandomAccessFile cpuStat = new RandomAccessFile("/proc/cpuinfo", "r");
String[] cpu = cpuStat.readLine().split(":"); // cpu信息的前一段是含有processor字符串,此处替换为不显示
cpuStat.close();
return cpu[1];
......@@ -140,11 +135,10 @@ public class CpuInfo {
* @return network traffic ,used ratio of process CPU and total CPU in
* certain interval
*/
public ArrayList<String> getCpuRatioInfo(String totalBatt,
String currentBatt, String temperature, String voltage) {
public ArrayList<String> getCpuRatioInfo(String totalBatt, String currentBatt, String temperature, String voltage) {
DecimalFormat fomart = new DecimalFormat();
// fomart.setGroupingUsed(false);
// fomart.setGroupingUsed(false);
fomart.setMaximumFractionDigits(2);
fomart.setMinimumFractionDigits(2);
......@@ -154,10 +148,8 @@ public class CpuInfo {
try {
String mDateTime2;
Calendar cal = Calendar.getInstance();
if ((Build.MODEL.equals("sdk"))
|| (Build.MODEL.equals("google_sdk"))) {
mDateTime2 = formatterFile.format(cal.getTime().getTime() + 8
* 60 * 60 * 1000);
if ((Build.MODEL.equals("sdk")) || (Build.MODEL.equals("google_sdk"))) {
mDateTime2 = formatterFile.format(cal.getTime().getTime() + 8 * 60 * 60 * 1000);
totalBatt = "N/A";
currentBatt = "N/A";
temperature = "N/A";
......@@ -174,45 +166,56 @@ public class CpuInfo {
traffic = -1;
else
traffic = (lastestTraffic - initialTraffic + 1023) / 1024;
processCpuRatio = fomart
.format(100 * ((double) (processCpu - processCpu2) / ((double) (totalCpu - totalCpu2))));
totalCpuRatio = fomart
.format(100 * ((double) ((totalCpu - idleCpu) - (totalCpu2 - idleCpu2)) / (double) (totalCpu - totalCpu2)));
processCpuRatio = fomart.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);
String pMemory = fomart.format((double) pidMemory / 1024);
long freeMemory = mi.getFreeMemorySize(context);
String fMemory = fomart.format((double) freeMemory / 1024);
String percent = "统计出错";
if (totalMemorySize != 0) {
percent = fomart
.format(((double) pidMemory / (double) totalMemorySize) * 100);
percent = fomart.format(((double) pidMemory / (double) totalMemorySize) * 100);
}
// whether certain device supports traffic statics or not
if (traffic == -1) {
EmmageeService.bw.write(mDateTime2 + "," + pMemory + ","
+ percent + "," + fMemory + "," + processCpuRatio
+ "," + totalCpuRatio + "," + "本程序或本设备不支持流量统计"
+ "," + totalBatt + "," + currentBatt + ","
+ temperature + "," + voltage + "\r\n");
} else {
EmmageeService.bw.write(mDateTime2 + "," + pMemory + ","
+ percent + "," + fMemory + "," + processCpuRatio
+ "," + totalCpuRatio + "," + traffic + ","
+ totalBatt + "," + currentBatt + "," + temperature
+ "," + voltage + "\r\n");
// 当应用的cpu使用率大于0时才写入文件中,过滤掉异常数据
if (isDouble(processCpuRatio) && isDouble(totalCpuRatio)) {
// whether certain device supports traffic statics or not
if (traffic == -1) {
EmmageeService.bw.write(mDateTime2 + "," + pMemory + "," + percent + "," + fMemory + "," + processCpuRatio + ","
+ totalCpuRatio + "," + "N/A" + "," + totalBatt + "," + currentBatt + "," + temperature + "," + voltage
+ "\r\n");
} else {
EmmageeService.bw.write(mDateTime2 + "," + pMemory + "," + percent + "," + fMemory + "," + processCpuRatio + ","
+ totalCpuRatio + "," + traffic + "," + totalBatt + "," + currentBatt + "," + temperature + "," + voltage + "\r\n");
}
totalCpu2 = totalCpu;
processCpu2 = processCpu;
idleCpu2 = idleCpu;
cpuUsedRatio.add(processCpuRatio);
cpuUsedRatio.add(totalCpuRatio);
cpuUsedRatio.add(String.valueOf(traffic));
}
}
totalCpu2 = totalCpu;
processCpu2 = processCpu;
idleCpu2 = idleCpu;
cpuUsedRatio.add(processCpuRatio);
cpuUsedRatio.add(totalCpuRatio);
cpuUsedRatio.add(String.valueOf(traffic));
} catch (IOException e) {
e.printStackTrace();
// PttService.closeOpenedStream()
}
return cpuUsedRatio;
}
/**
* 判断text是否是一个double类型数据
*
* @param text
* @return
*/
private boolean isDouble(String text) {
try {
Double.parseDouble(text);
} catch (NumberFormatException e) {
return false;
}
return true;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册