OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 based on adb. | 5 """Provides a variety of device interactions based on adb. |
6 | 6 |
7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
8 """ | 8 """ |
9 # pylint: disable=unused-argument | 9 # pylint: disable=unused-argument |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 import pylib.android_commands | 22 import pylib.android_commands |
23 from pylib import cmd_helper | 23 from pylib import cmd_helper |
24 from pylib import constants | 24 from pylib import constants |
25 from pylib.device import adb_wrapper | 25 from pylib.device import adb_wrapper |
26 from pylib.device import decorators | 26 from pylib.device import decorators |
27 from pylib.device import device_errors | 27 from pylib.device import device_errors |
28 from pylib.device import intent | 28 from pylib.device import intent |
29 from pylib.device import logcat_monitor | 29 from pylib.device import logcat_monitor |
30 from pylib.device.commands import install_commands | 30 from pylib.device.commands import install_commands |
31 from pylib.utils import apk_helper | 31 from pylib.utils import apk_helper |
| 32 from pylib.utils import base_error |
32 from pylib.utils import device_temp_file | 33 from pylib.utils import device_temp_file |
33 from pylib.utils import host_utils | 34 from pylib.utils import host_utils |
34 from pylib.utils import md5sum | 35 from pylib.utils import md5sum |
35 from pylib.utils import parallelizer | 36 from pylib.utils import parallelizer |
36 from pylib.utils import timeout_retry | 37 from pylib.utils import timeout_retry |
37 from pylib.utils import zip_utils | 38 from pylib.utils import zip_utils |
38 | 39 |
39 _DEFAULT_TIMEOUT = 30 | 40 _DEFAULT_TIMEOUT = 30 |
40 _DEFAULT_RETRIES = 3 | 41 _DEFAULT_RETRIES = 3 |
41 | 42 |
42 # A sentinel object for default values | 43 # A sentinel object for default values |
43 # TODO(jbudorick,perezju): revisit how default values are handled by | 44 # TODO(jbudorick,perezju): revisit how default values are handled by |
44 # the timeout_retry decorators. | 45 # the timeout_retry decorators. |
45 DEFAULT = object() | 46 DEFAULT = object() |
46 | 47 |
47 _CONTROL_USB_CHARGING_COMMANDS = [ | 48 _CONTROL_CHARGING_COMMANDS = [ |
48 { | 49 { |
49 # Nexus 4 | 50 # Nexus 4 |
50 'witness_file': '/sys/module/pm8921_charger/parameters/disabled', | 51 'witness_file': '/sys/module/pm8921_charger/parameters/disabled', |
51 'enable_command': 'echo 0 > /sys/module/pm8921_charger/parameters/disabled', | 52 'enable_command': 'echo 0 > /sys/module/pm8921_charger/parameters/disabled', |
52 'disable_command': | 53 'disable_command': |
53 'echo 1 > /sys/module/pm8921_charger/parameters/disabled', | 54 'echo 1 > /sys/module/pm8921_charger/parameters/disabled', |
54 }, | 55 }, |
55 { | 56 { |
56 # Nexus 5 | 57 # Nexus 5 |
57 # Setting the HIZ bit of the bq24192 causes the charger to actually ignore | 58 # Setting the HIZ bit of the bq24192 causes the charger to actually ignore |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 retries: number of retries | 181 retries: number of retries |
181 | 182 |
182 Returns: | 183 Returns: |
183 True if the device is online, False otherwise. | 184 True if the device is online, False otherwise. |
184 | 185 |
185 Raises: | 186 Raises: |
186 CommandTimeoutError on timeout. | 187 CommandTimeoutError on timeout. |
187 """ | 188 """ |
188 try: | 189 try: |
189 return self.adb.GetState() == 'device' | 190 return self.adb.GetState() == 'device' |
190 except device_errors.BaseError as exc: | 191 except base_error.BaseError as exc: |
191 logging.info('Failed to get state: %s', exc) | 192 logging.info('Failed to get state: %s', exc) |
192 return False | 193 return False |
193 | 194 |
194 @decorators.WithTimeoutAndRetriesFromInstance() | 195 @decorators.WithTimeoutAndRetriesFromInstance() |
195 def HasRoot(self, timeout=None, retries=None): | 196 def HasRoot(self, timeout=None, retries=None): |
196 """Checks whether or not adbd has root privileges. | 197 """Checks whether or not adbd has root privileges. |
197 | 198 |
198 Args: | 199 Args: |
199 timeout: timeout in seconds | 200 timeout: timeout in seconds |
200 retries: number of retries | 201 retries: number of retries |
(...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1411 """ | 1412 """ |
1412 result = {} | 1413 result = {} |
1413 # Skip the first line, which is just a header. | 1414 # Skip the first line, which is just a header. |
1414 for line in self.RunShellCommand( | 1415 for line in self.RunShellCommand( |
1415 ['dumpsys', 'battery'], check_return=True)[1:]: | 1416 ['dumpsys', 'battery'], check_return=True)[1:]: |
1416 k, v = line.split(': ', 1) | 1417 k, v = line.split(': ', 1) |
1417 result[k.strip()] = v.strip() | 1418 result[k.strip()] = v.strip() |
1418 return result | 1419 return result |
1419 | 1420 |
1420 @decorators.WithTimeoutAndRetriesFromInstance() | 1421 @decorators.WithTimeoutAndRetriesFromInstance() |
1421 def GetUsbCharging(self, timeout=None, retries=None): | 1422 def GetCharging(self, timeout=None, retries=None): |
1422 """Gets the USB charging state of the device. | 1423 """Gets the charging state of the device. |
1423 | 1424 |
1424 Args: | 1425 Args: |
1425 timeout: timeout in seconds | 1426 timeout: timeout in seconds |
1426 retries: number of retries | 1427 retries: number of retries |
1427 Returns: | 1428 Returns: |
1428 True if the device is charging via USB, false otherwise. | 1429 True if the device is charging, false otherwise. |
1429 """ | 1430 """ |
1430 return (self.GetBatteryInfo().get('USB powered', '').lower() | 1431 battery_info = self.GetBatteryInfo() |
1431 in ('true', '1', 'yes')) | 1432 for k in ('AC powered', 'USB powered', 'Wireless powered'): |
| 1433 if (k in battery_info and |
| 1434 battery_info[k].lower() in ('true', '1', 'yes')): |
| 1435 return True |
| 1436 return False |
1432 | 1437 |
1433 @decorators.WithTimeoutAndRetriesFromInstance() | 1438 @decorators.WithTimeoutAndRetriesFromInstance() |
1434 def SetUsbCharging(self, enabled, timeout=None, retries=None): | 1439 def SetCharging(self, enabled, timeout=None, retries=None): |
1435 """Enables or disables USB charging on the device. | 1440 """Enables or disables charging on the device. |
1436 | 1441 |
1437 Args: | 1442 Args: |
1438 enabled: A boolean indicating whether USB charging should be enabled or | 1443 enabled: A boolean indicating whether charging should be enabled or |
1439 disabled. | 1444 disabled. |
1440 timeout: timeout in seconds | 1445 timeout: timeout in seconds |
1441 retries: number of retries | 1446 retries: number of retries |
1442 """ | 1447 """ |
1443 if 'usb_charging_config' not in self._cache: | 1448 if 'charging_config' not in self._cache: |
1444 for c in _CONTROL_USB_CHARGING_COMMANDS: | 1449 for c in _CONTROL_CHARGING_COMMANDS: |
1445 if self.FileExists(c['witness_file']): | 1450 if self.FileExists(c['witness_file']): |
1446 self._cache['usb_charging_config'] = c | 1451 self._cache['charging_config'] = c |
1447 break | 1452 break |
1448 else: | 1453 else: |
1449 raise device_errors.CommandFailedError( | 1454 raise device_errors.CommandFailedError( |
1450 'Unable to find charging commands.') | 1455 'Unable to find charging commands.') |
1451 | 1456 |
1452 if enabled: | 1457 if enabled: |
1453 command = self._cache['usb_charging_config']['enable_command'] | 1458 command = self._cache['charging_config']['enable_command'] |
1454 else: | 1459 else: |
1455 command = self._cache['usb_charging_config']['disable_command'] | 1460 command = self._cache['charging_config']['disable_command'] |
1456 | 1461 |
1457 def set_and_verify_usb_charging(): | 1462 def set_and_verify_charging(): |
1458 self.RunShellCommand(command) | 1463 self.RunShellCommand(command, check_return=True) |
1459 return self.GetUsbCharging() == enabled | 1464 return self.GetCharging() == enabled |
1460 | 1465 |
1461 timeout_retry.WaitFor(set_and_verify_usb_charging, wait_period=1) | 1466 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) |
1462 | 1467 |
1463 @decorators.WithTimeoutAndRetriesFromInstance() | 1468 @decorators.WithTimeoutAndRetriesFromInstance() |
1464 def GetDevicePieWrapper(self, timeout=None, retries=None): | 1469 def GetDevicePieWrapper(self, timeout=None, retries=None): |
1465 """Gets the absolute path to the run_pie wrapper on the device. | 1470 """Gets the absolute path to the run_pie wrapper on the device. |
1466 | 1471 |
1467 We have to build our device executables to be PIE, but they need to be able | 1472 We have to build our device executables to be PIE, but they need to be able |
1468 to run on versions of android that don't support PIE (i.e. ICS and below). | 1473 to run on versions of android that don't support PIE (i.e. ICS and below). |
1469 To do so, we push a wrapper to the device that lets older android versions | 1474 To do so, we push a wrapper to the device that lets older android versions |
1470 run PIE executables. This method pushes that wrapper to the device if | 1475 run PIE executables. This method pushes that wrapper to the device if |
1471 necessary and returns the path to it. | 1476 necessary and returns the path to it. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1514 """ | 1519 """ |
1515 if not devices: | 1520 if not devices: |
1516 devices = adb_wrapper.AdbWrapper.GetDevices() | 1521 devices = adb_wrapper.AdbWrapper.GetDevices() |
1517 if not devices: | 1522 if not devices: |
1518 raise device_errors.NoDevicesError() | 1523 raise device_errors.NoDevicesError() |
1519 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1524 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1520 if async: | 1525 if async: |
1521 return parallelizer.Parallelizer(devices) | 1526 return parallelizer.Parallelizer(devices) |
1522 else: | 1527 else: |
1523 return parallelizer.SyncParallelizer(devices) | 1528 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |