1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/build/android/adb_device_functions.sh Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +#!/bin/bash 1.5 +# 1.6 +# Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.7 +# Use of this source code is governed by a BSD-style license that can be 1.8 +# found in the LICENSE file. 1.9 +# 1.10 +# A collection of functions useful for maintaining android devices 1.11 + 1.12 + 1.13 +# Run an adb command on all connected device in parallel. 1.14 +# Usage: adb_all command line to eval. Quoting is optional. 1.15 +# 1.16 +# Examples: 1.17 +# adb_all install Chrome.apk 1.18 +# adb_all 'shell cat /path/to/file' 1.19 +# 1.20 +adb_all() { 1.21 + if [[ $# == 0 ]]; then 1.22 + echo "Usage: adb_all <adb command>. Quoting is optional." 1.23 + echo "Example: adb_all install Chrome.apk" 1.24 + return 1 1.25 + fi 1.26 + local DEVICES=$(adb_get_devices -b) 1.27 + local NUM_DEVICES=$(echo $DEVICES | wc -w) 1.28 + if (( $NUM_DEVICES > 1 )); then 1.29 + echo "Looping over $NUM_DEVICES devices" 1.30 + fi 1.31 + _adb_multi "$DEVICES" "$*" 1.32 +} 1.33 + 1.34 + 1.35 +# Run a command on each connected device. Quoting the command is suggested but 1.36 +# not required. The script setups up variable DEVICE to correspond to the 1.37 +# current serial number. Intended for complex one_liners that don't work in 1.38 +# adb_all 1.39 +# Usage: adb_device_loop 'command line to eval' 1.40 +adb_device_loop() { 1.41 + if [[ $# == 0 ]]; then 1.42 + echo "Intended for more complex one-liners that cannot be done with" \ 1.43 + "adb_all." 1.44 + echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \ 1.45 + 'adb shell cat /data/local.prop)"' 1.46 + return 1 1.47 + fi 1.48 + local DEVICES=$(adb_get_devices) 1.49 + if [[ -z $DEVICES ]]; then 1.50 + return 1.51 + fi 1.52 + # Do not change DEVICE variable name - part of api 1.53 + for DEVICE in $DEVICES; do 1.54 + DEV_TYPE=$(adb -s $DEVICE shell getprop ro.product.device | sed 's/\r//') 1.55 + echo "Running on $DEVICE ($DEV_TYPE)" 1.56 + ANDROID_SERIAL=$DEVICE eval "$*" 1.57 + done 1.58 +} 1.59 + 1.60 +# Erases data from any devices visible on adb. To preserve a device, 1.61 +# disconnect it or: 1.62 +# 1) Reboot it into fastboot with 'adb reboot bootloader' 1.63 +# 2) Run wipe_all_devices to wipe remaining devices 1.64 +# 3) Restore device it with 'fastboot reboot' 1.65 +# 1.66 +# Usage: wipe_all_devices [-f] 1.67 +# 1.68 +wipe_all_devices() { 1.69 + if [[ -z $(which adb) || -z $(which fastboot) ]]; then 1.70 + echo "aborting: adb and fastboot not in path" 1.71 + return 1 1.72 + elif ! $(groups | grep -q 'plugdev'); then 1.73 + echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'" 1.74 + fi 1.75 + 1.76 + local DEVICES=$(adb_get_devices -b) 1.77 + 1.78 + if [[ $1 != '-f' ]]; then 1.79 + echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device." 1.80 + read -p "Hit enter to continue" 1.81 + fi 1.82 + 1.83 + _adb_multi "$DEVICES" "reboot bootloader" 1.84 + # Subshell to isolate job list 1.85 + ( 1.86 + for DEVICE in $DEVICES; do 1.87 + fastboot_erase $DEVICE & 1.88 + done 1.89 + wait 1.90 + ) 1.91 + 1.92 + # Reboot devices together 1.93 + for DEVICE in $DEVICES; do 1.94 + fastboot -s $DEVICE reboot 1.95 + done 1.96 +} 1.97 + 1.98 +# Wipe a device in fastboot. 1.99 +# Usage fastboot_erase [serial] 1.100 +fastboot_erase() { 1.101 + if [[ -n $1 ]]; then 1.102 + echo "Wiping $1" 1.103 + local SERIAL="-s $1" 1.104 + else 1.105 + if [ -z $(fastboot devices) ]; then 1.106 + echo "No devices in fastboot, aborting." 1.107 + echo "Check out wipe_all_devices to see if sufficient" 1.108 + echo "You can put a device in fastboot using adb reboot bootloader" 1.109 + return 1 1.110 + fi 1.111 + local SERIAL="" 1.112 + fi 1.113 + fastboot $SERIAL erase cache 1.114 + fastboot $SERIAL erase userdata 1.115 +} 1.116 + 1.117 +# Get list of devices connected via adb 1.118 +# Args: -b block until adb detects a device 1.119 +adb_get_devices() { 1.120 + local DEVICES="$(adb devices | grep 'device$')" 1.121 + if [[ -z $DEVICES && $1 == '-b' ]]; then 1.122 + echo '- waiting for device -' >&2 1.123 + local DEVICES="$(adb wait-for-device devices | grep 'device$')" 1.124 + fi 1.125 + echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/' 1.126 +} 1.127 + 1.128 +################################################### 1.129 +## HELPER FUNCTIONS 1.130 +################################################### 1.131 + 1.132 +# Run an adb command in parallel over a device list 1.133 +_adb_multi() { 1.134 + local DEVICES=$1 1.135 + local ADB_ARGS=$2 1.136 + ( 1.137 + for DEVICE in $DEVICES; do 1.138 + adb -s $DEVICE $ADB_ARGS & 1.139 + done 1.140 + wait 1.141 + ) 1.142 +}