automatically enable UI_MODE_NIGHT when gtk-theme-name contains "dark"

gtk-theme-name is only checked once at start up
This commit is contained in:
Julian Winkler 2024-03-10 12:05:33 +01:00
parent f49922cf1b
commit 005e2299bd
8 changed files with 87 additions and 5 deletions

View file

@ -80,6 +80,7 @@ libtranslationlayer_so = shared_library('translation_layer_main', [
'src/api-impl-jni/android_graphics_Canvas.c', 'src/api-impl-jni/android_graphics_Canvas.c',
'src/api-impl-jni/android_graphics_Paint.c', 'src/api-impl-jni/android_graphics_Paint.c',
'src/api-impl-jni/android_util_Log.c', 'src/api-impl-jni/android_util_Log.c',
'src/api-impl-jni/content/android_content_Context.c',
'src/api-impl-jni/database/android_database_SQLiteCommon.c', 'src/api-impl-jni/database/android_database_SQLiteCommon.c',
'src/api-impl-jni/database/android_database_SQLiteConnection.c', 'src/api-impl-jni/database/android_database_SQLiteConnection.c',
'src/api-impl-jni/graphics/android_graphics_BitmapFactory.c', 'src/api-impl-jni/graphics/android_graphics_BitmapFactory.c',

View file

@ -376,3 +376,31 @@ JNIEXPORT void JNICALL Java_android_content_res_AssetManager_copyTheme(JNIEnv *e
{ {
Theme_setTo(_PTR(dest), _PTR(src)); Theme_setTo(_PTR(dest), _PTR(src));
} }
JNIEXPORT void JNICALL Java_android_content_res_AssetManager_setConfiguration(
JNIEnv *env, jobject this, jint mcc, jint mnc, jstring locale,
jint orientation, jint touchscreen, jint density, jint keyboard,
jint keyboardHidden, jint navigation, jint screenWidth, jint screenHeight,
jint smallestScreenWidthDp, jint screenWidthDp, jint screenHeightDp,
jint screenLayout, jint uiMode, jint majorVersion)
{
struct AssetManager *asset_manager = _PTR(_GET_LONG_FIELD(this, "mObject"));
const struct ResTable_config config = {
.mcc = mcc,
.mnc = mnc,
.orientation = orientation,
.touchscreen = touchscreen,
.density = density,
.keyboard = keyboard,
.navigation = navigation,
.screenWidth = screenWidth,
.screenHeight = screenHeight,
.smallestScreenWidthDp = smallestScreenWidthDp,
.screenWidthDp = screenWidthDp,
.screenHeightDp = screenHeightDp,
.screenLayout = screenLayout,
.uiMode = uiMode,
.sdkVersion = majorVersion
};
AssetManager_setConfiguration(asset_manager, &config, NULL);
}

View file

@ -0,0 +1,20 @@
#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <string.h>
#include "../defines.h"
#include "../util.h"
#include "../generated_headers/android_content_Context.h"
JNIEXPORT void JNICALL Java_android_content_Context_native_1updateConfig(JNIEnv *env, jclass class, jobject config)
{
GtkSettings *settings = gtk_settings_get_default();
gchar *theme_name;
g_object_get(settings, "gtk-theme-name", &theme_name, NULL);
bool night_mode = strcasestr(theme_name, "dark") || strcasestr(theme_name, "black");
if (night_mode) {
_SET_INT_FIELD(config, "uiMode", /*UI_MODE_NIGHT_YES*/ 0x20);
}
}

View file

@ -0,0 +1,23 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class android_content_Context */
#ifndef _Included_android_content_Context
#define _Included_android_content_Context
#ifdef __cplusplus
extern "C" {
#endif
#undef android_content_Context_MODE_PRIVATE
#define android_content_Context_MODE_PRIVATE 0L
/*
* Class: android_content_Context
* Method: native_updateConfig
* Signature: (Landroid/content/res/Configuration;)V
*/
JNIEXPORT void JNICALL Java_android_content_Context_native_1updateConfig
(JNIEnv *, jclass, jobject);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -81,6 +81,14 @@ JNIEXPORT void JNICALL Java_android_content_res_AssetManager_setLocale
JNIEXPORT jobjectArray JNICALL Java_android_content_res_AssetManager_getLocales JNIEXPORT jobjectArray JNICALL Java_android_content_res_AssetManager_getLocales
(JNIEnv *, jobject); (JNIEnv *, jobject);
/*
* Class: android_content_res_AssetManager
* Method: setConfiguration
* Signature: (IILjava/lang/String;IIIIIIIIIIIIII)V
*/
JNIEXPORT void JNICALL Java_android_content_res_AssetManager_setConfiguration
(JNIEnv *, jobject, jint, jint, jstring, jint, jint, jint, jint, jint, jint, jint, jint, jint, jint, jint, jint, jint, jint);
/* /*
* Class: android_content_res_AssetManager * Class: android_content_res_AssetManager
* Method: getResourceIdentifier * Method: getResourceIdentifier

View file

@ -63,7 +63,6 @@ public class Context extends Object {
static AssetManager assets; static AssetManager assets;
static DisplayMetrics dm; static DisplayMetrics dm;
static Configuration config;
static Resources r; static Resources r;
static ApplicationInfo application_info; static ApplicationInfo application_info;
static Resources.Theme theme; static Resources.Theme theme;
@ -82,7 +81,8 @@ public class Context extends Object {
static { static {
assets = new AssetManager(); assets = new AssetManager();
dm = new DisplayMetrics(); dm = new DisplayMetrics();
config = new Configuration(); Configuration config = new Configuration();
native_updateConfig(config);
r = new Resources(assets, dm, config); r = new Resources(assets, dm, config);
theme = r.newTheme(); theme = r.newTheme();
application_info = new ApplicationInfo(); application_info = new ApplicationInfo();
@ -97,6 +97,8 @@ public class Context extends Object {
} }
} }
protected static native void native_updateConfig(Configuration config);
static Application createApplication(long native_window) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { static Application createApplication(long native_window) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
Application application; Application application;
ResXmlAttribute application_name = manifest.getApplicationElement().searchAttributeByResourceId(AndroidManifestBlock.ID_name); ResXmlAttribute application_name = manifest.getApplicationElement().searchAttributeByResourceId(AndroidManifestBlock.ID_name);

View file

@ -701,11 +701,11 @@ public final class AssetManager {
* applications. * applications.
* {@hide} * {@hide}
*/ */
public /*native*/ final void setConfiguration(int mcc, int mnc, String locale, public native final void setConfiguration(int mcc, int mnc, String locale,
int orientation, int touchscreen, int density, int keyboard, int orientation, int touchscreen, int density, int keyboard,
int keyboardHidden, int navigation, int screenWidth, int screenHeight, int keyboardHidden, int navigation, int screenWidth, int screenHeight,
int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp, int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp,
int screenLayout, int uiMode, int majorVersion) {} int screenLayout, int uiMode, int majorVersion);
/** /**
* Retrieve the resource identifier for the given resource name. * Retrieve the resource identifier for the given resource name.

View file

@ -208,7 +208,7 @@ public class Resources {
mCompatibilityInfo = compatInfo; mCompatibilityInfo = compatInfo;
} }
mToken = new WeakReference<IBinder>(token); mToken = new WeakReference<IBinder>(token);
// updateConfiguration(config, metrics); updateConfiguration(config, metrics);
// assets.ensureStringBlocks(); // assets.ensureStringBlocks();
} }