Index: platform_tools/android/bin/adb_wait_for_device |
diff --git a/platform_tools/android/bin/adb_wait_for_device b/platform_tools/android/bin/adb_wait_for_device |
index 914b5fb5cef076d664d5e6cdacff5ea27d594877..fc8a0f118dd5402ff2ec345104febf0be58c02d4 100755 |
--- a/platform_tools/android/bin/adb_wait_for_device |
+++ b/platform_tools/android/bin/adb_wait_for_device |
@@ -6,18 +6,58 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
source $SCRIPT_DIR/android_setup.sh |
source $SCRIPT_DIR/utils/setup_adb.sh |
-function get_battery_level { |
- STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)" |
- SPLIT=( $STATS ) |
+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
|
+ SPLIT=( $@ ) |
+ |
+ HAS_BATTERY=1 |
+ LEVEL="" |
+ |
for i in "${!SPLIT[@]}"; do |
if [ "${SPLIT[$i]}" = "level:" ]; then |
- echo "${SPLIT[$i+1]}" |
- return |
+ LEVEL="${SPLIT[$i+1]}" |
+ fi |
+ if [ "${SPLIT[$i]}" = "present:" ]; then |
+ PRESENT="$(echo "${SPLIT[$i+1]}" | tr -d '\r')" |
+ if [ "$PRESENT" -eq "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" |
+} |
+ |
+function get_battery_level { |
+ STATS="$($ADB $DEVICE_SERIAL shell dumpsys batteryproperties)" |
+ SPLIT=( $STATS ) |
+ RV="$(_get_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="$(_get_battery_level ${SPLIT[@]})" |
+ if [ "$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. |
- exit 0 |
+ echo "0" |
} |
set -e |