termux-api icon indicating copy to clipboard operation
termux-api copied to clipboard

API for dark mode detection

Open gXLg opened this issue 4 years ago • 12 comments

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

gXLg avatar Jun 09 '21 11:06 gXLg

I guess that would be android 10+ only implementation

creepy-pasta101 avatar Jun 09 '21 11:06 creepy-pasta101

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

gXLg avatar Jun 09 '21 12:06 gXLg

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.

maximilionus avatar May 19 '23 16:05 maximilionus

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

agnostic-apollo avatar May 19 '23 16:05 agnostic-apollo

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

gXLg avatar May 19 '23 17:05 gXLg

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]

maximilionus avatar May 19 '23 17:05 maximilionus

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

Rudxain avatar Sep 25 '23 20:09 Rudxain

BTW, if such a cmd is ever added, should it be a getter/setter or only getter?

Rudxain avatar Sep 25 '23 20:09 Rudxain

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

agnostic-apollo avatar Sep 25 '23 20:09 agnostic-apollo

@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).

maximilionus avatar Sep 25 '23 21:09 maximilionus

targetSdkVersion != compileSdkVersion.

agnostic-apollo avatar Sep 25 '23 21:09 agnostic-apollo

targetSdkVersion != compileSdkVersion.

I just read this. Thanks for mentioning that, I just learned something new about Android

Rudxain avatar Sep 25 '23 23:09 Rudxain