From 025181c2502e866904db5d818fd0cecaa73a96b3 Mon Sep 17 00:00:00 2001 From: Harry Bond Date: Thu, 22 May 2025 13:52:20 +0100 Subject: [PATCH] [android] improve auto theming ported from the big PR. Addresses the issue of the theme not changing when there's no location fix by setting to night at 6pm (still checks when sunset is if you have a location fix) Signed-off-by: Harry Bond --- .../app/organicmaps/util/ThemeSwitcher.java | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java b/android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java index d70055b0f..92ee3b8e1 100644 --- a/android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java +++ b/android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java @@ -8,6 +8,9 @@ import android.os.Build; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatDelegate; + +import java.util.Calendar; + import app.organicmaps.Framework; import app.organicmaps.MwmApplication; import app.organicmaps.R; @@ -29,30 +32,17 @@ public enum ThemeSwitcher @Override public void run() { - String nightTheme = MwmApplication.from(mContext).getString(R.string.theme_night); - String defaultTheme = MwmApplication.from(mContext).getString(R.string.theme_default); - String theme = defaultTheme; - Location last = LocationHelper.from(mContext).getSavedLocation(); - boolean navAuto = RoutingController.get().isNavigating() && ThemeUtils.isNavAutoTheme(mContext); - - if (navAuto || ThemeUtils.isAutoTheme(mContext)) - { - if (last == null) - theme = Config.getCurrentUiTheme(mContext); - else - { - long currentTime = System.currentTimeMillis() / 1000; - boolean day = Framework.nativeIsDayTime(currentTime, last.getLatitude(), last.getLongitude()); - theme = (day ? defaultTheme : nightTheme); - } - } - - setThemeAndMapStyle(theme); + // Cancel old checker UiThread.cancelDelayedTasks(mAutoThemeChecker); if (navAuto || ThemeUtils.isAutoTheme(mContext)) + { UiThread.runLater(mAutoThemeChecker, CHECK_INTERVAL_MS); + setThemeAndMapStyle(calcAutoTheme()); + } + else + setThemeAndMapStyle(Config.getCurrentUiTheme(mContext)); } }; @@ -159,4 +149,30 @@ public enum ThemeSwitcher else Framework.nativeMarkMapStyle(style); } + + /** + * Determine light/dark theme based on time and location, + * or fall back to time-based (06:00-18:00) when there's no location fix + * @return theme_light/dark string + */ + private String calcAutoTheme() + { + String defaultTheme = mContext.getResources().getString(R.string.theme_default); + String nightTheme = mContext.getResources().getString(R.string.theme_night); + Location last = LocationHelper.from(mContext).getSavedLocation(); + boolean day; + + if (last != null) + { + long currentTime = System.currentTimeMillis() / 1000; + day = Framework.nativeIsDayTime(currentTime, last.getLatitude(), last.getLongitude()); + } + else + { + int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); + day = (currentHour < 18 && currentHour > 6); + } + + return (day ? defaultTheme : nightTheme); + } }