Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1982)

Unified Diff: build/android/pylib/device/device_utils.py

Issue 1067853003: [Android] Move power implementation from device_utils to battery_utils (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « build/android/pylib/device/battery_utils_test.py ('k') | build/android/pylib/device/device_utils_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 e432904ad690af0bf7d3c76faedd476185a0f6ed..5c92e261dccc02b99b29479bbacd9b55e48edf30 100644
--- a/build/android/pylib/device/device_utils.py
+++ b/build/android/pylib/device/device_utils.py
@@ -1439,159 +1439,6 @@ class DeviceUtils(object):
"""
return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs)
- # TODO(rnephew): Remove when battery_utils is switched to.
- @decorators.WithTimeoutAndRetriesFromInstance()
- def GetBatteryInfo(self, timeout=None, retries=None):
- """Gets battery info for the device.
-
- Args:
- timeout: timeout in seconds
- retries: number of retries
- Returns:
- A dict containing various battery information as reported by dumpsys
- battery.
- """
- result = {}
- # Skip the first line, which is just a header.
- for line in self.RunShellCommand(
- ['dumpsys', 'battery'], check_return=True)[1:]:
- # 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
-
- # TODO(rnephew): Remove when battery_utils is switched to.
- @decorators.WithTimeoutAndRetriesFromInstance()
- def GetCharging(self, timeout=None, retries=None):
- """Gets the charging state of the device.
-
- Args:
- timeout: timeout in seconds
- retries: number of retries
- Returns:
- True if the device is charging, false otherwise.
- """
- battery_info = self.GetBatteryInfo()
- for k in ('AC powered', 'USB powered', 'Wireless powered'):
- if (k in battery_info and
- battery_info[k].lower() in ('true', '1', 'yes')):
- return True
- return False
-
- # TODO(rnephew): Remove when battery_utils is switched to.
- @decorators.WithTimeoutAndRetriesFromInstance()
- def SetCharging(self, enabled, timeout=None, retries=None):
- """Enables or disables charging on the device.
-
- Args:
- enabled: A boolean indicating whether charging should be enabled or
- disabled.
- timeout: timeout in seconds
- retries: number of retries
- """
- if 'charging_config' not in self._cache:
- for c in _CONTROL_CHARGING_COMMANDS:
- if self.FileExists(c['witness_file']):
- self._cache['charging_config'] = c
- break
- else:
- raise device_errors.CommandFailedError(
- 'Unable to find charging commands.')
-
- if enabled:
- command = self._cache['charging_config']['enable_command']
- else:
- command = self._cache['charging_config']['disable_command']
-
- def set_and_verify_charging():
- self.RunShellCommand(command, check_return=True)
- return self.GetCharging() == enabled
-
- timeout_retry.WaitFor(set_and_verify_charging, wait_period=1)
-
- # TODO(rnephew): Remove when battery_utils is switched to.
- @decorators.WithTimeoutAndRetriesFromInstance()
- def DisableBatteryUpdates(self, timeout=None, retries=None):
- """ Resets battery data and makes device appear like it is not
- charging so that it will collect power data since last charge.
-
- Args:
- timeout: timeout in seconds
- retries: number of retries
- """
- def battery_updates_disabled():
- return self.GetCharging() is False
-
- self.RunShellCommand(
- ['dumpsys', 'batterystats', '--reset'], check_return=True)
- battery_data = self.RunShellCommand(
- ['dumpsys', 'batterystats', '--charged', '--checkin'],
- check_return=True)
- ROW_TYPE_INDEX = 3
- PWI_POWER_INDEX = 5
- for line in battery_data:
- l = line.split(',')
- if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi'
- and l[PWI_POWER_INDEX] != 0):
- raise device_errors.CommandFailedError(
- 'Non-zero pmi value found after reset.')
- self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'],
- check_return=True)
- timeout_retry.WaitFor(battery_updates_disabled, wait_period=1)
-
- # TODO(rnephew): Remove when battery_utils is switched to.
- @decorators.WithTimeoutAndRetriesFromInstance()
- def EnableBatteryUpdates(self, timeout=None, retries=None):
- """ Restarts device charging so that dumpsys no longer collects power data.
-
- Args:
- timeout: timeout in seconds
- retries: number of retries
- """
- def battery_updates_enabled():
- return self.GetCharging() is True
-
- self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '1'],
- check_return=True)
- self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True)
- timeout_retry.WaitFor(battery_updates_enabled, wait_period=1)
-
- # TODO(rnephew): Remove when battery_utils is switched to.
- @contextlib.contextmanager
- def BatteryMeasurement(self, timeout=None, retries=None):
- """Context manager that enables battery data collection. It makes
- the device appear to stop charging so that dumpsys will start collecting
- power data since last charge. Once the with block is exited, charging is
- resumed and power data since last charge is no longer collected.
-
- Only for devices L and higher.
-
- Example usage:
- with BatteryMeasurement():
- browser_actions()
- get_power_data() # report usage within this block
- after_measurements() # Anything that runs after power
- # measurements are collected
-
- Args:
- timeout: timeout in seconds
- retries: number of retries
- """
- if self.build_version_sdk < constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP:
- raise device_errors.CommandFailedError('Device must be L or higher.')
- try:
- 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.
« no previous file with comments | « build/android/pylib/device/battery_utils_test.py ('k') | build/android/pylib/device/device_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698