def capture_event(serial_no): cmd = 'adb -s {} shell getevent -lp'.format(serial_no) ret = os.popen(cmd).readlines() xmin = 0.0 xmax = 0.0 ymin = 0.0 ymax = 0.0 screen_width = 0.0 screen_height = 0.0 def get_value(line): elems = line.split(',') for e in elems: if 'min' in e: min_str = e.strip().split(" ")[1] elif 'max' in e: max_str = e.strip().split(" ")[1] return (float(min_str), float(max_str)) for line in ret[1:]: if 'ABS_MT_POSITION_X' in line: (xmin, xmax) = get_value(line) elif 'ABS_MT_POSITION_Y' in line: (ymin, ymax) = get_value(line) cmd = 'adb -s {} shell wm size'.format(serial_no) ret = os.popen(cmd).readlines()[0].split(':')[1].strip().split('x') screen_width = float(ret[0].strip()) screen_height = float(ret[1].strip()) # print(xmin, xmax, ymin, ymax, screen_width, screen_height) # 360.3336422613531,1997.8537836682342 cmd = 'adb -s {} shell getevent -tl'.format(serial_no) with compat.popen(cmd) as pipe: _capture_event(pipe, screen_width, screen_height, xmin, xmax, ymin, ymax)
def __cmd_list(cmd, fn=None): print('[ cmd ] ', cmd, end='\n') if fn is None: run(cmd, shell=True) else: with compat.popen(cmd) as pipe: try: res = pipe.communicate()[0] fn(res) except KeyboardInterrupt as e: os.killpg(pipe.pid, signal.SIGINT) except TimeoutExpired as e: os.killpg(pipe.pid, signal.SIGINT)
def process_list(serial_no, pkg_name): # adb shell ps -ef |findstr "com.hawksjamesf" if __is_macos(): uid = os.popen('adb -s ' + serial_no + ' shell dumpsys package ' + pkg_name + ' | grep userId= ').read().strip() uid = 'u0_a' + uid.split('=')[1][-3:] else: uid = os.popen('adb -s ' + serial_no + ' shell dumpsys package ' + pkg_name + '| findstr userId= ').read().strip() uid = 'u0_a' + uid.split('=')[1][-3:] with compat.popen("adb -s " + serial_no + " shell ps - ef") as pipe: __process_list_interal(pipe, serial_no, uid)
def shell(cmd, fn=None): print('[ cmd ] ', cmd, end='\n') if fn is None: run(cmd, shell=True) else: with compat.popen(cmd) as pipe: try: res = pipe.communicate()[0] fn(res) print("command result : \n%s" % res) except TimeoutExpired as e: os.killpg(pipe.pid, signal.SIGINT) out_bytes = pipe.communicate()[0] print('command result : \n%s' % (out_bytes))
def capture_log(serial_no, tags, format=Format.NONE): if tags and len(tags) > 0: cmd = 'adb -s %s shell logcat *:S' % (serial_no) for t in tags: cmd = cmd + ' %s:V ' % t.strip() else: cmd = 'adb -s %s shell logcat' % (serial_no) if format != Format.NONE: cmd = cmd + ' -v %s' % format.name.lower() with compat.popen(cmd) as pipe: try: # for line in iter(lambda: pipe.stdout.readline(), ''): while True: line = pipe.stdout.readline() prior = __extra_priority(line, format) if "V" == prior: verbose(line) pass elif "D" == prior: debug(line) pass elif "I" == prior: info(line) pass elif "W" == prior: warn(line) pass elif "E" == prior: error(line) pass else: print(line) except KeyboardInterrupt as e: os.killpg(pipe.pid, signal.SIGINT) except TimeoutExpired as e: os.killpg(pipe.pid, signal.SIGINT)