| 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 import logging | 4 import logging |
| 5 import os | 5 import os |
| 6 import re | 6 import re |
| 7 import subprocess | 7 import subprocess |
| 8 | 8 |
| 9 from telemetry.core import util | 9 from telemetry.core import util |
| 10 from telemetry.internal.platform import device | 10 from telemetry.internal.platform import device |
| 11 from telemetry.internal.platform.profiler import monsoon | 11 from telemetry.internal.platform.profiler import monsoon |
| 12 | 12 |
| 13 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android') | 13 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android') |
| 14 from pylib import constants | 14 from pylib import constants |
| 15 from pylib.device import device_blacklist | |
| 16 from pylib.device import device_errors | 15 from pylib.device import device_errors |
| 17 from pylib.device import device_utils | 16 from pylib.device import device_utils |
| 18 | 17 |
| 19 | 18 |
| 20 class AndroidDevice(device.Device): | 19 class AndroidDevice(device.Device): |
| 21 """ Class represents information for connecting to an android device. | 20 """ Class represents information for connecting to an android device. |
| 22 | 21 |
| 23 Attributes: | 22 Attributes: |
| 24 device_id: the device's serial string created by adb to uniquely | 23 device_id: the device's serial string created by adb to uniquely |
| 25 identify an emulator/device instance. This string can be found by running | 24 identify an emulator/device instance. This string can be found by running |
| 26 'adb devices' command | 25 'adb devices' command |
| 27 enable_performance_mode: when this is set to True, android platform will be | 26 enable_performance_mode: when this is set to True, android platform will be |
| 28 set to high performance mode after browser is started. | 27 set to high performance mode after browser is started. |
| 29 """ | 28 """ |
| 30 def __init__(self, device_id, enable_performance_mode=True): | 29 def __init__(self, device_id, enable_performance_mode=True): |
| 31 super(AndroidDevice, self).__init__( | 30 super(AndroidDevice, self).__init__( |
| 32 name='Android device %s' % device_id, guid=device_id) | 31 name='Android device %s' % device_id, guid=device_id) |
| 33 self._device_id = device_id | 32 self._device_id = device_id |
| 34 self._enable_performance_mode = enable_performance_mode | 33 self._enable_performance_mode = enable_performance_mode |
| 35 | 34 |
| 36 @classmethod | 35 @classmethod |
| 37 def GetAllConnectedDevices(cls, blacklist): | 36 def GetAllConnectedDevices(cls): |
| 38 device_serials = GetDeviceSerials(blacklist) | 37 device_serials = GetDeviceSerials() |
| 39 return [cls(s) for s in device_serials] | 38 return [cls(s) for s in device_serials] |
| 40 | 39 |
| 41 @property | 40 @property |
| 42 def device_id(self): | 41 def device_id(self): |
| 43 return self._device_id | 42 return self._device_id |
| 44 | 43 |
| 45 @property | 44 @property |
| 46 def enable_performance_mode(self): | 45 def enable_performance_mode(self): |
| 47 return self._enable_performance_mode | 46 return self._enable_performance_mode |
| 48 | 47 |
| 49 | 48 |
| 50 def _ListSerialsOfHealthyOnlineDevices(blacklist): | 49 def _ListSerialsOfHealthyOnlineDevices(): |
| 51 return [d.adb.GetDeviceSerial() | 50 return [d.adb.GetDeviceSerial() |
| 52 for d in device_utils.DeviceUtils.HealthyDevices(blacklist) | 51 for d in device_utils.DeviceUtils.HealthyDevices() if |
| 53 if d.IsOnline()] | 52 d.IsOnline()] |
| 54 | 53 |
| 55 | 54 |
| 56 def GetDeviceSerials(blacklist): | 55 def GetDeviceSerials(): |
| 57 """Return the list of device serials of healthy devices. | 56 """Return the list of device serials of healthy devices. |
| 58 | 57 |
| 59 If a preferred device has been set with ANDROID_SERIAL, it will be first in | 58 If a preferred device has been set with ANDROID_SERIAL, it will be first in |
| 60 the returned list. The arguments specify what devices to include in the list. | 59 the returned list. The arguments specify what devices to include in the list. |
| 61 """ | 60 """ |
| 62 | 61 |
| 63 device_serials = _ListSerialsOfHealthyOnlineDevices(blacklist) | 62 device_serials = _ListSerialsOfHealthyOnlineDevices() |
| 64 | 63 |
| 65 # The monsoon provides power for the device, so for devices with no | 64 # The monsoon provides power for the device, so for devices with no |
| 66 # real battery, we need to turn them on after the monsoon enables voltage | 65 # real battery, we need to turn them on after the monsoon enables voltage |
| 67 # output to the device. | 66 # output to the device. |
| 68 if not device_serials: | 67 if not device_serials: |
| 69 try: | 68 try: |
| 70 m = monsoon.Monsoon(wait=False) | 69 m = monsoon.Monsoon(wait=False) |
| 71 m.SetUsbPassthrough(1) | 70 m.SetUsbPassthrough(1) |
| 72 m.SetVoltage(3.8) | 71 m.SetVoltage(3.8) |
| 73 m.SetMaxCurrent(8) | 72 m.SetMaxCurrent(8) |
| 74 logging.warn(""" | 73 logging.warn(""" |
| 75 Monsoon power monitor detected, but no Android devices. | 74 Monsoon power monitor detected, but no Android devices. |
| 76 | 75 |
| 77 The Monsoon's power output has been enabled. Please now ensure that: | 76 The Monsoon's power output has been enabled. Please now ensure that: |
| 78 | 77 |
| 79 1. The Monsoon's front and back USB are connected to the host. | 78 1. The Monsoon's front and back USB are connected to the host. |
| 80 2. The device is connected to the Monsoon's main and USB channels. | 79 2. The device is connected to the Monsoon's main and USB channels. |
| 81 3. The device is turned on. | 80 3. The device is turned on. |
| 82 | 81 |
| 83 Waiting for device... | 82 Waiting for device... |
| 84 """) | 83 """) |
| 85 util.WaitFor(_ListSerialsOfHealthyOnlineDevices(blacklist), 600) | 84 util.WaitFor(_ListSerialsOfHealthyOnlineDevices(), 600) |
| 86 device_serials = _ListSerialsOfHealthyOnlineDevices(blacklist) | 85 device_serials = _ListSerialsOfHealthyOnlineDevices() |
| 87 except IOError: | 86 except IOError: |
| 88 return [] | 87 return [] |
| 89 | 88 |
| 90 preferred_device = os.environ.get('ANDROID_SERIAL') | 89 preferred_device = os.environ.get('ANDROID_SERIAL') |
| 91 if preferred_device in device_serials: | 90 if preferred_device in device_serials: |
| 92 logging.warn( | 91 logging.warn( |
| 93 'ANDROID_SERIAL is defined. Put %s in the first of the' | 92 'ANDROID_SERIAL is defined. Put %s in the first of the' |
| 94 'discovered devices list.' % preferred_device) | 93 'discovered devices list.' % preferred_device) |
| 95 device_serials.remove(preferred_device) | 94 device_serials.remove(preferred_device) |
| 96 device_serials.insert(0, preferred_device) | 95 device_serials.insert(0, preferred_device) |
| 97 return device_serials | 96 return device_serials |
| 98 | 97 |
| 99 | 98 |
| 100 def GetDevice(finder_options): | 99 def GetDevice(finder_options): |
| 101 """Return a Platform instance for the device specified by |finder_options|.""" | 100 """Return a Platform instance for the device specified by |finder_options|.""" |
| 102 if not CanDiscoverDevices(): | 101 if not CanDiscoverDevices(): |
| 103 logging.info( | 102 logging.info( |
| 104 'No adb command found. Will not try searching for Android browsers.') | 103 'No adb command found. Will not try searching for Android browsers.') |
| 105 return None | 104 return None |
| 106 | 105 |
| 107 if (finder_options.device | 106 if finder_options.device and finder_options.device in GetDeviceSerials(): |
| 108 and finder_options.device in GetDeviceSerials(finder_options)): | |
| 109 return AndroidDevice( | 107 return AndroidDevice( |
| 110 finder_options.device, | 108 finder_options.device, |
| 111 enable_performance_mode=not finder_options.no_performance_mode) | 109 enable_performance_mode=not finder_options.no_performance_mode) |
| 112 | 110 |
| 113 if finder_options.android_blacklist_file: | 111 devices = AndroidDevice.GetAllConnectedDevices() |
| 114 blacklist = device_blacklist.Blacklist( | |
| 115 finder_options.android_blacklist_file) | |
| 116 else: | |
| 117 blacklist = None | |
| 118 | |
| 119 devices = AndroidDevice.GetAllConnectedDevices(blacklist) | |
| 120 if len(devices) == 0: | 112 if len(devices) == 0: |
| 121 logging.info('No android devices found.') | 113 logging.info('No android devices found.') |
| 122 return None | 114 return None |
| 123 if len(devices) > 1: | 115 if len(devices) > 1: |
| 124 logging.warn( | 116 logging.warn( |
| 125 'Multiple devices attached. Please specify one of the following:\n' + | 117 'Multiple devices attached. Please specify one of the following:\n' + |
| 126 '\n'.join([' --device=%s' % d.device_id for d in devices])) | 118 '\n'.join([' --device=%s' % d.device_id for d in devices])) |
| 127 return None | 119 return None |
| 128 return devices[0] | 120 return devices[0] |
| 129 | 121 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 143 logging.warn('adb devices gave a permissions error. ' | 135 logging.warn('adb devices gave a permissions error. ' |
| 144 'Consider running adb as root:') | 136 'Consider running adb as root:') |
| 145 logging.warn(' adb kill-server') | 137 logging.warn(' adb kill-server') |
| 146 logging.warn(' sudo `which adb` devices\n\n') | 138 logging.warn(' sudo `which adb` devices\n\n') |
| 147 return True | 139 return True |
| 148 except OSError: | 140 except OSError: |
| 149 pass | 141 pass |
| 150 try: | 142 try: |
| 151 os.environ['PATH'] = os.pathsep.join( | 143 os.environ['PATH'] = os.pathsep.join( |
| 152 [os.path.dirname(adb_path), os.environ['PATH']]) | 144 [os.path.dirname(adb_path), os.environ['PATH']]) |
| 153 device_utils.DeviceUtils.HealthyDevices(None) | 145 device_utils.DeviceUtils.HealthyDevices() |
| 154 return True | 146 return True |
| 155 except (device_errors.CommandFailedError, device_errors.CommandTimeoutError, | 147 except (device_errors.CommandFailedError, device_errors.CommandTimeoutError, |
| 156 OSError): | 148 OSError): |
| 157 return False | 149 return False |
| 158 | 150 |
| 159 | 151 |
| 160 def FindAllAvailableDevices(options): | 152 def FindAllAvailableDevices(_): |
| 161 """Returns a list of available devices. | 153 """Returns a list of available devices. |
| 162 """ | 154 """ |
| 163 if options.android_blacklist_file: | |
| 164 blacklist = device_blacklist.Blacklist(options.android_blacklist_file) | |
| 165 else: | |
| 166 blacklist = None | |
| 167 | |
| 168 if not CanDiscoverDevices(): | 155 if not CanDiscoverDevices(): |
| 169 return [] | 156 return [] |
| 170 else: | 157 else: |
| 171 return AndroidDevice.GetAllConnectedDevices(blacklist) | 158 return AndroidDevice.GetAllConnectedDevices() |
| OLD | NEW |