fix: the logic for isOutsideNotificationTimes

It was reversed before.
This commit is contained in:
Daisy 2025-01-10 15:05:15 +11:00 committed by José Rebelo
parent c2710c32ab
commit 624dc77bce
3 changed files with 83 additions and 9 deletions

View File

@ -127,7 +127,7 @@ public class GBApplication extends Application {
private static SharedPreferences sharedPrefs;
private static final String PREFS_VERSION = "shared_preferences_version";
//if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version
private static final int CURRENT_PREFS_VERSION = 47;
private static final int CURRENT_PREFS_VERSION = 48;
private static final LimitedQueue<Integer, String> mIDSenderLookup = new LimitedQueue<>(16);
private static GBPrefs prefs;
@ -1946,6 +1946,16 @@ public class GBApplication extends Application {
}
}
if (oldVersion < 48) {
// Fix the reversed notification time prefs
if (prefs.getNotificationTimesEnabled()) {
final String start = prefs.getString("notification_times_start", "08:00");
final String end = prefs.getString("notification_times_end", "22:00");
editor.putString("notification_times_start", end);
editor.putString("notification_times_end", start);
}
}
editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION));
editor.apply();
}

View File

@ -543,6 +543,16 @@ public class NotificationListener extends NotificationListenerService {
GBApplication.deviceService().onNotification(notificationSpec);
}
static boolean isOutsideNotificationTimes(final LocalTime now, final LocalTime start, final LocalTime end) {
if (start.isBefore(end)) {
// eg. 06:00 -> 22:00
return now.isBefore(start) || now.isAfter(end);
} else {
// goes past midnight, eg. 22:00 -> 06:00
return now.isBefore(start) && now.isAfter(end);
}
}
private static boolean isOutsideNotificationTimes(final GBPrefs prefs) {
if (!prefs.getNotificationTimesEnabled()) {
return false;
@ -551,14 +561,7 @@ public class NotificationListener extends NotificationListenerService {
final LocalTime now = LocalTime.now();
final LocalTime start = prefs.getNotificationTimesStart();
final LocalTime end = prefs.getNotificationTimesEnd();
final boolean shouldIgnore;
if (start.isBefore(end)) {
// eg. 06:00 -> 22:00
shouldIgnore = now.isAfter(start) && now.isBefore(end);
} else {
// goes past midnight, eg. 22:00 -> 06:00
shouldIgnore = now.isAfter(start) || now.isBefore(end);
}
final boolean shouldIgnore = isOutsideNotificationTimes(now, start, end);
if (shouldIgnore) {
LOG.debug("Ignoring notification outside of notification times {}/{}", start, end);

View File

@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents;
import org.junit.Test;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
@ -9,6 +10,7 @@ import nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivit
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilter;
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -108,4 +110,63 @@ public class NotificationListenerTest extends TestBase {
filter.setNotificationFilterMode(NotificationFilterActivity.NOTIFICATION_FILTER_MODE_NONE);
assertTrue(mNotificationListener.shouldContinueAfterFilter(body, wordList, filter));
}
@Test
public void isOutsideNotificationTimes_samedayWindow_tooEarly_MustReturnTrue() {
assertTrue(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(6, 0),
/* start= */ LocalTime.of(7, 0),
/* end= */ LocalTime.of(20, 0)
));
}
@Test
public void isOutsideNotificationTimes_samedayWindow_withinWindow_MustReturnFalse() {
assertFalse(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(10, 0),
/* start= */ LocalTime.of(7, 0),
/* end= */ LocalTime.of(20, 0)
));
}
@Test
public void isOutsideNotificationTimes_samedayWindow_tooLate_MustReturnTrue() {
assertTrue(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(21, 0),
/* start= */ LocalTime.of(7, 0),
/* end= */ LocalTime.of(20, 0)
));
}
@Test
public void isOutsideNotificationTimes_crossMidnightWindow_tooEarly_MustReturnTrue() {
assertTrue(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(18, 0),
/* start= */ LocalTime.of(20, 0),
/* end= */ LocalTime.of(7, 0)
));
}
@Test
public void isOutsideNotificationTimes_crossMidnightWindow_withinWindow_MustReturnFalse() {
assertFalse(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(21, 0),
/* start= */ LocalTime.of(20, 0),
/* end= */ LocalTime.of(7, 0)
));
assertFalse(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(6, 0),
/* start= */ LocalTime.of(20, 0),
/* end= */ LocalTime.of(7, 0)
));
}
@Test
public void isOutsideNotificationTimes_crossMidnightWindow_tooLate_MustReturnTrue() {
assertTrue(NotificationListener.isOutsideNotificationTimes(
/* now= */ LocalTime.of(8, 0),
/* start= */ LocalTime.of(20, 0),
/* end= */ LocalTime.of(7, 0)
));
}
}