adb.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. @retry
  154. def app_current_adb(self):
  155. """
  156. Copied from uiautomator2
  157. Returns:
  158. str: Package name.
  159. Raises:
  160. OSError
  161. For developer:
  162. Function reset_uiautomator need this function, so can't use jsonrpc here.
  163. """
  164. # Related issue: https://github.com/openatx/uiautomator2/issues/200
  165. # $ adb shell dumpsys window windows
  166. # Example output:
  167. # mCurrentFocus=Window{41b37570 u0 com.incall.apps.launcher/com.incall.apps.launcher.Launcher}
  168. # mFocusedApp=AppWindowToken{422df168 token=Token{422def98 ActivityRecord{422dee38 u0 com.example/.UI.play.PlayActivity t14}}}
  169. # Regexp
  170. # r'mFocusedApp=.*ActivityRecord{\w+ \w+ (?P<package>.*)/(?P<activity>.*) .*'
  171. # r'mCurrentFocus=Window{\w+ \w+ (?P<package>.*)/(?P<activity>.*)\}')
  172. _focusedRE = re.compile(
  173. r'mCurrentFocus=Window{.*\s+(?P<package>[^\s]+)/(?P<activity>[^\s]+)\}'
  174. )
  175. m = _focusedRE.search(self.adb_shell(['dumpsys', 'window', 'windows']))
  176. if m:
  177. return m.group('package')
  178. # try: adb shell dumpsys activity top
  179. _activityRE = re.compile(
  180. r'ACTIVITY (?P<package>[^\s]+)/(?P<activity>[^/\s]+) \w+ pid=(?P<pid>\d+)'
  181. )
  182. output = self.adb_shell(['dumpsys', 'activity', 'top'])
  183. ms = _activityRE.finditer(output)
  184. ret = None
  185. for m in ms:
  186. ret = m.group('package')
  187. if ret: # get last result
  188. return ret
  189. raise OSError("Couldn't get focused app")
  190. @retry
  191. def app_start_adb(self, package_name=None, allow_failure=False):
  192. """
  193. Args:
  194. package_name (str):
  195. allow_failure (bool):
  196. Returns:
  197. bool: If success to start
  198. """
  199. if not package_name:
  200. package_name = self.package
  201. result = self.adb_shell([
  202. 'monkey', '-p', package_name, '-c',
  203. 'android.intent.category.LAUNCHER', '--pct-syskeys', '0', '1'
  204. ])
  205. if 'No activities found' in result:
  206. # ** No activities found to run, monkey aborted.
  207. if allow_failure:
  208. return False
  209. else:
  210. logger.error(result)
  211. raise PackageNotInstalled(package_name)
  212. elif 'inaccessible' in result:
  213. # /system/bin/sh: monkey: inaccessible or not found
  214. pass
  215. else:
  216. # Events injected: 1
  217. # ## Network stats: elapsed time=4ms (0ms mobile, 0ms wifi, 4ms not connected)
  218. return True
  219. result = self.adb_shell(['dumpsys', 'package', package_name])
  220. res = re.search(r'android.intent.action.MAIN:\s+\w+ ([\w.\/]+) filter \w+\s+'
  221. r'.*\s+Category: "android.intent.category.LAUNCHER"',
  222. result)
  223. if res:
  224. activity_name = res.group(1)
  225. else:
  226. if allow_failure:
  227. return False
  228. else:
  229. logger.error(result)
  230. raise PackageNotInstalled(package_name)
  231. self.adb_shell(['am', 'start', '-a', 'android.intent.action.MAIN', '-c',
  232. 'android.intent.category.LAUNCHER', '-n', activity_name])
  233. @retry
  234. def app_stop_adb(self, package_name=None):
  235. """ Stop one application: am force-stop"""
  236. if not package_name:
  237. package_name = self.package
  238. self.adb_shell(['am', 'force-stop', package_name])
  239. @retry
  240. def dump_hierarchy_adb(self, temp: str = '/data/local/tmp/hierarchy.xml') -> etree._Element:
  241. """
  242. Args:
  243. temp (str): Temp file store on emulator.
  244. Returns:
  245. etree._Element:
  246. """
  247. # Remove existing file
  248. # self.adb_shell(['rm', '/data/local/tmp/hierarchy.xml'])
  249. # Dump hierarchy
  250. for _ in range(2):
  251. response = self.adb_shell(['uiautomator', 'dump', '--compressed', temp])
  252. if 'hierchary' in response:
  253. # UI hierchary dumped to: /data/local/tmp/hierarchy.xml
  254. break
  255. else:
  256. # <None>
  257. # Must kill uiautomator2
  258. self.app_stop_adb('com.github.uiautomator')
  259. self.app_stop_adb('com.github.uiautomator.test')
  260. continue
  261. # Read from device
  262. content = b''
  263. for chunk in self.adb.sync.iter_content(temp):
  264. if chunk:
  265. content += chunk
  266. else:
  267. break
  268. # Parse with lxml
  269. hierarchy = etree.fromstring(content)
  270. return hierarchy
  271. if __name__ == "__main__":
  272. adb = Adb(config='oas1')
  273. # logger.info(adb.app_start_adb())
  274. cv2.imshow("iiii", adb.screenshot_adb_nc())
  275. cv2.waitKey(0)