mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-19 04:53:36 +00:00
[android][sdk] Fix ThemeUtils usage in sdk
Signed-off-by: Andrei Shkrob <github@shkrob.dev>
This commit is contained in:
committed by
Konstantin Pastbin
parent
886d569895
commit
447266c328
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -89,7 +89,7 @@ class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ViewHolde
|
||||
@DrawableRes
|
||||
private static int getDrawableResIdByKey(@NonNull Context context, @NonNull String packageName, @NonNull String key)
|
||||
{
|
||||
final boolean isNightTheme = ThemeUtils.isNightTheme(context);
|
||||
final boolean isNightTheme = ThemeUtils.isNightTheme();
|
||||
String iconId = "ic_" + key;
|
||||
if (isNightTheme)
|
||||
iconId = iconId + "_night";
|
||||
|
||||
@@ -37,7 +37,7 @@ public class SearchActivity extends BaseMwmFragmentActivity
|
||||
@StyleRes
|
||||
public int getThemeResourceId(@NonNull String theme)
|
||||
{
|
||||
return ThemeUtils.getCardBgThemeResourceId(getApplicationContext(), theme);
|
||||
return ThemeUtils.getCardBgThemeResourceId(theme);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -47,7 +47,7 @@ abstract class BaseXmlSettingsFragment extends PreferenceFragmentCompat
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
int color;
|
||||
if (ThemeUtils.isDefaultTheme(requireContext()))
|
||||
if (ThemeUtils.isDefaultTheme())
|
||||
color = ContextCompat.getColor(requireContext(), R.color.bg_cards);
|
||||
else
|
||||
color = ContextCompat.getColor(requireContext(), R.color.bg_cards_night);
|
||||
|
||||
@@ -3,13 +3,11 @@ package app.organicmaps.settings;
|
||||
import static app.organicmaps.leftbutton.LeftButtonsHolder.DISABLE_BUTTON_CODE;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
@@ -441,19 +439,19 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment implements La
|
||||
private void initMapStylePrefsCallbacks()
|
||||
{
|
||||
final ListPreference pref = getPreference(getString(R.string.pref_map_style));
|
||||
|
||||
String curTheme = Config.getUiThemeSettings(requireContext());
|
||||
pref.setValue(curTheme);
|
||||
pref.setEntryValues(new CharSequence[] {Config.UiTheme.DEFAULT, Config.UiTheme.NIGHT, Config.UiTheme.AUTO,
|
||||
Config.UiTheme.NAV_AUTO});
|
||||
pref.setValue(Config.UiTheme.getUiThemeSettings());
|
||||
pref.setSummary(pref.getEntry());
|
||||
pref.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -48,12 +48,6 @@
|
||||
<!-- "Report a problem" predefined texts -->
|
||||
<string name="problem_does_not_exist" translatable="false">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.</string>
|
||||
|
||||
<!-- UI themes -->
|
||||
<string name="theme_default" translatable="false">default</string>
|
||||
<string name="theme_night" translatable="false">night</string>
|
||||
<string name="theme_auto" translatable="false">auto</string>
|
||||
<string name="theme_nav_auto" translatable="false">nav-auto</string>
|
||||
|
||||
<!-- Tags -->
|
||||
<string name="tag_menu_collapse" translatable="false">collapse</string>
|
||||
<string name="tag_height_limited" translatable="false">height limited</string>
|
||||
|
||||
@@ -53,14 +53,6 @@
|
||||
<item>@string/nav_auto</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="map_style_values"
|
||||
translatable="false">
|
||||
<item>@string/theme_default</item>
|
||||
<item>@string/theme_night</item>
|
||||
<item>@string/theme_auto</item>
|
||||
<item>@string/theme_nav_auto</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="speed_cameras">
|
||||
<item>@string/pref_tts_speedcams_auto</item>
|
||||
<item>@string/pref_tts_speedcams_always</item>
|
||||
|
||||
@@ -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"/>
|
||||
<SwitchPreferenceCompat
|
||||
android:key="@string/pref_3d"
|
||||
|
||||
Reference in New Issue
Block a user