Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / build / android / adb_device_functions.sh
blob66cc32fc4e398b03f605dcae9abfbb913c125891
1 #!/bin/bash
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.
7 # A collection of functions useful for maintaining android devices
10 # Run an adb command on all connected device in parallel.
11 # Usage: adb_all command line to eval. Quoting is optional.
13 # Examples:
14 # adb_all install Chrome.apk
15 # adb_all 'shell cat /path/to/file'
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
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"
28 _adb_multi "$DEVICES" "$*"
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
45 local DEVICES=$(adb_get_devices)
46 if [[ -z $DEVICES ]]; then
47 return
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
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'
63 # Usage: wipe_all_devices [-f]
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'"
73 local DEVICES=$(adb_get_devices -b)
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"
80 _adb_multi "$DEVICES" "reboot bootloader"
81 # Subshell to isolate job list
83 for DEVICE in $DEVICES; do
84 fastboot_erase $DEVICE &
85 done
86 wait
89 # Reboot devices together
90 for DEVICE in $DEVICES; do
91 fastboot -s $DEVICE reboot
92 done
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
108 local SERIAL=""
110 fastboot $SERIAL erase cache
111 fastboot $SERIAL erase userdata
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$')"
122 echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/'
125 ###################################################
126 ## HELPER FUNCTIONS
127 ###################################################
129 # Run an adb command in parallel over a device list
130 _adb_multi() {
131 local DEVICES=$1
132 local ADB_ARGS=$2
134 for DEVICE in $DEVICES; do
135 adb -s $DEVICE $ADB_ARGS &
136 done
137 wait