Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 30eb98a

Browse filesBrowse files
hb0ndbiodranik
authored andcommitted
[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>
1 parent 01314b6 commit 30eb98a
Copy full SHA for 30eb98a

File tree

1 file changed

+33
-19
lines changed
Filter options

1 file changed

+33
-19
lines changed

‎android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java

Copy file name to clipboardExpand all lines: android/app/src/main/java/app/organicmaps/util/ThemeSwitcher.java
+33-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import androidx.annotation.NonNull;
1010
import androidx.appcompat.app.AppCompatDelegate;
11+
12+
import java.util.Calendar;
13+
1114
import app.organicmaps.Framework;
1215
import app.organicmaps.MwmApplication;
1316
import app.organicmaps.R;
@@ -30,30 +33,15 @@ public enum ThemeSwitcher
3033
@Override
3134
public void run()
3235
{
33-
String nightTheme = MwmApplication.from(mContext).getString(R.string.theme_night);
34-
String defaultTheme = MwmApplication.from(mContext).getString(R.string.theme_default);
35-
String theme = defaultTheme;
36-
Location last = LocationHelper.from(mContext).getSavedLocation();
37-
3836
boolean navAuto = RoutingController.get().isNavigating() && ThemeUtils.isNavAutoTheme(mContext);
39-
40-
if (navAuto || ThemeUtils.isAutoTheme(mContext))
41-
{
42-
if (last == null)
43-
theme = Config.getCurrentUiTheme(mContext);
44-
else
45-
{
46-
long currentTime = System.currentTimeMillis() / 1000;
47-
boolean day = Framework.nativeIsDayTime(currentTime, last.getLatitude(), last.getLongitude());
48-
theme = (day ? defaultTheme : nightTheme);
49-
}
50-
}
51-
52-
setThemeAndMapStyle(theme);
37+
// Cancel old checker
5338
UiThread.cancelDelayedTasks(mAutoThemeChecker);
5439

5540
if (navAuto || ThemeUtils.isAutoTheme(mContext))
41+
{
5642
UiThread.runLater(mAutoThemeChecker, CHECK_INTERVAL_MS);
43+
setThemeAndMapStyle(calcAutoTheme());
44+
}
5745
}
5846
};
5947

@@ -157,4 +145,30 @@ private void SetMapStyle(MapStyle style)
157145
else
158146
MapStyle.mark(style);
159147
}
148+
149+
/**
150+
* Determine light/dark theme based on time and location,
151+
* or fall back to time-based (06:00-18:00) when there's no location fix
152+
* @return theme_light/dark string
153+
*/
154+
private String calcAutoTheme()
155+
{
156+
String defaultTheme = mContext.getResources().getString(R.string.theme_default);
157+
String nightTheme = mContext.getResources().getString(R.string.theme_night);
158+
Location last = LocationHelper.from(mContext).getSavedLocation();
159+
boolean day;
160+
161+
if (last != null)
162+
{
163+
long currentTime = System.currentTimeMillis() / 1000;
164+
day = Framework.nativeIsDayTime(currentTime, last.getLatitude(), last.getLongitude());
165+
}
166+
else
167+
{
168+
int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
169+
day = (currentHour < 18 && currentHour > 6);
170+
}
171+
172+
return (day ? defaultTheme : nightTheme);
173+
}
160174
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.