Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/bin/bash |
michael@0 | 2 | # |
michael@0 | 3 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 4 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | # found in the LICENSE file. |
michael@0 | 6 | # |
michael@0 | 7 | # A collection of functions useful for maintaining android devices |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | # Run an adb command on all connected device in parallel. |
michael@0 | 11 | # Usage: adb_all command line to eval. Quoting is optional. |
michael@0 | 12 | # |
michael@0 | 13 | # Examples: |
michael@0 | 14 | # adb_all install Chrome.apk |
michael@0 | 15 | # adb_all 'shell cat /path/to/file' |
michael@0 | 16 | # |
michael@0 | 17 | adb_all() { |
michael@0 | 18 | if [[ $# == 0 ]]; then |
michael@0 | 19 | echo "Usage: adb_all <adb command>. Quoting is optional." |
michael@0 | 20 | echo "Example: adb_all install Chrome.apk" |
michael@0 | 21 | return 1 |
michael@0 | 22 | fi |
michael@0 | 23 | local DEVICES=$(adb_get_devices -b) |
michael@0 | 24 | local NUM_DEVICES=$(echo $DEVICES | wc -w) |
michael@0 | 25 | if (( $NUM_DEVICES > 1 )); then |
michael@0 | 26 | echo "Looping over $NUM_DEVICES devices" |
michael@0 | 27 | fi |
michael@0 | 28 | _adb_multi "$DEVICES" "$*" |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | # Run a command on each connected device. Quoting the command is suggested but |
michael@0 | 33 | # not required. The script setups up variable DEVICE to correspond to the |
michael@0 | 34 | # current serial number. Intended for complex one_liners that don't work in |
michael@0 | 35 | # adb_all |
michael@0 | 36 | # Usage: adb_device_loop 'command line to eval' |
michael@0 | 37 | adb_device_loop() { |
michael@0 | 38 | if [[ $# == 0 ]]; then |
michael@0 | 39 | echo "Intended for more complex one-liners that cannot be done with" \ |
michael@0 | 40 | "adb_all." |
michael@0 | 41 | echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \ |
michael@0 | 42 | 'adb shell cat /data/local.prop)"' |
michael@0 | 43 | return 1 |
michael@0 | 44 | fi |
michael@0 | 45 | local DEVICES=$(adb_get_devices) |
michael@0 | 46 | if [[ -z $DEVICES ]]; then |
michael@0 | 47 | return |
michael@0 | 48 | fi |
michael@0 | 49 | # Do not change DEVICE variable name - part of api |
michael@0 | 50 | for DEVICE in $DEVICES; do |
michael@0 | 51 | DEV_TYPE=$(adb -s $DEVICE shell getprop ro.product.device | sed 's/\r//') |
michael@0 | 52 | echo "Running on $DEVICE ($DEV_TYPE)" |
michael@0 | 53 | ANDROID_SERIAL=$DEVICE eval "$*" |
michael@0 | 54 | done |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | # Erases data from any devices visible on adb. To preserve a device, |
michael@0 | 58 | # disconnect it or: |
michael@0 | 59 | # 1) Reboot it into fastboot with 'adb reboot bootloader' |
michael@0 | 60 | # 2) Run wipe_all_devices to wipe remaining devices |
michael@0 | 61 | # 3) Restore device it with 'fastboot reboot' |
michael@0 | 62 | # |
michael@0 | 63 | # Usage: wipe_all_devices [-f] |
michael@0 | 64 | # |
michael@0 | 65 | wipe_all_devices() { |
michael@0 | 66 | if [[ -z $(which adb) || -z $(which fastboot) ]]; then |
michael@0 | 67 | echo "aborting: adb and fastboot not in path" |
michael@0 | 68 | return 1 |
michael@0 | 69 | elif ! $(groups | grep -q 'plugdev'); then |
michael@0 | 70 | echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'" |
michael@0 | 71 | fi |
michael@0 | 72 | |
michael@0 | 73 | local DEVICES=$(adb_get_devices -b) |
michael@0 | 74 | |
michael@0 | 75 | if [[ $1 != '-f' ]]; then |
michael@0 | 76 | echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device." |
michael@0 | 77 | read -p "Hit enter to continue" |
michael@0 | 78 | fi |
michael@0 | 79 | |
michael@0 | 80 | _adb_multi "$DEVICES" "reboot bootloader" |
michael@0 | 81 | # Subshell to isolate job list |
michael@0 | 82 | ( |
michael@0 | 83 | for DEVICE in $DEVICES; do |
michael@0 | 84 | fastboot_erase $DEVICE & |
michael@0 | 85 | done |
michael@0 | 86 | wait |
michael@0 | 87 | ) |
michael@0 | 88 | |
michael@0 | 89 | # Reboot devices together |
michael@0 | 90 | for DEVICE in $DEVICES; do |
michael@0 | 91 | fastboot -s $DEVICE reboot |
michael@0 | 92 | done |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | # Wipe a device in fastboot. |
michael@0 | 96 | # Usage fastboot_erase [serial] |
michael@0 | 97 | fastboot_erase() { |
michael@0 | 98 | if [[ -n $1 ]]; then |
michael@0 | 99 | echo "Wiping $1" |
michael@0 | 100 | local SERIAL="-s $1" |
michael@0 | 101 | else |
michael@0 | 102 | if [ -z $(fastboot devices) ]; then |
michael@0 | 103 | echo "No devices in fastboot, aborting." |
michael@0 | 104 | echo "Check out wipe_all_devices to see if sufficient" |
michael@0 | 105 | echo "You can put a device in fastboot using adb reboot bootloader" |
michael@0 | 106 | return 1 |
michael@0 | 107 | fi |
michael@0 | 108 | local SERIAL="" |
michael@0 | 109 | fi |
michael@0 | 110 | fastboot $SERIAL erase cache |
michael@0 | 111 | fastboot $SERIAL erase userdata |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | # Get list of devices connected via adb |
michael@0 | 115 | # Args: -b block until adb detects a device |
michael@0 | 116 | adb_get_devices() { |
michael@0 | 117 | local DEVICES="$(adb devices | grep 'device$')" |
michael@0 | 118 | if [[ -z $DEVICES && $1 == '-b' ]]; then |
michael@0 | 119 | echo '- waiting for device -' >&2 |
michael@0 | 120 | local DEVICES="$(adb wait-for-device devices | grep 'device$')" |
michael@0 | 121 | fi |
michael@0 | 122 | echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/' |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | ################################################### |
michael@0 | 126 | ## HELPER FUNCTIONS |
michael@0 | 127 | ################################################### |
michael@0 | 128 | |
michael@0 | 129 | # Run an adb command in parallel over a device list |
michael@0 | 130 | _adb_multi() { |
michael@0 | 131 | local DEVICES=$1 |
michael@0 | 132 | local ADB_ARGS=$2 |
michael@0 | 133 | ( |
michael@0 | 134 | for DEVICE in $DEVICES; do |
michael@0 | 135 | adb -s $DEVICE $ADB_ARGS & |
michael@0 | 136 | done |
michael@0 | 137 | wait |
michael@0 | 138 | ) |
michael@0 | 139 | } |