aboutsummaryrefslogtreecommitdiffstats
path: root/ddms
diff options
context:
space:
mode:
Diffstat (limited to 'ddms')
-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;
+
}