From 0d01f916d484964eed55e7a7d0c6f1c67aef8f98 Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Fri, 4 Nov 2011 09:23:14 -0700 Subject: Add IDevice.getBatteryLevel() Change-Id: I1db67422e2f39206383a3bd760769b2ebbf46ed6 --- .../libs/ddmlib/src/com/android/ddmlib/Device.java | 72 ++++++++++++++++++++++ .../ddmlib/src/com/android/ddmlib/IDevice.java | 25 ++++++++ 2 files changed, 97 insertions(+) (limited to 'ddms/libs') 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. + *

+ * 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 null 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. + *

+ * The battery level may be cached. Only queries the device for its + * battery level if freshnessMs ms have expired since the last successful query. + * + * @param freshnessMs + * @return the battery level or null if it could not be retrieved + * @throws ShellCommandUnresponsiveException + */ + public Integer getBatteryLevel(long freshnessMs) throws TimeoutException, + AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException; + } -- cgit v1.1