Index: build/android/pylib/device/device_utils.py |
diff --git a/build/android/pylib/device/device_utils.py b/build/android/pylib/device/device_utils.py |
index b6fd7323bb9a3c71e566049719a963bba2ce9023..df35aebca4f34e230a0984a5e75b8392770b6fb3 100644 |
--- a/build/android/pylib/device/device_utils.py |
+++ b/build/android/pylib/device/device_utils.py |
@@ -9,6 +9,7 @@ Eventually, this will be based on adb_wrapper. |
# pylint: disable=unused-argument |
import collections |
+import contextlib |
import itertools |
import logging |
import multiprocessing |
@@ -69,7 +70,6 @@ _CONTROL_CHARGING_COMMANDS = [ |
}, |
] |
- |
@decorators.WithExplicitTimeoutAndRetries( |
_DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
def GetAVDs(): |
@@ -1414,8 +1414,16 @@ class DeviceUtils(object): |
# Skip the first line, which is just a header. |
for line in self.RunShellCommand( |
['dumpsys', 'battery'], check_return=True)[1:]: |
- k, v = line.split(': ', 1) |
- result[k.strip()] = v.strip() |
+ # If usb charging has been disabled, an extra line of header exists. |
+ if 'UPDATES STOPPED' in line: |
+ logging.warning('Dumpsys battery not receiving updates. ' |
+ 'Run dumpsys battery reset if this is in error.') |
+ elif ':' not in line: |
+ logging.warning('Unknown line found in dumpsys battery.') |
+ logging.warning(line) |
+ else: |
+ k, v = line.split(': ', 1) |
+ result[k.strip()] = v.strip() |
return result |
@decorators.WithTimeoutAndRetriesFromInstance() |
@@ -1466,6 +1474,63 @@ class DeviceUtils(object): |
timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) |
@decorators.WithTimeoutAndRetriesFromInstance() |
+ def _ResetBatteryData(self, timeout=None, retries=None): |
+ def reset_battery_data(): |
+ self.RunShellCommand(['dumpsys', 'batterystats', '--reset'], |
+ check_return=True) |
+ # Test that battery data was reset. |
+ battery_data = self.RunShellCommand( |
+ ['dumpsys', 'batterystats', '--charged', |
+ '--checkin'], check_return=True) |
+ for line in battery_data: |
+ l = line.split(',') |
+ if l[3] == 'pwi' and l[1] != 0: |
+ return False |
+ return True |
+ |
+ timeout_retry.WaitFor(reset_battery_data, wait_period=1) |
+ |
+ @decorators.WithTimeoutAndRetriesFromInstance() |
+ def _DisableBatteryUpdates(self, timeout=None, retries=None): |
perezju
2015/03/13 10:19:06
Thanks, this is looking a lot better. I'm still a
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
|
+ def disable_battery_updates(): |
+ self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], |
+ check_return=True) |
+ return self.GetCharging() is False |
+ |
+ timeout_retry.WaitFor(disable_battery_updates, wait_period=1) |
+ |
+ @decorators.WithTimeoutAndRetriesFromInstance() |
+ def _EnableBatteryUpdates(self, timeout=None, retries=None): |
+ def enable_battery_updates(): |
+ self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True) |
perezju
2015/03/13 10:19:07
ditto as above
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
|
+ return self.GetCharging() is True |
+ |
+ timeout_retry.WaitFor(enable_battery_updates, wait_period=1) |
+ |
+ @contextlib.contextmanager |
+ def BatteryMeasurement(self, timeout=None, retries=None): |
+ """Context manager that enables battery data collection when in. |
+ Only for devices L and higher. |
+ |
perezju
2015/03/13 10:19:06
nit: maybe the example would look a bit better wit
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
|
+ with BatteryMeasurement(): |
+ browser_actions() |
+ get_power_data() |
+ no_longer_collecting_data |
+ |
+ Args: |
+ timeout: timeout in seconds |
+ retries: number of retries |
+ """ |
+ if self.build_version_sdk < constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP: |
perezju
2015/03/13 10:19:07
nit: extra space after '<'
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
|
+ raise device_errors.CommandFailedError('Device must be L or higher.') |
+ try: |
+ self._ResetBatteryData(timeout=timeout, retries=retries) |
+ self._DisableBatteryUpdates(timeout=timeout, retries=retries) |
+ yield |
+ finally: |
+ self._EnableBatteryUpdates(timeout=timeout, retries=retries) |
+ |
+ @decorators.WithTimeoutAndRetriesFromInstance() |
def GetDevicePieWrapper(self, timeout=None, retries=None): |
"""Gets the absolute path to the run_pie wrapper on the device. |