Skip to content

Commit

Permalink
add batt monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleo committed Aug 14, 2013
1 parent 6f1f27d commit 408aacb
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 10 deletions.
10 changes: 10 additions & 0 deletions res/layout/floating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
android:layout_weight="0.4"
android:gravity="right"
android:paddingRight="10.0dip"
android:textColor="#FFFFFF" />

<TextView
android:id="@+id/batt"
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"
Expand Down
11 changes: 8 additions & 3 deletions src/com/netease/qa/emmagee/service/EmmageeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ 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;
Expand Down Expand Up @@ -146,7 +147,7 @@ public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
currentBatt = String.valueOf(level * 100 / scale) + "%";
totalBatt = String.valueOf(level * 100 / scale) + "%";

voltage = String.valueOf(intent.getIntExtra(
BatteryManager.EXTRA_VOLTAGE, -1) * 1.0 / 1000);
Expand Down Expand Up @@ -188,6 +189,8 @@ public void onStart(Intent intent, int startId) {
.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);

Expand Down Expand Up @@ -277,7 +280,7 @@ private void createResultCsv() {
+ uid + "\r\n");
bw.write("时间" + "," + "应用占用内存PSS(MB)" + "," + "应用占用内存比(%)" + ","
+ " 机器剩余内存(MB)" + "," + "应用占用CPU率(%)" + "," + "CPU总使用率(%)"
+ "," + "流量(KB)" + "\r\n");
+ "," + "流量(KB)"+ "," + "电量(%)"+ "," + "电流(mA)"+ "," + "温度(C)"+ "," + "电压(V)" + "\r\n");
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
}
Expand Down Expand Up @@ -400,7 +403,8 @@ private void dataRefresh() {
long freeMemory = memoryInfo.getFreeMemorySize(getBaseContext());
String freeMemoryKb = fomart.format((double) freeMemory / 1024);
String processMemory = fomart.format((double) pidMemory / 1024);
ArrayList<String> processInfo = cpuInfo.getCpuRatioInfo();
String currentBatt = String.valueOf(currentInfo.getValue());
ArrayList<String> processInfo = cpuInfo.getCpuRatioInfo(totalBatt,currentBatt,temperature,voltage);
if (isFloating) {
String processCpuRatio = "0";
String totalCpuRatio = "0";
Expand Down Expand Up @@ -430,6 +434,7 @@ private void dataRefresh() {
+ freeMemoryKb + "MB");
txtTotalMem.setText("占用CPU:" + processCpuRatio + "%"
+ ",总体CPU:" + totalCpuRatio + "%");
txtBatt.setText("电量:"+totalBatt+",电流:"+currentBatt+"mA");
if ("-1".equals(trafficSize)) {
txtTraffic.setText("本程序或本设备不支持流量统计");
} else if (isMb)
Expand Down
22 changes: 15 additions & 7 deletions src/com/netease/qa/emmagee/utils/CpuInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public String getCpuName() {
* @return network traffic ,used ratio of process CPU and total CPU in
* certain interval
*/
public ArrayList<String> getCpuRatioInfo() {
public ArrayList<String> getCpuRatioInfo(String totalBatt,
String currentBatt, String temperature, String voltage) {

DecimalFormat fomart = new DecimalFormat();
fomart.setMaximumFractionDigits(2);
Expand All @@ -155,6 +156,10 @@ public ArrayList<String> getCpuRatioInfo() {
|| (Build.MODEL.equals("google_sdk"))) {
mDateTime2 = formatterFile.format(cal.getTime().getTime() + 8
* 60 * 60 * 1000);
totalBatt = "N/A";
currentBatt = "N/A";
temperature = "N/A";
voltage = "N/A";
} else
mDateTime2 = formatterFile.format(cal.getTime().getTime());

Expand All @@ -167,10 +172,10 @@ public ArrayList<String> getCpuRatioInfo() {
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);
Expand All @@ -186,11 +191,14 @@ public ArrayList<String> getCpuRatioInfo() {
EmmageeService.bw.write(mDateTime2 + "," + pMemory + ","
+ percent + "," + fMemory + "," + processCpuRatio
+ "," + totalCpuRatio + "," + "本程序或本设备不支持流量统计"
+ "\r\n");
+ "," + totalBatt + "," + currentBatt + ","
+ temperature + "," + voltage + "\r\n");
} else {
EmmageeService.bw.write(mDateTime2 + "," + pMemory + ","
+ percent + "," + fMemory + "," + processCpuRatio
+ "," + totalCpuRatio + "," + traffic + "\r\n");
+ "," + totalCpuRatio + "," + traffic + ","
+ totalBatt + "," + currentBatt + "," + temperature
+ "," + voltage + "\r\n");
}
}
totalCpu2 = totalCpu;
Expand Down
161 changes: 161 additions & 0 deletions src/com/netease/qa/emmagee/utils/CurrentInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,166 @@
package com.netease.qa.emmagee.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

import android.os.Build;
import android.util.Log;

public class CurrentInfo {
private static final String LOG_TAG = "Emmagee-CurrentInfo";

public Long getValue() {
File f = null;
// htc desire hd / desire z?
if (Build.MODEL.toLowerCase().contains("desire hd")
|| Build.MODEL.toLowerCase().contains("desire z")) {
f = new File("/sys/class/power_supply/battery/batt_current");
if (f.exists())
return getCurrentValue(f, false);
}

// sony ericsson xperia x1
f = new File(
"/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/ds2746-battery/current_now");
if (f.exists())
return getCurrentValue(f, false);
// xdandroid
/* if (Build.MODEL.equalsIgnoreCase("MSM")) { */
f = new File(
"/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/battery/current_now");
if (f.exists())
return getCurrentValue(f, false);
/* } */
// droid eris
f = new File("/sys/class/power_supply/battery/smem_text");
if (f.exists())
return getSMemValue();
// some htc devices
f = new File("/sys/class/power_supply/battery/batt_current");
if (f.exists())
return getCurrentValue(f, false);
// nexus one
f = new File("/sys/class/power_supply/battery/current_now");
if (f.exists())
return getCurrentValue(f, true);
// samsung galaxy vibrant
f = new File("/sys/class/power_supply/battery/batt_chg_current");
if (f.exists())
return getCurrentValue(f, false);
// sony ericsson x10
f = new File("/sys/class/power_supply/battery/charger_current");
if (f.exists())
return getCurrentValue(f, false);
return null;
}

public static Long getSMemValue() {
boolean success = false;
String text = null;
try {
FileReader fr = new FileReader(
"/sys/class/power_supply/battery/smem_text");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
if (line.contains("I_MBAT")) {
text = line.substring(line.indexOf("I_MBAT: ") + 8);
success = true;
break;
}
line = br.readLine();
}
br.close();
fr.close();
} catch (Exception ex) {
Log.e(LOG_TAG, ex.getMessage());
ex.printStackTrace();
}
Long value = null;
if (success) {
try {
value = Long.parseLong(text);
} catch (NumberFormatException nfe) {
Log.e(LOG_TAG, nfe.getMessage());
value = null;

}
}
return value;
}

public static Long getBattAttrValue(File f, String dischargeField,
String chargeField) {
String text = null;
Long value = null;
try {
// @@@ debug
// StringReader fr = new
// StringReader("vref: 1248\r\nbatt_id: 3\r\nbatt_vol: 4068\r\nbatt_current: 0\r\nbatt_discharge_current: 123\r\nbatt_temperature: 329\r\nbatt_temp_protection:normal\r\nPd_M:0\r\nI_MBAT:-313\r\npercent_last(RP): 94\r\npercent_update: 71\r\nlevel: 71\r\nfirst_level: 100\r\nfull_level:100\r\ncapacity:1580\r\ncharging_source: USB\r\ncharging_enabled: Slow\r\n");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
final String chargeFieldHead = chargeField + ": ";
final String dischargeFieldHead = dischargeField + ": ";
while (line != null) {
if (line.contains(chargeField)) {
text = line.substring(line.indexOf(chargeFieldHead)
+ chargeFieldHead.length());
try {
value = Long.parseLong(text);
if (value != 0)
break;
} catch (NumberFormatException nfe) {
Log.e(LOG_TAG, nfe.getMessage(), nfe);
}
}
// "batt_discharge_current:"
if (line.contains(dischargeField)) {
text = line.substring(line.indexOf(dischargeFieldHead)
+ dischargeFieldHead.length());
try {
value = (-1) * Math.abs(Long.parseLong(text));
} catch (NumberFormatException nfe) {
Log.e(LOG_TAG, nfe.getMessage(), nfe);
}
break;
}
line = br.readLine();
}
br.close();
fr.close();
} catch (Exception ex) {
Log.e(LOG_TAG, ex.getMessage(), ex);
}
return value;
}

public Long getCurrentValue(File file, boolean convertToMillis) {
String text = null;
try {
FileInputStream fs = new FileInputStream(file);
DataInputStream ds = new DataInputStream(fs);
text = ds.readLine();
ds.close();
fs.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Long value = null;

if (text != null) {
try {
value = Long.parseLong(text);
} catch (NumberFormatException nfe) {
value = null;
}
if (convertToMillis)
value = value / 1000; // convert to milliampere
}

return value;
}
}

0 comments on commit 408aacb

Please sign in to comment.