8
8
9
9
import androidx .annotation .NonNull ;
10
10
import androidx .appcompat .app .AppCompatDelegate ;
11
+
12
+ import java .util .Calendar ;
13
+
11
14
import app .organicmaps .Framework ;
12
15
import app .organicmaps .MwmApplication ;
13
16
import app .organicmaps .R ;
@@ -30,30 +33,15 @@ public enum ThemeSwitcher
30
33
@ Override
31
34
public void run ()
32
35
{
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
-
38
36
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
53
38
UiThread .cancelDelayedTasks (mAutoThemeChecker );
54
39
55
40
if (navAuto || ThemeUtils .isAutoTheme (mContext ))
41
+ {
56
42
UiThread .runLater (mAutoThemeChecker , CHECK_INTERVAL_MS );
43
+ setThemeAndMapStyle (calcAutoTheme ());
44
+ }
57
45
}
58
46
};
59
47
@@ -157,4 +145,30 @@ private void SetMapStyle(MapStyle style)
157
145
else
158
146
MapStyle .mark (style );
159
147
}
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
+ }
160
174
}
0 commit comments