From 084c35ea6861d52e67b3d3084448b1f3182aca55 Mon Sep 17 00:00:00 2001 From: millanp Date: Wed, 17 Jan 2024 14:36:58 -0800 Subject: [PATCH] Make the dark mode override 'stick' (#1104) --- .../org/onebusaway/android/app/Application.java | 11 +++++++++++ .../onebusaway/android/ui/PreferencesActivity.java | 13 +------------ .../java/org/onebusaway/android/util/UIUtils.java | 13 +++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/onebusaway-android/src/main/java/org/onebusaway/android/app/Application.java b/onebusaway-android/src/main/java/org/onebusaway/android/app/Application.java index 073f58d24..4a01851c3 100644 --- a/onebusaway-android/src/main/java/org/onebusaway/android/app/Application.java +++ b/onebusaway-android/src/main/java/org/onebusaway/android/app/Application.java @@ -61,6 +61,7 @@ import edu.usf.cutr.open311client.models.Open311Option; import static com.google.android.gms.location.LocationServices.getFusedLocationProviderClient; +import static org.onebusaway.android.util.UIUtils.setAppTheme; public class Application extends MultiDexApplication { @@ -436,6 +437,7 @@ private void initOba() { } checkArrivalStylePreferenceDefault(); + checkDarkMode(); // Get the current app version. PackageManager pm = getPackageManager(); @@ -481,6 +483,15 @@ private void checkArrivalStylePreferenceDefault() { } } + private void checkDarkMode() { + String appThemePrefKey = getResources() + .getString(R.string.preference_key_app_theme); + String appThemePref = mPrefs.getString(appThemePrefKey, null); + if (appThemePref != null) { + setAppTheme(appThemePref); + } + } + private void initObaRegion() { // Read the region preference, look it up in the DB, then set the region. long id = mPrefs.getLong(getString(R.string.preference_key_region), -1); diff --git a/onebusaway-android/src/main/java/org/onebusaway/android/ui/PreferencesActivity.java b/onebusaway-android/src/main/java/org/onebusaway/android/ui/PreferencesActivity.java index 7260d090f..e134b70b0 100644 --- a/onebusaway-android/src/main/java/org/onebusaway/android/ui/PreferencesActivity.java +++ b/onebusaway-android/src/main/java/org/onebusaway/android/ui/PreferencesActivity.java @@ -20,6 +20,7 @@ import static org.onebusaway.android.util.PermissionUtils.RESTORE_BACKUP_PERMISSION_REQUEST; import static org.onebusaway.android.util.PermissionUtils.SAVE_BACKUP_PERMISSION_REQUEST; import static org.onebusaway.android.util.PermissionUtils.STORAGE_PERMISSIONS; +import static org.onebusaway.android.util.UIUtils.setAppTheme; import android.annotation.SuppressLint; import android.app.Activity; @@ -666,16 +667,4 @@ public void onRegionTaskFinished(boolean currentRegionChanged) { NavHelp.goHome(this, false); } } - - private void setAppTheme(String themeValue) { - if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_system_default))) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); - } - if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_dark))) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); - } - if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_light))) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); - } - } } diff --git a/onebusaway-android/src/main/java/org/onebusaway/android/util/UIUtils.java b/onebusaway-android/src/main/java/org/onebusaway/android/util/UIUtils.java index 764eafc61..1831c09f3 100644 --- a/onebusaway-android/src/main/java/org/onebusaway/android/util/UIUtils.java +++ b/onebusaway-android/src/main/java/org/onebusaway/android/util/UIUtils.java @@ -76,6 +76,7 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.widget.SearchView; import androidx.core.content.ContextCompat; import androidx.core.content.pm.ShortcutInfoCompat; @@ -2010,4 +2011,16 @@ public static void openBatteryIgnoreIntent(Activity activity) { intent.setData(Uri.parse("package:" + activity.getPackageName())); activity.startActivity(intent); } + + public static void setAppTheme(String themeValue) { + if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_system_default))) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); + } + if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_dark))) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } + if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_light))) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } + } }