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 26 matching lines...) Expand all Loading... | |
37 from pylib.utils import zip_utils | 37 from pylib.utils import zip_utils |
38 | 38 |
39 _DEFAULT_TIMEOUT = 30 | 39 _DEFAULT_TIMEOUT = 30 |
40 _DEFAULT_RETRIES = 3 | 40 _DEFAULT_RETRIES = 3 |
41 | 41 |
42 # A sentinel object for default values | 42 # A sentinel object for default values |
43 # TODO(jbudorick,perezju): revisit how default values are handled by | 43 # TODO(jbudorick,perezju): revisit how default values are handled by |
44 # the timeout_retry decorators. | 44 # the timeout_retry decorators. |
45 DEFAULT = object() | 45 DEFAULT = object() |
46 | 46 |
47 _CONTROL_USB_CHARGING_COMMANDS = [ | |
48 { | |
49 # Nexus 4 | |
50 'witness_file': '/sys/module/pm8921_charger/parameters/disabled', | |
51 'enable_command': 'echo 0 > /sys/module/pm8921_charger/parameters/disabled', | |
52 'disable_command': | |
53 'echo 1 > /sys/module/pm8921_charger/parameters/disabled', | |
54 }, | |
55 { | |
56 # Nexus 5 | |
57 # Setting the HIZ bit of the bq24192 causes the charger to actually ignore | |
58 # energy coming from USB. Setting the power_supply offline just updates the | |
59 # Android system to reflect that. | |
60 'witness_file': '/sys/kernel/debug/bq24192/INPUT_SRC_CONT', | |
61 'enable_command': ( | |
62 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' | |
63 'echo 1 > /sys/class/power_supply/usb/online'), | |
64 'disable_command': ( | |
65 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' | |
66 'chmod 644 /sys/class/power_supply/usb/online && ' | |
67 'echo 0 > /sys/class/power_supply/usb/online'), | |
68 }, | |
69 ] | |
70 | |
47 | 71 |
48 @decorators.WithExplicitTimeoutAndRetries( | 72 @decorators.WithExplicitTimeoutAndRetries( |
49 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 73 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
50 def GetAVDs(): | 74 def GetAVDs(): |
51 """Returns a list of Android Virtual Devices. | 75 """Returns a list of Android Virtual Devices. |
52 | 76 |
53 Returns: | 77 Returns: |
54 A list containing the configured AVDs. | 78 A list containing the configured AVDs. |
55 """ | 79 """ |
56 lines = cmd_helper.GetCmdOutput([ | 80 lines = cmd_helper.GetCmdOutput([ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 self.old_interface = device | 160 self.old_interface = device |
137 else: | 161 else: |
138 raise ValueError('Unsupported device value: %r' % device) | 162 raise ValueError('Unsupported device value: %r' % device) |
139 self._commands_installed = None | 163 self._commands_installed = None |
140 self._default_timeout = default_timeout | 164 self._default_timeout = default_timeout |
141 self._default_retries = default_retries | 165 self._default_retries = default_retries |
142 self._cache = {} | 166 self._cache = {} |
143 assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR) | 167 assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR) |
144 assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR) | 168 assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR) |
145 | 169 |
170 def __str__(self): | |
171 """Returns the device serial.""" | |
172 return self.adb.GetDeviceSerial() | |
173 | |
146 @decorators.WithTimeoutAndRetriesFromInstance() | 174 @decorators.WithTimeoutAndRetriesFromInstance() |
147 def IsOnline(self, timeout=None, retries=None): | 175 def IsOnline(self, timeout=None, retries=None): |
148 """Checks whether the device is online. | 176 """Checks whether the device is online. |
149 | 177 |
150 Args: | 178 Args: |
151 timeout: timeout in seconds | 179 timeout: timeout in seconds |
152 retries: number of retries | 180 retries: number of retries |
153 | 181 |
154 Returns: | 182 Returns: |
155 True if the device is online, False otherwise. | 183 True if the device is online, False otherwise. |
(...skipping 1207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1363 | 1391 |
1364 Parameters passed to this function are passed directly to | 1392 Parameters passed to this function are passed directly to |
1365 |logcat_monitor.LogcatMonitor| and are documented there. | 1393 |logcat_monitor.LogcatMonitor| and are documented there. |
1366 | 1394 |
1367 Args: | 1395 Args: |
1368 timeout: timeout in seconds | 1396 timeout: timeout in seconds |
1369 retries: number of retries | 1397 retries: number of retries |
1370 """ | 1398 """ |
1371 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) | 1399 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) |
1372 | 1400 |
1373 def __str__(self): | 1401 @decorators.WithTimeoutAndRetriesFromInstance() |
1374 """Returns the device serial.""" | 1402 def GetBatteryInfo(self, timeout=None, retries=None): |
1375 return self.adb.GetDeviceSerial() | 1403 """Gets battery info for the device. |
1404 | |
1405 Args: | |
1406 timeout: timeout in seconds | |
1407 retries: number of retries | |
1408 Returns: | |
1409 A dict containing various battery information as reported by dumpsys | |
1410 battery. | |
1411 """ | |
1412 result = {} | |
1413 # Skip the first line, which is just a header. | |
1414 for line in self.RunShellCommand(['dumpsys', 'battery'])[1:]: | |
perezju
2015/02/23 10:43:32
check_return=True?
jbudorick
2015/02/24 15:44:53
Nice catch. Done.
| |
1415 k, v = line.split(': ', 1) | |
1416 result[k.strip()] = v.strip() | |
1417 return result | |
1418 | |
1419 @decorators.WithTimeoutAndRetriesFromInstance() | |
1420 def GetUsbCharging(self, timeout=None, retries=None): | |
1421 """Gets the USB charging state of the device. | |
1422 | |
1423 Args: | |
1424 timeout: timeout in seconds | |
1425 retries: number of retries | |
1426 Returns: | |
1427 True if the device is charging via USB, false otherwise. | |
1428 """ | |
1429 return (self.GetBatteryInfo().get('USB powered', '').lower() | |
1430 in ('true', '1', 'yes')) | |
1431 | |
1432 @decorators.WithTimeoutAndRetriesFromInstance() | |
1433 def SetUsbCharging(self, enabled, timeout=None, retries=None): | |
1434 """Enables or disables USB charging on the device. | |
1435 | |
1436 Args: | |
1437 enabled: A boolean indicating whether USB charging should be enabled or | |
1438 disabled. | |
1439 timeout: timeout in seconds | |
1440 retries: number of retries | |
1441 """ | |
1442 if 'usb_charging' not in self._cache: | |
perezju
2015/02/23 10:43:32
should we call it usb_charging_config or usb_charg
jbudorick
2015/02/24 15:44:53
Ah, hadn't thought of that. Switched to 'usb_charg
| |
1443 for c in _CONTROL_USB_CHARGING_COMMANDS: | |
1444 if self.FileExists(c['witness_file']): | |
1445 self._cache['usb_charging'] = c | |
1446 break | |
1447 else: | |
1448 raise device_errors.CommandFailedError( | |
1449 'Unable to find charging commands.') | |
1450 | |
1451 if enabled: | |
1452 command = self._cache['usb_charging']['enable_command'] | |
1453 else: | |
1454 command = self._cache['usb_charging']['disable_command'] | |
1455 | |
1456 while self.GetUsbCharging() != enabled: | |
perezju
2015/02/23 10:43:32
This seems a bit blunt. Should we use WaitFor?
jbudorick
2015/02/24 15:44:53
Ha, it's certainly more than a bit blunt.
Switche
| |
1457 self.RunShellCommand(command) | |
1376 | 1458 |
1377 @decorators.WithTimeoutAndRetriesFromInstance() | 1459 @decorators.WithTimeoutAndRetriesFromInstance() |
1378 def GetDevicePieWrapper(self, timeout=None, retries=None): | 1460 def GetDevicePieWrapper(self, timeout=None, retries=None): |
1379 """Gets the absolute path to the run_pie wrapper on the device. | 1461 """Gets the absolute path to the run_pie wrapper on the device. |
1380 | 1462 |
1381 We have to build our device executables to be PIE, but they need to be able | 1463 We have to build our device executables to be PIE, but they need to be able |
1382 to run on versions of android that don't support PIE (i.e. ICS and below). | 1464 to run on versions of android that don't support PIE (i.e. ICS and below). |
1383 To do so, we push a wrapper to the device that lets older android versions | 1465 To do so, we push a wrapper to the device that lets older android versions |
1384 run PIE executables. This method pushes that wrapper to the device if | 1466 run PIE executables. This method pushes that wrapper to the device if |
1385 necessary and returns the path to it. | 1467 necessary and returns the path to it. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1426 Returns: | 1508 Returns: |
1427 A Parallelizer operating over |devices|. | 1509 A Parallelizer operating over |devices|. |
1428 """ | 1510 """ |
1429 if not devices: | 1511 if not devices: |
1430 devices = adb_wrapper.AdbWrapper.GetDevices() | 1512 devices = adb_wrapper.AdbWrapper.GetDevices() |
1431 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1513 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1432 if async: | 1514 if async: |
1433 return parallelizer.Parallelizer(devices) | 1515 return parallelizer.Parallelizer(devices) |
1434 else: | 1516 else: |
1435 return parallelizer.SyncParallelizer(devices) | 1517 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |