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 d70055b0f9..86af7fd03a 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,15 @@ 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()); + } } }; @@ -159,4 +147,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); + } }