-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadb-wrapper
More file actions
executable file
·355 lines (289 loc) · 6.6 KB
/
adb-wrapper
File metadata and controls
executable file
·355 lines (289 loc) · 6.6 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/bash -eu
#
# Wrapper around common adb operations
#
# -----------------------------------------------------------------------------
# Helper functions
function adb_send_keyevent()
{
local key=${1}
local event
case "${key}" in
power) event=26 ;;
enter) event=66 ;;
unlock) event=82 ;;
*)
echo "-- Invalid key name: ${key}" >&2
return
;;
esac
echo "-- Send keyevent: ${event} (${key})"
adb -s "${SERIAL}" shell input keyevent "${event}"
}
function adb_send_text()
{
local text=${1}
adb -s "${SERIAL}" shell input text "${text}"
}
function adb_start_app()
{
local app=${1}
echo "-- Start app: ${app}"
adb -s "${SERIAL}" shell monkey -p "${app}" 1
}
function adb_get_storage_dirs()
{
local rmt_dir
# The primary storage always exists
echo "/storage/self/primary"
while IFS= read -r rmt_dir ; do
case "${rmt_dir}" in
/storage/emulated|/storage/self)
continue
;;
*)
echo "${rmt_dir}"
;;
esac
done < <(adb -s "${SERIAL}" shell ls -d /storage/*)
}
function adb_pull_storage()
{
local rmt_dir=${1} dst_dir=${2}
local rmt_dirs storage_dir rmt_file rel_dir lcl_dir lcl_file
echo "-- Pull files from ${SERIAL} to ${dst_dir}"
rmt_dirs=()
while IFS= read -r storage_dir ; do
if adb -s "${SERIAL}" shell test -d "${storage_dir}"/"${rmt_dir}" ; then
rmt_dirs+=("${storage_dir}"/"${rmt_dir}")
fi
done < <(adb_get_storage_dirs)
if [ "${#rmt_dirs[@]}" -eq 0 ] ; then
return
fi
while IFS= read -r rmt_file ; do
case "${rmt_file}" in
*/.trashed-*) continue ;;
*/.thumbnails/*) continue ;;
esac
rel_dir=${rmt_file%/*}/
rel_dir=${rel_dir#*/"${rmt_dir}"/}
lcl_dir=${dst_dir}/${rel_dir}
lcl_file=${lcl_dir}/${rmt_file##*/}
if ! [ -e "${lcl_file}" ] ; then
mkdir -p "${lcl_dir}"
adb -s "${SERIAL}" pull "${rmt_file}" "${lcl_dir}"
fi
done < <(adb -s "${SERIAL}" shell find "${rmt_dirs[@]}" -type f)
}
function adb_push_storage()
{
local rmt_dir=${1}
shift
rmt_dir=/storage/self/primary/${rmt_dir}
echo "-- Push files to ${SERIAL}:${rmt_dir}"
if ! adb -s "${SERIAL}" shell test -d "${rmt_dir}" ; then
echo "-- No such remote directory: ${rmt_dir}" >&2
exit 1
fi
adb -s "${SERIAL}" push "${@}" "${rmt_dir}"
}
# -----------------------------------------------------------------------------
# Plumbing commands
function do_display_off()
{
if [ "$(do_display_state)" = "ON" ] ; then
adb_send_keyevent power
fi
}
function do_display_on()
{
if [ "$(do_display_state)" = "OFF" ] ; then
adb_send_keyevent power
fi
}
function do_display_state()
{
local state
state=$(adb -s "${SERIAL}" shell dumpsys power | \
grep -P '^Display Power: state=' | sed 's/.*=//')
if [ -n "${state}" ] ; then
echo "${state}"
return
fi
# Old Android
state=$(adb -s "${SERIAL}" shell dumpsys power | \
grep -P '^\s+mPowerState=')
if [ -n "${state}" ] ; then
if [ "${state/SCREEN_ON_BIT/}" != "${state}" ] ; then
echo "ON"
else
echo "OFF"
fi
return
fi
echo "UNKNOWN"
}
function do_serials()
{
adb devices | awk '$2 == "device" { print $1 }'
}
# -----------------------------------------------------------------------------
# Porcelain Commands
function do_pull_all()
{
do_pull_backup
do_pull_camera
do_pull_download
do_pull_pictures
}
function do_pull_backup()
{
adb_pull_storage Backup "${HOME}"/Backup/"${SERIAL}"/"$(date +'%Y-%m-%d')"
}
function do_pull_camera()
{
adb_pull_storage DCIM "${HOME}"/Pictures/"${SERIAL}"
}
function do_pull_download()
{
adb_pull_storage Download "${HOME}"/Downloads/"${SERIAL}"
}
function do_pull_pictures()
{
adb_pull_storage Pictures "${HOME}"/Pictures/"${SERIAL}"
}
function do_pull()
{
local d
if [ ${#} -lt 1 ] ; then
usage
exit 1
fi
for d in "${@}" ; do
adb_pull_storage "${d}" "${HOME}"/tmp/"${SERIAL}"/"${d}"
done
}
function do_push()
{
if [ ${#} -lt 2 ] ; then
usage
exit 1
fi
adb_push_storage "${@}"
}
function do_scrcpy()
{
# Unlock the device
# do_unlock
# Run scrcpy
echo "-- Run scrcpy"
scrcpy --serial "${SERIAL}" --turn-screen-off --stay-awake
}
function do_unlock()
{
local pin
# Get the pin from the password store
echo "-- Get pin from password store"
pin=$(pass show local/android/"${SERIAL}" | head -1)
# Unlock the device
echo "-- Unlock device (${SERIAL})"
adb_send_keyevent unlock
sleep 1
# FIXME: Check display/lockscreen state
adb_send_keyevent unlock
sleep 1
adb_send_text "${pin}"
adb_send_keyevent enter
}
# -----------------------------------------------------------------------------
# App commands
function do_start()
{
local app=${1}
local pkg
case "${app}" in
klapp) pkg=mobi.klapp.parent ;;
*)
echo "-- Unsupported app: ${app}" >&2
exit 1
;;
esac
adb_start_app "${pkg}"
sleep 1
do_scrcpy
}
# -----------------------------------------------------------------------------
# Usage and main entry point
function usage()
{
cat <<EOF
Usage: $(basename "${0}") [-h] [-s SERIAL] COMMAND [OPTS]
Wrapper around common adb operations.
App commands:
start APP Start an app.
Porcelain commands:
pull-all Pull all Backup/DCIM/Download/Pictures files.
pull-backup Pull files from folder Backup
pull-camera Pull files from folder DCIM.
pull-download Pull files from folder Download.
pull-pictures Pull files from folder Pictures.
pull DIR [DIR..] Pull files from folder <DIR> to ~/tmp/<SERIAL>/<DIR>.
push DIR ... Push files to folder <DIR>.
scrcpy Run scrcpy.
unlock Unlock device.
Plumbing commands:
display-off Turn display off.
display-on Turn display on.
display-state Show display state (ON|OFF|UNKNOWN)
serials List serial numbers of connected devices.
Optional arguments:
-h, --help Show this help text and exit.
-s, --serial SERIAL Serial number of target device. If not provided, targets
the first device from the list of connected devices.
EOF
}
SERIAL=
command=
while [ ${#} -gt 0 ] ; do
case "${1}" in
-h|--help)
usage
exit
;;
-s|--serial)
shift
SERIAL=${1}
;;
pull-all|pull-backup|pull-camera|pull-download|pull-pictures|scrcpy|unlock)
command=${1}
;;
pull|push)
command=${1}
shift
break
;;
display-off|display-on|display-state|serials)
command=${1}
;;
start)
command=${1}
shift
break
;;
*)
echo "Invalid argument: ${1}" >&2
exit 2
;;
esac
shift
done
if [ -z "${command}" ] ; then
usage
exit 2
fi
if [ "${command}" != "serials" ] && [ -z "${SERIAL}" ] ; then
# Get the serial number of the first device
SERIAL=$(do_serials | head -1)
fi
do_"${command//-/_}" "${@}"