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

Side by Side Diff: build/android/pylib/device/battery_utils.py

Issue 1066253003: Disable ac charging similar to usb charging (used by Nexus6) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Limit BatteryDisable/EnableUpdates to only L+ devices 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 unified diff | Download patch
« no previous file with comments | « no previous file | build/android/pylib/device/battery_utils_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2015 The Chromium Authors. All rights reserved. 1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Provides a variety of device interactions with power. 5 """Provides a variety of device interactions with power.
6 """ 6 """
7 # pylint: disable=unused-argument 7 # pylint: disable=unused-argument
8 8
9 import collections 9 import collections
10 import contextlib 10 import contextlib
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 timeout: timeout in seconds 230 timeout: timeout in seconds
231 retries: number of retries 231 retries: number of retries
232 232
233 Raises: 233 Raises:
234 device_errors.CommandFailedError: When resetting batterystats fails to 234 device_errors.CommandFailedError: When resetting batterystats fails to
235 reset power values. 235 reset power values.
236 """ 236 """
237 def battery_updates_disabled(): 237 def battery_updates_disabled():
238 return self.GetCharging() is False 238 return self.GetCharging() is False
239 239
240 if (self._device.build_version_sdk <
jbudorick 2015/04/08 17:43:11 Please add this exception to the Raises: section o
fmeawad 2015/04/08 18:00:23 Done.
241 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP):
242 raise device_errors.DeviceVersionError('Device must be L or higher.')
243
240 self._device.RunShellCommand( 244 self._device.RunShellCommand(
241 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True) 245 ['dumpsys', 'battery', 'reset'], check_return=True)
242 self._device.RunShellCommand( 246 self._device.RunShellCommand(
243 ['dumpsys', 'batterystats', '--reset'], check_return=True) 247 ['dumpsys', 'batterystats', '--reset'], check_return=True)
244 battery_data = self._device.RunShellCommand( 248 battery_data = self._device.RunShellCommand(
245 ['dumpsys', 'batterystats', '--charged', '--checkin'], 249 ['dumpsys', 'batterystats', '--charged', '--checkin'],
246 check_return=True) 250 check_return=True)
247 ROW_TYPE_INDEX = 3 251 ROW_TYPE_INDEX = 3
248 PWI_POWER_INDEX = 5 252 PWI_POWER_INDEX = 5
249 for line in battery_data: 253 for line in battery_data:
250 l = line.split(',') 254 l = line.split(',')
251 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi' 255 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi'
252 and l[PWI_POWER_INDEX] != 0): 256 and l[PWI_POWER_INDEX] != 0):
253 raise device_errors.CommandFailedError( 257 raise device_errors.CommandFailedError(
254 'Non-zero pmi value found after reset.') 258 'Non-zero pmi value found after reset.')
259 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'ac', '0'],
260 check_return=True)
255 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], 261 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'],
256 check_return=True) 262 check_return=True)
257 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) 263 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1)
258 264
259 # TODO(rnephew): Make private when all use cases can use the context manager. 265 # TODO(rnephew): Make private when all use cases can use the context manager.
260 def EnableBatteryUpdates(self, timeout=None, retries=None): 266 def EnableBatteryUpdates(self, timeout=None, retries=None):
261 """ Restarts device charging so that dumpsys no longer collects power data. 267 """ Restarts device charging so that dumpsys no longer collects power data.
262 268
263 Args: 269 Args:
264 timeout: timeout in seconds 270 timeout: timeout in seconds
265 retries: number of retries 271 retries: number of retries
266 """ 272 """
267 def battery_updates_enabled(): 273 def battery_updates_enabled():
268 return self.GetCharging() is True 274 return self.GetCharging() is True
269 275
270 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '1'], 276 if (self._device.build_version_sdk <
jbudorick 2015/04/08 17:43:11 same here
fmeawad 2015/04/08 18:00:23 Done.
271 check_return=True) 277 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP):
278 raise device_errors.DeviceVersionError('Device must be L or higher.')
279
272 self._device.RunShellCommand(['dumpsys', 'battery', 'reset'], 280 self._device.RunShellCommand(['dumpsys', 'battery', 'reset'],
273 check_return=True) 281 check_return=True)
274 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) 282 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1)
275 283
276 @contextlib.contextmanager 284 @contextlib.contextmanager
277 def BatteryMeasurement(self, timeout=None, retries=None): 285 def BatteryMeasurement(self, timeout=None, retries=None):
278 """Context manager that enables battery data collection. It makes 286 """Context manager that enables battery data collection. It makes
279 the device appear to stop charging so that dumpsys will start collecting 287 the device appear to stop charging so that dumpsys will start collecting
280 power data since last charge. Once the with block is exited, charging is 288 power data since last charge. Once the with block is exited, charging is
281 resumed and power data since last charge is no longer collected. 289 resumed and power data since last charge is no longer collected.
282 290
283 Only for devices L and higher. 291 Only for devices L and higher.
284 292
285 Example usage: 293 Example usage:
286 with BatteryMeasurement(): 294 with BatteryMeasurement():
287 browser_actions() 295 browser_actions()
288 get_power_data() # report usage within this block 296 get_power_data() # report usage within this block
289 after_measurements() # Anything that runs after power 297 after_measurements() # Anything that runs after power
290 # measurements are collected 298 # measurements are collected
291 299
292 Args: 300 Args:
293 timeout: timeout in seconds 301 timeout: timeout in seconds
294 retries: number of retries 302 retries: number of retries
295 303
296 Raises: 304 Raises:
297 device_errors.CommandFailedError: If device is not L or higher. 305 device_errors.CommandFailedError: If device is not L or higher.
fmeawad 2015/04/08 18:00:23 Also fixed this one to be DeviceVersionError
298 """ 306 """
299 if (self._device.build_version_sdk < 307 if (self._device.build_version_sdk <
300 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): 308 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP):
301 raise device_errors.DeviceVersionError('Device must be L or higher.') 309 raise device_errors.DeviceVersionError('Device must be L or higher.')
302 try: 310 try:
303 self.DisableBatteryUpdates(timeout=timeout, retries=retries) 311 self.DisableBatteryUpdates(timeout=timeout, retries=retries)
304 yield 312 yield
305 finally: 313 finally:
306 self.EnableBatteryUpdates(timeout=timeout, retries=retries) 314 self.EnableBatteryUpdates(timeout=timeout, retries=retries)
307 315
(...skipping 10 matching lines...) Expand all
318 battery_level = self.GetBatteryInfo().get('level') 326 battery_level = self.GetBatteryInfo().get('level')
319 if battery_level is None: 327 if battery_level is None:
320 logging.warning('Unable to find current battery level.') 328 logging.warning('Unable to find current battery level.')
321 battery_level = 100 329 battery_level = 100
322 else: 330 else:
323 logging.info('current battery level: %s', battery_level) 331 logging.info('current battery level: %s', battery_level)
324 battery_level = int(battery_level) 332 battery_level = int(battery_level)
325 return battery_level >= level 333 return battery_level >= level
326 334
327 timeout_retry.WaitFor(device_charged, wait_period=wait_period) 335 timeout_retry.WaitFor(device_charged, wait_period=wait_period)
OLDNEW
« no previous file with comments | « no previous file | build/android/pylib/device/battery_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698