adb.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import re
  2. from functools import wraps
  3. import cv2
  4. import numpy as np
  5. import time
  6. from adbutils.errors import AdbError
  7. from lxml import etree
  8. from module.base.decorator import Config
  9. from module.device.connection import Connection
  10. from module.device.method.utils import (RETRY_TRIES, retry_sleep, remove_prefix, handle_adb_error,
  11. ImageTruncated, PackageNotInstalled)
  12. from module.exception import RequestHumanTakeover, ScriptError
  13. from module.logger import logger
  14. def retry(func):
  15. @wraps(func)
  16. def retry_wrapper(self, *args, **kwargs):
  17. """
  18. Args:
  19. self (Adb):
  20. """
  21. init = None
  22. for _ in range(RETRY_TRIES):
  23. try:
  24. if callable(init):
  25. retry_sleep(_)
  26. init()
  27. return func(self, *args, **kwargs)
  28. # Can't handle
  29. except RequestHumanTakeover:
  30. break
  31. # When adb server was killed
  32. except ConnectionResetError as e:
  33. logger.error(e)
  34. def init():
  35. self.adb_reconnect()
  36. # AdbError
  37. except AdbError as e:
  38. if handle_adb_error(e):
  39. def init():
  40. self.adb_reconnect()
  41. else:
  42. break
  43. # Package not installed
  44. except PackageNotInstalled as e:
  45. logger.error(e)
  46. def init():
  47. self.detect_package()
  48. # ImageTruncated
  49. except ImageTruncated as e:
  50. logger.error(e)
  51. def init():
  52. pass
  53. # Unknown
  54. except Exception as e:
  55. logger.exception(e)
  56. def init():
  57. pass
  58. logger.critical(f'Retry {func.__name__}() failed')
  59. raise RequestHumanTakeover
  60. return retry_wrapper
  61. def load_screencap(data):
  62. """
  63. Args:
  64. data: Raw data from `screencap`
  65. Returns:
  66. np.ndarray:
  67. """
  68. # Load data
  69. header = np.frombuffer(data[0:12], dtype=np.uint32)
  70. channel = 4 # screencap sends an RGBA image
  71. width, height, _ = header # Usually to be 1280, 720, 1
  72. image = np.frombuffer(data, dtype=np.uint8)
  73. if image is None:
  74. raise ImageTruncated('Empty image after reading from buffer')
  75. try:
  76. image = image[-int(width * height * channel):].reshape(height, width, channel)
  77. except ValueError as e:
  78. # ValueError: cannot reshape array of size 0 into shape (720,1280,4)
  79. raise ImageTruncated(str(e))
  80. image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
  81. if image is None:
  82. raise ImageTruncated('Empty image after cv2.cvtColor')
  83. return image
  84. class Adb(Connection):
  85. __screenshot_method = [0, 1, 2]
  86. __screenshot_method_fixed = [0, 1, 2]
  87. @staticmethod
  88. def __load_screenshot(screenshot, method):
  89. if method == 0:
  90. pass
  91. elif method == 1:
  92. screenshot = screenshot.replace(b'\r\n', b'\n')
  93. elif method == 2:
  94. screenshot = screenshot.replace(b'\r\r\n', b'\n')
  95. else:
  96. raise ScriptError(f'Unknown method to load screenshots: {method}')
  97. # fix compatibility issues for adb screencap decode problem when the data is from vmos pro
  98. # When use adb screencap for a screenshot from vmos pro, there would be a header more than that from emulator
  99. # which would cause image decode problem. So i check and remove the header there.
  100. screenshot = remove_prefix(screenshot, b'long long=8 fun*=10\n')
  101. image = np.frombuffer(screenshot, np.uint8)
  102. if image is None:
  103. raise ImageTruncated('Empty image after reading from buffer')
  104. image = cv2.imdecode(image, cv2.IMREAD_COLOR)
  105. if image is None:
  106. raise ImageTruncated('Empty image after cv2.imdecode')
  107. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  108. if image is None:
  109. raise ImageTruncated('Empty image after cv2.cvtColor')
  110. return image
  111. def __process_screenshot(self, screenshot):
  112. for method in self.__screenshot_method_fixed:
  113. try:
  114. result = self.__load_screenshot(screenshot, method=method)
  115. self.__screenshot_method_fixed = [method] + self.__screenshot_method
  116. return result
  117. except (OSError, ImageTruncated):
  118. continue
  119. self.__screenshot_method_fixed = self.__screenshot_method
  120. if len(screenshot) < 500:
  121. logger.warning(f'Unexpected screenshot: {screenshot}')
  122. raise OSError(f'cannot load screenshot')
  123. @retry
  124. @Config.when(DEVICE_OVER_HTTP=False)
  125. def screenshot_adb(self):
  126. data = self.adb_shell(['screencap', '-p'], stream=True)
  127. if len(data) < 500:
  128. logger.warning(f'Unexpected screenshot: {data}')
  129. return self.__process_screenshot(data)
  130. @retry
  131. @Config.when(DEVICE_OVER_HTTP=True)
  132. def screenshot_adb(self):
  133. data = self.adb_shell(['screencap'], stream=True)
  134. if len(data) < 500:
  135. logger.warning(f'Unexpected screenshot: {data}')
  136. return load_screencap(data)
  137. @retry
  138. def screenshot_adb_nc(self):
  139. data = self.adb_shell_nc(['screencap'])
  140. if len(data) < 500:
  141. logger.warning(f'Unexpected screenshot: {data}')
  142. return load_screencap(data)
  143. @retry
  144. def click_adb(self, x, y):
  145. start = time.time()
  146. self.adb_shell(['input', 'tap', x, y])
  147. if time.time() - start <= 0.05:
  148. self.sleep(0.05)
  149. @retry
  150. def swipe_adb(self, p1, p2, duration=0.1):
  151. duration = int(duration * 1000)
  152. self.adb_shell(['input', 'swipe', *p1, *p2, duration])
  153. def long_click_adb(self, x: float, y: float, duration: float):
  154. """
  155. :param x:
  156. :param y:
  157. :param duration: 单位是s
  158. :return:
  159. """
  160. if not 0 < duration < 3:
  161. duration = duration / 1000
  162. self.adb_shell(['input', 'swipe', x, y, x, y, int(duration*1000)])
  163. @retry
  164. def app_current_adb(self):
  165. """
  166. Copied from uiautomator2
  167. Returns:
  168. str: Package name.
  169. Raises:
  170. OSError
  171. For developer:
  172. Function reset_uiautomator need this function, so can't use jsonrpc here.
  173. """
  174. # Related issue: https://github.com/openatx/uiautomator2/issues/200
  175. # $ adb shell dumpsys window windows
  176. # Example output:
  177. # mCurrentFocus=Window{41b37570 u0 com.incall.apps.launcher/com.incall.apps.launcher.Launcher}
  178. # mFocusedApp=AppWindowToken{422df168 token=Token{422def98 ActivityRecord{422dee38 u0 com.example/.UI.play.PlayActivity t14}}}
  179. # Regexp
  180. # r'mFocusedApp=.*ActivityRecord{\w+ \w+ (?P<package>.*)/(?P<activity>.*) .*'
  181. # r'mCurrentFocus=Window{\w+ \w+ (?P<package>.*)/(?P<activity>.*)\}')
  182. _focusedRE = re.compile(
  183. r'mCurrentFocus=Window{.*\s+(?P<package>[^\s]+)/(?P<activity>[^\s]+)\}'
  184. )
  185. m = _focusedRE.search(self.adb_shell(['dumpsys', 'window', 'windows']))
  186. if m:
  187. return m.group('package')
  188. # try: adb shell dumpsys activity top
  189. _activityRE = re.compile(
  190. r'ACTIVITY (?P<package>[^\s]+)/(?P<activity>[^/\s]+) \w+ pid=(?P<pid>\d+)'
  191. )
  192. output = self.adb_shell(['dumpsys', 'activity', 'top'])
  193. ms = _activityRE.finditer(output)
  194. ret = None
  195. for m in ms:
  196. ret = m.group('package')
  197. if ret: # get last result
  198. return ret
  199. raise OSError("Couldn't get focused app")
  200. @retry
  201. def app_start_adb(self, package_name=None, allow_failure=False):
  202. """
  203. Args:
  204. package_name (str):
  205. allow_failure (bool):
  206. Returns:
  207. bool: If success to start
  208. """
  209. if not package_name:
  210. package_name = self.package
  211. result = self.adb_shell([
  212. 'monkey', '-p', package_name, '-c',
  213. 'android.intent.category.LAUNCHER', '--pct-syskeys', '0', '1'
  214. ])
  215. if 'No activities found' in result:
  216. # ** No activities found to run, monkey aborted.
  217. if allow_failure:
  218. return False
  219. else:
  220. logger.error(result)
  221. raise PackageNotInstalled(package_name)
  222. elif 'inaccessible' in result:
  223. # /system/bin/sh: monkey: inaccessible or not found
  224. pass
  225. else:
  226. # Events injected: 1
  227. # ## Network stats: elapsed time=4ms (0ms mobile, 0ms wifi, 4ms not connected)
  228. return True
  229. logger.warning('Fallback to use am start')
  230. result = self.adb_shell(['dumpsys', 'package', package_name])
  231. res = re.search(r'android.intent.action.MAIN:\s+\w+ ([\w.\/]+) filter \w+\s+'
  232. r'.*\s+Category: "android.intent.category.LAUNCHER"',
  233. result)
  234. if res:
  235. activity_name = res.group(1)
  236. self.adb_shell(['am', 'start', '-a', 'android.intent.action.MAIN', '-c',
  237. 'android.intent.category.LAUNCHER', '-n', activity_name])
  238. elif 'com.netease.ntunisdk.base.deeplink.UniDeepLinkActivity' in result:
  239. activity_name = 'com.netease.ntunisdk.base.deeplink.UniDeepLinkActivity'
  240. result = self.adb_shell([
  241. 'am', 'start', '-n',
  242. f'{package_name}/com.netease.ntunisdk.base.deeplink.UniDeepLinkActivity'
  243. ])
  244. else:
  245. if allow_failure:
  246. return False
  247. else:
  248. logger.error(result)
  249. raise PackageNotInstalled(package_name)
  250. self.adb_shell(['am', 'start', '-a', 'android.intent.action.MAIN', '-c',
  251. 'android.intent.category.LAUNCHER', '-n', activity_name])
  252. @retry
  253. def app_stop_adb(self, package_name=None):
  254. """ Stop one application: am force-stop"""
  255. if not package_name:
  256. package_name = self.package
  257. self.adb_shell(['am', 'force-stop', package_name])
  258. @retry
  259. def dump_hierarchy_adb(self, temp: str = '/data/local/tmp/hierarchy.xml') -> etree._Element:
  260. """
  261. Args:
  262. temp (str): Temp file store on emulator.
  263. Returns:
  264. etree._Element:
  265. """
  266. # Remove existing file
  267. # self.adb_shell(['rm', '/data/local/tmp/hierarchy.xml'])
  268. # Dump hierarchy
  269. for _ in range(2):
  270. response = self.adb_shell(['uiautomator', 'dump', '--compressed', temp])
  271. if 'hierchary' in response:
  272. # UI hierchary dumped to: /data/local/tmp/hierarchy.xml
  273. break
  274. else:
  275. # <None>
  276. # Must kill uiautomator2
  277. self.app_stop_adb('com.github.uiautomator')
  278. self.app_stop_adb('com.github.uiautomator.test')
  279. continue
  280. # Read from device
  281. content = b''
  282. for chunk in self.adb.sync.iter_content(temp):
  283. if chunk:
  284. content += chunk
  285. else:
  286. break
  287. # Parse with lxml
  288. hierarchy = etree.fromstring(content)
  289. return hierarchy
  290. if __name__ == "__main__":
  291. adb = Adb(config='oas1')
  292. # logger.info(adb.app_start_adb())
  293. cv2.imshow("iiii", adb.screenshot_adb_nc())
  294. cv2.waitKey(0)