#!/bin/bash
adb_all() {
if [[ $# == 0 ]]; then
echo "Usage: adb_all <adb command>. Quoting is optional."
echo "Example: adb_all install Chrome.apk"
return 1
fi
local DEVICES=$(adb_get_devices -b)
local NUM_DEVICES=$(echo $DEVICES | wc -w)
if (( $NUM_DEVICES > 1 )); then
echo "Looping over $NUM_DEVICES devices"
fi
_adb_multi "$DEVICES" "$*"
}
adb_device_loop() {
if [[ $# == 0 ]]; then
echo "Intended for more complex one-liners that cannot be done with" \
"adb_all."
echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \
'adb shell cat /data/local.prop)"'
return 1
fi
local DEVICES=$(adb_get_devices)
if [[ -z $DEVICES ]]; then
return
fi
for DEVICE in $DEVICES; do
DEV_TYPE=$(adb -s $DEVICE shell getprop ro.product.device | sed 's/\r//')
echo "Running on $DEVICE ($DEV_TYPE)"
ANDROID_SERIAL=$DEVICE eval "$*"
done
}
wipe_all_devices() {
if [[ -z $(which adb) || -z $(which fastboot) ]]; then
echo "aborting: adb and fastboot not in path"
return 1
elif ! $(groups | grep -q 'plugdev'); then
echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'"
fi
local DEVICES=$(adb_get_devices -b)
if [[ $1 != '-f' ]]; then
echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device."
read -p "Hit enter to continue"
fi
_adb_multi "$DEVICES" "reboot bootloader"
(
for DEVICE in $DEVICES; do
fastboot_erase $DEVICE &
done
wait
)
for DEVICE in $DEVICES; do
fastboot -s $DEVICE reboot
done
}
fastboot_erase() {
if [[ -n $1 ]]; then
echo "Wiping $1"
local SERIAL="-s $1"
else
if [ -z $(fastboot devices) ]; then
echo "No devices in fastboot, aborting."
echo "Check out wipe_all_devices to see if sufficient"
echo "You can put a device in fastboot using adb reboot bootloader"
return 1
fi
local SERIAL=""
fi
fastboot $SERIAL erase cache
fastboot $SERIAL erase userdata
}
adb_get_devices() {
local DEVICES="$(adb devices | grep 'device$')"
if [[ -z $DEVICES && $1 == '-b' ]]; then
echo '- waiting for device -' >&2
local DEVICES="$(adb wait-for-device devices | grep 'device$')"
fi
echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/'
}
_adb_multi() {
local DEVICES=$1
local ADB_ARGS=$2
(
for DEVICE in $DEVICES; do
adb -s $DEVICE $ADB_ARGS &
done
wait
)
}