aboutsummaryrefslogtreecommitdiffstats
path: root/ddms/libs
diff options
context:
space:
mode:
authorBrett Chabot <brettchabot@android.com>2011-11-04 09:23:14 -0700
committerBrett Chabot <brettchabot@android.com>2011-11-04 17:05:56 -0700
commit0d01f916d484964eed55e7a7d0c6f1c67aef8f98 (patch)
tree06c0112d629e7bd313aac6b40fe7a63291a793d2 /ddms/libs
parent1507e2e00104ae377ff5aacfaf6a42975537fe38 (diff)
downloadsdk-0d01f916d484964eed55e7a7d0c6f1c67aef8f98.zip
sdk-0d01f916d484964eed55e7a7d0c6f1c67aef8f98.tar.gz
sdk-0d01f916d484964eed55e7a7d0c6f1c67aef8f98.tar.bz2
Add IDevice.getBatteryLevel()
Change-Id: I1db67422e2f39206383a3bd760769b2ebbf46ed6
Diffstat (limited to 'ddms/libs')
-rw-r--r--ddms/libs/ddmlib/src/com/android/ddmlib/Device.java72
-rw-r--r--ddms/libs/ddmlib/src/com/android/ddmlib/IDevice.java25
2 files changed, 97 insertions, 0 deletions
diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/Device.java b/ddms/libs/ddmlib/src/com/android/ddmlib/Device.java
index 8f02327..15420cd 100644
--- a/ddms/libs/ddmlib/src/com/android/ddmlib/Device.java
+++ b/ddms/libs/ddmlib/src/com/android/ddmlib/Device.java
@@ -65,6 +65,9 @@ final class Device implements IDevice {
private boolean mArePropertiesSet = false;
+ private Integer mLastBatteryLevel = null;
+ private long mLastBatteryCheckTime = 0;
+
/**
* Output receiver for "pm install package.apk" command line.
*/
@@ -103,6 +106,56 @@ final class Device implements IDevice {
}
}
+ /**
+ * Output receiver for "dumpsys battery" command line.
+ */
+ private static final class BatteryReceiver extends MultiLineReceiver {
+ private static final Pattern BATTERY_LEVEL = Pattern.compile("\\s*level: (\\d+)");
+ private static final Pattern SCALE = Pattern.compile("\\s*scale: (\\d+)");
+
+ private Integer mBatteryLevel = null;
+ private Integer mBatteryScale = null;
+
+ /**
+ * Get the parsed percent battery level.
+ * @return
+ */
+ public Integer getBatteryLevel() {
+ if (mBatteryLevel != null && mBatteryScale != null) {
+ return (mBatteryLevel * 100) / mBatteryScale;
+ }
+ return null;
+ }
+
+ @Override
+ public void processNewLines(String[] lines) {
+ for (String line : lines) {
+ Matcher batteryMatch = BATTERY_LEVEL.matcher(line);
+ if (batteryMatch.matches()) {
+ try {
+ mBatteryLevel = Integer.parseInt(batteryMatch.group(1));
+ } catch (NumberFormatException e) {
+ Log.w(LOG_TAG, String.format("Failed to parse %s as an integer",
+ batteryMatch.group(1)));
+ }
+ }
+ Matcher scaleMatch = SCALE.matcher(line);
+ if (scaleMatch.matches()) {
+ try {
+ mBatteryScale = Integer.parseInt(scaleMatch.group(1));
+ } catch (NumberFormatException e) {
+ Log.w(LOG_TAG, String.format("Failed to parse %s as an integer",
+ batteryMatch.group(1)));
+ }
+ }
+ }
+ }
+
+ public boolean isCancelled() {
+ return false;
+ }
+ }
+
/*
* (non-Javadoc)
* @see com.android.ddmlib.IDevice#getSerialNumber()
@@ -649,4 +702,23 @@ final class Device implements IDevice {
throws TimeoutException, AdbCommandRejectedException, IOException {
AdbHelper.reboot(into, AndroidDebugBridge.getSocketAddress(), this);
}
+
+ public Integer getBatteryLevel() throws TimeoutException, AdbCommandRejectedException,
+ IOException, ShellCommandUnresponsiveException {
+ // use default of 5 minutes
+ return getBatteryLevel(5 * 60 * 1000);
+ }
+
+ public Integer getBatteryLevel(long freshnessMs) throws TimeoutException,
+ AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException {
+ if (mLastBatteryLevel != null
+ && mLastBatteryCheckTime > (System.currentTimeMillis() - freshnessMs)) {
+ return mLastBatteryLevel;
+ }
+ BatteryReceiver receiver = new BatteryReceiver();
+ executeShellCommand("dumpsys battery", receiver);
+ mLastBatteryLevel = receiver.getBatteryLevel();
+ mLastBatteryCheckTime = System.currentTimeMillis();
+ return mLastBatteryLevel;
+ }
}
diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/IDevice.java b/ddms/libs/ddmlib/src/com/android/ddmlib/IDevice.java
index 324b6f2..9ba068b 100644
--- a/ddms/libs/ddmlib/src/com/android/ddmlib/IDevice.java
+++ b/ddms/libs/ddmlib/src/com/android/ddmlib/IDevice.java
@@ -445,4 +445,29 @@ public interface IDevice {
*/
public void reboot(String into)
throws TimeoutException, AdbCommandRejectedException, IOException;
+
+ /**
+ * Return the device's battery level, from 0 to 100 percent.
+ * <p/>
+ * The battery level may be cached. Only queries the device for its
+ * battery level if 5 minutes have expired since the last successful query.
+ *
+ * @return the battery level or <code>null</code> if it could not be retrieved
+ */
+ public Integer getBatteryLevel() throws TimeoutException,
+ AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException;
+
+ /**
+ * Return the device's battery level, from 0 to 100 percent.
+ * <p/>
+ * The battery level may be cached. Only queries the device for its
+ * battery level if <code>freshnessMs</code> ms have expired since the last successful query.
+ *
+ * @param freshnessMs
+ * @return the battery level or <code>null</code> if it could not be retrieved
+ * @throws ShellCommandUnresponsiveException
+ */
+ public Integer getBatteryLevel(long freshnessMs) throws TimeoutException,
+ AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException;
+
}