-- Leo's gemini proxy

-- Connecting to yaky.dev:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;lang=en-US

Android: boot on plug, launch app after boot, shutdown on condition

Part of the Volty MIR project, but can be used for non-interactive applications.


Volty MIR - [M]ap + [I]intelligent [R]everse Camera

To display maps, I need a device that has:

GNSS/GPS receiver

Offline maps

Video output

My trusty and crusty old Samsung Galaxy S5 has all that functionality, even video output to HDMI via MHL. I found the MHL cable at a thrift store for a dollar or so.

MHL (Mobile High Definition Link)


What I want the device to do is:

Boot as soon as I start the car (i.e. device is connected to power).

Launch a navigation app after booting.

Shut down if the car is off for some time (i.e. device is not connected to power but has some battery left)


For the map/navigation application, I chose Organic Maps, as it is lightweight, has a great interface, and works offline. I have used it for many years now.

Organic Maps

Other options are:

OsmAnd - more customizable, the OG of OpenStreetMap navigation

Osmin - similar to Organic Maps, and works great on Linux (PinePhone etc.)


Boot on plug


Through fastboot (recommended method)

This does not work on Samsung devices!

Boot to fastboot mode (aka download mode)

Install fastboot on main machine and run

fastboot oem off-mode-charge 0

This worked for Google Nexus 7, and probably works for most Google devices.

Be careful with this - if battery gets too low, device will be stuck in an annoying constantly crashing reboot loop until it is sufficiently charged. See below for automatic shutdown on condition.


Magisk module

This is what I ended up doing.

Rooted device with Magisk, and then downloaded and installed Magisk Autoboot module.

Magisk Autoboot

Although their documentation states it works for Android 7, I could not get it to work with Lineage 14 (Android 7 equivalent) for some reason. On Lineage 15, it works and my S5 takes about a minute to boot after connecting power.


playplm / plm / lpm (not tested)

This is a somewhat strange way to boot some Samsung devices: It relies on replacing the binary that displays the battery charge animation with a script that (re)boots the device instead. Requires root.

Go to /system/bin and find a file named "playplm", "plm", or "lpm". Then edit it to contain only:

#!/system/bin/sh
/system/bin/reboot

I did not find any of these files on my S5, so I don't know if this works.


Launch app after boot


Termux with Termux:Boot plugin

Termux is a terminal emulator for Android, which can run with and without root.

I installed Termux and Termux:Boot via F-Droid, and ran Termux:Boot once to initialize it.

Termux on F-Droid

Termux:Boot on F-Droi

Some references:

Getting started with Termux

Termux:Boot plugin

Per instructions, I created a startup script

mkdir -p ~/.termux/boot
nano ~/.termux/boot/start-maps.sh
#!/data/data/com.termux/files/usr/bin/sh
am start app.organicmaps/.SplashActivity

Side note: how to find app name and activity

Easier way is to install and open Activity Launcher, find the app and its name, then find a list of activities it has, and find the main activity. Then use those names in the script above.

Activity Launcher on F-Droid

In my case:

App name is app.organicmaps

Activity name is .SplashActivity


Another possibility is to get this information through ADB. Allow ADB access and connect mobile device to main machine. Run these commands on main machine.

Get a list of applications via ADB:

adb shell pm list packages -f -3

Get a list of activities via ADB:

PACKAGE=package.name
adb shell dumpsys package | grep -Eo $(printf "^[[:space:]]+[0-9a-f]+[[:space:]]+%s/[^[:space:]]+" "${PACKAGE}") | grep -oE "[^[:space:]]+$"

Tasker

I have not tried this. Might be easier if you don't like the terminal.

Create a Profile Event / System / Device Boot

Create a Task with Action: App / Launch App / (select the app)

Tasker also recommends adding Task - Wait for around 5 seconds first before doing other tasks to ensure that the system is ready after booting.

Link the Profile to the Task

Source on StackExchange


Easer

Easer is an open-source automation tool like Tasker. While it can be used for some scripting, I was unable to get it to launch Organic Maps on boot.


Shutdown on condition

I implemented this using Termux, Termux:API and crontab.

This requires root, because shutdown is a system function.


Root

I rooted my device using Magisk.

Official Magisk GitHub repo


Battery status

To check the battery status, I used Termux and Termux:API extension.

Termux:API on F-Droid

Termux API on Termux Wiki

To check battery status:

termux-battery-status

This returns JSON, which can be parsed using jq.

pkg install jq

Now I can extract the values I need, for example:

batt_pct=`termux-battery-status | jq -r '.percentage'`
batt_status=`termux-battery-status |jq -r '.status'`

Shutdown

To shut down Android:

su -c "reboot -p"

Note on shutdown commands

These are the various shutdown commands I found for Android.

Most of them seem to run fine in user's Termux session, but when they are run as a part of a cronjob, they run slowly, are buggy, and sometimes reboot the phone instead of shutting down:

sudo reboot -p
sudo /system/bin/svc power shutdown
sudo /system/bin/svc power reboot -p
sudo am start -a android.intent.action.ACTION_REBOOT

So stick with

su -c "reboot -p"

Script

Putting it all together: (with an extra spoken message using another API)

batt_pct=`termux-battery-status | jq -r '.percentage'`
batt_status=`termux-battery-status |jq -r '.status'`
if [ $batt_pct -lt 60 ] && [ "$batt_status" = 'DISCHARGING' ]
then
	termux-tts-speak 'Low power. Shutting down.'
	su -c "reboot -p"
fi

I put this in a file at

/data/data/com.termux/files/usr/bin/yaky-auto-shutdown

This directory is in $PATH, so no need to specify the full path in the future.

Test by running in Termux:

yaky-auto-shutdown

Automation

To automate this script, I used crontab. First, install it through Termux:

pkg install cronie

Edit crontab:

crontab -e

Add an entry to run the auto-shutdown script

* * * * * yaky-auto-shutdown

To run the cron daemon, add another script to execute at boot:

nano ~/.termux/boot/start-cron.sh
#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
crond

This will set a wakelock to prevent Android from killing the long-running process, and then launch crond.

Every minute, cron will run the script, which will check the battery percentage and status, and shut down the device if the specified conditions are met. Although shutdown command requires root, root privileges will be provided to Termux as necessary (as long as they have been provided)


Battery management

To reduce wear and damage to an already-old battery, I installed Battery Charge Limit app to keep the device charge between 70% and 80%. This requires root, of course.

Battery Charge Limit on F-Droid

There are several Magisk modules to handle charging and automatic shutdown, but I did not have much luck with those:

Advanced Charging Controller (ACC) on GitHub

Input Power Control (IP Control) on GitHub


Current issues

It appears that the MHL cable does not supply enough current (pun intended) to keep the device charged, so it is gradually losing battery charge even though it is plugged in. Battery status shows current of 300mA when charging via MHL cable, and 450mA when charging through a regular USB cable connected to a laptop USB port.

I am considering combining the MHL USB cable with a regular charging USB cable, or going battery-less.

Topic on AndroidCentral


home

contact

(C) 2024 CC BY Anton Yaky

built using spaceport

-- Response ended

-- Page fetched on Sun Jun 2 11:56:37 2024