API for dark mode detection
Feature Suugestion
Would be nice to be able to know, whether the device has currently dark mode enabled.
Could be useful for example in .bashrc:
if [[ "$(termux-dark-mode | jq .enabled)" == "true" ]]; then
cat ~/themes/dark_theme > ~/.termux/color.properties
fi
I guess that would be android 10+ only implementation
I guess that would be android 10+ only implementation
Termux:API already provides some features, that are not able to run on some devices, which e.g. don't have a infrared emitter
I wonder is there any other way to detect current scheme of system from Termux. Really want to put it in my start script to auto-switch themes.
Grant termux DUMP permission and run dumpsys uimode and grep output for mNightMode= for current setting, which may be auto. To get current state, grep mCurUiMode= for current flags.
Can get current setting with setting="$(cmd uimode night 2>&1 </dev/null)" as well.
https://android.googlesource.com/platform/frameworks/base/+/ccbf84f/services/java/com/android/server/UiModeManagerService.java
This indeed worked for me! (no root needed) To grant DUMP, use ADB and enter following:
adb shell pm grant com.termux android.permission.DUMP
I do also tried to get some info from getprop, but it's very vendor-dependent. Here's how the values change depending on system color mode.
$ diff prop_light.dump prop_dark.dump
1205c1205
< [sys.oplus.display.uimode]: [17]
---
> [sys.oplus.display.uimode]: [33]
I have a termux-is-dark-mode "polyfill" script in my ~/bin/:
#!/usr/bin/bash
set -f
[[ "$(cmd uimode night 2>&1 < /dev/null)" == *yes ]]
It allows clean code like:
if termux-is-dark-mode
then echo 🌙
else echo ☀️
fi
BTW, if such a cmd is ever added, should it be a getter/setter or only getter?
Something like termux-ui-night-mode can accept the get and set argument. However setting with cmd uimode night yes|no will require root or adb as ui_night_mode is a secure setting, which technically can be handled as termux supports both.
https://cs.android.com/android/platform/superproject/+/android-13.0.0_r74:frameworks/base/core/java/android/provider/Settings.java
@Rudxain I believe that the correct way of implementing this feature is a special API package CLI getter script (termux-theme for example), that will use the API implementation for theme managing (@TERMUX_PREFIX@/libexec/termux-api Theme for example) and return the output like:
{
"mode": "DARK", // Or "LIGHT"
"some": "more",
"info": "data"
}
The next problem is Termux:API target SDK version:
https://github.com/termux/termux-api/blob/8bd22936e66c3e6dcd271405281bc322542e0579/gradle.properties#L24
It's 28, and system-wide themes API is only available starting from the SDK 29 (Android 10).
targetSdkVersion != compileSdkVersion.
targetSdkVersion!=compileSdkVersion.
I just read this. Thanks for mentioning that, I just learned something new about Android