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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
#compdef adb -value-,ADB_TRACE,-default- -value-,ANDROID_SERIAL,-default- -value-,ANDROID_LOG_TAGS,-default-
_adb() {
# rely on localoptions
setopt nonomatch
local -a ADB_DEVICE_SPECIFICATION
local LOG_REDIRECT
if [[ $1 = -l ]]; then
# Run to load _adb and associated functions but do
# nothing else.
return
fi
if [[ $service = -value-* ]]; then
#_message compstate=$compstate[parameter]
case $compstate[parameter] in
(ADB_TRACE)
_adb_trace_opts
;;
(ANDROID_SERIAL)
_adb_device_serial
;;
(ANDROID_LOG_TAGS)
_adb_logcat_filter_specification
;;
esac
# We do not handle values anywhere else.
return
fi
local -a ALL_ADB_COMMANDS
ALL_ADB_COMMANDS=(
"backup"
"bugreport"
"connect"
"devices"
"disable-verity"
"disconnect"
"emu"
"enable-verity"
"exec-out"
"forward"
"get-devpath"
"get-serialno"
"get-state"
"help"
"install"
"install-multiple"
"jdwp"
"keygen"
"kill-server"
"logcat"
"ppp"
"pull"
"push"
"reboot"
"reboot-bootloader"
"remount"
"restore"
"reverse"
"root"
"shell"
"sideload"
"start-server"
"status-window"
"sync"
"tcpip"
"uninstall"
"unroot"
"usb"
"version"
"wait-for-device"
)
(( $+functions[_adb_device_specification] )) && _adb_device_specification
if ! adb ${ADB_DEVICE_SPECIFICATION} shell exit 2>/dev/null; then
# early bail-out until a single valid device/emulator is specified and up-and-running
[[ $words[CURRENT-1] = -s ]] || _message -r "No (started) device specified, completions do not yet work"
#TODO handle -t transport-id
_arguments \
'-s[serial]: :_adb_device_serial' \
'( -e)-d[device]' \
'(-d )-e[emulator]' \
'1:option:_adb_options_handler' \
'*: : _default'
return
fi
(( $+functions[_adb_check_log_redirect] )) && _adb_check_log_redirect
(( $+functions[_adb_dispatch_command] )) && _adb_dispatch_command
}
(( $+functions[_adb_dispatch_command] )) ||
_adb_dispatch_command () {
local curcontext="${curcontext}"
local integer last_command_pos=-1
(( $+functions[_adb_sanitize_context] )) && _adb_sanitize_context
if [[ ${last_command_pos} -gt 0 ]]
then
shift ${last_command_pos}-1 words
CURRENT=$(( ${CURRENT} - ${last_command_pos} + 1 ))
fi
case ${curcontext} in
(*:adb-shell:|*:adb-exec-out:)
(( $+functions[_adb_dispatch_shell] )) && _adb_dispatch_shell
;;
(*:adb-backup:)
(( $+functions[_adb_dispatch_backup] )) && _adb_dispatch_backup
;;
(*:adb-connect:|*:adb-disconnect:)
(( $+functions[_adb_dispatch_connection_handling] )) && _adb_dispatch_connection_handling
;;
(*:adb-logcat:)
(( $+functions[_adb_dispatch_logcat] )) && _adb_dispatch_logcat
;;
(*:adb-push:)
(( $+functions[_adb_dispatch_push] )) && _adb_dispatch_push
;;
(*:adb-pull:)
(( $+functions[_adb_dispatch_pull] )) && _adb_dispatch_pull
;;
(*:adb-install:)
(( $+functions[_adb_dispatch_install] )) && _adb_dispatch_install
;;
(*:adb-uninstall:)
(( $+functions[_adb_dispatch_uninstall] )) && _adb_dispatch_uninstall
;;
(*:adb-*)
_default
;;
(*)
_arguments \
'(-d -e)-s[serial]: :_adb_device_serial' \
'(-s -e)-d[device]' \
'(-d -s)-e[emulator]' \
'*:option:_adb_options_handler'
;;
esac
}
(( $+functions[_adb_sanitize_context] )) ||
_adb_sanitize_context () {
local -a mywords
for adbcommand in "${ALL_ADB_COMMANDS[@]}"
do
if [[ -n "${adbcommand}" ]] && [[ ${words[(I)${adbcommand}]} -gt 0 ]]
then
last_command_pos=${words[(I)${adbcommand}]}
mywords[${last_command_pos}]=${adbcommand}
fi
done
##expand unquoted to remove sparse elements
mywords=( ${mywords[@]} )
(( $#mywords )) && curcontext="${curcontext%:*}-${mywords[-1]}:"
}
(( $+functions[_adb_device_specification] )) ||
_adb_device_specification () {
if [[ $words[2] == -[de] ]]; then
ADB_DEVICE_SPECIFICATION=($words[2])
elif [[ $words[2] == -[st] ]]; then
ADB_DEVICE_SPECIFICATION=($words[2,3])
fi
}
(( $+functions[_adb_dispatch_shell] )) ||
_adb_dispatch_shell () {
if [[ $CURRENT -le 2 ]]
then
(( $+functions[_adb_shell_commands_handler] )) && _adb_shell_commands_handler
return
fi
case ${words[2]} in
(am)
(( $+functions[_adb_activity_manager_handler] )) && _adb_activity_manager_handler
;;
(pm)
(( $+functions[_adb_package_manager_handler] )) && _adb_package_manager_handler
;;
(cmd)
(( $+functions[_adb_cmd_handler] )) && _adb_cmd_handler
;;
(dumpsys)
(( $+functions[_adb_dumpsys_handler] )) && _adb_dumpsys_handler
;;
(start|stop)
(( $+functions[_adb_startstop] )) && _adb_startstop ${words[2]}
;;
(*)
_arguments '*: :_adb_remote_folder'
;;
esac
}
(( $+functions[_adb_dispatch_backup] )) ||
_adb_dispatch_backup() {
_arguments \
'-f[specify backup file]:backup file:_files' \
'-apk[backup .apk files]' '!(-apk)-noapk' \
'-obb[backup .obb files]' '!(-obb)-noobb' \
'-shared[backup shared storage]' '!(-shared)-noshared' \
'-all[backup all installed apps]' \
'-nosystem[include system apps when backing up all apps]' '!(-nosystem)-system' \
'*:package name:_adb_installed_packages'
}
(( $+functions[_adb_pm_list] )) ||
_adb_pm_list () {
case ${words[4]} in
(packages)
_arguments -s '-f[see their associated file]' \
':'
;;
(permissions)
_arguments -s '-g[organize by group]' \
'-f[print all information]' \
'-d[only list dangerous permissions]' \
'-u[only list user visible permissions]' \
'-s[short summary]' \
':'
;;
(permission-groups)
;;
(instrumentation)
_arguments -s '-f[see their associated file]' \
':'
;;
(features)
;;
(users)
;;
(*)
_wanted pm_list_argument expl 'pm list argument' compadd packages permission-groups permissions instrumentation features users
;;
esac
}
(( $+functions[_adb_intent_handler] )) ||
_adb_intent_handler () {
_message -r "<INTENT> specifications include these flags:
[-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--esn <EXTRA_KEY> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>] [-f <FLAGS>]
[--grant-read-uri-permission] [--grant-write-uri-permission]
[--debug-log-resolution]
[--activity-brought-to-front] [--activity-clear-top]
[--activity-clear-when-task-reset] [--activity-exclude-from-recents]
[--activity-launched-from-history] [--activity-multiple-task]
[--activity-no-animation] [--activity-no-history]
[--activity-no-user-action] [--activity-previous-is-top]
[--activity-reorder-to-front] [--activity-reset-task-if-needed]
[--activity-single-top]
[--receiver-registered-only] [--receiver-replace-pending]
[<URI>]"
}
(( $+functions[_adb_activity_manager_handler] )) ||
_adb_activity_manager_handler () {
if [[ $CURRENT -le 3 ]]
then
_wanted am_argument expl 'am argument' compadd start startservice broadcast instrument profile
return
fi
case ${words[3]} in
(start)
_arguments -s '-D[enable debugging]' \
'-W[wait for launch to complete]' \
'*:intent:_adb_intent_handler'
;;
(startservice)
_arguments -s '*:intent:_adb_intent_handler'
;;
(broadcast)
_arguments -s '*:intent:_adb_intent_handler'
;;
(instrument)
_arguments -s '-r[print raw results]' \
'-e[set argument NAME to VALUE]:<NAME> <VALUE>:' \
'-p[write profiling data to FILE]:<FILE>:' \
'-w[wait for instrumentation to finish]' \
':'
;;
(profile)
_message -r "<PROCESS> start/stop <FILE>"
;;
esac
}
(( $+functions[_adb_package_manager_handler] )) ||
_adb_package_manager_handler () {
case ${words[3]} in
(list)
(( $+functions[_adb_pm_list] )) && _adb_pm_list
;;
(path)
(( $+functions[_adb_installed_packages] )) && _adb_installed_packages
;;
(enable)
(( $+functions[_adb_installed_packages] )) && _adb_installed_packages
;;
(disable)
(( $+functions[_adb_installed_packages] )) && _adb_installed_packages
;;
(setInstallLocation)
_wanted install-locations expl 'install location' compadd -d "(0:auto 1:internal 2:external)" 0 1 2
;;
(getInstallLocation)
;;
(*)
_wanted pm_argument expl 'pm argument' compadd list path install uninstall enable disable setInstallLocation getInstallLocation
;;
esac
}
(( $+functions[_adb_cmd_handler] )) ||
_adb_cmd_handler () {
local -a cmds
cmds=(${${${(f)"$(adb ${ADB_DEVICE_SPECIFICATION} exec-out cmd -l)"}[2,-1]}##[[:space:]]##})
_wanted dumpsys expl 'cmd command' compadd ${cmds%$'\r'}
}
(( $+functions[_adb_dumpsys_handler] )) ||
_adb_dumpsys_handler () {
if [[ $CURRENT -eq 3 ]]; then
local -a services
services=(${${${(f)"$(adb ${ADB_DEVICE_SPECIFICATION} exec-out dumpsys -l)"}[2,-1]}##[[:space:]]##})
_wanted dumpsys expl 'dumpsys service' compadd - ${services%$'\r'}
elif [[ $CURRENT -ge 4 && $words[3] == SurfaceFlinger ]]; then
(( CURRENT -= 2 ))
shift 2 words
typeset -A opt_args
local context state state_descr line
_arguments -R \
'--file[start/stop continuous dumpsys to file]' \
'--no-limit[disable wrapping around continuous dumpsys at ~20MB]' \
'--full-dump[set fullDump = true]' \
'--allocated_buffers[show allocated buffers dump]' \
'--display-id[show display id dump]' \
'--dispsync[show dispsync dump]' \
'--edid[raw edid dump for specified hwc display (binary output)]:*:hwc display:->hwcdisplay' \
'--latency[show latency stats]:*:layer:->layer' \
'--latency-clear[clear latency stats]:*:layer:->layer' \
'--list[list all layers]' \
'--planner[pass planner dump arguments]:*:planner arguments:->planner' \
'--static-screen[show static screen stats]' \
'--timestats[pass time stats arguments]:*:timestats arguments:->timestats' \
'--vsync[show vsync dump]' \
'--wide-color[show wide color info]' \
'--frametimeline[pass frametimeline arguments]:*:frametimeline arguments:->frametimeline' \
'--mini[show mini dump]'
if [[ $? == 300 ]]; then
case $state in
layer)
typeset -a layers
layers=(${${(f)"$(adb ${ADB_DEVICE_SPECIFICATION} exec-out dumpsys SurfaceFlinger --list)"}[2,-1]})
_wanted dumpsys expl 'layers' compadd - ${layers%$'\r'}
;;
planner)
_arguments \
{--compare,-c}'[compare predictions of two hashes]:left: :right: :' \
{--describe,-d}'[describe hash]:hash: :' \
{--help,-h}'[help]' \
{--similar,-s}'[show similar stacks matching given plan]:plan: :' \
{--layers,-l}'[show current layers]'
;;
timestats)
_arguments -disable -enable -clear -dump -maxlayers'[only with -dump]:int:'
;;
frametimeline)
_arguments -jank -all
;;
esac
fi
fi
}
(( $+functions[_adb_startstop] )) ||
_adb_startstop () {
local -a services
local -A service_status
services=(${(f)"$(adb ${ADB_DEVICE_SPECIFICATION} exec-out getprop)"})
service_status=( ${(s,: ,)${(j,: ,)${(M)services:#\[init.svc.*}}} )
local -A map=(start stopped stop running)
services=( ${(k)service_status[(R)*$map[$1]*]} )
_wanted services expl "$map[$1] services" compadd - ${${${services#\[}%\]}#init.svc.}
}
(( $+functions[_adb_dispatch_uninstall] )) ||
_adb_dispatch_uninstall () {
_arguments \
'-k[keep data and cache]' \
'--user[uninstall for user id]:user id:_adb_users' \
'1:installed package:_adb_installed_packages'
}
(( $+functions[_adb_dispatch_install] )) ||
_adb_dispatch_install () {
argcount=${#${(M)words#-*}}
if [[ $CURRENT -gt (( argcount + 2 )) ]]
then
_message -r "Notice: you can only install one package at a time"
return
fi
_arguments \
'-l[forward lock]' \
'-r[reinstall]' \
'-s[install on sd]' \
'*:apk file:_path_files -g "*(/)|*.apk"'
}
(( $+functions[_adb_dispatch_push] )) ||
_adb_dispatch_push () {
if [[ $CURRENT -gt 3 ]]
then
_message -r "Notice: you can only push a single item at a time"
return
fi
if [[ $CURRENT -gt 2 ]]
then
_arguments '*: :_adb_remote_folder'
else
_arguments '*:local file/folder:_files'
fi
}
(( $+functions[_adb_dispatch_pull] )) ||
_adb_dispatch_pull () {
if [[ $CURRENT -gt 3 ]]
then
_message -r "Notice: you can only pull a single item at a time"
return
fi
if [[ $CURRENT -gt 2 ]]
then
_arguments '*:local file/folder:_files'
else
_arguments '*: :_adb_remote_folder'
fi
}
(( $+functions[_adb_dispatch_connection_handling] )) ||
_adb_dispatch_connection_handling () {
if compset -P '*:'
then
local expl
_wanted ports expl port compadd "$@" 5555
else
_hosts -qS:
fi
}
(( $+functions[_adb_check_log_redirect] )) ||
_adb_check_log_redirect () {
LOG_REDIRECT=${$(adb ${ADB_DEVICE_SPECIFICATION} shell getprop log.redirect-stdio 2>/dev/null)//
/}
[[ ${LOG_REDIRECT[1,4]} == "true" ]] && _message -r "Notice: stdio log redirection enabled on the device, so some completions will not work"
}
(( $+functions[_adb_trace_opts] )) ||
_adb_trace_opts() {
_values -s , 'adb trace option' \
'(1 adb sockets packets rwx usb sync sysdeps transport jdwp)all' \
'(all adb sockets packets rwx usb sync sysdeps transport jdwp)1' \
'adb' \
'sockets' \
'packets' \
'rwx' \
'usb' \
'sync' \
'sysdeps' \
'transport' \
'jdwp'
}
(( $+functions[_adb_device_serial] )) ||
_adb_device_serial() {
local expl
local -a devices device_desc
local device
devices=( $(adb devices -l | sed -n -e 's/:/\\:/' -e 's/^\([^[:space:]]*\)[[:space:]]*.*product:\([^[:space:]]*\).*$/\1:\2/p') )
zstyle -a :completion:${curcontext} device-names device_desc
for device in $device_desc; do
if [[ -n $devices[(r)${device%:*}:*] ]]; then
devices[(i)${device%:*}:*]=$device
elif [[ -n $devices[(r)*:${device%:*}] ]]; then
devices[(i)*:${device%:*}]=${${devices[(r)*:${device%:*}]}%:*}:${device#*:}
fi
done
_describe -t dev_serial 'available device' devices
}
(( $+functions[_adb_logcat_filter_specification] )) ||
_adb_logcat_filter_specification() {
zstyle ":completion:${curcontext}:" cache-policy _adb_cache_policy_single_command
local cacheid=logcat_filter_cache_${$(adb ${ADB_DEVICE_SPECIFICATION} get-serialno)}
typeset -a logcat_filter_tags
if _cache_invalid "$cacheid" || ! _retrieve_cache "$cacheid"
then
logcat_filter_tags=( $(command adb ${ADB_DEVICE_SPECIFICATION} logcat -d -v brief | sed -n 's#^[VDIWEF]/\([^[:space:](]*\).*#\1#p' |sort | uniq) )
_store_cache "$cacheid" logcat_filter_tags
fi
local expl
if compset -P '*:'
then
_wanted filter expl filter compadd W S E I D V \*
else
_wanted filtertags expl filtertags compadd -qS: ${logcat_filter_tags[@]} \*
fi
}
(( $+functions[_adb_dispatch_logcat] )) ||
_adb_dispatch_logcat() {
_arguments \
'(-c -g)-s[set default filter to silent]' \
'(-c -g)-f[log output to file (defaults to stdout)]:logfile:_files' \
'(-c -g -d)-r[rotate log at specified size, requires -f]:log size (kbytes) [16]' \
'(-c -g -d)-n[specify max number of rotated logs]:number [4]' \
'(-c -g -d)-v[log format]:format: _values "format" brief process tag thread raw time threadtime long' \
'(-d -t -g)-c[clear log]' \
'(-c -g)-d[dump log]' \
'(-c -g)-t[print only recent lines (implies -d)]:linecount:_guard "[0-9]#" "numeric value"' \
'(-c -g)-B[output log in binary]' \
'(-c -g)*:filtering:_adb_logcat_filter_specification'
}
(( $+functions[_adb_options_handler] )) ||
_adb_options_handler() {
local expl
_wanted adb_options expl 'adb option' compadd "${ALL_ADB_COMMANDS[@]}"
}
(( $+functions[_adb_shell_commands_handler] )) ||
_adb_shell_commands_handler() {
local expl
_wanted adb_shell_commands expl 'adb shell command' compadd ls pm am mkdir rmdir rm cat cmd dumpsys start stop
}
(( $+functions[_adb_device_available] )) ||
_adb_device_available() {
[[ $(adb ${ADB_DEVICE_SPECIFICATION} get-state 2>&1) == "device" ]] && return 0
return 1
}
(( $+functions[_adb_remote_folder] )) ||
_adb_remote_folder () {
typeset -a files dirs
local pref=${PREFIX}
if [[ $pref != */* ]]; then
pref=
elif [[ $pref != */ ]]; then
pref=${pref%/*}/
fi
# yes, this ls is sickening to look at, but android doesn't have printf or find
files=(${${(f)"$(adb ${ADB_DEVICE_SPECIFICATION} shell 'ls -1d 2> /dev/null '$pref'*/ '$pref'*')"}%$'\r'})
dirs=(${${(M)files:#*/}%/})
files=(${${files:|dirs}:#*\*(/|)})
_adb_device_available && \
_wanted adb_remote_folder expl 'file/folder on device' _multi_parts $@ / files
}
(( $+functions[_adb_installed_packages] )) ||
_adb_installed_packages() {
local update_policy
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
if [[ -z "$update_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy _adb_cache_policy_single_command
fi
local cacheid=package_cache_${$(adb ${ADB_DEVICE_SPECIFICATION} get-serialno)}
typeset -a installed_packages
if _cache_invalid "$cacheid" || ! _retrieve_cache "$cacheid"
then
installed_packages=(${$( adb ${ADB_DEVICE_SPECIFICATION} shell pm list packages )//#package:/})
_store_cache "$cacheid" installed_packages
fi
_wanted adb_installed_packages expl 'packages that are installed' compadd ${installed_packages}
}
(( $+functions[_adb_users] )) ||
_adb_users() {
local -a users
users=( ${${${(M)${(f)"$(adb shell pm list users)"}:#*UserInfo*}#*UserInfo\{}%:*} )
_describe -t users 'user' users
}
(( $+functions[_adb_cache_policy_single_command] )) ||
_adb_cache_policy_single_command () {
typeset -a old
# cache is valid for 1 minute
old=( "$1"(mm+1) )
(( $#old ))
}
(( $+functions[_adb_cache_policy_daily] )) ||
_adb_cache_policy_daily () {
typeset -a old
# cache is valid for a day
old=( "$1"(mh+12) )
(( $#old ))
}
_adb $@
|