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 { | 9 # Helper function used by get_battery_level. Parses the battery level from |
10 # dumpsys output. | |
11 function _parse_battery_level { | |
10 SPLIT=( $@ ) | 12 SPLIT=( $@ ) |
11 | 13 |
12 HAS_BATTERY=1 | 14 HAS_BATTERY=1 |
13 LEVEL="" | 15 LEVEL="" |
14 | 16 |
15 for i in "${!SPLIT[@]}"; do | 17 for i in "${!SPLIT[@]}"; do |
16 if [ "${SPLIT[$i]}" = "level:" ]; then | 18 if [ "${SPLIT[$i]}" = "level:" ]; then |
17 LEVEL="${SPLIT[$i+1]}" | 19 LEVEL="${SPLIT[$i+1]}" |
18 fi | 20 fi |
19 if [ "${SPLIT[$i]}" = "present:" ]; then | 21 if [ "${SPLIT[$i]}" = "present:" ]; then |
20 PRESENT="$(echo "${SPLIT[$i+1]}" | tr -d '\r')" | 22 PRESENT="$(echo "${SPLIT[$i+1]}" | tr -d '\r')" |
21 if [ "$PRESENT" -eq "0" ]; then | 23 if [ "$PRESENT" = "0" ]; then |
borenet
2015/12/14 18:26:24
Prevents error if PRESENT is not an integer, eg. "
| |
22 HAS_BATTERY=0 | 24 HAS_BATTERY=0 |
23 fi | 25 fi |
24 if [ "$PRESENT" = "false" ]; then | 26 if [ "$PRESENT" = "false" ]; then |
25 HAS_BATTERY=0 | 27 HAS_BATTERY=0 |
26 fi | 28 fi |
27 fi | 29 fi |
28 done | 30 done |
29 | 31 |
30 if [ "$HAS_BATTERY" = "1" ]; then | 32 if [ "$HAS_BATTERY" = "1" ]; then |
31 echo "$LEVEL" | tr -d '\r' | 33 echo "$LEVEL" | tr -d '\r' |
32 return | 34 return |
33 fi | 35 fi |
34 # If there's no battery, report a full battery. | 36 # If there's no battery, report a full battery. |
35 echo "Device has no battery." 1>&2 | 37 echo "Device has no battery." 1>&2 |
36 echo "100" | 38 echo "100" |
37 } | 39 } |
38 | 40 |
41 # Echo the battery level percentage of the attached Android device. | |
39 function get_battery_level { | 42 function get_battery_level { |
40 STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)" | 43 STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)" |
41 SPLIT=( $STATS ) | 44 SPLIT=( $STATS ) |
42 RV="$(_get_battery_level ${SPLIT[@]})" | 45 RV="$(_parse_battery_level ${SPLIT[@]})" |
43 if [ -n "$RV" ]; then | 46 if [ -n "$RV" ]; then |
44 echo "$RV" | 47 echo "$RV" |
45 return | 48 return |
46 fi | 49 fi |
47 | 50 |
48 echo "Battery level fallback..." 1>&2 | 51 echo "Battery level fallback..." 1>&2 |
49 | 52 |
50 STATS="$($ADB $DEVICE_SERIAL shell dumpsys battery)" | 53 STATS="$($ADB $DEVICE_SERIAL shell dumpsys battery)" |
51 SPLIT=( $STATS ) | 54 SPLIT=( $STATS ) |
52 RV="$(_get_battery_level ${SPLIT[@]})" | 55 RV="$(_parse_battery_level ${SPLIT[@]})" |
53 if [ "$RV" != "-1" ]; then | 56 if [ "$RV" != "-1" ]; then |
54 echo "$RV" | 57 echo "$RV" |
55 return | 58 return |
56 fi | 59 fi |
57 | 60 |
58 echo "Could not determine battery level!" 1>&2 | 61 echo "Could not determine battery level!" 1>&2 |
59 # Just exit to prevent hanging forever or failing the build. | 62 # Just exit to prevent hanging forever or failing the build. |
60 echo "0" | 63 echo "0" |
61 } | 64 } |
62 | 65 |
63 set -e | 66 set -e |
64 | 67 |
65 # Wait for the device to be connected and fully booted. | 68 # Wait for the device to be connected and fully booted. |
66 while [ "$($ADB $DEVICE_SERIAL shell getprop sys.boot_completed | tr -d '\r')" ! = "1" ]; do | 69 while [ "$($ADB $DEVICE_SERIAL shell getprop sys.boot_completed | tr -d '\r')" ! = "1" ]; do |
67 echo "Waiting for the device to be connected and ready." | 70 echo "Waiting for the device to be connected and ready." |
68 sleep 5 | 71 sleep 5 |
69 done | 72 done |
70 | 73 |
71 # Wait for battery charge. | 74 # Wait for battery charge. |
72 DESIRED_BATTERY_LEVEL=30 | 75 DESIRED_BATTERY_LEVEL=30 |
73 CURRENT_BATTERY_LEVEL="$(get_battery_level)" | 76 CURRENT_BATTERY_LEVEL="$(get_battery_level)" |
74 while [ "${CURRENT_BATTERY_LEVEL}" -lt "${DESIRED_BATTERY_LEVEL}" ]; do | 77 while [ "${CURRENT_BATTERY_LEVEL}" -lt "${DESIRED_BATTERY_LEVEL}" ]; do |
75 echo "Battery level is ${CURRENT_BATTERY_LEVEL}; waiting to charge to ${DESIRE D_BATTERY_LEVEL}" | 78 echo "Battery level is ${CURRENT_BATTERY_LEVEL}; waiting to charge to ${DESIRE D_BATTERY_LEVEL}" |
76 sleep 5 | 79 sleep 5 |
77 CURRENT_BATTERY_LEVEL="$(get_battery_level)" | 80 CURRENT_BATTERY_LEVEL="$(get_battery_level)" |
78 done | 81 done |
79 | 82 |
80 echo "Ready!" | 83 echo "Ready!" |
OLD | NEW |