aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/bin/adb_wait_for_charge
blob: f05c44f4557b75843ea4a72bb62371fd91df9738 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
#
# Wait for the device to be charged enough for testing.

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SKIP_TOOLCHAIN_SETUP="true"
source $SCRIPT_DIR/android_setup.sh
source $SCRIPT_DIR/utils/setup_adb.sh

# Helper function used by get_battery_level. Parses the battery level from
# dumpsys output.
function _parse_battery_level {
  SPLIT=( $@ )

  HAS_BATTERY=1
  LEVEL=""

  for i in "${!SPLIT[@]}"; do
    if [ "${SPLIT[$i]}" = "level:" ]; then
      LEVEL="${SPLIT[$i+1]}"
    fi
    if [ "${SPLIT[$i]}" = "present:" ]; then
      PRESENT="$(echo "${SPLIT[$i+1]}" | tr -d '\r')"
      if [ "$PRESENT" = "0" ]; then
        HAS_BATTERY=0
      fi
      if [ "$PRESENT" = "false" ]; then
        HAS_BATTERY=0
      fi
    fi
  done

  if [ "$HAS_BATTERY" = "1" ]; then
    echo "$LEVEL" | tr -d '\r'
    return
  fi
  # If there's no battery, report a full battery.
  echo "Device has no battery." 1>&2
  echo "100"
}

# Echo the battery level percentage of the attached Android device.
function get_battery_level {
  STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)"
  SPLIT=( $STATS )
  RV="$(_parse_battery_level ${SPLIT[@]})"
  if [ -n "$RV" ]; then
    echo "$RV"
    return
  fi

  echo "Battery level fallback..." 1>&2

  STATS="$($ADB $DEVICE_SERIAL shell dumpsys battery)"
  SPLIT=( $STATS )
  RV="$(_parse_battery_level ${SPLIT[@]})"
  if [ -n "$RV" ] && [ "$RV" != "-1" ]; then
    echo "$RV"
    return
  fi

  echo "Could not determine battery level!" 1>&2
  # Just exit to prevent hanging forever or failing the build.
  echo "0"
}

# Wait for battery charge.
DESIRED_BATTERY_LEVEL=80
CURRENT_BATTERY_LEVEL="$(get_battery_level)"
while [ "${CURRENT_BATTERY_LEVEL}" -lt "${DESIRED_BATTERY_LEVEL}" ]; do
  echo "Battery level is ${CURRENT_BATTERY_LEVEL}; waiting to charge to ${DESIRED_BATTERY_LEVEL}"
  sleep 5
  CURRENT_BATTERY_LEVEL="$(get_battery_level)"
done

echo "Charged!"