3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 # A collection of functions useful for maintaining android devices
10 # Run an adb command on all connected device in parallel.
11 # Usage: adb_all command line to eval. Quoting is optional.
14 # adb_all install Chrome.apk
15 # adb_all 'shell cat /path/to/file'
18 if [[ $# == 0 ]]; then
19 echo "Usage: adb_all <adb command>. Quoting is optional."
20 echo "Example: adb_all install Chrome.apk"
23 local DEVICES
=$
(adb_get_devices
-b)
24 local NUM_DEVICES
=$
(echo $DEVICES |
wc -w)
25 if (( $NUM_DEVICES > 1 )); then
26 echo "Looping over $NUM_DEVICES devices"
28 _adb_multi
"$DEVICES" "$*"
32 # Run a command on each connected device. Quoting the command is suggested but
33 # not required. The script setups up variable DEVICE to correspond to the
34 # current serial number. Intended for complex one_liners that don't work in
36 # Usage: adb_device_loop 'command line to eval'
38 if [[ $# == 0 ]]; then
39 echo "Intended for more complex one-liners that cannot be done with" \
41 echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \
42 'adb shell cat /data/local.prop)"'
45 local DEVICES
=$
(adb_get_devices
)
46 if [[ -z $DEVICES ]]; then
49 # Do not change DEVICE variable name - part of api
50 for DEVICE
in $DEVICES; do
51 DEV_TYPE
=$
(adb
-s $DEVICE shell getprop ro.product.device |
sed 's/\r//')
52 echo "Running on $DEVICE ($DEV_TYPE)"
53 ANDROID_SERIAL
=$DEVICE eval "$*"
57 # Erases data from any devices visible on adb. To preserve a device,
59 # 1) Reboot it into fastboot with 'adb reboot bootloader'
60 # 2) Run wipe_all_devices to wipe remaining devices
61 # 3) Restore device it with 'fastboot reboot'
63 # Usage: wipe_all_devices [-f]
66 if [[ -z $
(which adb
) ||
-z $
(which fastboot
) ]]; then
67 echo "aborting: adb and fastboot not in path"
69 elif ! $
(groups |
grep -q 'plugdev'); then
70 echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'"
73 local DEVICES
=$
(adb_get_devices
-b)
75 if [[ $1 != '-f' ]]; then
76 echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device."
77 read -p "Hit enter to continue"
80 _adb_multi
"$DEVICES" "reboot bootloader"
81 # Subshell to isolate job list
83 for DEVICE
in $DEVICES; do
84 fastboot_erase
$DEVICE &
89 # Reboot devices together
90 for DEVICE
in $DEVICES; do
91 fastboot
-s $DEVICE reboot
95 # Wipe a device in fastboot.
96 # Usage fastboot_erase [serial]
102 if [ -z $
(fastboot devices
) ]; then
103 echo "No devices in fastboot, aborting."
104 echo "Check out wipe_all_devices to see if sufficient"
105 echo "You can put a device in fastboot using adb reboot bootloader"
110 fastboot
$SERIAL erase cache
111 fastboot
$SERIAL erase userdata
114 # Get list of devices connected via adb
115 # Args: -b block until adb detects a device
117 local DEVICES
="$(adb devices | grep 'device$')"
118 if [[ -z $DEVICES && $1 == '-b' ]]; then
119 echo '- waiting for device -' >&2
120 local DEVICES
="$(adb wait-for-device devices | grep 'device$')"
122 echo "$DEVICES" |
awk -vORS=' ' '{print $1}' |
sed 's/ $/\n/'
125 ###################################################
127 ###################################################
129 # Run an adb command in parallel over a device list
134 for DEVICE
in $DEVICES; do
135 adb
-s $DEVICE $ADB_ARGS &