michael@0: #!/bin/bash michael@0: # michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: # michael@0: # A collection of functions useful for maintaining android devices michael@0: michael@0: michael@0: # Run an adb command on all connected device in parallel. michael@0: # Usage: adb_all command line to eval. Quoting is optional. michael@0: # michael@0: # Examples: michael@0: # adb_all install Chrome.apk michael@0: # adb_all 'shell cat /path/to/file' michael@0: # michael@0: adb_all() { michael@0: if [[ $# == 0 ]]; then michael@0: echo "Usage: adb_all . Quoting is optional." michael@0: echo "Example: adb_all install Chrome.apk" michael@0: return 1 michael@0: fi michael@0: local DEVICES=$(adb_get_devices -b) michael@0: local NUM_DEVICES=$(echo $DEVICES | wc -w) michael@0: if (( $NUM_DEVICES > 1 )); then michael@0: echo "Looping over $NUM_DEVICES devices" michael@0: fi michael@0: _adb_multi "$DEVICES" "$*" michael@0: } michael@0: michael@0: michael@0: # Run a command on each connected device. Quoting the command is suggested but michael@0: # not required. The script setups up variable DEVICE to correspond to the michael@0: # current serial number. Intended for complex one_liners that don't work in michael@0: # adb_all michael@0: # Usage: adb_device_loop 'command line to eval' michael@0: adb_device_loop() { michael@0: if [[ $# == 0 ]]; then michael@0: echo "Intended for more complex one-liners that cannot be done with" \ michael@0: "adb_all." michael@0: echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \ michael@0: 'adb shell cat /data/local.prop)"' michael@0: return 1 michael@0: fi michael@0: local DEVICES=$(adb_get_devices) michael@0: if [[ -z $DEVICES ]]; then michael@0: return michael@0: fi michael@0: # Do not change DEVICE variable name - part of api michael@0: for DEVICE in $DEVICES; do michael@0: DEV_TYPE=$(adb -s $DEVICE shell getprop ro.product.device | sed 's/\r//') michael@0: echo "Running on $DEVICE ($DEV_TYPE)" michael@0: ANDROID_SERIAL=$DEVICE eval "$*" michael@0: done michael@0: } michael@0: michael@0: # Erases data from any devices visible on adb. To preserve a device, michael@0: # disconnect it or: michael@0: # 1) Reboot it into fastboot with 'adb reboot bootloader' michael@0: # 2) Run wipe_all_devices to wipe remaining devices michael@0: # 3) Restore device it with 'fastboot reboot' michael@0: # michael@0: # Usage: wipe_all_devices [-f] michael@0: # michael@0: wipe_all_devices() { michael@0: if [[ -z $(which adb) || -z $(which fastboot) ]]; then michael@0: echo "aborting: adb and fastboot not in path" michael@0: return 1 michael@0: elif ! $(groups | grep -q 'plugdev'); then michael@0: echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'" michael@0: fi michael@0: michael@0: local DEVICES=$(adb_get_devices -b) michael@0: michael@0: if [[ $1 != '-f' ]]; then michael@0: echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device." michael@0: read -p "Hit enter to continue" michael@0: fi michael@0: michael@0: _adb_multi "$DEVICES" "reboot bootloader" michael@0: # Subshell to isolate job list michael@0: ( michael@0: for DEVICE in $DEVICES; do michael@0: fastboot_erase $DEVICE & michael@0: done michael@0: wait michael@0: ) michael@0: michael@0: # Reboot devices together michael@0: for DEVICE in $DEVICES; do michael@0: fastboot -s $DEVICE reboot michael@0: done michael@0: } michael@0: michael@0: # Wipe a device in fastboot. michael@0: # Usage fastboot_erase [serial] michael@0: fastboot_erase() { michael@0: if [[ -n $1 ]]; then michael@0: echo "Wiping $1" michael@0: local SERIAL="-s $1" michael@0: else michael@0: if [ -z $(fastboot devices) ]; then michael@0: echo "No devices in fastboot, aborting." michael@0: echo "Check out wipe_all_devices to see if sufficient" michael@0: echo "You can put a device in fastboot using adb reboot bootloader" michael@0: return 1 michael@0: fi michael@0: local SERIAL="" michael@0: fi michael@0: fastboot $SERIAL erase cache michael@0: fastboot $SERIAL erase userdata michael@0: } michael@0: michael@0: # Get list of devices connected via adb michael@0: # Args: -b block until adb detects a device michael@0: adb_get_devices() { michael@0: local DEVICES="$(adb devices | grep 'device$')" michael@0: if [[ -z $DEVICES && $1 == '-b' ]]; then michael@0: echo '- waiting for device -' >&2 michael@0: local DEVICES="$(adb wait-for-device devices | grep 'device$')" michael@0: fi michael@0: echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/' michael@0: } michael@0: michael@0: ################################################### michael@0: ## HELPER FUNCTIONS michael@0: ################################################### michael@0: michael@0: # Run an adb command in parallel over a device list michael@0: _adb_multi() { michael@0: local DEVICES=$1 michael@0: local ADB_ARGS=$2 michael@0: ( michael@0: for DEVICE in $DEVICES; do michael@0: adb -s $DEVICE $ADB_ARGS & michael@0: done michael@0: wait michael@0: ) michael@0: }