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_blocking_get_devices) |
| 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_blocking_get_devices) |
| 46 # Do not change DEVICE variable name - part of api |
| 47 for DEVICE in $DEVICES; do |
| 48 DEV_TYPE=$(adb -s $DEVICE shell getprop ro.product.device | sed 's/\r//') |
| 49 echo "Running on $DEVICE ($DEV_TYPE)" |
| 50 ANDROID_SERIAL=$DEVICE eval "$*" |
| 51 done |
| 52 } |
| 53 |
| 54 # Erases data from any devices visible on adb. To preserve a device, |
| 55 # disconnect it or: |
| 56 # 1) Reboot it into fastboot with 'adb reboot bootloader' |
| 57 # 2) Run wipe_all_devices to wipe remaining devices |
| 58 # 3) Restore device it with 'fastboot reboot' |
| 59 # |
| 60 # Usage: wipe_all_devices [-f] |
| 61 # |
| 62 wipe_all_devices() { |
| 63 if [[ -z $(which adb) || -z $(which fastboot) ]]; then |
| 64 echo "aborting: adb and fastboot not in path" |
| 65 return 1 |
| 66 elif ! $(groups | grep -q 'plugdev'); then |
| 67 echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'" |
| 68 fi |
| 69 |
| 70 local DEVICES=$(adb_blocking_get_devices) |
| 71 |
| 72 if [[ $1 != '-f' ]]; then |
| 73 echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device." |
| 74 read -p "Hit enter to continue" |
| 75 fi |
| 76 |
| 77 _adb_multi "$DEVICES" "root" |
| 78 _adb_multi "$DEVICES" "wait-for-device" |
| 79 _adb_multi "$DEVICES" "reboot bootloader" |
| 80 for DEVICE in $DEVICES; do |
| 81 fastboot_erase $DEVICE |
| 82 done |
| 83 |
| 84 # Reboot devices together |
| 85 for DEVICE in $DEVICES; do |
| 86 fastboot -s $DEVICE reboot |
| 87 done |
| 88 } |
| 89 |
| 90 # Wipe a device in fastboot. |
| 91 # Usage fastboot_erase [serial] |
| 92 fastboot_erase() { |
| 93 if [[ -n $1 ]]; then |
| 94 echo "Wiping $1" |
| 95 local SERIAL="-s $1" |
| 96 else |
| 97 if [ -z $(fastboot devices) ]; then |
| 98 echo "No devices in fastboot, aborting." |
| 99 echo "Check out wipe_all_devices to see if sufficient" |
| 100 echo "You can put a device in fastboot using adb reboot bootloader" |
| 101 return 1 |
| 102 fi |
| 103 local SERIAL="" |
| 104 fi |
| 105 fastboot $SERIAL erase cache |
| 106 fastboot $SERIAL erase userdata |
| 107 } |
| 108 |
| 109 # Block until adb detects a device, then return list of serials |
| 110 adb_blocking_get_devices() { |
| 111 local DEVICES="$(adb devices | grep 'device$')" |
| 112 if [[ -z $DEVICES ]]; then |
| 113 echo '- waiting for device -' >&2 |
| 114 local DEVICES="$(adb wait-for-device devices | grep 'device$')" |
| 115 fi |
| 116 echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/' |
| 117 } |
| 118 |
| 119 ################################################### |
| 120 ## HELPER FUNCTIONS |
| 121 ################################################### |
| 122 |
| 123 # Run an adb command in parallel over a device list |
| 124 _adb_multi() { |
| 125 local DEVICES=$1 |
| 126 local ADB_ARGS=$2 |
| 127 ( |
| 128 for DEVICE in $DEVICES; do |
| 129 adb -s $DEVICE $ADB_ARGS & |
| 130 done |
| 131 wait |
| 132 ) |
| 133 } |
OLD | NEW |