OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 # |
| 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. |
| 6 # |
| 7 # A collection of functions useful for maintaining android devices |
| 8 |
| 9 |
| 10 # Run an adb command on all connected device in parallel. |
| 11 # Usage: adb_all command line to eval. Quoting is optional. |
| 12 # |
| 13 # Examples: |
| 14 # adb_all install Chrome.apk |
| 15 # adb_all 'shell cat /path/to/file' |
| 16 # |
| 17 adb_all() { |
| 18 if [[ $# == 0 ]]; then |
| 19 echo "Usage: adb_all <adb command>. Quoting is optional." |
| 20 echo "Example: adb_all install Chrome.apk" |
| 21 return 1 |
| 22 fi |
| 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" |
| 27 fi |
| 28 _adb_multi "$DEVICES" "$*" |
| 29 } |
| 30 |
| 31 |
| 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 |
| 35 # adb_all |
| 36 # Usage: adb_device_loop 'command line to eval' |
| 37 adb_device_loop() { |
| 38 if [[ $# == 0 ]]; then |
| 39 echo "Intended for more complex one-liners that cannot be done with" \ |
| 40 "adb_all." |
| 41 echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \ |
| 42 'adb shell cat /data/local.prop)"' |
| 43 return 1 |
| 44 fi |
| 45 local DEVICES=$(adb_get_devices) |
| 46 if [[ -z $DEVICES ]]; then |
| 47 return |
| 48 fi |
| 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 "$*" |
| 54 done |
| 55 } |
| 56 |
| 57 # Erases data from any devices visible on adb. To preserve a device, |
| 58 # disconnect it or: |
| 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' |
| 62 # |
| 63 # Usage: wipe_all_devices [-f] |
| 64 # |
| 65 wipe_all_devices() { |
| 66 if [[ -z $(which adb) || -z $(which fastboot) ]]; then |
| 67 echo "aborting: adb and fastboot not in path" |
| 68 return 1 |
| 69 elif ! $(groups | grep -q 'plugdev'); then |
| 70 echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'" |
| 71 fi |
| 72 |
| 73 local DEVICES=$(adb_get_devices -b) |
| 74 |
| 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" |
| 78 fi |
| 79 |
| 80 _adb_multi "$DEVICES" "reboot bootloader" |
| 81 # Subshell to isolate job list |
| 82 ( |
| 83 for DEVICE in $DEVICES; do |
| 84 fastboot_erase $DEVICE & |
| 85 done |
| 86 wait |
| 87 ) |
| 88 |
| 89 # Reboot devices together |
| 90 for DEVICE in $DEVICES; do |
| 91 fastboot -s $DEVICE reboot |
| 92 done |
| 93 } |
| 94 |
| 95 # Wipe a device in fastboot. |
| 96 # Usage fastboot_erase [serial] |
| 97 fastboot_erase() { |
| 98 if [[ -n $1 ]]; then |
| 99 echo "Wiping $1" |
| 100 local SERIAL="-s $1" |
| 101 else |
| 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" |
| 106 return 1 |
| 107 fi |
| 108 local SERIAL="" |
| 109 fi |
| 110 fastboot $SERIAL erase cache |
| 111 fastboot $SERIAL erase userdata |
| 112 } |
| 113 |
| 114 # Get list of devices connected via adb |
| 115 # Args: -b block until adb detects a device |
| 116 adb_get_devices() { |
| 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$')" |
| 121 fi |
| 122 echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/' |
| 123 } |
| 124 |
| 125 ################################################### |
| 126 ## HELPER FUNCTIONS |
| 127 ################################################### |
| 128 |
| 129 # Run an adb command in parallel over a device list |
| 130 _adb_multi() { |
| 131 local DEVICES=$1 |
| 132 local ADB_ARGS=$2 |
| 133 ( |
| 134 for DEVICE in $DEVICES; do |
| 135 adb -s $DEVICE $ADB_ARGS & |
| 136 done |
| 137 wait |
| 138 ) |
| 139 } |
OLD | NEW |