目前业务有需要监控在使用安卓手机时的操作位置序列,在运行monitor_click函数之后开始监控,点击位置、时间以及序号都会记录到列表中,如果记录完成则ctrl+C退出程序即可,代码记录如下
def get_base_info():
# 记录实际屏幕大小和event框架的大小,后续需要算比值
base_info = subprocess.Popen("adb shell getevent -p", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
info_str = base_info.stdout.read().decode('gbk')
# print(info_str)
regex_screen_w = "(0030 : value 0, min 0, max ?[\d]+)"
regex_screen_h = "(0031 : value 0, min 0, max ?[\d]+)"
w = re.findall(regex_screen_w, info_str)
h = re.findall(regex_screen_h, info_str)
w = w[0].split()[-1]
h = h[0].split()[-1]
regex_event_w = "(0035 : value 0, min 0, max ?[\d]+)"
regex_event_h = "(0036 : value 0, min 0, max ?[\d]+)"
e_w = re.findall(regex_event_w, info_str)
e_h = re.findall(regex_event_h, info_str)
e_w = e_w[0].split()[-1]
e_h = e_h[0].split()[-1]
# print(w,h, e_w,e_h)
return int(w), int(h), int(e_w), int(e_h)
def monitor_click():
def record_click(click_sequence, click_w, click_h, screenshots_cnt):
click_sequence.append({"w":click_w, "h":click_h, "time":time.time(), "cnt":screenshots_cnt})
print("add to sequence")
cmd = ['adb','shell', 'getevent']
file_out = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
click_sequence = []
screenshots_cnt = 0
try:
while True:
line = file_out.stdout.readline().decode('gbk')
if '0035' in line:
click_w = line.split()[-1]
click_w = int(click_w, 16)
flag = False
elif '0036' in line:
click_h = line.split()[-1]
click_h = int(click_h, 16)
flag = True
else:
flag = False
if flag:
print("click w: {}, click_h {}".format(click_w, click_h))
if click_sequence == []:
record_click(click_sequence, click_w, click_h, screenshots_cnt)
screenshots_cnt += 1
elif time.time() - click_sequence[-1]["time"] > 0.5:
record_click(click_sequence, click_w, click_h, screenshots_cnt)
screenshots_cnt += 1
if subprocess.Popen.poll(file_out)==0: #判断子进程是否结束
break
except:
print("Program Stop!!!!!")
return click_sequence