[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 <me@hbond.xyz>
This commit is contained in:
Harry Bond
2025-03-10 18:27:47 +00:00
committed by Konstantin Pastbin
parent e2718ecbc6
commit 38802dd29a

View File

@@ -8,6 +8,9 @@ import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import java.util.Calendar;
import app.organicmaps.Framework; import app.organicmaps.Framework;
import app.organicmaps.MwmApplication; import app.organicmaps.MwmApplication;
import app.organicmaps.R; import app.organicmaps.R;
@@ -29,30 +32,15 @@ public enum ThemeSwitcher
@Override @Override
public void run() 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); boolean navAuto = RoutingController.get().isNavigating() && ThemeUtils.isNavAutoTheme(mContext);
// Cancel old checker
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);
UiThread.cancelDelayedTasks(mAutoThemeChecker); UiThread.cancelDelayedTasks(mAutoThemeChecker);
if (navAuto || ThemeUtils.isAutoTheme(mContext)) if (navAuto || ThemeUtils.isAutoTheme(mContext))
{
UiThread.runLater(mAutoThemeChecker, CHECK_INTERVAL_MS); UiThread.runLater(mAutoThemeChecker, CHECK_INTERVAL_MS);
setThemeAndMapStyle(calcAutoTheme());
}
} }
}; };
@@ -159,4 +147,30 @@ public enum ThemeSwitcher
else else
Framework.nativeMarkMapStyle(style); 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);
}
} }