Close

Map: Android device setup

A project log for Volty MIR - [M]ap & [I]ntelligent [R]everse Camera

Building a display unit capable of showing maps and reverse camera feed for my first-gen Chevy Volt.

anton-yakyAnton Yaky 05/17/2024 at 01:490 Comments

To display maps, I need a device that has:

My trusty and crusty old Samsung Galaxy S5 has all that functionality, even video output to HDMI via MHL.

What I want the device to do is:

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.
Other options I considered:

Boot on plug

The recommended method is to enable this function through fastboot. This does not work on Samsung devices, though, but I will leave it here for reference.
Boot to fastboot mode (aka download mode) on the device.
Install fastboot on main machine and run:

fastboot oem off-mode-charge 0

Since my device is a Samsung, I rooted it with Magisk, and then downloaded and installed Magisk Autoboot module. Although their documentation states it works for Android 7, I could not get it to work with custom Lineage 14 (Android 7 equivalent) for some reason. On a clean Lineage 15, it works, and S5 takes about a minute to boot after connecting power.

Launch app after boot

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

Per instructions, I created a startup script:

mkdir -p ~/.termux/boot
nano ~/.termux/boot/start-maps.sh

 And set the script to start Organic Maps:

#!/data/data/com.termux/files/usr/bin/sh
am start app.organicmaps/.SplashActivity

This should also be possible to implement using Tasker, but I have not tried this. Might be easier if you don't like the terminal.

According to a StackExchange post:

Shutdown on condition

I implemented this using Termux, Termux:API and crontab.
This requires root, because shutdown is a system function.

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

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'`

To shut down Android:

su -c "reboot -p"     

Note: There are multiple ways to shut down an Android device, and many of those commands work fine in user's Termux session, but are very slow and buggy when they are run through crontab.

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 there is no need to specify the full path in the future.

To automate this script, I used crontab:

pkg install cronie
crontab -e

Added an entry to run the auto-shutdown script:

* * * * * yaky-auto-shutdown

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

nano ~/.termux/boot/start-cron.sh

And the script is:

#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
crond

This sets a wakelock (to prevent Android from killing the long-running crond process), and then launches crond.
Every minute, cron runs the script, which checks the battery percentage and status, and shuts 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 at least once before)

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.

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

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

This post and some additional information is also available on my blog.


Discussions