Skip to content

Commit

Permalink
#106: Default Time zone in app doesn't change, when Android System Ti…
Browse files Browse the repository at this point in the history
…me Zone changes
  • Loading branch information
yvolk committed Dec 31, 2023
1 parent 5a5d160 commit dda4864
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -140,7 +140,7 @@ public void testPeriodicAlarmTimeDuringTimeGap() {
54 + 1 + periodMinutes - 60, zone);
assertEquals(expWinnipeg, exactMinutesPlusMinutes(nowWinnipeg, periodMinutes));
} finally {
DateTimeZone.setDefault(defaultZone);
MyClock.setDefaultTimeZone(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static void registerReceivers(Map<Integer, InstanceSettings> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/java/org/andstatus/todoagenda/util/MyClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* A clock, the can be changed independently from a Device clock
*
* @author [email protected]
*/
public class MyClock {
Expand All @@ -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) {
Expand All @@ -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();
}
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit dda4864

Please sign in to comment.