Index: tools/telemetry/telemetry/core/platform/android_device.py |
diff --git a/tools/telemetry/telemetry/core/platform/android_device.py b/tools/telemetry/telemetry/core/platform/android_device.py |
index 70dbf460a66a9800c1449dc279ca4575c448c247..73fad60e9f523f79e9377194377efc4c1099231e 100644 |
--- a/tools/telemetry/telemetry/core/platform/android_device.py |
+++ b/tools/telemetry/telemetry/core/platform/android_device.py |
@@ -2,6 +2,10 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
import logging |
+import os |
+import re |
+import subprocess |
+import sys |
from telemetry.core import util |
from telemetry.core.backends import adb_commands |
@@ -52,7 +56,7 @@ Waiting for device... |
device_serials = adb_commands.GetAttachedDevices() |
except IOError: |
return [] |
- return [AndroidDevice(s) for s in device_serials] |
+ return [cls(s) for s in device_serials] |
@property |
def device_id(self): |
@@ -61,3 +65,59 @@ Waiting for device... |
@property |
def enable_performance_mode(self): |
return self._enable_performance_mode |
+ |
+ |
+def GetDevice(finder_options): |
+ """Return a Platform instance for the device specified by |finder_options|.""" |
+ if not CanDiscoverDevices(): |
+ logging.info( |
+ 'No adb command found. Will not try searching for Android browsers.') |
+ return None |
+ |
+ if finder_options.android_device: |
+ return AndroidDevice( |
+ finder_options.android_device, |
+ enable_performance_mode=not finder_options.no_performance_mode) |
+ |
+ devices = AndroidDevice.GetAllConnectedDevices() |
+ if len(devices) == 0: |
+ logging.info('No android devices found.') |
+ return None |
+ if len(devices) > 1: |
+ logging.warn( |
+ 'Multiple devices attached. Please specify one of the following:\n' + |
+ '\n'.join([' --device=%s' % d.device_id for d in devices])) |
+ return None |
+ return devices[0] |
+ |
+ |
+def CanDiscoverDevices(): |
+ """Returns true if devices are discoverable via adb.""" |
+ if not adb_commands.IsAndroidSupported(): |
+ logging.info( |
+ 'Android build commands unavailable on this machine. ' |
+ 'Have you installed Android build dependencies?') |
+ return False |
+ try: |
+ with open(os.devnull, 'w') as devnull: |
+ adb_process = subprocess.Popen( |
+ ['adb', 'devices'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
+ stdin=devnull) |
+ stdout = adb_process.communicate()[0] |
+ if re.search(re.escape('????????????\tno permissions'), stdout) != None: |
+ logging.warn('adb devices gave a permissions error. ' |
+ 'Consider running adb as root:') |
+ logging.warn(' adb kill-server') |
+ logging.warn(' sudo `which adb` devices\n\n') |
+ return True |
+ except OSError: |
+ pass |
+ chromium_adb_path = os.path.join( |
+ util.GetChromiumSrcDir(), 'third_party', 'android_tools', 'sdk', |
+ 'platform-tools', 'adb') |
+ if sys.platform.startswith('linux') and os.path.exists(chromium_adb_path): |
+ os.environ['PATH'] = os.pathsep.join( |
+ [os.path.dirname(chromium_adb_path), os.environ['PATH']]) |
+ return True |
+ return False |
+ |