From 447266c3282a7a7cbbc26233a951b8c9dd62c278 Mon Sep 17 00:00:00 2001 From: Andrei Shkrob Date: Fri, 8 Aug 2025 17:48:12 +0200 Subject: [PATCH] [android][sdk] Fix ThemeUtils usage in sdk Signed-off-by: Andrei Shkrob --- .../java/app/organicmaps/MwmActivity.java | 8 +- .../java/app/organicmaps/SplashActivity.java | 7 +- .../base/BaseMwmDialogFragment.java | 2 +- .../base/BaseMwmFragmentActivity.java | 12 +-- .../bookmarks/BookmarkCategoriesActivity.java | 2 +- .../bookmarks/BookmarkListActivity.java | 2 +- .../app/organicmaps/car/util/ThemeUtils.java | 34 +++---- .../editor/HoursMinutesPickerFragment.java | 8 +- .../maplayer/MapButtonsController.java | 2 +- .../traffic/widget/TrafficButton.java | 10 +- .../organicmaps/routing/TransitStepView.java | 2 +- .../main/java/app/organicmaps/sdk/Map.java | 10 +- .../java/app/organicmaps/sdk/util/Config.java | 97 ++++++++++++------- .../organicmaps/search/CategoriesAdapter.java | 2 +- .../organicmaps/search/SearchActivity.java | 2 +- .../settings/BaseXmlSettingsFragment.java | 2 +- .../settings/SettingsPrefsFragment.java | 33 +++---- .../app/organicmaps/util/ThemeSwitcher.java | 33 ++++--- .../java/app/organicmaps/util/ThemeUtils.java | 62 +++--------- .../widget/RoutingToolbarButton.java | 7 +- .../src/main/res/values/donottranslate.xml | 6 -- .../app/src/main/res/values/string-arrays.xml | 8 -- android/app/src/main/res/xml/prefs_main.xml | 1 - 23 files changed, 162 insertions(+), 190 deletions(-) diff --git a/android/app/src/main/java/app/organicmaps/MwmActivity.java b/android/app/src/main/java/app/organicmaps/MwmActivity.java index a96b47db8..e6b651be9 100644 --- a/android/app/src/main/java/app/organicmaps/MwmActivity.java +++ b/android/app/src/main/java/app/organicmaps/MwmActivity.java @@ -458,12 +458,10 @@ public class MwmActivity extends BaseMwmFragmentActivity @StyleRes protected int getThemeResourceId(@NonNull String theme) { - Context context = getApplicationContext(); - - if (ThemeUtils.isDefaultTheme(context, theme)) + if (Config.UiTheme.isDefault(theme)) return R.style.MwmTheme_MainActivity; - if (ThemeUtils.isNightTheme(context, theme)) + if (Config.UiTheme.isNight(theme)) return R.style.MwmTheme_Night_MainActivity; return super.getThemeResourceId(theme); @@ -585,7 +583,7 @@ public class MwmActivity extends BaseMwmFragmentActivity private void refreshLightStatusBar() { - UiUtils.setLightStatusBar(this, !(ThemeUtils.isNightTheme(this) || RoutingController.get().isPlanning() + UiUtils.setLightStatusBar(this, !(ThemeUtils.isNightTheme() || RoutingController.get().isPlanning() || ChoosePositionMode.get() != ChoosePositionMode.None)); } diff --git a/android/app/src/main/java/app/organicmaps/SplashActivity.java b/android/app/src/main/java/app/organicmaps/SplashActivity.java index 620f6d500..2771538ff 100644 --- a/android/app/src/main/java/app/organicmaps/SplashActivity.java +++ b/android/app/src/main/java/app/organicmaps/SplashActivity.java @@ -60,11 +60,10 @@ public class SplashActivity extends AppCompatActivity { super.onCreate(savedInstanceState); - final Context context = getApplicationContext(); - final String theme = Config.getCurrentUiTheme(context); - if (ThemeUtils.isDefaultTheme(context, theme)) + final String theme = Config.UiTheme.getCurrent(); + if (Config.UiTheme.isDefault(theme)) setTheme(R.style.MwmTheme_Splash); - else if (ThemeUtils.isNightTheme(context, theme)) + else if (Config.UiTheme.isNight(theme)) setTheme(R.style.MwmTheme_Night_Splash); else throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme); diff --git a/android/app/src/main/java/app/organicmaps/base/BaseMwmDialogFragment.java b/android/app/src/main/java/app/organicmaps/base/BaseMwmDialogFragment.java index 0cd943754..1f513dac2 100644 --- a/android/app/src/main/java/app/organicmaps/base/BaseMwmDialogFragment.java +++ b/android/app/src/main/java/app/organicmaps/base/BaseMwmDialogFragment.java @@ -15,7 +15,7 @@ public class BaseMwmDialogFragment extends DialogFragment @StyleRes protected final int getFullscreenTheme() { - return ThemeUtils.isNightTheme(requireContext()) ? getFullscreenDarkTheme() : getFullscreenLightTheme(); + return ThemeUtils.isNightTheme() ? getFullscreenDarkTheme() : getFullscreenLightTheme(); } protected int getStyle() diff --git a/android/app/src/main/java/app/organicmaps/base/BaseMwmFragmentActivity.java b/android/app/src/main/java/app/organicmaps/base/BaseMwmFragmentActivity.java index 4b72a84f0..518373f8f 100644 --- a/android/app/src/main/java/app/organicmaps/base/BaseMwmFragmentActivity.java +++ b/android/app/src/main/java/app/organicmaps/base/BaseMwmFragmentActivity.java @@ -1,7 +1,6 @@ package app.organicmaps.base; import android.content.ComponentName; -import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.media.AudioManager; @@ -24,7 +23,6 @@ import app.organicmaps.sdk.util.Config; import app.organicmaps.sdk.util.concurrency.UiThread; import app.organicmaps.sdk.util.log.Logger; import app.organicmaps.util.RtlUtils; -import app.organicmaps.util.ThemeUtils; import com.google.android.material.appbar.MaterialToolbar; import java.util.Objects; @@ -40,12 +38,10 @@ public abstract class BaseMwmFragmentActivity extends AppCompatActivity @StyleRes protected int getThemeResourceId(@NonNull String theme) { - Context context = getApplicationContext(); - - if (ThemeUtils.isDefaultTheme(context, theme)) + if (Config.UiTheme.isDefault(theme)) return R.style.MwmTheme; - if (ThemeUtils.isNightTheme(context, theme)) + if (Config.UiTheme.isNight(theme)) return R.style.MwmTheme_Night; throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme); @@ -62,7 +58,7 @@ public abstract class BaseMwmFragmentActivity extends AppCompatActivity protected final void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mThemeName = Config.getCurrentUiTheme(getApplicationContext()); + mThemeName = Config.UiTheme.getCurrent(); setTheme(getThemeResourceId(mThemeName)); EdgeToEdge.enable(this, SystemBarStyle.dark(Color.TRANSPARENT)); RtlUtils.manageRtl(this); @@ -122,7 +118,7 @@ public abstract class BaseMwmFragmentActivity extends AppCompatActivity public void onPostResume() { super.onPostResume(); - if (!mThemeName.equals(Config.getCurrentUiTheme(getApplicationContext()))) + if (!mThemeName.equals(Config.UiTheme.getCurrent())) { // Workaround described in https://code.google.com/p/android/issues/detail?id=93731 UiThread.runLater(this::recreate); diff --git a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesActivity.java b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesActivity.java index ea54ae664..a3f8c28f2 100644 --- a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesActivity.java +++ b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesActivity.java @@ -42,7 +42,7 @@ public class BookmarkCategoriesActivity extends BaseToolbarActivity @StyleRes public int getThemeResourceId(@NonNull String theme) { - return ThemeUtils.getWindowBgThemeResourceId(getApplicationContext(), theme); + return ThemeUtils.getWindowBgThemeResourceId(theme); } @Override diff --git a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java index c0451773e..8dd111fac 100644 --- a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java +++ b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java @@ -41,7 +41,7 @@ public class BookmarkListActivity extends BaseToolbarActivity @StyleRes public int getThemeResourceId(@NonNull String theme) { - return ThemeUtils.getCardBgThemeResourceId(getApplicationContext(), theme); + return ThemeUtils.getCardBgThemeResourceId(theme); } @Override diff --git a/android/app/src/main/java/app/organicmaps/car/util/ThemeUtils.java b/android/app/src/main/java/app/organicmaps/car/util/ThemeUtils.java index 4f087e207..5ade7fda6 100644 --- a/android/app/src/main/java/app/organicmaps/car/util/ThemeUtils.java +++ b/android/app/src/main/java/app/organicmaps/car/util/ThemeUtils.java @@ -10,19 +10,20 @@ import androidx.car.app.CarContext; import app.organicmaps.R; import app.organicmaps.sdk.MapStyle; import app.organicmaps.sdk.routing.RoutingController; +import app.organicmaps.sdk.util.Config; public final class ThemeUtils { public enum ThemeMode { - AUTO(R.string.auto, R.string.theme_auto), - LIGHT(R.string.off, R.string.theme_default), - NIGHT(R.string.on, R.string.theme_night); + AUTO(R.string.auto, Config.UiTheme.AUTO), + LIGHT(R.string.off, Config.UiTheme.DEFAULT), + NIGHT(R.string.on, Config.UiTheme.NIGHT); - ThemeMode(@StringRes int titleId, @StringRes int prefsKeyId) + ThemeMode(@StringRes int titleId, @NonNull String config) { mTitleId = titleId; - mPrefsKeyId = prefsKeyId; + mConfig = config; } @StringRes @@ -31,16 +32,16 @@ public final class ThemeUtils return mTitleId; } - @StringRes - public int getPrefsKeyId() + @NonNull + public String getConfig() { - return mPrefsKeyId; + return mConfig; } @StringRes private final int mTitleId; - @StringRes - private final int mPrefsKeyId; + @NonNull + private final String mConfig; } private static final String ANDROID_AUTO_PREFERENCES_FILE_KEY = "ANDROID_AUTO_PREFERENCES_FILE_KEY"; @@ -79,23 +80,20 @@ public final class ThemeUtils @UiThread public static void setThemeMode(@NonNull CarContext context, @NonNull ThemeMode themeMode) { - getSharedPreferences(context).edit().putString(THEME_KEY, context.getString(themeMode.getPrefsKeyId())).commit(); + getSharedPreferences(context).edit().putString(THEME_KEY, themeMode.getConfig()).commit(); update(context, themeMode); } @NonNull public static ThemeMode getThemeMode(@NonNull CarContext context) { - final String autoTheme = context.getString(R.string.theme_auto); - final String lightTheme = context.getString(R.string.theme_default); - final String nightTheme = context.getString(R.string.theme_night); - final String themeMode = getSharedPreferences(context).getString(THEME_KEY, autoTheme); + final String themeMode = getSharedPreferences(context).getString(THEME_KEY, ThemeMode.AUTO.getConfig()); - if (themeMode.equals(autoTheme)) + if (themeMode.equals(ThemeMode.AUTO.getConfig())) return ThemeMode.AUTO; - else if (themeMode.equals(lightTheme)) + else if (themeMode.equals(ThemeMode.LIGHT.getConfig())) return ThemeMode.LIGHT; - else if (themeMode.equals(nightTheme)) + else if (themeMode.equals(ThemeMode.NIGHT.getConfig())) return ThemeMode.NIGHT; else throw new IllegalArgumentException("Unsupported value"); diff --git a/android/app/src/main/java/app/organicmaps/editor/HoursMinutesPickerFragment.java b/android/app/src/main/java/app/organicmaps/editor/HoursMinutesPickerFragment.java index b1566c407..20a70d7f4 100644 --- a/android/app/src/main/java/app/organicmaps/editor/HoursMinutesPickerFragment.java +++ b/android/app/src/main/java/app/organicmaps/editor/HoursMinutesPickerFragment.java @@ -79,8 +79,8 @@ public class HoursMinutesPickerFragment extends BaseMwmDialogFragment mTabs.getTabAt(mSelectedTab).select(); @StyleRes - final int theme = ThemeUtils.isNightTheme(requireContext()) ? R.style.MwmMain_DialogFragment_TimePicker_Night - : R.style.MwmMain_DialogFragment_TimePicker; + final int theme = ThemeUtils.isNightTheme() ? R.style.MwmMain_DialogFragment_TimePicker_Night + : R.style.MwmMain_DialogFragment_TimePicker; final AlertDialog dialog = new MaterialAlertDialogBuilder(requireActivity(), theme) .setView(root) .setNegativeButton(R.string.cancel, null) @@ -141,8 +141,8 @@ public class HoursMinutesPickerFragment extends BaseMwmDialogFragment MaterialTextView tabView = (MaterialTextView) inflater.inflate(R.layout.tab_timepicker, mTabs, false); tabView.setText(getResources().getString(R.string.editor_time_from)); final ColorStateList textColor = AppCompatResources.getColorStateList( - requireContext(), ThemeUtils.isNightTheme(requireContext()) ? R.color.accent_color_selector_night - : R.color.accent_color_selector); + requireContext(), + ThemeUtils.isNightTheme() ? R.color.accent_color_selector_night : R.color.accent_color_selector); tabView.setTextColor(textColor); mTabs.addTab(mTabs.newTab().setCustomView(tabView), true); tabView = (MaterialTextView) inflater.inflate(R.layout.tab_timepicker, mTabs, false); diff --git a/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java b/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java index fb368af39..28e760076 100644 --- a/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java +++ b/android/app/src/main/java/app/organicmaps/maplayer/MapButtonsController.java @@ -214,7 +214,7 @@ public class MapButtonsController extends Fragment // helpButton.setImageResource(R.drawable.ic_launcher); // } // // Keep this button colorful in normal theme. - // if (!ThemeUtils.isNightTheme(requireContext())) + // if (!ThemeUtils.isNightTheme()) // helpButton.getDrawable().setTintList(null); } else if (leftButtonView != null) diff --git a/android/app/src/main/java/app/organicmaps/maplayer/traffic/widget/TrafficButton.java b/android/app/src/main/java/app/organicmaps/maplayer/traffic/widget/TrafficButton.java index aedc3e45b..ccf986ab1 100644 --- a/android/app/src/main/java/app/organicmaps/maplayer/traffic/widget/TrafficButton.java +++ b/android/app/src/main/java/app/organicmaps/maplayer/traffic/widget/TrafficButton.java @@ -43,22 +43,20 @@ public class TrafficButton void turnOff() { stopWaitingAnimation(); - mButton.setImageResource(ThemeUtils.isNightTheme(mButton.getContext()) ? R.drawable.ic_traffic_on_night - : R.drawable.ic_traffic_on); + mButton.setImageResource(ThemeUtils.isNightTheme() ? R.drawable.ic_traffic_on_night : R.drawable.ic_traffic_on); } void turnOn() { stopWaitingAnimation(); - mButton.setImageResource(ThemeUtils.isNightTheme(mButton.getContext()) ? R.drawable.ic_traffic_on_night - : R.drawable.ic_traffic_on); + mButton.setImageResource(ThemeUtils.isNightTheme() ? R.drawable.ic_traffic_on_night : R.drawable.ic_traffic_on); } void markAsOutdated() { stopWaitingAnimation(); - mButton.setImageResource(ThemeUtils.isNightTheme(mButton.getContext()) ? R.drawable.ic_traffic_outdated_night - : R.drawable.ic_traffic_outdated); + mButton.setImageResource(ThemeUtils.isNightTheme() ? R.drawable.ic_traffic_outdated_night + : R.drawable.ic_traffic_outdated); } void startWaitingAnimation() diff --git a/android/app/src/main/java/app/organicmaps/routing/TransitStepView.java b/android/app/src/main/java/app/organicmaps/routing/TransitStepView.java index ed6ed4944..352ed3d0c 100644 --- a/android/app/src/main/java/app/organicmaps/routing/TransitStepView.java +++ b/android/app/src/main/java/app/organicmaps/routing/TransitStepView.java @@ -90,7 +90,7 @@ public class TransitStepView extends View implements MultilineLayoutManager.Sque { mDrawable = null; mText = info.getDistance() + " " + info.getDistanceUnits(); - mTextPaint.setColor(ThemeUtils.isDefaultTheme(getContext()) ? Color.BLACK : Color.WHITE); + mTextPaint.setColor(ThemeUtils.isDefaultTheme() ? Color.BLACK : Color.WHITE); } else { diff --git a/android/app/src/main/java/app/organicmaps/sdk/Map.java b/android/app/src/main/java/app/organicmaps/sdk/Map.java index cc386ddd1..c2524340b 100644 --- a/android/app/src/main/java/app/organicmaps/sdk/Map.java +++ b/android/app/src/main/java/app/organicmaps/sdk/Map.java @@ -148,7 +148,7 @@ public final class Map { assert mLocationHelper != null : "LocationHelper must be initialized before calling onSurfaceCreated"; - if (isThemeChangingProcess(context)) + if (isThemeChangingProcess()) { Logger.d(TAG, "Theme changing process, skip 'onSurfaceCreated' callback"); return; @@ -203,7 +203,7 @@ public final class Map public void onSurfaceChanged(final Context context, final Surface surface, Rect surfaceFrame, boolean isSurfaceCreating) { - if (isThemeChangingProcess(context)) + if (isThemeChangingProcess()) { Logger.d(TAG, "Theme changing process, skip 'onSurfaceChanged' callback"); return; @@ -265,7 +265,7 @@ public final class Map public void onPause(final Context context) { - mUiThemeOnPause = Config.getCurrentUiTheme(context); + mUiThemeOnPause = Config.UiTheme.getCurrent(); // Pause/Resume can be called without surface creation/destroy. if (mSurfaceAttached) @@ -370,9 +370,9 @@ public final class Map nativeApplyWidgets(); } - private boolean isThemeChangingProcess(final Context context) + private boolean isThemeChangingProcess() { - return mUiThemeOnPause != null && !mUiThemeOnPause.equals(Config.getCurrentUiTheme(context)); + return mUiThemeOnPause != null && !mUiThemeOnPause.equals(Config.UiTheme.getCurrent()); } // Engine diff --git a/android/app/src/main/java/app/organicmaps/sdk/util/Config.java b/android/app/src/main/java/app/organicmaps/sdk/util/Config.java index fbdf46154..8396ed6f6 100644 --- a/android/app/src/main/java/app/organicmaps/sdk/util/Config.java +++ b/android/app/src/main/java/app/organicmaps/sdk/util/Config.java @@ -7,7 +7,6 @@ import androidx.annotation.NonNull; import androidx.preference.PreferenceManager; import app.organicmaps.BuildConfig; import app.organicmaps.R; -import app.organicmaps.util.ThemeUtils; public final class Config { @@ -24,8 +23,6 @@ public final class Config private static final String KEY_MISC_DISCLAIMER_ACCEPTED = "IsDisclaimerApproved"; private static final String KEY_MISC_LOCATION_REQUESTED = "LocationRequested"; - private static final String KEY_MISC_UI_THEME = "UiTheme"; - private static final String KEY_MISC_UI_THEME_SETTINGS = "UiThemeSettings"; private static final String KEY_MISC_USE_MOBILE_DATA = "UseMobileData"; private static final String KEY_MISC_USE_MOBILE_DATA_TIMESTAMP = "UseMobileDataTimestamp"; private static final String KEY_MISC_USE_MOBILE_DATA_ROAMING = "UseMobileDataRoaming"; @@ -218,47 +215,77 @@ public final class Config setBool(KEY_MISC_LOCATION_REQUESTED); } - @NonNull - public static String getCurrentUiTheme(@NonNull Context context) + public static class UiTheme { - // This is the actual map theme, only set to theme_default/night - final String defaultTheme = context.getString(R.string.theme_default); - final String res = getString(KEY_MISC_UI_THEME, defaultTheme); + private static final String KEY_UI_THEME = "UiTheme"; + private static final String KEY_UI_THEME_SETTINGS = "UiThemeSettings"; - if (ThemeUtils.isValidTheme(context, res)) - return res; + public static final String AUTO = "auto"; + public static final String NIGHT = "night"; + public static final String NAV_AUTO = "nav_auto"; + public static final String DEFAULT = "default"; - return defaultTheme; - } + public static boolean isAuto(@NonNull String theme) + { + return AUTO.equals(theme); + } - public static void setCurrentUiTheme(@NonNull Context context, @NonNull String theme) - { - if (getCurrentUiTheme(context).equals(theme)) - return; + public static boolean isNavAuto(@NonNull String theme) + { + return NAV_AUTO.equals(theme); + } - setString(KEY_MISC_UI_THEME, theme); - } + public static boolean isNight(@NonNull String theme) + { + return NIGHT.equals(theme); + } - @NonNull - public static String getUiThemeSettings(@NonNull Context context) - { - // This is the default theme *mode*, eg. auto/dark/nav_auto/light. - final String defaultSetting = context.getString(R.string.theme_nav_auto); - final String res = getString(KEY_MISC_UI_THEME_SETTINGS, defaultSetting); - if (ThemeUtils.isValidTheme(context, res) || ThemeUtils.isAutoTheme(context, res) - || ThemeUtils.isNavAutoTheme(context, res)) - return res; + public static boolean isDefault(@NonNull String theme) + { + return DEFAULT.equals(theme); + } - return defaultSetting; - } + @NonNull + public static String getCurrent() + { + final String res = getString(KEY_UI_THEME, DEFAULT); + if (isValid(res)) + return res; - public static boolean setUiThemeSettings(@NonNull Context context, String theme) - { - if (getUiThemeSettings(context).equals(theme)) - return false; + return DEFAULT; + } - setString(KEY_MISC_UI_THEME_SETTINGS, theme); - return true; + public static void setCurrent(@NonNull String theme) + { + if (getCurrent().equals(theme)) + return; + + setString(KEY_UI_THEME, theme); + } + + @NonNull + public static String getUiThemeSettings() + { + final String res = getString(KEY_UI_THEME_SETTINGS, DEFAULT); + if (isValid(res) || isAuto(res) || isNavAuto(res)) + return res; + + return DEFAULT; + } + + public static boolean setUiThemeSettings(String theme) + { + if (getUiThemeSettings().equals(theme)) + return false; + + setString(KEY_UI_THEME_SETTINGS, theme); + return true; + } + + private static boolean isValid(@NonNull String theme) + { + return DEFAULT.equals(theme) || NIGHT.equals(theme); + } } public static boolean isLargeFontsSize() diff --git a/android/app/src/main/java/app/organicmaps/search/CategoriesAdapter.java b/android/app/src/main/java/app/organicmaps/search/CategoriesAdapter.java index fe509cacb..658d6d53d 100644 --- a/android/app/src/main/java/app/organicmaps/search/CategoriesAdapter.java +++ b/android/app/src/main/java/app/organicmaps/search/CategoriesAdapter.java @@ -89,7 +89,7 @@ class CategoriesAdapter extends RecyclerView.Adapter { final String themeName = (String) newValue; - if (!Config.setUiThemeSettings(requireContext(), themeName)) + if (!Config.UiTheme.setUiThemeSettings(themeName)) return true; ThemeSwitcher.INSTANCE.restart(false); - ThemeMode mode = ThemeMode.getInstance(requireContext().getApplicationContext(), themeName); - CharSequence summary = pref.getEntries()[mode.ordinal()]; + final ThemeMode mode = ThemeMode.getInstance(themeName); + final CharSequence summary = pref.getEntries()[mode.ordinal()]; pref.setSummary(summary); return true; }); @@ -552,24 +550,25 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment implements La enum ThemeMode { - DEFAULT(R.string.theme_default), - NIGHT(R.string.theme_night), - AUTO(R.string.theme_auto), - NAV_AUTO(R.string.theme_nav_auto); + DEFAULT(Config.UiTheme.DEFAULT), + NIGHT(Config.UiTheme.NIGHT), + AUTO(Config.UiTheme.AUTO), + NAV_AUTO(Config.UiTheme.NAV_AUTO); - private final int mModeStringId; + @NonNull + private final String mMode; - ThemeMode(@StringRes int modeStringId) + ThemeMode(@NonNull String mode) { - mModeStringId = modeStringId; + mMode = mode; } @NonNull - public static ThemeMode getInstance(@NonNull Context context, @NonNull String src) + public static ThemeMode getInstance(@NonNull String src) { for (ThemeMode each : values()) { - if (context.getResources().getString(each.mModeStringId).equals(src)) + if (each.mMode.equals(src)) return each; } return AUTO; 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 14a40d9da..c82c0d5ed 100644 --- a/android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java +++ b/android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java @@ -8,7 +8,6 @@ import android.os.Build; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatDelegate; import app.organicmaps.MwmApplication; -import app.organicmaps.R; import app.organicmaps.downloader.DownloaderStatusIcon; import app.organicmaps.sdk.Framework; import app.organicmaps.sdk.MapStyle; @@ -28,17 +27,23 @@ public enum ThemeSwitcher @Override public void run() { - boolean isNavAuto = RoutingController.get().isNavigating() && ThemeUtils.isNavAutoTheme(mContext); + boolean navAuto = RoutingController.get().isNavigating() && ThemeUtils.isNavAutoTheme(); // Cancel old checker UiThread.cancelDelayedTasks(mAutoThemeChecker); - if (isNavAuto || ThemeUtils.isAutoTheme(mContext)) + String theme; + if (navAuto || ThemeUtils.isAutoTheme()) { UiThread.runLater(mAutoThemeChecker, CHECK_INTERVAL_MS); - setThemeAndMapStyle(calcAutoTheme()); + theme = calcAutoTheme(); } - else // Only reached when an auto mode should be light - setThemeAndMapStyle(mContext.getResources().getString(R.string.theme_default)); + else + { + // Happens when exiting the Navigation mode. Should restore the light. + theme = Config.UiTheme.DEFAULT; + } + + setThemeAndMapStyle(theme); } }; @@ -64,8 +69,8 @@ public enum ThemeSwitcher public void restart(boolean isRendererActive) { mRendererActive = isRendererActive; - String theme = Config.getUiThemeSettings(mContext); - if (ThemeUtils.isAutoTheme(mContext, theme) || ThemeUtils.isNavAutoTheme(mContext, theme)) + String theme = Config.UiTheme.getUiThemeSettings(); + if (ThemeUtils.isAutoTheme() || ThemeUtils.isNavAutoTheme()) { mAutoThemeChecker.run(); return; @@ -78,10 +83,10 @@ public enum ThemeSwitcher private void setThemeAndMapStyle(@NonNull String theme) { UiModeManager uiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE); - String oldTheme = Config.getCurrentUiTheme(mContext); + String oldTheme = Config.UiTheme.getCurrent(); MapStyle style; - if (ThemeUtils.isNightTheme(mContext, theme)) + if (ThemeUtils.isNightTheme()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) uiModeManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES); @@ -112,7 +117,7 @@ public enum ThemeSwitcher if (!theme.equals(oldTheme)) { - Config.setCurrentUiTheme(mContext, theme); + Config.UiTheme.setCurrent(theme); DownloaderStatusIcon.clearCache(); final Activity a = MwmApplication.from(mContext).getTopActivity(); @@ -152,9 +157,7 @@ public enum ThemeSwitcher @NonNull private String calcAutoTheme() { - String defaultTheme = mContext.getResources().getString(R.string.theme_default); - String nightTheme = mContext.getResources().getString(R.string.theme_night); - Location last = MwmApplication.from(mContext).getLocationHelper().getSavedLocation(); + final Location last = MwmApplication.from(mContext).getLocationHelper().getSavedLocation(); boolean day; if (last != null) @@ -168,6 +171,6 @@ public enum ThemeSwitcher day = (currentHour < 18 && currentHour > 6); } - return (day ? defaultTheme : nightTheme); + return (day ? Config.UiTheme.DEFAULT : Config.UiTheme.NIGHT); } } diff --git a/android/app/src/main/java/app/organicmaps/util/ThemeUtils.java b/android/app/src/main/java/app/organicmaps/util/ThemeUtils.java index bd866ca0c..8f44e5654 100644 --- a/android/app/src/main/java/app/organicmaps/util/ThemeUtils.java +++ b/android/app/src/main/java/app/organicmaps/util/ThemeUtils.java @@ -16,7 +16,8 @@ public final class ThemeUtils private ThemeUtils() {} - public static @ColorInt int getColor(@NonNull Context context, @AttrRes int attr) + @ColorInt + public static int getColor(@NonNull Context context, @AttrRes int attr) { if (!context.getTheme().resolveAttribute(attr, VALUE_BUFFER, true)) throw new IllegalArgumentException("Failed to resolve color theme attribute"); @@ -44,76 +45,45 @@ public final class ThemeUtils return VALUE_BUFFER.resourceId; } - public static boolean isDefaultTheme(@NonNull Context context) + public static boolean isDefaultTheme() { - return isDefaultTheme(context, Config.getCurrentUiTheme(context)); + return Config.UiTheme.isDefault(Config.UiTheme.getCurrent()); } - public static boolean isDefaultTheme(@NonNull Context context, String theme) + public static boolean isNightTheme() { - String defaultTheme = context.getString(R.string.theme_default); - return defaultTheme.equals(theme); + return Config.UiTheme.isNight(Config.UiTheme.getCurrent()); } - public static boolean isNightTheme(@NonNull Context context) + public static boolean isAutoTheme() { - return isNightTheme(context, Config.getCurrentUiTheme(context)); + return Config.UiTheme.isAuto(Config.UiTheme.getUiThemeSettings()); } - public static boolean isNightTheme(@NonNull Context context, String theme) + public static boolean isNavAutoTheme() { - String nightTheme = context.getString(R.string.theme_night); - return nightTheme.equals(theme); - } - - public static boolean isAutoTheme(@NonNull Context context) - { - return isAutoTheme(context, Config.getUiThemeSettings(context)); - } - - public static boolean isAutoTheme(@NonNull Context context, String theme) - { - String autoTheme = context.getString(R.string.theme_auto); - return autoTheme.equals(theme); - } - - public static boolean isNavAutoTheme(@NonNull Context context) - { - return isNavAutoTheme(context, Config.getUiThemeSettings(context)); - } - - public static boolean isNavAutoTheme(@NonNull Context context, String theme) - { - String navAutoTheme = context.getString(R.string.theme_nav_auto); - return navAutoTheme.equals(theme); - } - - public static boolean isValidTheme(@NonNull Context context, String theme) - { - String defaultTheme = context.getString(R.string.theme_default); - String nightTheme = context.getString(R.string.theme_night); - return (defaultTheme.equals(theme) || nightTheme.equals(theme)); + return Config.UiTheme.isNavAuto(Config.UiTheme.getUiThemeSettings()); } @StyleRes - public static int getCardBgThemeResourceId(@NonNull Context context, @NonNull String theme) + public static int getCardBgThemeResourceId(@NonNull String theme) { - if (isDefaultTheme(context, theme)) + if (Config.UiTheme.isDefault(theme)) return R.style.MwmTheme_CardBg; - if (isNightTheme(context, theme)) + if (Config.UiTheme.isNight(theme)) return R.style.MwmTheme_Night_CardBg; throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme); } @StyleRes - public static int getWindowBgThemeResourceId(@NonNull Context context, @NonNull String theme) + public static int getWindowBgThemeResourceId(@NonNull String theme) { - if (isDefaultTheme(context, theme)) + if (Config.UiTheme.isDefault(theme)) return R.style.MwmTheme_WindowBg; - if (isNightTheme(context, theme)) + if (Config.UiTheme.isNight(theme)) return R.style.MwmTheme_Night_WindowBg; throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme); diff --git a/android/app/src/main/java/app/organicmaps/widget/RoutingToolbarButton.java b/android/app/src/main/java/app/organicmaps/widget/RoutingToolbarButton.java index c98423a74..e51e84105 100644 --- a/android/app/src/main/java/app/organicmaps/widget/RoutingToolbarButton.java +++ b/android/app/src/main/java/app/organicmaps/widget/RoutingToolbarButton.java @@ -35,10 +35,9 @@ public class RoutingToolbarButton extends AppCompatRadioButton private void initView() { - setBackgroundResource(ThemeUtils.isNightTheme(getContext()) ? R.drawable.routing_toolbar_button_night - : R.drawable.routing_toolbar_button); - setButtonTintList(ThemeUtils.isNightTheme(getContext()) ? R.color.routing_toolbar_icon_tint_night - : R.color.routing_toolbar_icon_tint); + final boolean isNightTheme = ThemeUtils.isNightTheme(); + setBackgroundResource(isNightTheme ? R.drawable.routing_toolbar_button_night : R.drawable.routing_toolbar_button); + setButtonTintList(isNightTheme ? R.color.routing_toolbar_icon_tint_night : R.color.routing_toolbar_icon_tint); } public void progress() diff --git a/android/app/src/main/res/values/donottranslate.xml b/android/app/src/main/res/values/donottranslate.xml index bf33acb4a..b6c52881a 100644 --- a/android/app/src/main/res/values/donottranslate.xml +++ b/android/app/src/main/res/values/donottranslate.xml @@ -48,12 +48,6 @@ The amenity has gone or never existed. This is an auto-generated note from CoMaps application: a user reports a POI that is visible on a map (which can be a month old), but cannot be found on the ground. - - default - night - auto - nav-auto - collapse height limited diff --git a/android/app/src/main/res/values/string-arrays.xml b/android/app/src/main/res/values/string-arrays.xml index c8678622b..9af6c10d3 100644 --- a/android/app/src/main/res/values/string-arrays.xml +++ b/android/app/src/main/res/values/string-arrays.xml @@ -53,14 +53,6 @@ @string/nav_auto - - @string/theme_default - @string/theme_night - @string/theme_auto - @string/theme_nav_auto - - @string/pref_tts_speedcams_auto @string/pref_tts_speedcams_always diff --git a/android/app/src/main/res/xml/prefs_main.xml b/android/app/src/main/res/xml/prefs_main.xml index 81d1e9a79..32964f38b 100644 --- a/android/app/src/main/res/xml/prefs_main.xml +++ b/android/app/src/main/res/xml/prefs_main.xml @@ -130,7 +130,6 @@ android:title="@string/pref_map_style_title" app:singleLineTitle="false" android:entries="@array/map_style" - android:entryValues="@array/map_style_values" android:order="1"/>