diff --git a/META-INF/com/google/android/update-binary b/META-INF/com/google/android/update-binary index cefd409..374a9a6 100644 --- a/META-INF/com/google/android/update-binary +++ b/META-INF/com/google/android/update-binary @@ -12,12 +12,12 @@ SKIPUNZIP=1 echo id=acc -umask 077 +umask 0077 # log mkdir -p /data/adb/${id}-data/logs -chmod -R 700 /data/adb/${id}-data/logs +chmod -R 0700 /data/adb/${id}-data/logs exec 2>/data/adb/${id}-data/logs/install.log set -x @@ -26,9 +26,14 @@ exxit() { local e=$? set +eo pipefail rm -rf /dev/.${id}-install - [ $e -ne 0 ] && echo || { - rm /sbin/.$id/.ghost-charging ### - /sbin/acca --daemon > /dev/null || /sbin/accd ### workaround, Magisk 20.4+ + [ $e -ne 0 ] && echo || { ### + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd || { + pkill -9 -f $installDir/service.sh + $installDir/service.sh --override + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd + } || e=12 } exit $e } 2>/dev/null @@ -45,19 +50,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" @@ -78,8 +83,8 @@ fi get_prop() { sed -n "s|^$1=||p" ${2:-$srcDir/module.prop}; } set_perms() { - local owner=${2:-0} perms=600 target=$(readlink -f $1) - if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=700; fi + local owner=${2:-0} perms=0600 target=$(readlink -f $1) + if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=0700; fi chmod $perms $target chown $owner:$owner $target restorecon $target > /dev/null 2>&1 || : @@ -110,15 +115,23 @@ srcDir=${srcDir/#"${0##*/}"/"."} name=$(get_prop name) author=$(get_prop author) version=$(get_prop version) +magiskModDir=/sbin/.magisk/modules versionCode=$(get_prop versionCode) -installDir=${1:-/data/data/mattecarra.${id}app/files} ### +: ${installDir:=/data/data/mattecarra.${id}app/files} ### config=/data/adb/${id}-data/config.txt +[ -d $magiskModDir ] && magisk=true || magisk=false + # check/set parent installation directory -[ -d $installDir ] || installDir=/sbin/.magisk/modules + +[ -d $installDir ] || installDir=$magiskModDir [ -d $installDir ] || installDir=/data/adb -[ -d $installDir ] || { echo "(!) /data/adb/ not found\n"; exit 1; } + +[ -d $installDir ] || { + echo "(!) /data/adb/ not found\n" + exit 1 +} ### @@ -129,7 +142,6 @@ GPLv3+ (i) Installing in $installDir/$id/..." -# install ash $srcDir/$id/uninstall.sh install rm /data/adb/${id}-data/logs/bootlooped 2>/dev/null || : cp -R $srcDir/$id/ $installDir/ @@ -137,51 +149,47 @@ installDir=$(readlink -f $installDir/$id) cp $srcDir/module.prop $installDir/ mkdir -p ${config%/*}/info cp -f $srcDir/*.md ${config%/*}/info -[ $installDir == /data/adb/$id ] || ln -s $installDir /data/adb/ - - -if [ $installDir != /sbin/.magisk/modules/$id ]; then - - mv $installDir/service.sh $installDir/${id}-init.sh - - # enable upgrading through Magisk Manager - ln -s $installDir /sbin/.magisk/modules/$id 2>/dev/null || : - [ ! -d /data/adb/service.d ] || { -# alternate initialization script -echo "#!/system/bin/sh -# alternate $id initializer -(until [ -d /storage/emulated/0/?ndroid ]; do sleep 10; done -if [ -f $installDir/${id}-init.sh ]; then - $installDir/${id}-init.sh -else - rm \$0 -fi -exit 0 &) & -exit 0" > /data/adb/service.d/${id}-init.sh +case $installDir in + /data/*/files/*$id) + ! $magisk || { + cp -R $installDir $magiskModDir/ -# post-uninstall cleanup script +# front-end post-uninstall cleanup script echo "#!/system/bin/sh -# $id post-uninstall cleanup -(until [ -d /storage/emulated/0/?ndroid ]; do sleep 15; done +# $id front-end post-uninstall cleanup script +(until [ -d /sdcard/?ndroid ]; do sleep 15; done if [ ! -f $installDir/module.prop ]; then - rm /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null + rm -rf /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null fi exit 0 &) & exit 0" > /data/adb/service.d/${id}-cleanup.sh - chmod 700 /data/adb/service.d/${id}-*.sh - } + chmod 0700 /data/adb/service.d/${id}-cleanup.sh + } + ln $installDir/service.sh $installDir/${id}-init.sh + + # TODO + # upgrade bundled version + #cp -f $srcDir/install-tarball.sh ${installDir%/*}/ + #tar -cvf - . -C $srcDir --exclude .git | gzip -9 > ${installDir%/*}/acc_bundle.tar.gz + ;; +esac -else - # workaround for Magisk "forgetting service.sh" issue - ln $installDir/service.sh $installDir/post-fs-data.sh -fi +[ $installDir == /data/adb/$id ] || { -# disable magic mount (Magisk) -touch /sbin/.magisk/modules/$id/skip_mount 2>/dev/null || : + ln -s $installDir /data/adb/ + + ! $magisk || { + # workaround for Magisk "forgetting service.sh" issue + ln $magiskModDir/$id/service.sh $magiskModDir/$id/post-fs-data.sh + + # disable magic mount (Magisk) + touch $magiskModDir/$id/skip_mount + } +} # restore config backup @@ -204,13 +212,14 @@ cp -f $srcDir/bin/${id}-uninstaller.zip /data/media/0/ # set perms set_perms_recursive ${config%/*} -chmod 666 /data/media/0/${id}-uninstaller.zip +chmod 0666 /data/media/0/${id}-uninstaller.zip case $installDir in /data/*/files/*$id) pkg=${installDir%/files/*$id} pkg=${pkg##/data*/} owner=$(grep $pkg /data/system/packages.list | cut -d ' ' -f 2) set_perms_recursive $installDir $owner + ! $magisk || set_perms_recursive $magiskModDir/$id # Termux:Boot ! $termux || { @@ -232,8 +241,6 @@ echo "- Done " - - # print links and changelog sed -En "\|## LINKS|,\$p" ${config%/*}/info/README.md \ | grep -v '^---' | sed 's/^## //' @@ -249,14 +256,9 @@ echo " [ $installDir == /data/adb ] && echo -e "\n(i) Use init.d or an app to run $installDir/${id}-init.sh on boot to initialize ${id}." - echo # initialize $id -if [ -f $installDir/service.sh ]; then - $installDir/service.sh --override -else - $installDir/${id}-init.sh --override -fi +$installDir/service.sh --override exit 0 diff --git a/README.md b/README.md index c83b26d..c85f38d 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ In interactive mode, it also asks the user whether they want to download and ins ``` #DC# -configVerCode=202004170 +configVerCode=202004200 capacity=(-1 101 70 75 +0 false) temperature=(70 80 90) cooldownRatio=() @@ -191,12 +191,11 @@ rebootOnPause= switchDelay=1.5 language=en wakeUnlock=() -prioritizeBattIdleMode=false +prioritizeBattIdleMode=true forceChargingStatusFullAt100= runCmdOnPause=() dynPowerSaving=0 autoShutdownAlertCmd=(vibrate 5 0.1) -calibrationAlertCmd=(vibrate 5 0.1) chargDisabledNotifCmd=(vibrate 3 0.1) chargEnabledNotifCmd=(vibrate 4 0.1) errorAlertCmd=(vibrate 6 0.1) @@ -254,7 +253,7 @@ errorAlertCmd=(vibrate 6 0.1) # dynPowerSaving=dyn_power_saving=seconds -# +# # VARIABLE ALIASES/SORTCUTS @@ -299,7 +298,6 @@ errorAlertCmd=(vibrate 6 0.1) # dps dyn_power_saving # asac auto_shutdown_alert_cmd -# cac calibration_alert_cmd # cdnc charg_disabled_notif_cmd # cenc charg_enabled_notif_cmd # eac error_alert_cmd @@ -465,11 +463,10 @@ errorAlertCmd=(vibrate 6 0.1) # * On top of loop_delay_discharging # auto_shutdown_alert_cmd (asac) # -# calibration_alert_cmd (cac) # # charg_disabled_notif_cmd (cdnc) # # charg_enabled_notif_cmd (cenc) # # error_alert_cmd (eac) # -# As the names suggest, these properties dictate commands acc/s run at each event. +# As the names suggest, these properties dictate commands acc/d/s should run at each event. # The default command is "vibrate " # Termux APIs can be used for notifications, TTS, toasts and more. For details, refer to https://wiki.termux.com/wiki/Termux:API . @@ -532,11 +529,6 @@ Options acc -c less acc -c cat - -C|--calibrate [update freq] Charge to true 100% - e.g., - acc -C (update info every 3 seconds) - acc -C 0.5 (update info every half a second) - -d|--disable [#%, #s, #m or #h (optional)] Disable charging e.g., acc -d 70% (do not recharge until capacity <= 70%) @@ -623,7 +615,9 @@ Options -s|--set p|--print [egrep regex (default: ".")] Print current config without blank lines (refer to previous examples) -s|--set r|--reset Restore default config - e.g., acc -s r + e.g., + acc -s r + rm /data/adb/acc-data/config.txt (failsafe) -s|--set s|charging_switch Enforce a specific charging switch e.g., acc -s s @@ -688,6 +682,8 @@ Exit Codes 8. Daemon already running ("--daemon start") 9. Daemon not running ("--daemon" and "--daemon stop") 10. "--test" failed + 11. Current (mA) out of range + 12. install.sh failed to initialize acc or start accd Logs are exported automatically ("--log --export") on exit codes 1, 2, 7 and 10. @@ -759,7 +755,7 @@ Refer back to `DEFAULT CONFIGURATION (switch_delay)`. ### Battery Capacity (% Level) Doesn't Seem Right -The "smart" battery may need calibration. +The "smart" battery may require calibration. Refer to the `FAQ` section below for details. If we're talking about a Pixel device, the issue goes beyond that. @@ -771,7 +767,7 @@ Refer back to `DEFAULT CONFIGURATION > capacity_sync`. Recent/custom kernels (e.g., Kirisakura) support battery idle mode. However, at the time of this writing, the feature is not production quality. ACC has custom code to cover its pitfalls, though. -`battery/op_disable_charge 0 1` must be enforced manually (`acc -s s` or `acc -s s="battery/op_disable_charge 0 1"`). +`battery/op_disable_charge 0 1` must be enforced manually (`acc -s s` or `acc -s s="battery/op_disable_charge 0 1"`) and accd, restarted afterwards. ### Bootloop, ACC Not Found @@ -833,7 +829,9 @@ Automatic exporting happens under specific conditions (refer back to `SETUP/USAG ### Restore Default Config -`acc --set --reset` or `acc -s r` +This can save you a lot of time and grief. + +`acc --set --reset`, `acc -s r` or `rm /data/adb/acc-data/config.txt` (failsafe) ### Slow Charging @@ -934,8 +932,24 @@ Refer back to `POWER SUPPLY LOGS (HELP NEEDED)`. > Why, when and how should I calibrate the battery? With modern battery management systems, that's generally unnecessary. -If your battery is underperforming, refer to https://batteryuniversity.com/index.php/learn/article/battery_calibration . -The calibration command is `acc -C`. + +However, if your battery is underperforming, you may want to try the following procedure: + +1. Let the battery charge until VOLTAGE_NOW >= VOLTAGE_MAX* and CURRENT_NOW drops to 3% of the rated mAh capacity, or less. + +2. Let it discharge until the phone shuts off. + +3. Turn the phone back on to consume any "residual" charge left. +Repeat this until the device refuses to stay/turn on. +Next, try booting straight into download, fastboot or recovery mode. +Repeat until the phone totally refuses to stay/turn on. + +4. Charge to 100% without turning it on. +Leave the device plugged in for another hour or so. +This emulates step 1. +Done. + +For additional information, refer to https://batteryuniversity.com/index.php/learn/article/battery_calibration . > What if even after calibrating the battery, ACC and Android battery level reports still differ? @@ -1042,6 +1056,18 @@ A common workaround is having `resume_capacity = pause_capacity - 1`. --- ## LATEST CHANGES +**v2020.4.20-beta (202004200)** +- Blacklisted troublesome Pixel [1-3]* charging switches +- Fixed absurd charging current and voltage reports (AccA) +- Fixed upgrade and downgrade issues (AccA) +- Include AccA logcat in log archive +- Optimized installers +- prioritizeBattIdleMode=true by default +- Removed `acc --calibrate`, in favor of universal and comprehensive calibration instructions (README > FAQ) +- Under the hood enhancements +- Updated documentation +- Workaround for encryption preventing AccA from initializing ACC + **v2020.4.17-beta (202004170)** - acc -C: replace misleading 100% with 99% - acc -s: restoring individual defaults is as simple as running `acc -s var1= var2= ...` @@ -1058,11 +1084,3 @@ A common workaround is having `resume_capacity = pause_capacity - 1`. - Renamed cooldown(_c|C)urrent/cooldown(_c|C)ustom - Native support for MediaTek mt6795 devices, e.g., Redmi Note 2 (hermes) - Updated build script and documentation - -**v2020.4.8-beta (202004080)** -- acc -t: fixed "charging-switches: no such file" error -- accd: fixed crash on plug/unplug that affected some users -- Current control optimizations -- Enhanced battery calibration helper (acc -C) -- More intuitive versioning scheme -- Stricter config integrity checks (auto-reset broken config) diff --git a/TODO.txt b/TODO.txt index 736d7f2..6ef4675 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,12 +4,6 @@ cooldown full POSIX compliance -sometimes, acc is not initialized - Magisk forgetting to run acc boot scripts? - logs/bootlooped? - -Termux APIs - # getval index $var getval() { local index=$1 @@ -26,9 +20,6 @@ setval() { eval $var=\""${values/$val_/$val}"\" } -enforce default values on null - -alert cmds - doc and print-config.sh - -AccA and encryption +install.sh + remove legacy cp + upgrade acc_bundle.tar.gz and install-tarball.sh diff --git a/Termux:API.md b/Termux:API.md new file mode 100644 index 0000000..7d7d28e --- /dev/null +++ b/Termux:API.md @@ -0,0 +1,47 @@ +**Playing With Termux:API - Notifications, Toasts, Text-to-speech and More** + + +1) Install Termux, Termux:Boot and Termux:API APKs. +If you're not willing to pay for Termux add-ons, go for the F-Droid* versions of these AND Termux itself. +Since package signatures mismatch, you can't** install the add-ons from F-Droid if Termux was obtained from Play Store and vice versa. + +2) On Termux, paste and run, as a regular user: `mkfifo ~/acc-fifo; mkdir -p ~/.termux/boot; pkg install termux-api` + +3) Write your termux-api script that gets its input from ~/acc-fifo. Place the file in ~/.termux/boot/. +Example (text-to-speech): +```#!/data/data/com.termux/files/usr/bin/sh +# This file is called sore-throat. +# It talks too much. +while true; do cat ~/acc-fifo; done | termux-tts-speak``` + +4) Open Termux:Boot to run the script and enable auto-start. + + +5) ACC has the following: + +auto_shutdown_alert_cmd (asac) +charg_disabled_notif_cmd (cdnc) +charg_enabled_notif_cmd (cenc) +error_alert_cmd (eac) + +As the names suggest, these properties dictate commands acc/d/s should run at each event. +The default command is "vibrate " + +Let's assume you want the phone to say "Warning! Battery is low. System will shutdown soon." +To set that up... + +Write a script that communicates with that from step 3: + +```# This file is called warning-script. +# It's cool af! +! pgrep -f termux-tts-speak || echo 'Warning! Battery is low. System will shutdown soon.' > /data/data/com.termux/files/home/acc-fifo``` + +Run `acc -s auto_shutdown_alert_cmd=". /path/to/warning-script."` + + +\* https://duckduckgo.com/lite/?q=termux%20F-Droid + +\*\* There's a workaround, but that's a story for another day. + +Recommend reading: https://wiki.termux.com/wiki/Termux:API + diff --git a/acc/acc.sh b/acc/acc.sh index 0b0fa46..fd76f28 100644 --- a/acc/acc.sh +++ b/acc/acc.sh @@ -100,7 +100,7 @@ test_charging_switch() { local failed=false switchDelay=20 - chmod +w $1 ${4-} \ + chmod u+w $1 ${4-} \ && echo "${3//::/ }" > $1 \ && echo "${6//::/ }" > ${4:-/dev/null} \ && sleep $switchDelay @@ -138,9 +138,6 @@ exxit() { eval "${errorAlertCmd[@]-}" } rm /dev/.acc-config 2>/dev/null - ! ${restartDaemon-false} || { - ! $daemonWasUp || /sbin/accd $config - } exit $exitCode } @@ -227,28 +224,6 @@ case "${1-}" in shift; edit $config "$@" ;; - -C|--calibrate) - daemon_ctrl stop > /dev/null && daemonWasUp=true || daemonWasUp=false - restartDaemon=true - [ -f $batt/charge_counter -a -f $batt/charge_full ] || { - acca --watch${2-} "$(print_calibration)" - return 0 - } - print_quit CTRL-C - sleep 2 - while [[ $(cat $batt/charge_counter) -lt $(cat $batt/charge_full) ]]; do - for i in "¦ % ¦" "\\ > % < /" "- >> % << -" "/ >>> % <<< \\"; do - clear - echo -n "$i" | sed "s/%/[$(cat $batt/capacity)%]/" | sed 's/100/99/' - sleep 0.2 - done - unset i - done - echo "¦ [100%] ¦" - eval "${calibrationAlertCmd[@]-}" - print_discharge - ;; - -d|--disable) shift not_charging && print_already_discharging || { @@ -329,29 +304,27 @@ case "${1-}" in -t|--test) shift - ! not_charging || print_unplugged - print_wait + print_unplugged + daemon_ctrl stop > /dev/null && daemonWasUp=true || daemonWasUp=false cp $config /dev/.acc-config config=/dev/.acc-config - exec 3>&1 forceVibrations=true - daemon_ctrl stop && daemonWasUp=true || daemonWasUp=false + exec 3>&1 set +eo pipefail 2>/dev/null - enable_charging + trap '$daemonWasUp && /sbin/accd $config__' EXIT + not_charging && enable_charging > /dev/null not_charging && { (print_wait_plug - trap '$daemonWasUp && { - /sbin/accd $config__ - print_started - }' EXIT while not_charging; do sleep 1 set +x done) } + print_wait + case "${2-}" in "") exitCode=10 @@ -370,12 +343,6 @@ case "${1-}" in esac : ${exitCode=$?} - - $daemonWasUp && { - /sbin/accd $config__ - print_started - } - exit $exitCode ;; diff --git a/acc/acca.sh b/acc/acca.sh index 0bb229e..11f14df 100644 --- a/acc/acca.sh +++ b/acc/acca.sh @@ -39,6 +39,7 @@ case "$@" in cd /sys/class/power_supply/ batt=$(echo *attery/capacity | cut -d ' ' -f 1 | sed 's|/capacity||') . /sbin/.acc/acc/batt-info.sh + verbose=false batt_info "${2-}" exit $? ;; diff --git a/acc/accd.sh b/acc/accd.sh index f143df5..2916097 100644 --- a/acc/accd.sh +++ b/acc/accd.sh @@ -83,6 +83,7 @@ is_charging() { capacity[5]=true [ ${loopDelay[0]} -le 5 ] || loopDelay[0]=5 [ ${loopDelay[1]} -le 10 ] || loopDelay[1]=10 + dynPowerSaving=0 . $modPath/write-config.sh } else @@ -194,7 +195,7 @@ ctrl_charging() { [ -f ${config%/*}/.rebootedOnPause ] || { # wakeUnlock # won't run under "battery idle" mode ("not charging" status) - if grep -iq dis $batt/status && chmod +w /sys/power/wake_unlock; then + if grep -iq dis $batt/status && chmod u+w /sys/power/wake_unlock; then for wakelock in "${wakeUnlock[@]-}"; do echo $wakelock > /sys/power/wake_unlock || : done diff --git a/acc/default-config.txt b/acc/default-config.txt index 36dd43c..406d6b7 100644 --- a/acc/default-config.txt +++ b/acc/default-config.txt @@ -1,4 +1,4 @@ -configVerCode=202004170 +configVerCode=202004200 capacity=(-1 101 70 75 +0 false) temperature=(70 80 90) cooldownRatio=() @@ -14,12 +14,11 @@ rebootOnPause= switchDelay=1.5 language=en wakeUnlock=() -prioritizeBattIdleMode=false +prioritizeBattIdleMode=true forceChargingStatusFullAt100= runCmdOnPause=() dynPowerSaving=0 autoShutdownAlertCmd=(vibrate 5 0.1) -calibrationAlertCmd=(vibrate 5 0.1) chargDisabledNotifCmd=(vibrate 3 0.1) chargEnabledNotifCmd=(vibrate 4 0.1) errorAlertCmd=(vibrate 6 0.1) @@ -77,7 +76,7 @@ errorAlertCmd=(vibrate 6 0.1) # dynPowerSaving=dyn_power_saving=seconds -# +# # VARIABLE ALIASES/SORTCUTS @@ -122,7 +121,6 @@ errorAlertCmd=(vibrate 6 0.1) # dps dyn_power_saving # asac auto_shutdown_alert_cmd -# cac calibration_alert_cmd # cdnc charg_disabled_notif_cmd # cenc charg_enabled_notif_cmd # eac error_alert_cmd @@ -288,10 +286,9 @@ errorAlertCmd=(vibrate 6 0.1) # * On top of loop_delay_discharging # auto_shutdown_alert_cmd (asac) # -# calibration_alert_cmd (cac) # # charg_disabled_notif_cmd (cdnc) # # charg_enabled_notif_cmd (cenc) # # error_alert_cmd (eac) # -# As the names suggest, these properties dictate commands acc/s run at each event. +# As the names suggest, these properties dictate commands acc/d/s should run at each event. # The default command is "vibrate " # Termux APIs can be used for notifications, TTS, toasts and more. For details, refer to https://wiki.termux.com/wiki/Termux:API . diff --git a/acc/init.sh b/acc/init.sh index d6385ad..c469216 100644 --- a/acc/init.sh +++ b/acc/init.sh @@ -6,7 +6,7 @@ # devs: triple hashtags (###) mark custom code -umask 077 +umask 0077 ( # log mkdir -p /data/adb/${id}-data/logs @@ -64,7 +64,7 @@ umask 077 ctrlFile2="$(echo $4 | cut -d ' ' -f 1)" [ -f "$ctrlFile1" ] && { [ -f "$ctrlFile2" -o -z "$ctrlFile2" ] && { - chmod +r $ctrlFile1 || continue + chmod u+r $ctrlFile1 || continue if grep -Eq "^(${2//::/ }|${3//::/ })$" $ctrlFile1 \ || ! cat $ctrlFile1 > /dev/null || [ -z "$(cat $ctrlFile1)" ] then @@ -80,7 +80,7 @@ umask 077 ls -1 */BatterySenseVoltage */ISenseVoltage */batt_vol */InstatVolt \ */constant_charge_voltage* */voltage_max */batt_tune_float_voltage 2>/dev/null | \ while read file; do - chmod +r $file 2>/dev/null && grep -Eq '^4[1-4][0-9]{2}' $file || continue + chmod u+r $file 2>/dev/null && grep -Eq '^4[1-4][0-9]{2}' $file || continue echo ${file}::$(sed -n 's/^..../vvvv/p' $file)::$(cat $file) \ >> $TMPDIR/ch-volt-ctrl-files_ done @@ -93,7 +93,7 @@ umask 077 ls -1 */input_current_limited */restrict*_ch*g* \ /sys/class/qcom-battery/restrict*_ch*g* 2>/dev/null | \ while read file; do - chmod +r $file 2>/dev/null || continue + chmod u+r $file 2>/dev/null || continue grep -q '^0$' $file && echo ${file}::1::0 >> $TMPDIR/ch-curr-ctrl-files done @@ -109,7 +109,7 @@ umask 077 *dcp_input */wc_input */car_charge \ */dcp_charge */wc_charge 2>/dev/null | \ while read file; do - chmod +r $file 2>/dev/null || continue + chmod u+r $file 2>/dev/null || continue defaultValue=$(cat $file) [ $defaultValue -ne 0 ] && { if [ $defaultValue -lt 10000 ]; then diff --git a/acc/logf.sh b/acc/logf.sh index ad9fae0..822fb7e 100644 --- a/acc/logf.sh +++ b/acc/logf.sh @@ -3,13 +3,14 @@ logf() { if [[ "${1:-x}" == -*e* ]]; then exec 2>> ${log:-/dev/null} - set +eo pipefail + set +eo pipefail 2>/dev/null cd $TMPDIR - cp oem-custom oem-custom.txt 2>/dev/null cp ch-switches charging-switches.txt + cp oem-custom oem-custom.txt 2>/dev/null cp ch-curr-ctrl-files charging-current-ctrl-files.txt cp ch-volt-ctrl-files charging-voltage-ctrl-files.txt + [ -d /data/app/mattecarra.accapp* ] && logcat -de acca > AccA-logcat.log for file in /cache/magisk.log /data/cache/magisk.log; do [ -f $file ] && cp $file ./ && break @@ -26,7 +27,7 @@ logf() { tar -c *.log *.txt \ | gzip -9 > /data/media/0/acc-logs-$device.tar.gz - chmod 777 /data/media/0/acc-logs-$device.tar.gz + chmod 0666 /data/media/0/acc-logs-$device.tar.gz rm *.txt magisk.log in*.log power*.log $isAccd || echo "(i) /sdcard/acc-logs-$device.tar.gz" diff --git a/acc/misc-functions.sh b/acc/misc-functions.sh index e070468..3dceea1 100644 --- a/acc/misc-functions.sh +++ b/acc/misc-functions.sh @@ -12,7 +12,7 @@ apply_on_boot() { file=${1-} value=${2-} { $exitCmd && ! $force; } && default=${2-} || default=${3:-${2-}} - [ -f "$file" ] && chmod +w $file && eval "echo \$$arg" > $file || : + [ -f "$file" ] && chmod u+w $file && eval "echo \$$arg" > $file || : done $exitCmd && [ $arg == value ] && exit 0 || : @@ -26,7 +26,7 @@ apply_on_plug() { file=${1-} value=${2-} default=${3:-${2-}} - [ -f "$file" ] && chmod +w $file && eval "echo \$$arg" > $file || : + [ -f "$file" ] && chmod u+w $file && eval "echo \$$arg" > $file || : done } @@ -42,7 +42,7 @@ cycle_switches() { # toggle primary switch on="${chargingSwitch[1]//::/ }" off="${chargingSwitch[2]//::/ }" - chmod +w ${chargingSwitch[0]} \ + chmod u+w ${chargingSwitch[0]} \ && eval "echo "\$$1"" > ${chargingSwitch[0]} \ || continue @@ -50,7 +50,7 @@ cycle_switches() { [ ! -f "${chargingSwitch[3]-}" ] || { on="${chargingSwitch[4]//::/ }" off="${chargingSwitch[5]//::/ }" - chmod +w ${chargingSwitch[3]} \ + chmod u+w ${chargingSwitch[3]} \ && eval "echo "\$$1"" > ${chargingSwitch[3]} || : } @@ -99,10 +99,10 @@ disable_charging() { if [[ ${chargingSwitch[0]:-x} == */* ]]; then if [ -f ${chargingSwitch[0]} ]; then # toggle primary switch - if chmod +w ${chargingSwitch[0]} && echo "${chargingSwitch[2]//::/ }" > ${chargingSwitch[0]}; then + if chmod u+w ${chargingSwitch[0]} && echo "${chargingSwitch[2]//::/ }" > ${chargingSwitch[0]}; then [ ! -f "${chargingSwitch[3]-}" ] || { # toggle secondary switch - chmod +w ${chargingSwitch[3]} && echo "${chargingSwitch[5]//::/ }" > ${chargingSwitch[3]} || { + chmod u+w ${chargingSwitch[3]} && echo "${chargingSwitch[5]//::/ }" > ${chargingSwitch[3]} || { $isAccd || print_switch_fails unset_switch cycle_switches_off @@ -137,9 +137,7 @@ disable_charging() { return 7 # total failure fi - set +euo pipefail 2>/dev/null eval "${runCmdOnPause[@]-}" - set -euo pipefail 2>/dev/null || : ${cooldown-false} || { eval "${chargDisabledNotifCmd[@]-}" @@ -186,7 +184,7 @@ enable_charging() { [ "${2-}" == noap ] || apply_on_plug } - (chmod +w ${chargingSwitch[0]-} ${chargingSwitch[3]-} \ + (chmod u+w ${chargingSwitch[0]-} ${chargingSwitch[3]-} \ && echo ${chargingSwitch[1]-} > ${chargingSwitch[0]-} \ && echo ${chargingSwitch[4]-} > ${chargingSwitch[3]-}) 2>/dev/null \ || cycle_switches on @@ -309,7 +307,7 @@ wait_plug() { # environment -umask 077 +umask 0077 modPath=/sbin/.acc/acc export TMPDIR=${modPath%/*} config=/data/adb/acc-data/config.txt diff --git a/acc/oem-custom.sh b/acc/oem-custom.sh index 2bcdd7e..75f4d4d 100644 --- a/acc/oem-custom.sh +++ b/acc/oem-custom.sh @@ -9,17 +9,8 @@ set_prop_() { sed -i "\|^${1}=|s|=.*|=$2|" ${3:-$config}; } # patch/reset [broken/obsolete] config configVer=0$(get_prop configVerCode 2>/dev/null || :) defaultConfVer=$(get_prop configVerCode $modPath/default-config.txt) -if (. $config) && ! grep_ '^versionCode='; then - [ $configVer -eq $defaultConfVer ] || { - - # patch cooldownCurrent and dynPowerSaving=120 - ! grep_ 'cooldown(_c|C)urrent' || { - sed -Ei -e '/cooldown(_c|C)urrent/s/rrent/stom/g' \ - -e '/dynPowerSaving=120/s/120/0/' $config - } - - /sbin/acca --set dummy= - } +if (. $config) && ! grep_ '^dynPowerSaving=120|^versionCode='; then + [ $configVer -eq $defaultConfVer ] || /sbin/acca --set dummy= else cp -f $modPath/default-config.txt $config rm /sdcard/acc-logs-*.tar.bz2 || : ### legacy @@ -29,7 +20,7 @@ fi 2>/dev/null # OnePlus 7/Pro battery idle mode if grep_ '^chargingSwitch=.*/op_disable_charge'; then [ -f $TMPDIR/oem-custom ] \ - || echo "chmod +w battery/input_suspend; echo 1 > battery/op_disable_charge; echo 0 > battery/input_suspend" > $TMPDIR/oem-custom + || echo "chmod u+w battery/input_suspend; echo 1 > battery/op_disable_charge; echo 0 > battery/input_suspend" > $TMPDIR/oem-custom grep_ "^runCmdOnPause=.*$TMPDIR/oem-custom" \ || set_prop_ runCmdOnPause "(. $TMPDIR/oem-custom)" switchDelay=$(get_prop switchDelay) @@ -62,4 +53,13 @@ echo 30 > usb/razer_charge_limit_dropdown) 2>/dev/null || : sed -i /ChargerEnable/d $modPath/charging-switches.txt } } + + +# Pixel [1-3]*, exclude charge_control_limit switches (troublesome) +! grep_ '\[Pixel(\]| [1-3])' $TMPDIR/acc-power_supply-*.log || { + ! grep_ charge_control_limit $modPath/charging-switches.txt || { + sed -i /charge_control_limit/d $TMPDIR/ch-switches + sed -i /charge_control_limit/d $modPath/charging-switches.txt + } +} ) diff --git a/acc/power-supply-logger.sh b/acc/power-supply-logger.sh index 22e08b4..5af2a26 100644 --- a/acc/power-supply-logger.sh +++ b/acc/power-supply-logger.sh @@ -27,7 +27,7 @@ gather_ps_data() { } # log -umask 077 +umask 0077 exec 2>/data/adb/${id}-data/logs/power-supply-logger.sh.log set -x diff --git a/acc/print-config.sh b/acc/print-config.sh index b46b499..57804d8 100644 --- a/acc/print-config.sh +++ b/acc/print-config.sh @@ -47,8 +47,6 @@ dyn_power_saving=${dynPowerSaving} auto_shutdown_alert_cmd=${autoShutdownAlertCmd[@]} -calibration_alert_cmd=${calibrationAlertCmd[@]} - charg_enabled_notif_cmd=${chargEnabledNotifCmd[@]} error_alert_cmd=${errorAlertCmd[@]} diff --git a/acc/read-ch-curr-ctrl-files-p2.sh b/acc/read-ch-curr-ctrl-files-p2.sh index 7998cca..6fb8fe6 100644 --- a/acc/read-ch-curr-ctrl-files-p2.sh +++ b/acc/read-ch-curr-ctrl-files-p2.sh @@ -7,7 +7,7 @@ grep -q ::v $TMPDIR/ch-curr-ctrl-files || { ls -1 */current_max */input_current_max 2>/dev/null | \ while read file; do - chmod +r $file || continue + chmod u+r $file || continue defaultValue=$(cat $file) if [ $defaultValue -lt 10000 -a $defaultValue -ne 0 ]; then # milliamps diff --git a/acc/service.sh b/acc/service.sh index a25e1aa..33d5c20 100644 --- a/acc/service.sh +++ b/acc/service.sh @@ -5,6 +5,7 @@ id=acc +umask 0077 TMPDIR=/sbin/.$id # prevent unnecessary runs @@ -16,7 +17,7 @@ TMPDIR=/sbin/.$id # prepare Almquist Shell mkdir -p /dev/.busybox -chmod 700 /dev/.busybox +chmod 0700 /dev/.busybox ln -sf `which busybox` /dev/.busybox/ash # start the engine diff --git a/acc/set-prop.sh b/acc/set-prop.sh index 5303e83..1dd19f4 100644 --- a/acc/set-prop.sh +++ b/acc/set-prop.sh @@ -50,7 +50,7 @@ set_prop() { print_known_switches echo . $modPath/select.sh - select_ chargingSwitch $(print_auto) $(cat $TMPDIR/ch-switches) $(print_exit) + select_ chargingSwitch $(print_auto; cat $TMPDIR/ch-switches; print_exit) [ ${chargingSwitch:-x} != $(print_exit) ] || exit 0 [ ${chargingSwitch:-x} != $(print_auto) ] || charging_switch= unset IFS @@ -110,7 +110,7 @@ set_prop() { apply_current $2 || return 1 else echo "(!) [0-9999] ($(print_mA | sed 's/^ //')) $(print_only)" - return 1 + return 11 fi fi diff --git a/acc/setup-busybox.sh b/acc/setup-busybox.sh index 55b9e6a..a5ee4a8 100644 --- a/acc/setup-busybox.sh +++ b/acc/setup-busybox.sh @@ -12,19 +12,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" diff --git a/acc/strings.sh b/acc/strings.sh index 7c2835b..348d46e 100644 --- a/acc/strings.sh +++ b/acc/strings.sh @@ -121,11 +121,6 @@ Options acc -c less acc -c cat - -C|--calibrate [update freq] Charge to true 100% - e.g., - acc -C (update info every 3 seconds) - acc -C 0.5 (update info every half a second) - -d|--disable [#%, #s, #m or #h (optional)] Disable charging e.g., acc -d 70% (do not recharge until capacity <= 70%) @@ -212,7 +207,9 @@ Options -s|--set p|--print [egrep regex (default: ".")] Print current config without blank lines (refer to previous examples) -s|--set r|--reset Restore default config - e.g., acc -s r + e.g., + acc -s r + rm /data/adb/acc-data/config.txt (failsafe) -s|--set s|charging_switch Enforce a specific charging switch e.g., acc -s s @@ -277,6 +274,8 @@ Exit Codes 8. Daemon already running ("--daemon start") 9. Daemon not running ("--daemon" and "--daemon stop") 10. "--test" failed + 11. Current (mA) out of range + 12. install.sh failed to initialize acc or start accd Logs are exported automatically ("--log --export") on exit codes 1, 2, 7 and 10. @@ -443,12 +442,8 @@ print_m_mode() { echo "(i) Manual mode" } -print_discharge() { - echo "(i) Now let the battery discharge until the system shuts down" -} - print_wait() { - echo "(i) This may take a few minutes..." + echo "(i) Alright, this may take a few minutes..." } print_already_charging() { @@ -458,8 +453,3 @@ print_already_charging() { print_already_discharging() { echo "(i) Already not changing" } - -print_calibration() { - echo "Let the battery charge until VOLTAGE_NOW >= VOLTAGE_MAX* and CURRENT_NOW drops to 3% of the rated mAh capacity or less. -Next, let it discharge until the system shuts down." -} diff --git a/acc/uninstall.sh b/acc/uninstall.sh index d9e04e8..aa88fb3 100644 --- a/acc/uninstall.sh +++ b/acc/uninstall.sh @@ -18,19 +18,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" diff --git a/acc/write-config.sh b/acc/write-config.sh index c54b41a..6b04fea 100644 --- a/acc/write-config.sh +++ b/acc/write-config.sh @@ -57,7 +57,7 @@ eac="${error_alert_cmd-${eac-${errorAlertCmd[@]}}}" { echo "configVerCode=$(cat $TMPDIR/.config-ver) capacity=(${sc:--1} ${cc:-101} ${rc:-70} ${pc:-75} ${co:-+0} ${cs:-false}) -temperature=(${ct:-70} ${mt:-80} ${mtp:90}) +temperature=(${ct:-70} ${mt:-80} ${mtp:-90}) cooldownRatio=($cch $cp) cooldownCustom=($ccu) resetBattStats=(${rbsp:-false} ${rbsu:-false}) @@ -76,7 +76,6 @@ forceChargingStatusFullAt100=$ff runCmdOnPause=($rcp) dynPowerSaving=${dps:-0} autoShutdownAlertCmd=(${asac:-vibrate 5 0.1}) -calibrationAlertCmd=(${cac:-vibrate 5 0.1}) chargDisabledNotifCmd=(${cdnc:-vibrate 3 0.1}) chargEnabledNotifCmd=(${cenc:-vibrate 4 0.1}) errorAlertCmd=(${eac:-vibrate 6 0.1}) diff --git a/bin/acc-uninstaller.zip b/bin/acc-uninstaller.zip index e394371..f6a9d37 100644 Binary files a/bin/acc-uninstaller.zip and b/bin/acc-uninstaller.zip differ diff --git a/build.sh b/build.sh index c4388d0..5fe9921 100644 --- a/build.sh +++ b/build.sh @@ -79,6 +79,7 @@ set +e # unify installers for flashable zip (customize.sh and update-binary are copies of install.sh) { cp -u install.sh customize.sh +cp -u install.sh install-current.sh ### legacy cp -u install.sh META-INF/com/google/android/update-binary; } 2>/dev/null diff --git a/customize.sh b/customize.sh index cefd409..374a9a6 100644 --- a/customize.sh +++ b/customize.sh @@ -12,12 +12,12 @@ SKIPUNZIP=1 echo id=acc -umask 077 +umask 0077 # log mkdir -p /data/adb/${id}-data/logs -chmod -R 700 /data/adb/${id}-data/logs +chmod -R 0700 /data/adb/${id}-data/logs exec 2>/data/adb/${id}-data/logs/install.log set -x @@ -26,9 +26,14 @@ exxit() { local e=$? set +eo pipefail rm -rf /dev/.${id}-install - [ $e -ne 0 ] && echo || { - rm /sbin/.$id/.ghost-charging ### - /sbin/acca --daemon > /dev/null || /sbin/accd ### workaround, Magisk 20.4+ + [ $e -ne 0 ] && echo || { ### + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd || { + pkill -9 -f $installDir/service.sh + $installDir/service.sh --override + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd + } || e=12 } exit $e } 2>/dev/null @@ -45,19 +50,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" @@ -78,8 +83,8 @@ fi get_prop() { sed -n "s|^$1=||p" ${2:-$srcDir/module.prop}; } set_perms() { - local owner=${2:-0} perms=600 target=$(readlink -f $1) - if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=700; fi + local owner=${2:-0} perms=0600 target=$(readlink -f $1) + if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=0700; fi chmod $perms $target chown $owner:$owner $target restorecon $target > /dev/null 2>&1 || : @@ -110,15 +115,23 @@ srcDir=${srcDir/#"${0##*/}"/"."} name=$(get_prop name) author=$(get_prop author) version=$(get_prop version) +magiskModDir=/sbin/.magisk/modules versionCode=$(get_prop versionCode) -installDir=${1:-/data/data/mattecarra.${id}app/files} ### +: ${installDir:=/data/data/mattecarra.${id}app/files} ### config=/data/adb/${id}-data/config.txt +[ -d $magiskModDir ] && magisk=true || magisk=false + # check/set parent installation directory -[ -d $installDir ] || installDir=/sbin/.magisk/modules + +[ -d $installDir ] || installDir=$magiskModDir [ -d $installDir ] || installDir=/data/adb -[ -d $installDir ] || { echo "(!) /data/adb/ not found\n"; exit 1; } + +[ -d $installDir ] || { + echo "(!) /data/adb/ not found\n" + exit 1 +} ### @@ -129,7 +142,6 @@ GPLv3+ (i) Installing in $installDir/$id/..." -# install ash $srcDir/$id/uninstall.sh install rm /data/adb/${id}-data/logs/bootlooped 2>/dev/null || : cp -R $srcDir/$id/ $installDir/ @@ -137,51 +149,47 @@ installDir=$(readlink -f $installDir/$id) cp $srcDir/module.prop $installDir/ mkdir -p ${config%/*}/info cp -f $srcDir/*.md ${config%/*}/info -[ $installDir == /data/adb/$id ] || ln -s $installDir /data/adb/ - - -if [ $installDir != /sbin/.magisk/modules/$id ]; then - - mv $installDir/service.sh $installDir/${id}-init.sh - - # enable upgrading through Magisk Manager - ln -s $installDir /sbin/.magisk/modules/$id 2>/dev/null || : - [ ! -d /data/adb/service.d ] || { -# alternate initialization script -echo "#!/system/bin/sh -# alternate $id initializer -(until [ -d /storage/emulated/0/?ndroid ]; do sleep 10; done -if [ -f $installDir/${id}-init.sh ]; then - $installDir/${id}-init.sh -else - rm \$0 -fi -exit 0 &) & -exit 0" > /data/adb/service.d/${id}-init.sh +case $installDir in + /data/*/files/*$id) + ! $magisk || { + cp -R $installDir $magiskModDir/ -# post-uninstall cleanup script +# front-end post-uninstall cleanup script echo "#!/system/bin/sh -# $id post-uninstall cleanup -(until [ -d /storage/emulated/0/?ndroid ]; do sleep 15; done +# $id front-end post-uninstall cleanup script +(until [ -d /sdcard/?ndroid ]; do sleep 15; done if [ ! -f $installDir/module.prop ]; then - rm /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null + rm -rf /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null fi exit 0 &) & exit 0" > /data/adb/service.d/${id}-cleanup.sh - chmod 700 /data/adb/service.d/${id}-*.sh - } + chmod 0700 /data/adb/service.d/${id}-cleanup.sh + } + ln $installDir/service.sh $installDir/${id}-init.sh + + # TODO + # upgrade bundled version + #cp -f $srcDir/install-tarball.sh ${installDir%/*}/ + #tar -cvf - . -C $srcDir --exclude .git | gzip -9 > ${installDir%/*}/acc_bundle.tar.gz + ;; +esac -else - # workaround for Magisk "forgetting service.sh" issue - ln $installDir/service.sh $installDir/post-fs-data.sh -fi +[ $installDir == /data/adb/$id ] || { -# disable magic mount (Magisk) -touch /sbin/.magisk/modules/$id/skip_mount 2>/dev/null || : + ln -s $installDir /data/adb/ + + ! $magisk || { + # workaround for Magisk "forgetting service.sh" issue + ln $magiskModDir/$id/service.sh $magiskModDir/$id/post-fs-data.sh + + # disable magic mount (Magisk) + touch $magiskModDir/$id/skip_mount + } +} # restore config backup @@ -204,13 +212,14 @@ cp -f $srcDir/bin/${id}-uninstaller.zip /data/media/0/ # set perms set_perms_recursive ${config%/*} -chmod 666 /data/media/0/${id}-uninstaller.zip +chmod 0666 /data/media/0/${id}-uninstaller.zip case $installDir in /data/*/files/*$id) pkg=${installDir%/files/*$id} pkg=${pkg##/data*/} owner=$(grep $pkg /data/system/packages.list | cut -d ' ' -f 2) set_perms_recursive $installDir $owner + ! $magisk || set_perms_recursive $magiskModDir/$id # Termux:Boot ! $termux || { @@ -232,8 +241,6 @@ echo "- Done " - - # print links and changelog sed -En "\|## LINKS|,\$p" ${config%/*}/info/README.md \ | grep -v '^---' | sed 's/^## //' @@ -249,14 +256,9 @@ echo " [ $installDir == /data/adb ] && echo -e "\n(i) Use init.d or an app to run $installDir/${id}-init.sh on boot to initialize ${id}." - echo # initialize $id -if [ -f $installDir/service.sh ]; then - $installDir/service.sh --override -else - $installDir/${id}-init.sh --override -fi +$installDir/service.sh --override exit 0 diff --git a/framework-details.txt b/framework-details.txt index 1ae2ec8..7eb9030 100644 --- a/framework-details.txt +++ b/framework-details.txt @@ -25,7 +25,7 @@ bin/ $id-uninstaller.zip (generated by build.sh; update-binary is a copy of $id/uninstall.sh) META-INF/com/google/android/ - update-binary (copy of install-current.sh) + update-binary (copy of install.sh) updater-script .gitattributes @@ -36,10 +36,10 @@ bundle.bat bundle.sh (copies tarball and install-tarball.sh to front-end-src-code/app/src/main/res/raw/) check-syntax.bat check-syntax.sh -customize.sh (copy of install-current.sh, for Magisk) +customize.sh (copy of install.sh, for Magisk) framework-details.txt (this file) -install-current.sh (main installer) -install-latest.sh (upgrader, online installer) +install.sh (main installer) +install-online.sh (upgrader, online installer) install-tarball.sh License.md module.prop @@ -96,7 +96,7 @@ bin/ acc-uninstaller.zip (generated by build.sh; update-binary is a copy of acc/uninstall.sh) META-INF/com/google/android/ - update-binary (copy of install-current.sh) + update-binary (copy of install.sh) updater-script .gitattributes @@ -107,10 +107,10 @@ bundle.bat bundle.sh (copies tarball and install-tarball.sh to front-end-src-code/app/src/main/res/raw/) check-syntax.bat check-syntax.sh -customize.sh (copy of install-current.sh, for Magisk) +customize.sh (copy of install.sh, for Magisk) framework-details.txt (this file) -install-current.sh (main installer) -install-latest.sh (upgrader, online installer) +install.sh (main installer) +install-online.sh (upgrader, online installer) install-tarball.sh License.md module.prop @@ -130,6 +130,6 @@ Tasks Executed By build.sh 5) Update busybox config (from $id/setup-busybox.sh) in $id/uninstall.sh and install scripts. -6) Unify installers for flashable zip (customize.sh and update-binary are copies of install-current.sh). +6) Unify installers for flashable zip (customize.sh and update-binary are copies of install.sh). 7) Generate flashable zips (main and uninstaller) and tarball. diff --git a/install-current.sh b/install-current.sh new file mode 100644 index 0000000..374a9a6 --- /dev/null +++ b/install-current.sh @@ -0,0 +1,264 @@ +#!/system/bin/sh +# ACC Installer/Upgrader +# Copyright (c) 2019-2020, VR25 (xda-developers) +# License: GPLv3+ +# +# devs: triple hashtags (###) mark custom code + + +# override the official Magisk module installer +SKIPUNZIP=1 + + +echo +id=acc +umask 0077 + + +# log +mkdir -p /data/adb/${id}-data/logs +chmod -R 0700 /data/adb/${id}-data/logs +exec 2>/data/adb/${id}-data/logs/install.log +set -x + + +exxit() { + local e=$? + set +eo pipefail + rm -rf /dev/.${id}-install + [ $e -ne 0 ] && echo || { ### + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd || { + pkill -9 -f $installDir/service.sh + $installDir/service.sh --override + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd + } || e=12 + } + exit $e +} 2>/dev/null + +trap exxit EXIT + + +# set up busybox +#BB# +if [ -d /sbin/.magisk/busybox ]; then + case $PATH in + /sbin/.magisk/busybox:*) :;; + *) PATH=/sbin/.magisk/busybox:$PATH;; + esac +else + mkdir -p /dev/.busybox + chmod 0700 /dev/.busybox + case $PATH in + /dev/.busybox:*) :;; + *) PATH=/dev/.busybox:$PATH;; + esac + [ -x /dev/.busybox/busybox ] || { + if [ -f /data/adb/magisk/busybox ]; then + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox + /data/adb/magisk/busybox --install -s /dev/.busybox + elif which busybox > /dev/null; then + busybox --install -s /dev/.busybox + elif [ -f /data/adb/busybox ]; then + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox + /data/adb/busybox --install -s /dev/.busybox + else + echo "(!) Install busybox or simply place it in /data/adb/" + exit 3 + fi + } +fi +#/BB# + + +# root check +[ $(id -u) -ne 0 ] && { + echo "(!) $0 must run as root (su)" + exit 4 +} + + +get_prop() { sed -n "s|^$1=||p" ${2:-$srcDir/module.prop}; } + +set_perms() { + local owner=${2:-0} perms=0600 target=$(readlink -f $1) + if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=0700; fi + chmod $perms $target + chown $owner:$owner $target + restorecon $target > /dev/null 2>&1 || : +} + +set_perms_recursive() { + local owner=${2-0} target="" + find $1 2>/dev/null | while read target; do set_perms $target $owner; done +} + +set -euo pipefail 2>/dev/null || : + + +# set source code directory +[ -f $PWD/${0##*/} ] && srcDir=$PWD || srcDir=${0%/*} +srcDir=${srcDir/#"${0##*/}"/"."} + + +# extract flashable zip if source code is unavailable +[ -f $srcDir/module.prop ] || { + srcDir=/dev/.${id}-install + rm -rf $srcDir 2>/dev/null || : + mkdir $srcDir + unzip "$3" -d $srcDir/ >&2 +} + + +name=$(get_prop name) +author=$(get_prop author) +version=$(get_prop version) +magiskModDir=/sbin/.magisk/modules +versionCode=$(get_prop versionCode) +: ${installDir:=/data/data/mattecarra.${id}app/files} ### +config=/data/adb/${id}-data/config.txt + +[ -d $magiskModDir ] && magisk=true || magisk=false + + +# check/set parent installation directory + +[ -d $installDir ] || installDir=$magiskModDir +[ -d $installDir ] || installDir=/data/adb + +[ -d $installDir ] || { + echo "(!) /data/adb/ not found\n" + exit 1 +} + + +### +echo "$name $version ($versionCode) +© 2017-2020, $author +GPLv3+ + +(i) Installing in $installDir/$id/..." + + +ash $srcDir/$id/uninstall.sh install +rm /data/adb/${id}-data/logs/bootlooped 2>/dev/null || : +cp -R $srcDir/$id/ $installDir/ +installDir=$(readlink -f $installDir/$id) +cp $srcDir/module.prop $installDir/ +mkdir -p ${config%/*}/info +cp -f $srcDir/*.md ${config%/*}/info + + +case $installDir in + /data/*/files/*$id) + ! $magisk || { + cp -R $installDir $magiskModDir/ + +# front-end post-uninstall cleanup script +echo "#!/system/bin/sh +# $id front-end post-uninstall cleanup script +(until [ -d /sdcard/?ndroid ]; do sleep 15; done +if [ ! -f $installDir/module.prop ]; then + rm -rf /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null +fi +exit 0 &) & +exit 0" > /data/adb/service.d/${id}-cleanup.sh + + chmod 0700 /data/adb/service.d/${id}-cleanup.sh + } + ln $installDir/service.sh $installDir/${id}-init.sh + + # TODO + # upgrade bundled version + #cp -f $srcDir/install-tarball.sh ${installDir%/*}/ + #tar -cvf - . -C $srcDir --exclude .git | gzip -9 > ${installDir%/*}/acc_bundle.tar.gz + ;; +esac + + +[ $installDir == /data/adb/$id ] || { + + ln -s $installDir /data/adb/ + + ! $magisk || { + # workaround for Magisk "forgetting service.sh" issue + ln $magiskModDir/$id/service.sh $magiskModDir/$id/post-fs-data.sh + + # disable magic mount (Magisk) + touch $magiskModDir/$id/skip_mount + } +} + + +# restore config backup +[ -f $config ] || cp /data/media/0/.${id}-config-backup.txt $config 2>/dev/null || : + + +# flashable uninstaller +cp -f $srcDir/bin/${id}-uninstaller.zip /data/media/0/ + + +# Termux, fix sha-bang +[[ $installDir != *com.termux* ]] && termux=false || { + termux=true + for f in $installDir/*.sh; do + ! grep -q '^#\!/.*/sh' $f \ + || sed -i 's|^#!/.*/sh|#!/data/data/com.termux/files/usr/bin/bash|' $f + done +} + + +# set perms +set_perms_recursive ${config%/*} +chmod 0666 /data/media/0/${id}-uninstaller.zip +case $installDir in + /data/*/files/*$id) + pkg=${installDir%/files/*$id} + pkg=${pkg##/data*/} + owner=$(grep $pkg /data/system/packages.list | cut -d ' ' -f 2) + set_perms_recursive $installDir $owner + ! $magisk || set_perms_recursive $magiskModDir/$id + + # Termux:Boot + ! $termux || { + mkdir -p ${installDir%/*}/.termux/boot + ln -sf $installDir/${id}-init.sh ${installDir%/*}/.termux/boot + set_perms_recursive ${installDir%/*}/.termux $owner + } + ;; + *) + set_perms_recursive $installDir + ;; +esac + + +set +euo pipefail 2>/dev/null || : + + +echo "- Done + + +" +# print links and changelog +sed -En "\|## LINKS|,\$p" ${config%/*}/info/README.md \ + | grep -v '^---' | sed 's/^## //' + + +### +echo " + + +(i) Rebooting is unnecessary. +- $id can be used right now. +- $id daemon started." + + +[ $installDir == /data/adb ] && echo -e "\n(i) Use init.d or an app to run $installDir/${id}-init.sh on boot to initialize ${id}." +echo + +# initialize $id +$installDir/service.sh --override + +exit 0 diff --git a/install-online.sh b/install-online.sh index 3ccadcf..8f92eaa 100644 --- a/install-online.sh +++ b/install-online.sh @@ -14,7 +14,7 @@ set +x echo id=acc -umask 077 +umask 0077 # log mkdir -p /data/adb/${id}-data/logs @@ -33,19 +33,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" @@ -108,12 +108,12 @@ then } # download and install tarball - export installDir0=$(echo "$@" | sed -E "s/-c|--changelog|-f|--force|-k|--insecure|-n|--non-interactive|%|$reference| //g") + export installDir=$(echo "$@" | sed -E "s/-c|--changelog|-f|--force|-k|--insecure|-n|--non-interactive|%|$reference| //g") set +euo pipefail 2>/dev/null || : trap - EXIT echo curl -L $insecure $tarball | tar -xz \ - && /system/bin/sh ${id}-${reference}/install.sh + && ash ${id}-${reference}/install.sh else echo diff --git a/install-tarball.sh b/install-tarball.sh index 1beab0f..8ef2b14 100644 --- a/install-tarball.sh +++ b/install-tarball.sh @@ -4,7 +4,7 @@ # License: GPLv3+ id=acc -umask 077 +umask 0077 # log mkdir -p /data/adb/${1:-$id}-data/logs @@ -20,19 +20,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" @@ -48,7 +48,7 @@ fi exit 4 } -umask 0 +umask 0000 set -e # get into the target directory @@ -73,12 +73,13 @@ copy_log() { trap copy_log EXIT # extract tarball -rm -rf ${1:-$id}_*/ 2>/dev/null -tar -xf ${1:-$id}_*gz +rm -rf ${1:-$id}*/ 2>/dev/null +tar -xf ${1:-$id}*gz # install ${1:-$id} -export installDir0="$2" -/system/bin/sh ${1:-$id}_*/install.sh -rm -rf ${1-$id}_*/ +test -f ${1:-$id}*/install.sh || i=-current +export installDir="$2" +ash ${1:-$id}*/install${i}.sh "$2" +rm -rf ${1-$id}*/ exit 0 diff --git a/install.sh b/install.sh index cefd409..374a9a6 100644 --- a/install.sh +++ b/install.sh @@ -12,12 +12,12 @@ SKIPUNZIP=1 echo id=acc -umask 077 +umask 0077 # log mkdir -p /data/adb/${id}-data/logs -chmod -R 700 /data/adb/${id}-data/logs +chmod -R 0700 /data/adb/${id}-data/logs exec 2>/data/adb/${id}-data/logs/install.log set -x @@ -26,9 +26,14 @@ exxit() { local e=$? set +eo pipefail rm -rf /dev/.${id}-install - [ $e -ne 0 ] && echo || { - rm /sbin/.$id/.ghost-charging ### - /sbin/acca --daemon > /dev/null || /sbin/accd ### workaround, Magisk 20.4+ + [ $e -ne 0 ] && echo || { ### + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd || { + pkill -9 -f $installDir/service.sh + $installDir/service.sh --override + rm /sbin/.$id/.ghost-charging + /sbin/acca --daemon > /dev/null || /sbin/accd + } || e=12 } exit $e } 2>/dev/null @@ -45,19 +50,19 @@ if [ -d /sbin/.magisk/busybox ]; then esac else mkdir -p /dev/.busybox - chmod 700 /dev/.busybox + chmod 0700 /dev/.busybox case $PATH in /dev/.busybox:*) :;; *) PATH=/dev/.busybox:$PATH;; esac [ -x /dev/.busybox/busybox ] || { if [ -f /data/adb/magisk/busybox ]; then - [ -x /data/adb/magisk/busybox ] || chmod 700 /data/adb/magisk/busybox + [ -x /data/adb/magisk/busybox ] || chmod 0700 /data/adb/magisk/busybox /data/adb/magisk/busybox --install -s /dev/.busybox elif which busybox > /dev/null; then busybox --install -s /dev/.busybox elif [ -f /data/adb/busybox ]; then - [ -x /data/adb/busybox ] || chmod 700 /data/adb/busybox + [ -x /data/adb/busybox ] || chmod 0700 /data/adb/busybox /data/adb/busybox --install -s /dev/.busybox else echo "(!) Install busybox or simply place it in /data/adb/" @@ -78,8 +83,8 @@ fi get_prop() { sed -n "s|^$1=||p" ${2:-$srcDir/module.prop}; } set_perms() { - local owner=${2:-0} perms=600 target=$(readlink -f $1) - if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=700; fi + local owner=${2:-0} perms=0600 target=$(readlink -f $1) + if echo $target | grep -q '.*\.sh$' || [ -d $target ]; then perms=0700; fi chmod $perms $target chown $owner:$owner $target restorecon $target > /dev/null 2>&1 || : @@ -110,15 +115,23 @@ srcDir=${srcDir/#"${0##*/}"/"."} name=$(get_prop name) author=$(get_prop author) version=$(get_prop version) +magiskModDir=/sbin/.magisk/modules versionCode=$(get_prop versionCode) -installDir=${1:-/data/data/mattecarra.${id}app/files} ### +: ${installDir:=/data/data/mattecarra.${id}app/files} ### config=/data/adb/${id}-data/config.txt +[ -d $magiskModDir ] && magisk=true || magisk=false + # check/set parent installation directory -[ -d $installDir ] || installDir=/sbin/.magisk/modules + +[ -d $installDir ] || installDir=$magiskModDir [ -d $installDir ] || installDir=/data/adb -[ -d $installDir ] || { echo "(!) /data/adb/ not found\n"; exit 1; } + +[ -d $installDir ] || { + echo "(!) /data/adb/ not found\n" + exit 1 +} ### @@ -129,7 +142,6 @@ GPLv3+ (i) Installing in $installDir/$id/..." -# install ash $srcDir/$id/uninstall.sh install rm /data/adb/${id}-data/logs/bootlooped 2>/dev/null || : cp -R $srcDir/$id/ $installDir/ @@ -137,51 +149,47 @@ installDir=$(readlink -f $installDir/$id) cp $srcDir/module.prop $installDir/ mkdir -p ${config%/*}/info cp -f $srcDir/*.md ${config%/*}/info -[ $installDir == /data/adb/$id ] || ln -s $installDir /data/adb/ - - -if [ $installDir != /sbin/.magisk/modules/$id ]; then - - mv $installDir/service.sh $installDir/${id}-init.sh - - # enable upgrading through Magisk Manager - ln -s $installDir /sbin/.magisk/modules/$id 2>/dev/null || : - [ ! -d /data/adb/service.d ] || { -# alternate initialization script -echo "#!/system/bin/sh -# alternate $id initializer -(until [ -d /storage/emulated/0/?ndroid ]; do sleep 10; done -if [ -f $installDir/${id}-init.sh ]; then - $installDir/${id}-init.sh -else - rm \$0 -fi -exit 0 &) & -exit 0" > /data/adb/service.d/${id}-init.sh +case $installDir in + /data/*/files/*$id) + ! $magisk || { + cp -R $installDir $magiskModDir/ -# post-uninstall cleanup script +# front-end post-uninstall cleanup script echo "#!/system/bin/sh -# $id post-uninstall cleanup -(until [ -d /storage/emulated/0/?ndroid ]; do sleep 15; done +# $id front-end post-uninstall cleanup script +(until [ -d /sdcard/?ndroid ]; do sleep 15; done if [ ! -f $installDir/module.prop ]; then - rm /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null + rm -rf /data/adb/$id /data/adb/${id}-data /data/adb/modules/$id \$0 2>/dev/null fi exit 0 &) & exit 0" > /data/adb/service.d/${id}-cleanup.sh - chmod 700 /data/adb/service.d/${id}-*.sh - } + chmod 0700 /data/adb/service.d/${id}-cleanup.sh + } + ln $installDir/service.sh $installDir/${id}-init.sh + + # TODO + # upgrade bundled version + #cp -f $srcDir/install-tarball.sh ${installDir%/*}/ + #tar -cvf - . -C $srcDir --exclude .git | gzip -9 > ${installDir%/*}/acc_bundle.tar.gz + ;; +esac -else - # workaround for Magisk "forgetting service.sh" issue - ln $installDir/service.sh $installDir/post-fs-data.sh -fi +[ $installDir == /data/adb/$id ] || { -# disable magic mount (Magisk) -touch /sbin/.magisk/modules/$id/skip_mount 2>/dev/null || : + ln -s $installDir /data/adb/ + + ! $magisk || { + # workaround for Magisk "forgetting service.sh" issue + ln $magiskModDir/$id/service.sh $magiskModDir/$id/post-fs-data.sh + + # disable magic mount (Magisk) + touch $magiskModDir/$id/skip_mount + } +} # restore config backup @@ -204,13 +212,14 @@ cp -f $srcDir/bin/${id}-uninstaller.zip /data/media/0/ # set perms set_perms_recursive ${config%/*} -chmod 666 /data/media/0/${id}-uninstaller.zip +chmod 0666 /data/media/0/${id}-uninstaller.zip case $installDir in /data/*/files/*$id) pkg=${installDir%/files/*$id} pkg=${pkg##/data*/} owner=$(grep $pkg /data/system/packages.list | cut -d ' ' -f 2) set_perms_recursive $installDir $owner + ! $magisk || set_perms_recursive $magiskModDir/$id # Termux:Boot ! $termux || { @@ -232,8 +241,6 @@ echo "- Done " - - # print links and changelog sed -En "\|## LINKS|,\$p" ${config%/*}/info/README.md \ | grep -v '^---' | sed 's/^## //' @@ -249,14 +256,9 @@ echo " [ $installDir == /data/adb ] && echo -e "\n(i) Use init.d or an app to run $installDir/${id}-init.sh on boot to initialize ${id}." - echo # initialize $id -if [ -f $installDir/service.sh ]; then - $installDir/service.sh --override -else - $installDir/${id}-init.sh --override -fi +$installDir/service.sh --override exit 0 diff --git a/module.prop b/module.prop index d9bae90..0b773c6 100644 --- a/module.prop +++ b/module.prop @@ -1,6 +1,6 @@ id=acc name=Advanced Charging Controller (ACC) -version=v2020.4.17-beta -versionCode=202004170 +version=v2020.4.20-beta +versionCode=202004200 author=VR25 (patreon.com/vr25) description=ACC is an Android software. It's primarily intended for extending battery service life. In a nutshell, this is achieved through limiting charging current, temperature and voltage. Any root solution is supported. A recent stable Magisk version is recommended. If you're reading this from Magisk Manager > Downloads, tap here to open the documentation. Once there, if you're lazy, jump to the quick start section. diff --git a/old-changelog.md b/old-changelog.md index 6e4af40..95d8d3c 100644 --- a/old-changelog.md +++ b/old-changelog.md @@ -1,3 +1,11 @@ +**v2020.4.8-beta (202004080)** +- acc -t: fixed "charging-switches: no such file" error +- accd: fixed crash on plug/unplug that affected some users +- Current control optimizations +- Enhanced battery calibration helper (acc -C) +- More intuitive versioning scheme +- Stricter config integrity checks (auto-reset broken config) + **2020.4.4-dev (202004040)** - acc -(e|d): do not do unnecessary work - acc -f: fixed capacity limit bypass