From 31c8a649f6870d3694363fed38499ca8423e3003 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:44:29 +0000 Subject: [PATCH] Carbox hardening: safe config, boot handling, parking mode, detection and permission fixes (v0.2.4) - Config in /data/adb/caros (parsed with a key whitelist, never sourced); /sdcard override - post-fs-data minimal; service waits for sys.boot_completed; profile re-applied periodically - Per-policy CPU limit, KEYCODE_SLEEP, Nova via HOME role, cmd power for battery saver - Parking mode, auto shutdown, optional lock screen removal, Doze exemption for AA/Maps/Waze - Fix notification listener handling, hotspot syntax, remove fake A2DP properties Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01RP6NCRZe65fMVJkBnkNwTN --- .github/ISSUE_TEMPLATE/bug_report.md | 4 +- .github/copilot-instructions.md | 10 +- .github/workflows/release.yml | 2 +- CHANGELOG.md | 26 ++ CONTRIBUTING.md | 12 +- EXAMPLES.md | 6 +- FAQ.md | 19 +- README.md | 41 ++- caros_config.sh | 66 ++++- grant_permissions.sh | 131 ++++----- module.prop | 6 +- post-fs-data.sh | 38 +-- service.sh | 401 +++++++++++++++------------ 13 files changed, 429 insertions(+), 333 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 393051a..cf08b30 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -29,7 +29,7 @@ assignees: '' - **Car Make/Model**: ## Configuration - + ```bash AUDI_BT_NAMES="Your Car" AUDI_BT_MAC="" @@ -39,7 +39,7 @@ ALLOW_BT_IN_WIRED=1 ## Logs - + ``` Paste logs here ``` diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index a0400f6..4b780fb 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -50,7 +50,7 @@ CarOS Profile Switcher is a Magisk module for Android that automatically manages #### Configuration Flow 1. Defaults defined in `caros_config.sh` (`DEFAULT_*` variables) -2. User config at `/sdcard/CarOS/config.env` (generated from defaults) +2. User config at `/data/adb/caros/config.env` (generated from defaults) 3. `apply_defaults()` ensures all variables have values #### Service Loop @@ -148,16 +148,16 @@ check_with_cache(){ ### Path Conventions - Module files: `/data/adb/modules/caros-switcher/` -- User config: `/sdcard/CarOS/config.env` -- Logs: `/data/adb/modules/caros-switcher/log.txt` -- State: `/data/adb/modules/caros-switcher/state.json` +- User config: `/data/adb/caros/config.env` +- Logs: `/data/adb/caros/log.txt` +- State: `/data/adb/caros/state.json` ## Testing & Validation ### Before Committing 1. Validate shell syntax: `bash -n script.sh` 2. Test on actual device (if possible) -3. Check logs for errors: `adb shell cat /data/adb/modules/caros-switcher/log.txt` +3. Check logs for errors: `adb shell cat /data/adb/caros/log.txt` 4. Verify version consistency across files ### Log Analysis diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 52e3fbf..1521719 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -56,7 +56,7 @@ jobs: 2. Magisk → Modules → Installer depuis le stockage 3. Choisir le ZIP puis redémarrer - Configuration : \`/sdcard/CarOS/config.env\` (voir [EXAMPLES.md](https://github.com/${REPO}/blob/main/EXAMPLES.md)). + Configuration : \`/data/adb/caros/config.env\` (voir [EXAMPLES.md](https://github.com/${REPO}/blob/main/EXAMPLES.md)). **Prérequis** : Magisk v20.4+, Android Auto, accès root. EOT diff --git a/CHANGELOG.md b/CHANGELOG.md index dca80ad..6cb0ef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,32 @@ All notable changes to CarOS Profile Switcher will be documented in this file. ## [Unreleased] +## [0.2.4] - 2026-11-14 + +### Added +- Carbox options: `ENABLE_WIRELESS_PROFILE` (0 for wired-only), `PARKING_MODE` (cut scans/location, force Doze while idle), `AUTO_SHUTDOWN_PCT` (shut down when idle, unplugged and low), `DISABLE_LOCKSCREEN`, `KEEP_BT_IN_IDLE` +- `grant_permissions.sh` exempts Android Auto, Maps and Waze from Doze/background limits +- The current profile is re-applied every 5 minutes, and the config is re-read live + +### Changed +- Config moved to `/data/adb/caros/config.env` (root only); `/sdcard/CarOS/config.env` is still read as an optional override. Config files are parsed (whitelisted keys, never sourced), so an app can no longer run commands as root through the config +- Logs and state moved to `/data/adb/caros/` +- `post-fs-data.sh` is minimal; everything runs in `service.sh` after `sys.boot_completed` +- Wired detection uses USB accessory mode; Bluetooth detection no longer matches "disconnected" +- Charge limit only reduces current (never suspends charging) and restores original values +- Bluetooth/Wi-Fi/data use `svc` and `cmd wifi`; battery saver uses `cmd power set-mode` +- CPU limit is per cpufreq policy; screen off uses KEYCODE_SLEEP; Nova set through the HOME role +- Polling every 10 s (instead of 3 s) while idle +- The hotspot password is generated randomly when the config is created + +### Fixed +- Idle profile ignored `DATA_OFF_OUTSIDE` +- Nova fallback overwrote the default keyboard +- Notification-listener list grew with invalid entries at every permission run +- Wi-Fi hotspot used an invalid security type +- Removed non-functional A2DP-sink system properties (feature is experimental and needs ROM support) + + ### Added - **🎵 Audio Streaming Feature**: Stream audio from another device to car speakers via two methods: - **Bluetooth A2DP Sink** (`ENABLE_BT_AUDIO_SINK`): Receive audio via Bluetooth diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 98c1e7e..fbc6e19 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -84,7 +84,7 @@ caros-profile-switcher/ 1. Build the module: `./build.ps1` or `./build.sh` 2. Install via Magisk Manager 3. Reboot your device -4. Check logs: `adb shell cat /data/adb/modules/caros-switcher/log.txt` +4. Check logs: `adb shell cat /data/adb/caros/log.txt` 5. Verify state changes work correctly ## 🔄 Release Process @@ -133,7 +133,7 @@ git push origin main --tags ## 🐛 Debugging Tips ### Enable Verbose Logging -In `/sdcard/CarOS/config.env`: +In `/data/adb/caros/config.env`: ```bash VERBOSE=1 ``` @@ -141,16 +141,16 @@ VERBOSE=1 ### View Logs ```bash # Full log -adb shell cat /data/adb/modules/caros-switcher/log.txt +adb shell cat /data/adb/caros/log.txt # Last 50 lines -adb shell tail -n 50 /data/adb/modules/caros-switcher/log.txt +adb shell tail -n 50 /data/adb/caros/log.txt # Follow in real-time -adb shell tail -f /data/adb/modules/caros-switcher/log.txt +adb shell tail -f /data/adb/caros/log.txt # Check current state -adb shell cat /data/adb/modules/caros-switcher/state.json +adb shell cat /data/adb/caros/state.json ``` ### Common Issues diff --git a/EXAMPLES.md b/EXAMPLES.md index a18ed7a..876a4ac 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -238,7 +238,7 @@ VERBOSE=1 # Full logging ### Method 2: Check Logs 1. Enable verbose logging: `VERBOSE=1` 2. Connect to your car -3. View logs: `adb shell cat /data/adb/modules/caros-switcher/log.txt` +3. View logs: `adb shell cat /data/adb/caros/log.txt` 4. Look for lines mentioning Bluetooth device names ### Method 3: Dumpsys (Advanced) @@ -286,10 +286,10 @@ AUDI_BT_NAMES="Car \\(2024\\)" ``` ### Testing Your Configuration -After editing `/sdcard/CarOS/config.env`: +After editing `/data/adb/caros/config.env`: 1. Don't need to reboot (config reloads automatically) 2. Connect to your car -3. Check logs: `adb shell tail -f /data/adb/modules/caros-switcher/log.txt` +3. Check logs: `adb shell tail -f /data/adb/caros/log.txt` 4. Should see "State change: IDLE -> WIRELESS" or "-> WIRED" ### Common Pitfalls diff --git a/FAQ.md b/FAQ.md index bd15104..7f73cc9 100644 --- a/FAQ.md +++ b/FAQ.md @@ -40,10 +40,10 @@ Check these: 2. Android Auto app is installed 3. You've paired your car via Bluetooth 4. Module is enabled in Magisk Manager -5. Check logs: `adb shell cat /data/adb/modules/caros-switcher/log.txt` +5. Check logs: `adb shell cat /data/adb/caros/log.txt` ### Where is the configuration file? -`/sdcard/CarOS/config.env` +`/data/adb/caros/config.env` The module creates this automatically on first boot. You can edit it with any text editor. @@ -188,7 +188,7 @@ Check: 4. Verify process name: `pidof com.google.android.projection.gearhead` ### Profiles not switching -1. Check logs: `adb shell cat /data/adb/modules/caros-switcher/log.txt` +1. Check logs: `adb shell cat /data/adb/caros/log.txt` 2. Verify Bluetooth name matches: `AUDI_BT_NAMES="Your Car Name"` 3. Try MAC address matching instead 4. Ensure Android Auto is actually running (not just connected) @@ -236,9 +236,10 @@ Yes, but changes will be lost on module updates. Better to: ### Where are the module files located? ``` /data/adb/modules/caros-switcher/ # Module directory -/data/adb/modules/caros-switcher/log.txt # Logs -/data/adb/modules/caros-switcher/state.json # Current state -/sdcard/CarOS/config.env # User configuration +/data/adb/caros/ # Config, logs, state +/data/adb/caros/log.txt # Logs +/data/adb/caros/state.json # Current state +/data/adb/caros/config.env # User configuration ``` ### How do I uninstall? @@ -247,7 +248,7 @@ Yes, but changes will be lost on module updates. Better to: 3. Tap **Remove** on CarOS Profile Switcher 4. Reboot -Configuration file at `/sdcard/CarOS/` will remain (delete manually if desired). +Configuration and logs in `/data/adb/caros/` (and an optional `/sdcard/CarOS/`) will remain (delete manually if desired). ### Can I pause the module temporarily? Yes: @@ -314,7 +315,7 @@ If issues occur, boot to recovery and disable/remove via Magisk. ### How do I report bugs? 1. Enable `VERBOSE=1` in config 2. Reproduce the issue -3. Collect logs: `adb shell cat /data/adb/modules/caros-switcher/log.txt` +3. Collect logs: `adb shell cat /data/adb/caros/log.txt` 4. Open a GitHub issue with logs and details ### Can I contribute? @@ -343,5 +344,5 @@ Shell script (Bash/sh) - standard for Magisk modules. **Remember:** Always include logs when asking for help! ```bash -adb shell cat /data/adb/modules/caros-switcher/log.txt +adb shell cat /data/adb/caros/log.txt ``` diff --git a/README.md b/README.md index f143634..4ab794d 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ A Magisk module for Android that automatically switches device profiles based on - **🎵 Audio Streaming**: Stream audio from another device to car speakers via Bluetooth A2DP sink or WiFi hotspot ### Configuration -- User-configurable via `/sdcard/CarOS/config.env` +- User-configurable via `/data/adb/caros/config.env` - Supports multiple car Bluetooth names (regex patterns) - MAC address matching support - Verbose logging for troubleshooting @@ -46,7 +46,15 @@ A Magisk module for Android that automatically switches device profiles based on ## ⚙️ Configuration -After installation, edit the configuration file at `/sdcard/CarOS/config.env`: +The config is created at first boot in `/data/adb/caros/config.env` (readable by root only). For convenience, `/sdcard/CarOS/config.env` is read afterwards as an optional override: create it by hand and put only the keys you want to change. Only known `KEY=VALUE` keys are read; nothing in these files is executed. Changes are picked up within a few minutes without rebooting. + +```powershell +# Edit from a PowerShell prompt with adb (rooted phone) +adb shell su -c "cat /data/adb/caros/config.env" +adb shell su -c "tail -n 50 /data/adb/caros/log.txt" +``` + +After installation, edit the configuration file at `/data/adb/caros/config.env`: > 💡 **Quick Start**: See [EXAMPLES.md](EXAMPLES.md) for ready-to-use configurations for Audi, VW, BMW, Mercedes, and more! @@ -86,7 +94,7 @@ ENABLE_WIFI_AUDIO_HOTSPOT=0 # WiFi hotspot for audio streaming (alternative to # WiFi hotspot settings (if using WiFi audio streaming) WIFI_AUDIO_HOTSPOT_SSID="CarOS-Audio" -WIFI_AUDIO_HOTSPOT_PASSWORD="caros123" +WIFI_AUDIO_HOTSPOT_PASSWORD="choose-a-password" # Verbose logging VERBOSE=1 @@ -122,7 +130,7 @@ When enabled (`ENABLE_WIFI_AUDIO_HOTSPOT=1`), your CarOS device creates a WiFi h ```bash ENABLE_WIFI_AUDIO_HOTSPOT=1 WIFI_AUDIO_HOTSPOT_SSID="CarOS-Audio" - WIFI_AUDIO_HOTSPOT_PASSWORD="caros123" + WIFI_AUDIO_HOTSPOT_PASSWORD="choose-a-password" ``` 2. Connect your CarOS phone to car via USB 3. On the emitting phone: @@ -241,12 +249,12 @@ echo "Release created: rel/CarOS_Profile_Switcher-v${VERSION}.zip" ### Check Logs View module logs: ```bash -adb shell cat /data/adb/modules/caros-switcher/log.txt +adb shell cat /data/adb/caros/log.txt ``` ### Check Current State ```bash -adb shell cat /data/adb/modules/caros-switcher/state.json +adb shell cat /data/adb/caros/state.json ``` ### Common Issues @@ -324,3 +332,24 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file --- **Note**: This module requires root access and modifies system behavior. Use at your own risk. Always test in a safe environment first. + + +## 🚙 Carbox setup (always-plugged old phone) + +Suggested `config.env` for a phone that stays in the car and only does wired Android Auto: + +```bash +ENABLE_WIRELESS_PROFILE=0 # wired only +KEEP_BT_IN_IDLE=0 # nothing needs Bluetooth when the car is off +DATA_OFF_OUTSIDE=1 +KEEP_WIFI_IN_IDLE=0 +PARKING_MODE=1 # no background scans/location, forced Doze while idle +AUTO_SHUTDOWN_PCT=10 # protect the battery if the car does not charge it +DISABLE_LOCKSCREEN=1 # only works with no PIN/pattern; security trade-off +IDLE_MAX_CPU_FREQ=1000000 # kHz, adapt to your SoC +``` + +Things the module cannot do from a shell script: +- **Power on when the car starts.** After a shutdown the phone must boot by itself when USB power returns. This depends on the device (some ROMs/bootloaders boot on charger insertion; others need `fastboot oem off-mode-charge 0` or similar). Test it on your phone. +- **Limit the battery to 80 %.** Use [ACC (Advanced Charging Controller)](https://github.com/VR-25/acc); this module deliberately never suspends charging. +- **Internet without a SIM.** Save the Wi-Fi of your main phone's hotspot and set `KEEP_WIFI_IN_WIRED=1`. diff --git a/caros_config.sh b/caros_config.sh index c19a272..7afc505 100644 --- a/caros_config.sh +++ b/caros_config.sh @@ -1,5 +1,5 @@ #!/system/bin/sh -# === CarOS Profile Switcher Centralized Configuration v0.2.3 === +# === CarOS Profile Switcher Centralized Configuration v0.2.4 === # This file contains all default configuration for the module # It is sourced by post-fs-data.sh and service.sh to ensure consistency @@ -54,16 +54,32 @@ DEFAULT_ENABLE_WIFI_AUDIO_HOTSPOT=0 # WiFi hotspot SSID for audio streaming DEFAULT_WIFI_AUDIO_HOTSPOT_SSID="CarOS-Audio" -# WiFi hotspot password for audio streaming (min 8 characters) -DEFAULT_WIFI_AUDIO_HOTSPOT_PASSWORD="caros123" +# WiFi hotspot password for audio streaming (min 8 characters). +# Empty = a random password is generated when config.env is first created +DEFAULT_WIFI_AUDIO_HOTSPOT_PASSWORD="" + +# Enable the WIRELESS profile (Bluetooth connected to the car + Android Auto running). +# Set to 0 for a wired-only "carbox" setup. +DEFAULT_ENABLE_WIRELESS_PROFILE=1 + +# Carbox / parking mode (applies in IDLE only; all best effort) +# 1 = cut background scans, location and force Doze while the car is off +DEFAULT_PARKING_MODE=0 +# Shut the phone down when idle, unplugged and battery is below this % (0 = disabled) +DEFAULT_AUTO_SHUTDOWN_PCT=0 +# Remove the lock screen at boot so Android Auto can start unattended (1 = enable). +# Only works if no PIN/pattern/password is set. Security trade-off: use with care. +DEFAULT_DISABLE_LOCKSCREEN=0 # Verbose logs DEFAULT_VERBOSE=1 # Function to generate user configuration content generate_user_config() { +# Random hotspot password so no weak default ships +WIFI_AUDIO_HOTSPOT_PASSWORD_GEN="$(tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c 12)" cat </dev/null || true # Check if permission management is enabled @@ -18,106 +20,75 @@ if [ "$AUTO_GRANT_PERMISSIONS" != "1" ]; then exit 0 fi -log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [PERMS] $@" >> "$LOGFILE"; } - -# List of apps that need notification permissions -NOTIFICATION_APPS=( - "com.google.android.projection.gearhead" # Android Auto - "com.google.android.apps.maps" # Google Maps - "com.waze" # Waze - "com.teslacoilsw.launcher" # Nova Launcher - "com.google.android.gms" # Google Play Services - "com.spotify.music" # Spotify (common in-car app) -) - -# List of apps that need location permissions -LOCATION_APPS=( - "com.google.android.apps.maps" # Google Maps - "com.waze" # Waze - "com.google.android.projection.gearhead" # Android Auto -) - -# Grant notification permission to an app +log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [PERMS] $*" >> "$LOGFILE"; } + +# Space-separated package lists (no bash arrays: this must run under Android's mksh) +# Android Auto, Google Maps, Waze, Nova Launcher, Google Play Services, Spotify +NOTIFICATION_APPS="com.google.android.projection.gearhead com.google.android.apps.maps com.waze com.teslacoilsw.launcher com.google.android.gms com.spotify.music" +LOCATION_APPS="com.google.android.apps.maps com.waze com.google.android.projection.gearhead" +# Never let Doze / background restrictions kill these when the phone is unattended +KEEP_ALIVE_APPS="com.google.android.projection.gearhead com.google.android.apps.maps com.waze" + +PACKAGES="$(pm list packages 2>/dev/null)" +is_installed() { echo "$PACKAGES" | grep -q "^package:$1$"; } + +# Grant notification access to an app grant_notification_permission() { - local package="$1" - - # Check if app is installed - if ! pm list packages | grep -q "^package:${package}$"; then - return 0 # Skip if not installed - fi - + package="$1" + is_installed "$package" || return 0 log "Granting notification access to $package" - - # Method 1: Direct notification listener access (Android Auto needs this) - cmd notification allow_listener "$package" 2>/dev/null - - # Method 2: POST_NOTIFICATIONS permission (Android 13+) + + # Runtime permission (Android 13+) pm grant "$package" android.permission.POST_NOTIFICATIONS 2>/dev/null - - # Method 3: Settings approach - settings put secure enabled_notification_listeners \ - "$(settings get secure enabled_notification_listeners 2>/dev/null):${package}" 2>/dev/null + + # Notification listener access needs full component names (package/Class), not a bare package + for comp in $(cmd package query-services --brief -a android.service.notification.NotificationListenerService 2>/dev/null | grep "^$package/"); do + cmd notification allow_listener "$comp" 2>/dev/null + done } # Grant location permissions to an app grant_location_permission() { - local package="$1" - - # Check if app is installed - if ! pm list packages | grep -q "^package:${package}$"; then - return 0 # Skip if not installed - fi - + package="$1" + is_installed "$package" || return 0 log "Granting location access to $package" - - # Grant all location-related permissions + pm grant "$package" android.permission.ACCESS_FINE_LOCATION 2>/dev/null pm grant "$package" android.permission.ACCESS_COARSE_LOCATION 2>/dev/null pm grant "$package" android.permission.ACCESS_BACKGROUND_LOCATION 2>/dev/null - - # Set app ops for location (background + foreground) + appops set "$package" COARSE_LOCATION allow 2>/dev/null appops set "$package" FINE_LOCATION allow 2>/dev/null } +# Exempt an app from battery optimisation / background restrictions +grant_keep_alive() { + package="$1" + is_installed "$package" || return 0 + log "Exempting $package from Doze / background limits" + cmd deviceidle whitelist "+$package" >/dev/null 2>&1 + cmd appops set "$package" RUN_ANY_IN_BACKGROUND allow 2>/dev/null + cmd appops set "$package" RUN_IN_BACKGROUND allow 2>/dev/null +} + # Grant Bluetooth permissions for audio sink functionality grant_bluetooth_audio_permissions() { - # Check if Bluetooth audio sink is enabled - if [ "$ENABLE_BT_AUDIO_SINK" != "1" ]; then - return 0 - fi - + [ "$ENABLE_BT_AUDIO_SINK" = "1" ] || return 0 log "Granting Bluetooth audio permissions for A2DP sink mode" - - # Grant system-level Bluetooth permissions (Android 12+) + pm grant com.android.bluetooth android.permission.BLUETOOTH_CONNECT 2>/dev/null pm grant com.android.bluetooth android.permission.BLUETOOTH_SCAN 2>/dev/null pm grant com.android.bluetooth android.permission.BLUETOOTH_ADVERTISE 2>/dev/null - - # Grant audio permissions - pm grant com.android.bluetooth android.permission.MODIFY_AUDIO_SETTINGS 2>/dev/null pm grant com.android.bluetooth android.permission.RECORD_AUDIO 2>/dev/null - - # Set app ops for Bluetooth + appops set com.android.bluetooth BLUETOOTH_SCAN allow 2>/dev/null appops set com.android.bluetooth BLUETOOTH_CONNECT allow 2>/dev/null appops set com.android.bluetooth BLUETOOTH_ADVERTISE allow 2>/dev/null } -# Main execution log "Starting permission grants..." - -# Grant notification permissions -for app in "${NOTIFICATION_APPS[@]}"; do - grant_notification_permission "$app" -done - -# Grant location permissions -for app in "${LOCATION_APPS[@]}"; do - grant_location_permission "$app" -done - -# Grant Bluetooth audio permissions +for app in $NOTIFICATION_APPS; do grant_notification_permission "$app"; done +for app in $LOCATION_APPS; do grant_location_permission "$app"; done +for app in $KEEP_ALIVE_APPS; do grant_keep_alive "$app"; done grant_bluetooth_audio_permissions - log "Permission grants completed" diff --git a/module.prop b/module.prop index a28ac94..a95b2f2 100644 --- a/module.prop +++ b/module.prop @@ -1,6 +1,6 @@ id=caros-switcher name=CarOS Profile Switcher (Wired & Wireless AA) -version=0.2.3 -versionCode=4 +version=0.2.4 +versionCode=5 author=Vincent+ChatGPT -description=Auto WIRED/WIRELESS switching + Smart WiFi + Nova default + fast charge limit. Config: /sdcard/CarOS/config.env. v0.2.3: Smart WiFi + VW Polo 6 compatible + Auto permissions +description=Auto WIRED/WIRELESS switching + Smart WiFi + Nova default + charge limit + carbox parking mode. Config: /data/adb/caros/config.env. v0.2.4: reliable detection, safe charge limit, safer config diff --git a/post-fs-data.sh b/post-fs-data.sh index f170a3a..648cd15 100644 --- a/post-fs-data.sh +++ b/post-fs-data.sh @@ -1,37 +1,7 @@ #!/system/bin/sh +# post-fs-data runs early and blocks boot: keep it minimal. +# /sdcard, pm, settings and cmd are NOT available yet. Everything else +# (config creation, permissions, profiles) is done by service.sh after boot. MODDIR=${0%/*} -LOGDIR=/data/adb/modules/caros-switcher -CONFDIR=/sdcard/CarOS -# Source the centralized configuration -. "$MODDIR/caros_config.sh" - -# Wait for /sdcard to be mounted (for LineageOS) -for i in $(seq 1 30); do - if [ -d "/sdcard" ] && [ -w "/sdcard" ]; then - break - fi - sleep 1 -done - -mkdir -p "$LOGDIR" -mkdir -p "$CONFDIR" 2>/dev/null - -CONF="$CONFDIR/config.env" -if [ ! -f "$CONF" ]; then - # Check that /sdcard is accessible - if [ -w "/sdcard" ]; then - generate_user_config > "$CONF" - chmod 0644 "$CONF" - else - # If /sdcard is not accessible, let service.sh handle it - echo "$(date) [post-fs-data] /sdcard not writable, config will be created by service.sh" >> "$LOGDIR/log.txt" - fi -fi - -chmod 0755 "$MODDIR/service.sh" -chmod 0755 "$MODDIR/post-fs-data.sh" -chmod 0755 "$MODDIR/grant_permissions.sh" - -# Grant permissions to critical apps (async to not delay boot) -"$MODDIR/grant_permissions.sh" & +chmod 0755 "$MODDIR/service.sh" "$MODDIR/post-fs-data.sh" "$MODDIR/grant_permissions.sh" diff --git a/service.sh b/service.sh index f678ec1..ae289f6 100644 --- a/service.sh +++ b/service.sh @@ -1,35 +1,50 @@ #!/system/bin/sh MODDIR=${0%/*} -LOGFILE=/data/adb/modules/caros-switcher/log.txt -CONFFILE=/sdcard/CarOS/config.env -STATEFILE=/data/adb/modules/caros-switcher/state.json - -# Source the centralized configuration +# State lives outside the module folder so it survives module updates +DATADIR=/data/adb/caros +LOGFILE=$DATADIR/log.txt +CONFFILE=$DATADIR/config.env +# Optional user override, easy to edit from Android (only whitelisted keys are read) +USERCONF=/sdcard/CarOS/config.env +STATEFILE=$DATADIR/state.json +mkdir -p "$DATADIR" 2>/dev/null + +# Source the centralized configuration (defaults + safe config loader) . "$MODDIR/caros_config.sh" # Rotate logs if > 1MB -if [ -f "$LOGFILE" ] && [ "$(stat -c%s "$LOGFILE" 2>/dev/null || echo 0)" -gt 1048576 ]; then - mv "$LOGFILE" "${LOGFILE}.old" 2>/dev/null -fi +rotate_log(){ + if [ -f "$LOGFILE" ] && [ "$(stat -c%s "$LOGFILE" 2>/dev/null || echo 0)" -gt 1048576 ]; then + mv "$LOGFILE" "${LOGFILE}.old" 2>/dev/null + fi +} +rotate_log -log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $@" >> "$LOGFILE"; } -error_log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $@" >> "$LOGFILE"; } +log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $*" >> "$LOGFILE"; } +error_log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $*" >> "$LOGFILE"; } + +# Wait for the system to be fully booted: pm/cmd/settings/svc are not usable before +while [ "$(getprop sys.boot_completed)" != "1" ]; do sleep 2; done # Create configuration if it doesn't exist if [ ! -f "$CONFFILE" ]; then - mkdir -p "$(dirname "$CONFFILE")" log "Creating config file: $CONFFILE" generate_user_config > "$CONFFILE" - chmod 644 "$CONFFILE" - log "Config file created successfully" + chmod 600 "$CONFFILE" fi -[ -f "$CONFFILE" ] && . "$CONFFILE" +read_config(){ + load_config "$CONFFILE" + load_config "$USERCONF" + apply_defaults +} -# Apply default values -apply_defaults +# /sdcard may be mounted slightly after boot_completed (FUSE / file-based encryption) +n=0 +while [ ! -d /sdcard/Android ] && [ "$n" -lt 30 ]; do sleep 2; n=$((n+1)); done +read_config -vlog(){ [ "$VERBOSE" = "1" ] && log "[DBG]" "$@"; } +vlog(){ [ "$VERBOSE" = "1" ] && log "[DBG]" "$@"; return 0; } wifi_on(){ cmd wifi set-wifi-enabled enabled >/dev/null 2>&1 || svc wifi enable >/dev/null 2>&1 || error_log "Failed to enable WiFi" @@ -96,37 +111,53 @@ bt_off(){ svc bluetooth disable >/dev/null 2>&1 || cmd bluetooth_manager disable >/dev/null 2>&1 || error_log "Failed to disable Bluetooth" } -battery_saver_on(){ settings put global low_power 1 || error_log "Failed to enable battery saver"; } -battery_saver_off(){ settings put global low_power 0 || error_log "Failed to disable battery saver"; } +battery_saver_on(){ cmd power set-mode 1 >/dev/null 2>&1 || settings put global low_power 1 >/dev/null 2>&1 || error_log "Failed to enable battery saver"; } +battery_saver_off(){ cmd power set-mode 0 >/dev/null 2>&1 || settings put global low_power 0 >/dev/null 2>&1 || error_log "Failed to disable battery saver"; } +# Cluster-aware: each cpufreq policy (little/big cores) has its own frequency table +# (the cpuN/cpufreq entries are symlinks to these policy directories, so the glob +# below visits every policy at least once; writing twice is harmless). set_cpu_max(){ MAX="$1" - if [ -n "$MAX" ]; then - SUCCESS=0 - for cpu in /sys/devices/system/cpu/cpu[0-9]*; do - if [ -e "$cpu/cpufreq/scaling_max_freq" ] && [ -w "$cpu/cpufreq/scaling_max_freq" ]; then - echo "$MAX" > "$cpu/cpufreq/scaling_max_freq" 2>/dev/null && SUCCESS=1 - fi - done - [ "$SUCCESS" = "0" ] && error_log "Failed to set CPU max frequency to $MAX" - fi + [ -n "$MAX" ] || return 0 + SUCCESS=0 + for pol in /sys/devices/system/cpu/cpufreq/policy* /sys/devices/system/cpu/cpu[0-9]*/cpufreq; do + [ -w "$pol/scaling_max_freq" ] || continue + target="$MAX" + read -r cmax < "$pol/cpuinfo_max_freq" 2>/dev/null + [ -n "$cmax" ] && [ "$target" -gt "$cmax" ] && target="$cmax" + # Highest supported frequency not above the target + if [ -r "$pol/scaling_available_frequencies" ]; then + read -r freqs < "$pol/scaling_available_frequencies" + best="" + for f in $freqs; do + if [ "$f" -le "$target" ] && { [ -z "$best" ] || [ "$f" -gt "$best" ]; }; then best="$f"; fi + done + [ -n "$best" ] && target="$best" + fi + echo "$target" > "$pol/scaling_max_freq" 2>/dev/null && SUCCESS=1 + done + [ "$SUCCESS" = "0" ] && error_log "Failed to set CPU max frequency to $MAX" + return 0 } reset_cpu_max(){ SUCCESS=0 - for cpu in /sys/devices/system/cpu/cpu[0-9]*; do - if [ -e "$cpu/cpufreq/scaling_max_freq" ] && [ -e "$cpu/cpufreq/cpuinfo_max_freq" ] && [ -w "$cpu/cpufreq/scaling_max_freq" ]; then - cat "$cpu/cpufreq/cpuinfo_max_freq" > "$cpu/cpufreq/scaling_max_freq" 2>/dev/null && SUCCESS=1 - fi + for pol in /sys/devices/system/cpu/cpufreq/policy* /sys/devices/system/cpu/cpu[0-9]*/cpufreq; do + [ -w "$pol/scaling_max_freq" ] && [ -r "$pol/cpuinfo_max_freq" ] || continue + read -r cmax < "$pol/cpuinfo_max_freq" + echo "$cmax" > "$pol/scaling_max_freq" 2>/dev/null && SUCCESS=1 done - [ "$SUCCESS" = "0" ] && error_log "Failed to reset CPU max frequency" + [ "$SUCCESS" = "0" ] && vlog "No writable CPU frequency control found" + return 0 } -screen_off(){ input keyevent 26 >/dev/null 2>&1; } +# KEYCODE_SLEEP (223) never wakes the screen, unlike the POWER toggle (26) +screen_off(){ input keyevent 223 >/dev/null 2>&1; } screen_on(){ input keyevent 224 >/dev/null 2>&1 || input keyevent 82 >/dev/null 2>&1; } # Limit charging current only (never suspend/disable charging: the phone must # keep charging in the car). Original values are saved to restore them later. -CHARGE_SAVE_DIR=/data/adb/modules/caros-switcher/charge_orig +CHARGE_SAVE_DIR=$DATADIR/charge_orig CHARGE_NODES="/sys/class/power_supply/usb/input_current_limit /sys/class/power_supply/battery/constant_charge_current_max" charge_limit_on(){ @@ -168,122 +199,78 @@ charge_limit_off(){ set_nova_default(){ [ "$SET_NOVA_DEFAULT" = "1" ] || return 0 - - # Vérifier si Nova Launcher est installé - if ! pm list packages | grep -q "com.teslacoilsw.launcher"; then + NOVA=com.teslacoilsw.launcher + + if ! pm list packages 2>/dev/null | grep -q "^package:$NOVA$"; then vlog "Nova Launcher not installed, skipping default launcher setup" return 1 fi - - # Définir Nova Launcher comme launcher par défaut - if cmd package set-home-activity com.teslacoilsw.launcher/com.android.launcher3.Launcher >/dev/null 2>&1; then - vlog "Nova Launcher set as default launcher" - return 0 - elif pm set-home-activity com.teslacoilsw.launcher/com.android.launcher3.Launcher >/dev/null 2>&1; then - vlog "Nova Launcher set as default launcher (fallback method)" - return 0 - else - # Méthode alternative avec settings - settings put secure default_input_method com.teslacoilsw.launcher >/dev/null 2>&1 - vlog "Nova Launcher setup attempted via settings" + + # Android 10+: the HOME role is the supported way + if cmd role add-role-holder --user 0 android.app.role.HOME "$NOVA" >/dev/null 2>&1; then + vlog "Nova Launcher set as HOME role holder" return 0 fi + + # Older: resolve the real HOME activity of the package instead of guessing its class + ACT="$(cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME -p "$NOVA" 2>/dev/null | tail -n 1)" + case "$ACT" in + "$NOVA"/*) + if cmd package set-home-activity "$ACT" >/dev/null 2>&1 || pm set-home-activity "$ACT" >/dev/null 2>&1; then + vlog "Nova Launcher set as default launcher ($ACT)" + return 0 + fi + ;; + esac + error_log "Could not set Nova Launcher as default launcher" + return 1 } -# Enable Bluetooth audio sink mode (A2DP sink) +# Bluetooth audio sink (EXPERIMENTAL): there is no shell command to switch the +# Bluetooth stack into A2DP sink mode. This only makes the phone discoverable; it +# works only on ROMs that already ship an A2DP sink profile. enable_bt_audio_sink(){ [ "$ENABLE_BT_AUDIO_SINK" = "1" ] || return 0 - - vlog "Enabling Bluetooth audio sink mode" - - # Ensure Bluetooth is on bt_on sleep 1 - - # Make device discoverable for pairing - # Timeout 0 means stay discoverable indefinitely while in car - settings put global bluetooth_discoverable_timeout 0 2>/dev/null - - # Enable Bluetooth discoverability via dumpsys - cmd bluetooth_manager enable 2>/dev/null - cmd bluetooth_manager set-discoverability on 2>/dev/null || { - # Fallback method - settings put global bluetooth_on 1 2>/dev/null - } - - # Try to enable A2DP sink mode via system properties - setprop persist.bluetooth.a2dp_sink.enabled true 2>/dev/null - setprop bluetooth.a2dp.sink.enabled true 2>/dev/null - - # Try to set Bluetooth class to audio sink - setprop bluetooth.device.class 0x240404 2>/dev/null - - # Enable audio routing to main speaker for Bluetooth audio - # This ensures incoming Bluetooth audio plays through system audio - settings put system bluetooth_audio_output 0 2>/dev/null - - vlog "Bluetooth audio sink mode enabled - device should be discoverable" - log "BT Audio Sink: Device is now discoverable for audio streaming" + # 0 = stay discoverable while in the car + settings put global bluetooth_discoverable_timeout 0 >/dev/null 2>&1 + log "BT Audio Sink (experimental): discoverable timeout disabled, requires ROM A2DP sink support" } -# Disable Bluetooth audio sink mode disable_bt_audio_sink(){ [ "$ENABLE_BT_AUDIO_SINK" = "1" ] || return 0 - - vlog "Disabling Bluetooth audio sink mode" - - # Disable discoverability to save battery when not in car - settings put global bluetooth_discoverable_timeout 120 2>/dev/null - cmd bluetooth_manager set-discoverability off 2>/dev/null - - # Disable A2DP sink mode - setprop persist.bluetooth.a2dp_sink.enabled false 2>/dev/null - setprop bluetooth.a2dp.sink.enabled false 2>/dev/null - - # Reset Bluetooth class to default (phone) - setprop bluetooth.device.class 0x5A020C 2>/dev/null - + settings put global bluetooth_discoverable_timeout 120 >/dev/null 2>&1 vlog "Bluetooth audio sink mode disabled" } # Enable WiFi hotspot for audio streaming enable_wifi_audio_hotspot(){ [ "$ENABLE_WIFI_AUDIO_HOTSPOT" = "1" ] || return 0 - + + if [ "${#WIFI_AUDIO_HOTSPOT_PASSWORD}" -lt 8 ]; then + error_log "WiFi hotspot password must be at least 8 characters, hotspot not started" + return 1 + fi + vlog "Enabling WiFi hotspot for audio streaming" - - # Ensure WiFi is on wifi_on sleep 1 - - # Stop any existing hotspot - cmd wifi stop-softap 2>/dev/null + cmd wifi stop-softap >/dev/null 2>&1 sleep 1 - - # Start WiFi hotspot with configured SSID and password - # Method 1: Using cmd wifi (Android 11+) - cmd wifi start-softap "$WIFI_AUDIO_HOTSPOT_SSID" wpa2-psk "$WIFI_AUDIO_HOTSPOT_PASSWORD" 2>/dev/null || { - # Method 2: Using settings (fallback) - settings put global wifi_ap_ssid "$WIFI_AUDIO_HOTSPOT_SSID" 2>/dev/null - settings put global wifi_ap_passwd "$WIFI_AUDIO_HOTSPOT_PASSWORD" 2>/dev/null - svc wifi enable 2>/dev/null - } - - vlog "WiFi hotspot enabled - SSID: $WIFI_AUDIO_HOTSPOT_SSID" - log "WiFi Audio Hotspot: Enabled for audio streaming (SSID: $WIFI_AUDIO_HOTSPOT_SSID)" + + # Android 11+: cmd wifi start-softap + if cmd wifi start-softap "$WIFI_AUDIO_HOTSPOT_SSID" wpa2 "$WIFI_AUDIO_HOTSPOT_PASSWORD" >/dev/null 2>&1; then + log "WiFi Audio Hotspot: enabled (SSID: $WIFI_AUDIO_HOTSPOT_SSID)" + else + error_log "WiFi hotspot could not be started (cmd wifi start-softap failed)" + return 1 + fi } -# Disable WiFi hotspot for audio streaming disable_wifi_audio_hotspot(){ [ "$ENABLE_WIFI_AUDIO_HOTSPOT" = "1" ] || return 0 - - vlog "Disabling WiFi hotspot for audio streaming" - - # Stop WiFi hotspot - cmd wifi stop-softap 2>/dev/null || { - settings put global wifi_ap_ssid "" 2>/dev/null - } - + cmd wifi stop-softap >/dev/null 2>&1 vlog "WiFi hotspot disabled" } @@ -303,18 +290,14 @@ aa_process_active(){ if [ -n "$AA_CACHE" ] && [ $(($(date +%s) - AA_CACHE_TIME)) -lt 2 ]; then [ "$AA_CACHE" = "1" ] && return 0 || return 1 fi - + AA_CACHE_TIME=$(date +%s) + # Only the process counts: "dumpsys activity services" also matches idle background services if pidof com.google.android.projection.gearhead >/dev/null 2>&1; then AA_CACHE=1 return 0 fi - - if dumpsys activity services 2>/dev/null | grep -qi com.google.android.projection.gearhead; then - AA_CACHE=1 - return 0 - fi - + AA_CACHE=0 return 1 } @@ -351,44 +334,84 @@ bt_connected_to_audi(){ return 1 } -apply_wired_profile(){ - if [ "$KEEP_DATA_IN_CAR" = "1" ]; then +apply_data(){ + if [ "$KEEP_DATA_IN_CAR" = "1" ]; then data_on sleep 2 # Give data time to connect check_data_connection - else + else data_off - fi + fi +} + +# --- Carbox / parking mode helpers (best effort) --------------------------------- +parking_on(){ + [ "$PARKING_MODE" = "1" ] || return 0 + settings put global wifi_scan_always_enabled 0 >/dev/null 2>&1 + settings put global ble_scan_always_enabled 0 >/dev/null 2>&1 + cmd location set-location-enabled false >/dev/null 2>&1 || settings put secure location_mode 0 >/dev/null 2>&1 + # Force Doze (only effective while the screen is off and the phone is not charging) + cmd deviceidle force-idle >/dev/null 2>&1 + vlog "Parking mode on" +} +parking_off(){ + [ "$PARKING_MODE" = "1" ] || return 0 + cmd deviceidle unforce >/dev/null 2>&1 + cmd location set-location-enabled true >/dev/null 2>&1 || settings put secure location_mode 3 >/dev/null 2>&1 + vlog "Parking mode off" +} + +# Shut the phone down when idle, unplugged and low, to protect an unattended battery +check_auto_shutdown(){ + [ "$AUTO_SHUTDOWN_PCT" -gt 0 ] || return 0 + BAT=/sys/class/power_supply/battery + [ -r "$BAT/capacity" ] || return 0 + read -r LEVEL < "$BAT/capacity" + read -r CHG < "$BAT/status" + case "$CHG" in + Charging|Full) return 0 ;; + esac + case "$LEVEL" in ''|*[!0-9]*) return 0 ;; esac + if [ "$LEVEL" -lt "$AUTO_SHUTDOWN_PCT" ]; then + log "Battery at ${LEVEL}% (< ${AUTO_SHUTDOWN_PCT}%) and not charging: shutting down" + reboot -p >/dev/null 2>&1 || svc power shutdown >/dev/null 2>&1 + fi +} + +disable_lockscreen(){ + [ "$DISABLE_LOCKSCREEN" = "1" ] || return 0 + if locksettings set-disabled true >/dev/null 2>&1; then + log "Lock screen disabled" + else + error_log "Could not disable the lock screen (a PIN/pattern/password is probably set)" + fi +} + +# --- Profiles --------------------------------------------------------------------- +apply_wired_profile(){ + apply_data if [ "$ALLOW_BT_IN_WIRED" = "1" ]; then bt_on; else bt_off; fi if [ "$KEEP_WIFI_IN_WIRED" = "1" ]; then wifi_on; else wifi_off; fi battery_saver_off + parking_off reset_cpu_max - [ "$LIMIT_QUICK_CHARGE_WIRED" = "1" ] && charge_limit_on + if [ "$LIMIT_QUICK_CHARGE_WIRED" = "1" ]; then charge_limit_on; else charge_limit_off; fi set_nova_default # Enable audio streaming (Bluetooth or WiFi hotspot based on config) enable_bt_audio_sink enable_wifi_audio_hotspot - screen_off } apply_wireless_profile(){ bt_on wifi_on - if [ "$KEEP_DATA_IN_CAR" = "1" ]; then - data_on - sleep 2 # Give data time to connect - check_data_connection - else - data_off - fi + apply_data battery_saver_off + parking_off reset_cpu_max charge_limit_off set_nova_default - # Note: BT audio sink NOT enabled in wireless mode because Bluetooth is already - # being used to connect to the car. WiFi may also be used by wireless AA. - # Audio streaming features disabled in wireless mode. - screen_off + # BT audio sink is not enabled in wireless mode: Bluetooth (and WiFi) are used by the car link. } apply_idle_profile(){ @@ -405,6 +428,15 @@ apply_idle_profile(){ charge_limit_off disable_bt_audio_sink disable_wifi_audio_hotspot + parking_on +} + +apply_profile(){ + case "$1" in + WIRED) apply_wired_profile ;; + WIRELESS) apply_wireless_profile ;; + IDLE) apply_idle_profile ;; + esac } STATE="" @@ -413,61 +445,72 @@ AA_CACHE="" AA_CACHE_TIME=0 BT_CACHE="" BT_CACHE_TIME=0 -log "CarOS Profile Switcher service v0.2.3 started" +LAST_APPLY=0 +LAST_SHUTDOWN_CHECK=0 +REAPPLY_EVERY=300 # seconds: re-apply the current profile in case something changed radios behind our back +log "CarOS Profile Switcher service v0.2.4 started" + +if [ "$ENABLE_WIRELESS_PROFILE" = "1" ] && [ "$KEEP_WIFI_IN_IDLE" != "1" ]; then + log "[WARN] Wireless Android Auto needs WiFi before the phone connects: set KEEP_WIFI_IN_IDLE=1, or ENABLE_WIRELESS_PROFILE=0 for a wired-only setup" +fi -# Grant permissions on service start (async) +disable_lockscreen + +# Grant permissions once per boot (async) "$MODDIR/grant_permissions.sh" & while true; do LOOP_COUNT=$((LOOP_COUNT + 1)) - - # Periodic log to prove the service is working - [ $((LOOP_COUNT % 100)) -eq 0 ] && log "Service alive, loop #$LOOP_COUNT" - - # Re-grant permissions periodically (every ~30 minutes = 600 loops) - if [ $((LOOP_COUNT % 600)) -eq 0 ]; then - "$MODDIR/grant_permissions.sh" & + NOW=$(date +%s) + + # Periodic log to prove the service is working; also re-read the config so edits apply live + if [ $((LOOP_COUNT % 100)) -eq 0 ]; then + log "Service alive, loop #$LOOP_COUNT, state $STATE" + rotate_log + read_config fi - + WIRED=0 WIRELESS=0 - - # Check with error handling + if usb_connected && aa_process_active; then WIRED=1 vlog "Detected WIRED mode" - elif bt_connected_to_audi && aa_process_active; then + elif [ "$ENABLE_WIRELESS_PROFILE" = "1" ] && bt_connected_to_audi && aa_process_active; then WIRELESS=1 vlog "Detected WIRELESS mode" fi - + NEWSTATE="IDLE" if [ "$WIRED" = "1" ]; then NEWSTATE="WIRED" - elif [ "$WIRELESS" = "1" ]; then + elif [ "$WIRELESS" = "1" ]; then NEWSTATE="WIRELESS" fi - + if [ "$NEWSTATE" != "$STATE" ]; then log "State change: $STATE -> $NEWSTATE" - case "$NEWSTATE" in - WIRED) - apply_wired_profile - log "Applied WIRED profile" - ;; - WIRELESS) - apply_wireless_profile - log "Applied WIRELESS profile" - ;; - IDLE) - apply_idle_profile - log "Applied IDLE profile" - ;; - esac + apply_profile "$NEWSTATE" + # Screen off only on a real transition (not on the periodic re-apply) + [ "$NEWSTATE" = "IDLE" ] || screen_off + log "Applied $NEWSTATE profile" STATE="$NEWSTATE" - # Save state + LAST_APPLY=$(date +%s) echo "{\"state\":\"$STATE\",\"timestamp\":\"$(date)\"}" > "$STATEFILE" 2>/dev/null + elif [ $((NOW - LAST_APPLY)) -ge "$REAPPLY_EVERY" ]; then + vlog "Re-applying $STATE profile" + apply_profile "$STATE" + LAST_APPLY=$(date +%s) + fi + + if [ "$STATE" = "IDLE" ]; then + # Poll less often (and run fewer dumpsys) while the car is off + if [ $((NOW - LAST_SHUTDOWN_CHECK)) -ge 60 ]; then + check_auto_shutdown + LAST_SHUTDOWN_CHECK=$NOW + fi + sleep 10 + else + sleep 3 fi - - sleep 3 done