-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathadbrecord.py
executable file
·196 lines (169 loc) · 6.96 KB
/
adbrecord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/python
'''
Copyright 2016-Present, Tzutalin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import argparse
import re
import locale
import subprocess
from subprocess import PIPE
import sys
import os
import time
__version__ = '1.0.1'
EVENT_LINE_RE = re.compile(r"(\S+): (\S+) (\S+) (\S+)$")
STORE_LINE_RE = re.compile(r"(\S+) (\S+) (\S+) (\S+) (\S+)$")
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def dlog(msg):
print(str(msg))
def ilog(msg):
print(Colors.OKBLUE + str(msg) + Colors.ENDC)
def elog(msg):
print(Colors.FAIL + str(msg) + Colors.ENDC)
class AdbEventRecorder(object):
def __init__(self, adb):
self.adb_command = adb
self.adb_shell_command = adb + ['shell']
def push(self, src, dst):
if subprocess.call(self.adb_command + ['push', src, dst]) != 0:
raise OSError('push failed')
def goToActivity(self, activity):
ilog('Go to the activity:' + activity)
if subprocess.call(self.adb_shell_command + ['am', 'start', '-a', activity]) != 0:
raise OSError('push failed')
def checkPermission(self):
ilog('Checking permission')
if subprocess.call(self.adb_command + ['root']) != 0:
raise OSError('Insufficient permissions')
def listAllEvent(self):
ilog('List all events')
adb = subprocess.Popen(self.adb_shell_command + ['getevent', '-i'], stdin=PIPE, stdout=PIPE,
stderr=PIPE, shell=True)
while adb.poll() is None:
try:
line = adb.stdout.readline().decode('utf-8', 'replace').strip()
if len(line) != 0:
dlog(line)
except KeyboardInterrupt:
break
def displayAllEvents(self):
adb = subprocess.Popen(self.adb_shell_command + ['getevent', '-r', '-q'], stdin=PIPE, stdout=PIPE,
stderr=PIPE)
while adb.poll() is None:
try:
millis = int(round(time.time() * 1000))
line = adb.stdout.readline().decode('utf-8', 'replace').strip()
if len(line) != 0:
dlog("{} {}".format(millis, line))
except KeyboardInterrupt:
break
if len(line) == 0:
break
def record(self, fpath, eventNum=None):
ilog('Start recording')
record_command = self.adb_shell_command + ['getevent']
adb = subprocess.Popen(record_command,
stdin=PIPE, stdout=PIPE,
stderr=PIPE)
outputFile = open(fpath, 'w')
while adb.poll() is None:
try:
millis = int(round(time.time() * 1000))
line = adb.stdout.readline().decode('utf-8', 'replace').strip()
match = EVENT_LINE_RE.match(line.strip())
if match is not None:
dev, etype, ecode, data = match.groups()
## Filter event
if eventNum is not None and '/dev/input/event%s' % (eventNum) != dev:
continue
## Write to the file
etype, ecode, data = int(etype, 16), int(ecode, 16), int(data, 16)
rline = "%s %s %s %s %s\n" % (millis, dev, etype, ecode, data)
dlog(rline)
outputFile.write(rline)
except KeyboardInterrupt:
break
if len(line) == 0:
break
outputFile.close()
ilog('End recording')
def play(self, fpath, repeat=False):
ilog('Start playing')
while True:
lastTs = None
with open(fpath) as fp:
for line in fp:
match = STORE_LINE_RE.match(line.strip())
ts, dev, etype, ecode, data = match.groups()
ts = float(ts)
if lastTs and (ts - lastTs) > 0:
delta_second = (ts - lastTs) / 1000
time.sleep(delta_second)
lastTs = ts
cmds = self.adb_shell_command + ['sendevent', dev, etype, ecode, data]
dlog(cmds)
if subprocess.call(cmds) != 0:
raise OSError('sendevent failed')
if repeat == False:
break
ilog('End playing')
def main(*args):
parser = argparse.ArgumentParser(
description='Record events from an Android device')
parser.add_argument('-e', '--adb', metavar='COMMAND', default='adb', type=str,
help='Use the given adb binary and arguments.')
parser.add_argument('--device', action='store_true',
help='Directs command to the only connected USB device; ' +
'returns an error if more than one USB device is ' +
'present. ' +
'Corresponds to the "-d" option of adb.')
parser.add_argument('--repeat', action='store_true',
help='Repeat to play the events.')
parser.add_argument('--show', action='store_true',
help='Show all of the events from the device')
parser.add_argument('-n', '--event', type=str,
help='The event number, n, to record /dev/input/event[n]')
parser.add_argument('-r', '--record', type=str,
help='Store the record data to the file')
parser.add_argument('-p', '--play', type=str,
help='Play the record data')
parser.add_argument('--activity', type=str,
help='Go the activity when play the record events')
args = parser.parse_args()
adb = args.adb.split(' ')
if args.device:
adb += ['-d']
adb_recorder = AdbEventRecorder(adb)
adb_recorder.listAllEvent()
if args.record:
adb_recorder.checkPermission()
adb_recorder.record(args.record, args.event)
elif args.play and os.path.exists(args.play):
if args.activity:
adb_recorder.goToActivity(args.activity)
adb_recorder.play(args.play, args.repeat)
elif args.show:
adb_recorder.checkPermission()
adb_recorder.displayAllEvents()
else:
elog('Add -r [Path] to record')
elog('Add -p [Path] to play')
if __name__ == '__main__':
main(*sys.argv)