From dda4864b83b8fbc83593db7ca1e811e06799b02f Mon Sep 17 00:00:00 2001 From: Yuri Volkov Date: Sun, 31 Dec 2023 18:02:51 +0300 Subject: [PATCH] #106: Default Time zone in app doesn't change, when Android System Time Zone changes --- ...galInstantDueToTimeZoneTransitionTest.java | 6 ++--- .../EnvironmentChangedReceiver.java | 2 ++ .../todoagenda/RemoteViewsFactory.java | 1 + .../todoagenda/prefs/InstanceSettings.java | 2 +- .../andstatus/todoagenda/util/MyClock.java | 23 ++++++++++++------- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/androidTest/java/org/andstatus/todoagenda/IllegalInstantDueToTimeZoneTransitionTest.java b/app/src/androidTest/java/org/andstatus/todoagenda/IllegalInstantDueToTimeZoneTransitionTest.java index 8fd367c5..179b1106 100644 --- a/app/src/androidTest/java/org/andstatus/todoagenda/IllegalInstantDueToTimeZoneTransitionTest.java +++ b/app/src/androidTest/java/org/andstatus/todoagenda/IllegalInstantDueToTimeZoneTransitionTest.java @@ -5,6 +5,7 @@ import org.andstatus.todoagenda.calendar.CalendarEvent; import org.andstatus.todoagenda.prefs.OrderedEventSource; import org.andstatus.todoagenda.provider.QueryRow; +import org.andstatus.todoagenda.util.MyClock; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.LocalDateTime; @@ -112,10 +113,9 @@ private long toMillis(String iso8601time) { /** https://github.com/andstatus/todoagenda/issues/13 */ @Test public void testPeriodicAlarmTimeDuringTimeGap() { - DateTimeZone defaultZone = DateTimeZone.getDefault(); try { DateTimeZone zone = DateTimeZone.forID("America/Winnipeg"); - DateTimeZone.setDefault(zone); + MyClock.setDefaultTimeZone(zone); int periodMinutes = 10; DateTime nowUtc = new DateTime(2020, 3, 8, 2, 15, @@ -140,7 +140,7 @@ public void testPeriodicAlarmTimeDuringTimeGap() { 54 + 1 + periodMinutes - 60, zone); assertEquals(expWinnipeg, exactMinutesPlusMinutes(nowWinnipeg, periodMinutes)); } finally { - DateTimeZone.setDefault(defaultZone); + MyClock.setDefaultTimeZone(null); } } } diff --git a/app/src/main/java/org/andstatus/todoagenda/EnvironmentChangedReceiver.java b/app/src/main/java/org/andstatus/todoagenda/EnvironmentChangedReceiver.java index c353fe48..c812645f 100644 --- a/app/src/main/java/org/andstatus/todoagenda/EnvironmentChangedReceiver.java +++ b/app/src/main/java/org/andstatus/todoagenda/EnvironmentChangedReceiver.java @@ -47,6 +47,8 @@ public static void registerReceivers(Map instances) { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); filter.addAction(Intent.ACTION_DREAMING_STOPPED); + filter.addAction(Intent.ACTION_TIME_CHANGED); + filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); context.registerReceiver(receiver, filter); EnvironmentChangedReceiver oldReceiver = registeredReceiver.getAndSet(receiver); diff --git a/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java b/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java index 46a17421..439b4eb4 100644 --- a/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java +++ b/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java @@ -183,6 +183,7 @@ static void updateWidget(Context context, int widgetId) { InstanceSettings settings = AllSettings.instanceFromId(context, widgetId); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_initial); + settings.clock().updateZone(); configureWidgetHeader(settings, rv); configureWidgetEntriesList(settings, rv); diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java b/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java index 260f637e..362cc00a 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java @@ -155,7 +155,7 @@ public class InstanceSettings { static final String PREF_LOCK_TIME_ZONE = "lockTimeZone"; static final String PREF_LOCKED_TIME_ZONE_ID = "lockedTimeZoneId"; - private volatile MyClock clock = new MyClock(); + private final MyClock clock = new MyClock(); static final String PREF_SNAPSHOT_MODE = "snapshotMode"; final static String PREF_REFRESH_PERIOD_MINUTES = "refreshPeriodMinutes"; diff --git a/app/src/main/java/org/andstatus/todoagenda/util/MyClock.java b/app/src/main/java/org/andstatus/todoagenda/util/MyClock.java index d710f8f4..ad126ef7 100644 --- a/app/src/main/java/org/andstatus/todoagenda/util/MyClock.java +++ b/app/src/main/java/org/andstatus/todoagenda/util/MyClock.java @@ -11,6 +11,7 @@ /** * A clock, the can be changed independently from a Device clock + * * @author yvolk@yurivolkov.com */ public class MyClock { @@ -21,10 +22,16 @@ public class MyClock { private volatile DateTime snapshotDate = null; private volatile DateTime snapshotDateSetAt = null; private volatile String lockedTimeZoneId = ""; - private volatile DateTimeZone zone; + private static volatile DateTimeZone defaultTimeZone = null; + private volatile DateTimeZone zone = getDefaultTimeZone(); - public MyClock() { - zone = DateTimeZone.getDefault(); + + public static void setDefaultTimeZone(DateTimeZone zone) { + defaultTimeZone = zone; + } + + public static DateTimeZone getDefaultTimeZone() { + return defaultTimeZone != null ? defaultTimeZone : DateTimeZone.forTimeZone(java.util.TimeZone.getDefault()); } public void setSnapshotMode(SnapshotMode snapshotModeIn, InstanceSettings settings) { @@ -47,13 +54,13 @@ public void setLockedTimeZoneId(String timeZoneId) { updateZone(); } - private void updateZone() { + public void updateZone() { if (snapshotMode == SnapshotMode.SNAPSHOT_TIME && snapshotDate != null) { zone = snapshotDate.getZone(); } else if (StringUtil.nonEmpty(lockedTimeZoneId)) { zone = DateTimeZone.forID(lockedTimeZoneId); } else { - zone = DateTimeZone.getDefault(); + zone = getDefaultTimeZone(); } } @@ -76,8 +83,8 @@ public DateTime now(DateTimeZone zone) { DateTime snapshotDate = this.snapshotDate; if (getSnapshotMode() == SnapshotMode.SNAPSHOT_TIME && snapshotDate != null) { return PermissionsUtil.isTestMode() - ? getTimeMachineDate(zone) - : snapshotDate.withZone(zone); + ? getTimeMachineDate(zone) + : snapshotDate.withZone(zone); } else { return DateTime.now(zone); } @@ -132,7 +139,7 @@ public int getNumberOfDaysTo(DateTime date) { public int getNumberOfMinutesTo(DateTime date) { return Minutes.minutesBetween(now(date.getZone()), date) - .getMinutes(); + .getMinutes(); } public DateTime startOfTomorrow() {