1 # /* vim: set ai ts=4 ft=sh: */
3 # Copyright 2011, The Android Open Source Project
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 if ! check_type "$1" >/dev/null; then
23 if check_type _init_completion >/dev/null; then
24 _init_completion || return
27 local where i cur serial
30 serial="${ANDROID_SERIAL:-none}"
32 for ((i=1; i <= COMP_CWORD; i++)); do
33 cur="${COMP_WORDS[i]}"
45 if [[ $where == OPT_SERIAL ]]; then
56 if [[ $where == COMMAND && $i -ge $COMP_CWORD ]]; then
61 COMMAND="devices connect disconnect push pull sync shell emu logcat lolcat forward jdwp install uninstall bugreport help version start-server kill-server get-state get-serialno status-window remount reboot reboot-bootloader root usb tcpip disable-verity"
64 OPTIONS|OPT_SERIAL|OPT_PATH)
65 COMPREPLY=( $(compgen -W "$OPTIONS $COMMAND" -- "$cur") )
68 local devices=$(command adb devices 2> /dev/null | grep -v "List of devices" | awk '{ print $1 }')
69 COMPREPLY=( $(compgen -W "${devices}" -- ${cur}) )
72 if [[ $i -eq $COMP_CWORD ]]; then
73 COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") )
78 _adb_cmd_install "$serial" $i
81 _adb_cmd_sideload "$serial" $i
84 _adb_cmd_pull "$serial" $i
87 _adb_cmd_push "$serial" $i
90 if [[ $COMP_CWORD == $i ]]; then
91 args="bootloader recovery"
92 COMPREPLY=( $(compgen -W "${args}" -- "${COMP_WORDS[i]}") )
96 _adb_cmd_shell "$serial" $i
99 _adb_cmd_uninstall "$serial" $i
110 local serial i cur where
116 for ((; i <= COMP_CWORD; i++)); do
117 cur="${COMP_WORDS[i]}"
129 cur="${COMP_WORDS[COMP_CWORD]}"
130 if [[ $where == OPTIONS ]]; then
131 COMPREPLY=( $(compgen -W "-d -l -r -s" -- "${cur}") )
135 _adb_util_complete_local_file "${cur}" '!*.apk'
138 _adb_cmd_sideload() {
144 cur="${COMP_WORDS[COMP_CWORD]}"
146 _adb_util_complete_local_file "${cur}" '!*.zip'
150 local serial IFS=$'\n' i cur
155 cur="${COMP_WORDS[COMP_CWORD]}"
157 if [[ $COMP_CWORD == $i ]]; then
158 _adb_util_complete_local_file "${cur}"
159 elif [[ $COMP_CWORD == $(($i+1)) ]]; then
160 if [ "${cur}" == "" ]; then
163 _adb_util_list_files $serial "${cur}"
168 local serial IFS=$'\n' i cur
173 cur="${COMP_WORDS[COMP_CWORD]}"
175 if [[ $COMP_CWORD == $i ]]; then
176 if [ "${cur}" == "" ]; then
179 _adb_util_list_files $serial "${cur}"
180 elif [[ $COMP_CWORD == $(($i+1)) ]]; then
181 _adb_util_complete_local_file "${cur}"
186 local serial IFS=$'\n' i cur
192 cur="${COMP_WORDS[i]}"
193 if [ "$serial" != "none" ]; then
197 if [[ $i -eq $COMP_CWORD && ${cur:0:1} != "/" ]]; then
198 paths=$(command adb ${args[@]} shell echo '$'PATH 2> /dev/null | tr -d '\r' | tr : '\n')
199 COMMAND=$(command adb ${args[@]} shell ls $paths '2>' /dev/null | tr -d '\r' | {
200 while read -r tmp; do
202 printf '%s\n' "$command"
205 COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") )
212 _adb_shell_file_command $serial $i "--color -A -C -F -H -L -R -S -Z -a -c -d -f -h -i -k -l -m -n -p -q -r -s -t -u -x -1"
215 _adb_shell_file_command $serial $i "-h -e -t -u -v"
218 _adb_cmd_shell_dumpsys "$serial" $i
221 _adb_cmd_shell_am "$serial" $i
224 _adb_cmd_shell_pm "$serial" $i
227 _adb_util_list_files $serial "$cur"
237 _adb_cmd_shell_dumpsys() {
247 if [ "$serial" != "none" ]; then
251 if (( $i == $COMP_CWORD )) ; then
252 cur="${COMP_WORDS[COMP_CWORD]}"
253 # First line is a header, so need "1d".
254 candidates=$(command adb ${args[@]} shell dumpsys -l 2> /dev/null | sed -e '1d;s/^ *//' | tr -d '\r')
255 candidates="-l $candidates"
256 COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
264 _adb_cmd_shell_am() {
273 if (( $i == $COMP_CWORD )) ; then
274 cur="${COMP_WORDS[COMP_CWORD]}"
275 candidates="broadcast clear-debug-app clear-watch-heap dumpheap force-stop get-config get-inactive hang idle-maintenance instrument kill kill-all monitor package-importance profile restart screen-compat send-trim-memory set-debug-app set-inactive set-watch-heap stack start startservice start-user stopservice stop-user suppress-resize-config-changes switch-user task to-app-uri to-intent-uri to-uri"
276 COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
285 _adb_cmd_shell_pm() {
294 if (( $i == $COMP_CWORD )) ; then
295 cur="${COMP_WORDS[COMP_CWORD]}"
296 candidates="-l -lf -p clear create-user default-state disable"
297 candidates+=" disable-until-used disable-user dump enable"
298 candidates+=" get-app-link get-install-location get-max-users"
299 candidates+=" get-max-running-users grant hide install"
300 candidates+=" install-abandon install-commit install-create"
301 candidates+=" install-write list move-package"
302 candidates+=" move-primary-storage path remove-user"
303 candidates+=" reset-permissions revoke set-app-link"
304 candidates+=" set-installer set-install-location"
305 candidates+=" set-permission-enforced trim-caches unhide"
306 candidates+=" uninstall"
307 COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
311 if (( $i + 1 == $COMP_CWORD )) && [[ "${COMP_WORDS[COMP_CWORD -1]}" == "list" ]] ; then
312 cur="${COMP_WORDS[COMP_CWORD]}"
313 candidates="packages permission-groups permissions instrumentation features libraries users"
314 COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
322 _adb_cmd_uninstall() {
323 local serial i where cur packages
327 if [ "$serial" != "none" ]; then
332 for ((; i <= COMP_CWORD; i++)); do
333 cur="${COMP_WORDS[i]}"
345 cur="${COMP_WORDS[COMP_CWORD]}"
346 if [[ $where == OPTIONS ]]; then
347 COMPREPLY=( $(compgen -W "-k" -- "${cur}") )
351 command adb ${args[@]} shell pm list packages '2>' /dev/null 2> /dev/null | tr -d '\r' | {
352 while read -r tmp; do
353 local package=${tmp#package:}
354 echo -n "${package} "
359 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${packages}" -- "${cur}") )
362 _adb_shell_file_command() {
363 local serial i cur file options
368 if [ "$serial" != "none" ]; then
374 for ((; i <= COMP_CWORD; i++)); do
375 cur="${COMP_WORDS[i]}"
387 file="${COMP_WORDS[COMP_CWORD]}"
388 if [[ ${file} == "" ]]; then
395 COMPREPLY=( $(compgen -W "$options" -- "$cur") )
398 _adb_util_list_files $serial "$file"
405 _adb_util_list_files() {
406 local serial dir IFS=$'\n'
413 if [ "$serial" != "none" ]; then
417 if [[ $( command adb ${args[@]} shell ls -dF / '2>/dev/null' | tr -d '\r' ) == "d /" ]] ; then
418 toks=( ${toks[@]-} $(
419 command adb ${args[@]} shell ls -dF ${file}"*" '2>' /dev/null 2> /dev/null | tr -d '\r' | {
420 while read -r tmp; do
422 filename=${tmp:${#filetype}+1}
423 if [[ ${filetype:${#filetype}-1:1} == d ]]; then
424 printf '%s/\n' "$filename"
426 printf '%s\n' "$filename"
432 toks=( ${toks[@]-} $(
433 command adb ${args[@]} shell ls -dp ${file}"*" '2>/dev/null' 2> /dev/null | tr -d '\r'
437 # Since we're probably doing file completion here, don't add a space after.
438 if [[ $(check_type compopt) == "builtin" ]]; then
442 COMPREPLY=( ${COMPREPLY[@]:-} "${toks[@]}" )
445 _adb_util_complete_local_file()
447 local file xspec i j IFS=$'\n'
453 # Since we're probably doing file completion here, don't add a space after.
454 if [[ $(check_type compopt) == "builtin" ]]; then
456 if [[ "${xspec}" == "" ]]; then
457 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") )
460 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") )
463 # Work-around for shells with no compopt
465 dirs=( $(compgen -d -- "${cur}" ) )
467 if [[ "${xspec}" == "" ]]; then
468 files=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") )
470 files=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") )
474 for i in "${files[@]}"; do
476 for j in "${dirs[@]}"; do
477 if [[ $i == $j ]]; then
482 [[ -n $skip ]] || printf "%s\n" "$i"
486 COMPREPLY=( ${COMPREPLY[@]:-} $(
487 for i in "${dirs[@]}"; do
495 if [[ $(check_type compopt) == "builtin" ]]; then
498 complete -o nospace -F _adb adb