From 5dcce1965ecdadd568007dba0b7fe4f9591f869e Mon Sep 17 00:00:00 2001 From: Rash419 Date: Wed, 9 Aug 2023 14:59:08 +0530 Subject: [PATCH] android: enable dark mode in documents Signed-off-by: Rash419 Change-Id: I108601f78f4d7bbf704b5f5f89b20c8f8a597355 --- .../androidapp/SettingsActivity.java | 2 ++ .../libreoffice/androidlib/LOActivity.java | 23 +++++++++++++++++++ browser/html/cool.html.m4 | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/android/app/src/main/java/org/libreoffice/androidapp/SettingsActivity.java b/android/app/src/main/java/org/libreoffice/androidapp/SettingsActivity.java index e42017f33..56e03e4fd 100644 --- a/android/app/src/main/java/org/libreoffice/androidapp/SettingsActivity.java +++ b/android/app/src/main/java/org/libreoffice/androidapp/SettingsActivity.java @@ -17,6 +17,7 @@ import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import org.libreoffice.androidapp.ui.LibreOfficeUIActivity; +import org.libreoffice.androidlib.LOActivity; public class SettingsActivity extends AppCompatActivity { @@ -86,6 +87,7 @@ public class SettingsActivity extends AppCompatActivity { } AppCompatDelegate.setDefaultNightMode(dayNightMode); prefs.edit().putInt(LibreOfficeUIActivity.NIGHT_MODE_KEY, dayNightMode).commit(); + prefs.edit().putInt(LOActivity.NIGHT_MODE_KEY, dayNightMode).commit(); getActivity().recreate(); } diff --git a/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java b/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java index e741d6bb2..5ad8ad3cb 100644 --- a/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java +++ b/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java @@ -23,6 +23,7 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; +import android.content.res.Configuration; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; @@ -74,6 +75,7 @@ import java.util.Map; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -96,6 +98,7 @@ public class LOActivity extends AppCompatActivity { private static final String CLIPBOARD_COOL_SIGNATURE = "cool-clip-magic-4a22437e49a8-"; public static final String RECENT_DOCUMENTS_KEY = "RECENT_DOCUMENTS_LIST"; private static String USER_NAME_KEY = "USER_NAME"; + public static final String NIGHT_MODE_KEY = "NIGHT_MODE"; private File mTempFile = null; @@ -858,6 +861,11 @@ public class LOActivity extends AppCompatActivity { if (isLargeScreen() && !isChromeOS()) finalUrlToLoad += "&userinterfacemode=notebookbar"; + + if(isDarkMode()) { + finalUrlToLoad += "&darkTheme=true"; + } + // load the page mWebView.loadUrl(finalUrlToLoad); @@ -866,6 +874,21 @@ public class LOActivity extends AppCompatActivity { loadDocumentMillis = android.os.SystemClock.uptimeMillis(); } + private boolean isDarkMode() { + SharedPreferences recentPrefs = getSharedPreferences(EXPLORER_PREFS_KEY, MODE_PRIVATE); + int mode = recentPrefs.getInt(NIGHT_MODE_KEY, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); + switch (mode) { + case -1: + int darkModeFlag = getBaseContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; + return darkModeFlag == Configuration.UI_MODE_NIGHT_YES; + case 1: + return false; + case 2: + return true; + } + return false; + } + static { System.loadLibrary("androidapp"); } diff --git a/browser/html/cool.html.m4 b/browser/html/cool.html.m4 index 24bb5bedd..69caa2060 100644 --- a/browser/html/cool.html.m4 +++ b/browser/html/cool.html.m4 @@ -396,6 +396,10 @@ m4_ifelse(IOSAPP,[true], m4_ifelse(ANDROIDAPP,[true], [window.userInterfaceMode = window.getParameterByName('userinterfacemode');]) +m4_ifelse(ANDROIDAPP,[true], + [var darkTheme = window.getParameterByName('darkTheme'); + if (darkTheme) {window.uiDefaults = {'darkTheme': true};}]) + m4_ifelse(EMSCRIPTENAPP,[true], [window.userInterfaceMode = 'notebookbar';])