OLD | NEW |
---|---|
1 #!/bin/bash | 1 #!/bin/bash |
2 # | 2 # |
3 # Wait for the device to be ready to run tests. | 3 # Wait for the device to be ready to run tests. |
4 | 4 |
5 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | 5 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
6 source $SCRIPT_DIR/android_setup.sh | 6 source $SCRIPT_DIR/android_setup.sh |
7 source $SCRIPT_DIR/utils/setup_adb.sh | 7 source $SCRIPT_DIR/utils/setup_adb.sh |
8 | 8 |
9 function _get_battery_level { | |
rmistry
2015/12/14 18:16:09
Maybe better name is parse_battery_level (or somet
borenet
2015/12/14 18:26:31
Done in https://codereview.chromium.org/1527713002
| |
10 SPLIT=( $@ ) | |
11 | |
12 HAS_BATTERY=1 | |
13 LEVEL="" | |
14 | |
15 for i in "${!SPLIT[@]}"; do | |
16 if [ "${SPLIT[$i]}" = "level:" ]; then | |
17 LEVEL="${SPLIT[$i+1]}" | |
18 fi | |
19 if [ "${SPLIT[$i]}" = "present:" ]; then | |
20 PRESENT="$(echo "${SPLIT[$i+1]}" | tr -d '\r')" | |
21 if [ "$PRESENT" -eq "0" ]; then | |
22 HAS_BATTERY=0 | |
23 fi | |
24 if [ "$PRESENT" = "false" ]; then | |
25 HAS_BATTERY=0 | |
26 fi | |
27 fi | |
28 done | |
29 | |
30 if [ "$HAS_BATTERY" = "1" ]; then | |
31 echo "$LEVEL" | tr -d '\r' | |
32 return | |
33 fi | |
34 # If there's no battery, report a full battery. | |
35 echo "Device has no battery." 1>&2 | |
36 echo "100" | |
37 } | |
38 | |
9 function get_battery_level { | 39 function get_battery_level { |
10 STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)" | 40 STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)" |
11 SPLIT=( $STATS ) | 41 SPLIT=( $STATS ) |
12 for i in "${!SPLIT[@]}"; do | 42 RV="$(_get_battery_level ${SPLIT[@]})" |
13 if [ "${SPLIT[$i]}" = "level:" ]; then | 43 if [ -n "$RV" ]; then |
14 echo "${SPLIT[$i+1]}" | 44 echo "$RV" |
15 return | 45 return |
16 fi | 46 fi |
17 done | 47 |
48 echo "Battery level fallback..." 1>&2 | |
49 | |
50 STATS="$($ADB $DEVICE_SERIAL shell dumpsys battery)" | |
51 SPLIT=( $STATS ) | |
52 RV="$(_get_battery_level ${SPLIT[@]})" | |
53 if [ "$RV" != "-1" ]; then | |
54 echo "$RV" | |
55 return | |
56 fi | |
57 | |
18 echo "Could not determine battery level!" 1>&2 | 58 echo "Could not determine battery level!" 1>&2 |
19 # Just exit to prevent hanging forever or failing the build. | 59 # Just exit to prevent hanging forever or failing the build. |
20 exit 0 | 60 echo "0" |
21 } | 61 } |
22 | 62 |
23 set -e | 63 set -e |
24 | 64 |
25 # Wait for the device to be connected and fully booted. | 65 # Wait for the device to be connected and fully booted. |
26 while [ "$($ADB $DEVICE_SERIAL shell getprop sys.boot_completed | tr -d '\r')" ! = "1" ]; do | 66 while [ "$($ADB $DEVICE_SERIAL shell getprop sys.boot_completed | tr -d '\r')" ! = "1" ]; do |
27 echo "Waiting for the device to be connected and ready." | 67 echo "Waiting for the device to be connected and ready." |
28 sleep 5 | 68 sleep 5 |
29 done | 69 done |
30 | 70 |
31 # Wait for battery charge. | 71 # Wait for battery charge. |
32 DESIRED_BATTERY_LEVEL=30 | 72 DESIRED_BATTERY_LEVEL=30 |
33 CURRENT_BATTERY_LEVEL="$(get_battery_level)" | 73 CURRENT_BATTERY_LEVEL="$(get_battery_level)" |
34 while [ "${CURRENT_BATTERY_LEVEL}" -lt "${DESIRED_BATTERY_LEVEL}" ]; do | 74 while [ "${CURRENT_BATTERY_LEVEL}" -lt "${DESIRED_BATTERY_LEVEL}" ]; do |
35 echo "Battery level is ${CURRENT_BATTERY_LEVEL}; waiting to charge to ${DESIRE D_BATTERY_LEVEL}" | 75 echo "Battery level is ${CURRENT_BATTERY_LEVEL}; waiting to charge to ${DESIRE D_BATTERY_LEVEL}" |
36 sleep 5 | 76 sleep 5 |
37 CURRENT_BATTERY_LEVEL="$(get_battery_level)" | 77 CURRENT_BATTERY_LEVEL="$(get_battery_level)" |
38 done | 78 done |
39 | 79 |
40 echo "Ready!" | 80 echo "Ready!" |
OLD | NEW |