termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.28k stars 452 forks source link

API for dark mode detection #425

Open gXLg opened 3 years ago

gXLg commented 3 years ago

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
suhan-paradkar commented 3 years ago

I guess that would be android 10+ only implementation

gXLg commented 3 years ago

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

maximilionus commented 1 year ago

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.

agnostic-apollo commented 1 year ago

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

gXLg commented 1 year ago

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
maximilionus commented 1 year ago

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]
Rudxain commented 1 year ago

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 commented 1 year ago

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

agnostic-apollo commented 1 year ago

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

maximilionus commented 1 year ago

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

agnostic-apollo commented 1 year ago

targetSdkVersion != compileSdkVersion.

Rudxain commented 1 year ago

targetSdkVersion != compileSdkVersion.

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