Skip to content

Commit c8d5b84

Browse files
committed
Do not allow flashing if the battery is too low
1 parent ff8069a commit c8d5b84

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

zip-content/inc/common-functions.sh

+20-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ _parse_kernel_cmdline()
6767
local _var
6868
if test ! -e '/proc/cmdline'; then return 2; fi
6969

70-
if _var="$(grep -o -m 1 -e "androidboot\.${1:?}=[^ ]*" -- '/proc/cmdline' | cut -d '=' -f 2 -s)"; then
70+
if _var="$(grep -o -m 1 -e "androidboot\.${1:?}=[^ ]*" -- '/proc/cmdline' | cut -d '=' -f '2-' -s)"; then
7171
printf '%s\n' "${_var?}"
7272
return 0
7373
fi
@@ -93,6 +93,16 @@ _detect_verity_status()
9393
fi
9494
}
9595

96+
_detect_battery_level()
97+
{
98+
if test -n "${DEVICE_DUMPSYS?}" && _val="$("${DEVICE_DUMPSYS:?}" battery | grep -m 1 -F -e 'level:' | cut -d ':' -f '2-' -s)" && _val="${_val# }" && test -n "${_val?}"; then
99+
printf '%s\n' "${_val:?}"
100+
return 0
101+
fi
102+
103+
return 1
104+
}
105+
96106
is_verity_enabled()
97107
{
98108
case "${VERITY_MODE?}" in
@@ -692,6 +702,7 @@ display_info()
692702
ui_msg "Device: ${BUILD_DEVICE?}"
693703
ui_msg "Product: ${BUILD_PRODUCT?}"
694704
ui_msg "Emulator: ${IS_EMU:?}"
705+
ui_msg "Battery level: ${BATTERY_LEVEL:-unknown}"
695706
ui_msg_empty_line
696707
ui_msg "First installation: ${FIRST_INSTALLATION:?}"
697708
ui_msg "Boot mode: ${BOOTMODE:?}"
@@ -720,7 +731,7 @@ display_info()
720731
ui_msg "System path: ${SYS_PATH:?}"
721732
ui_msg "Priv-app dir: ${PRIVAPP_DIRNAME:?}"
722733
#ui_msg "Android root ENV: ${ANDROID_ROOT-}"
723-
ui_msg "$(write_separator_line "${#MODULE_NAME}" '-' || true)"
734+
ui_msg "$(write_separator_line "${#MODULE_NAME}" '-' || :)"
724735
}
725736

726737
initialize()
@@ -782,6 +793,13 @@ initialize()
782793
if test -e '/dev/block/mapper'; then readonly DYNAMIC_PARTITIONS='true'; else readonly DYNAMIC_PARTITIONS='false'; fi
783794
export DYNAMIC_PARTITIONS
784795

796+
BATTERY_LEVEL="$(_detect_battery_level)" || BATTERY_LEVEL=''
797+
readonly BATTERY_LEVEL
798+
export BATTERY_LEVEL
799+
if test -n "${BATTERY_LEVEL?}" && test "${BATTERY_LEVEL:?}" -le 15; then
800+
ui_error "The battery is too low. Current level: ${BATTERY_LEVEL?}%" 108
801+
fi
802+
785803
_find_and_mount_system
786804
cp -pf "${SYS_PATH:?}/build.prop" "${TMP_PATH:?}/build.prop" # Cache the file for faster access
787805

0 commit comments

Comments
 (0)