diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java index b725c4b7..e1d4066a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java @@ -39,21 +39,24 @@ public class AlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { LOG.info("will resend sunrise and sunset events"); - GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, 1); - GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, 2); - GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, 1); - GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, 2); + final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(); + dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1); + + /* + * rotate ids ud reuse the id from two days ago for tomorrow, this way we will have + * sunrise /sunset for 3 days while sending only sunrise/sunset per day + */ + byte id_tomorrow = (byte) ((dateTimeTomorrow.getTimeInMillis() / (1000L * 60L * 60L * 24L)) % 3); + + GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, id_tomorrow); + GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, id_tomorrow); Prefs prefs = GBApplication.getPrefs(); float latitude = prefs.getFloat("location_latitude", 0); float longitude = prefs.getFloat("location_longitude", 0); - final GregorianCalendar dateTimeToday = new GregorianCalendar(); - final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(); - dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1); - GregorianCalendar[] sunriseTransitSetToday = SPA.calculateSunriseTransitSet(dateTimeToday, latitude, longitude, DeltaT.estimate(dateTimeToday)); - GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeToday)); + GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeTomorrow)); CalendarEventSpec calendarEventSpec = new CalendarEventSpec(); calendarEventSpec.durationInSeconds = 0; @@ -61,26 +64,16 @@ public class AlarmReceiver extends BroadcastReceiver { calendarEventSpec.type = CalendarEventSpec.TYPE_SUNRISE; calendarEventSpec.title = "Sunrise"; - if (sunriseTransitSetToday[0] != null) { - calendarEventSpec.id = 1; - calendarEventSpec.timestamp = (int) (sunriseTransitSetToday[0].getTimeInMillis() / 1000); - GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec); - } if (sunriseTransitSetTomorrow[0] != null) { - calendarEventSpec.id = 2; + calendarEventSpec.id = id_tomorrow; calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[0].getTimeInMillis() / 1000); GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec); } calendarEventSpec.type = CalendarEventSpec.TYPE_SUNSET; calendarEventSpec.title = "Sunset"; - if (sunriseTransitSetToday[2] != null) { - calendarEventSpec.id = 1; - calendarEventSpec.timestamp = (int) (sunriseTransitSetToday[2].getTimeInMillis() / 1000); - GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec); - } if (sunriseTransitSetTomorrow[2] != null) { - calendarEventSpec.id = 2; + calendarEventSpec.id = id_tomorrow; calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[2].getTimeInMillis() / 1000); GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec); }