Include Organizer and Reminders when reading calendar events

Also use the named column indexes instead of numeric ids when retrieving the contents to make it more clear and more robust in case further fields are added later.

Reminders are set as absolute timestamp.
This commit is contained in:
Daniele Gobbetti 2024-04-25 11:46:34 +02:00
parent 2190c82ed7
commit 173e2d29b0
3 changed files with 82 additions and 30 deletions

View File

@ -16,21 +16,24 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.util.calendar; package nodomain.freeyourgadget.gadgetbridge.util.calendar;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class CalendarEvent { public class CalendarEvent {
private long begin; private final long begin;
private long end; private final long end;
private long id; private final long id;
private String title; private final String title;
private String description; private final String description;
private String location; private final String location;
private String calName; private final String calName;
private String calAccountName; private final String calAccountName;
private int color; private final String organizer;
private boolean allDay; private final int color;
private final boolean allDay;
private List<Long> remindersAbsoluteTs;
public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName, String calAccountName, int color, boolean allDay) { public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName, String calAccountName, int color, boolean allDay, String organizer) {
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
this.id = id; this.id = id;
@ -41,6 +44,15 @@ public class CalendarEvent {
this.calAccountName = calAccountName; this.calAccountName = calAccountName;
this.color = color; this.color = color;
this.allDay = allDay; this.allDay = allDay;
this.organizer = organizer;
}
public List<Long> getRemindersAbsoluteTs() {
return remindersAbsoluteTs;
}
public void setRemindersAbsoluteTs(List<Long> remindersAbsoluteTs) {
this.remindersAbsoluteTs = remindersAbsoluteTs;
} }
public long getBegin() { public long getBegin() {
@ -76,6 +88,10 @@ public class CalendarEvent {
return title; return title;
} }
public String getOrganizer() {
return organizer;
}
public String getDescription() { public String getDescription() {
return description; return description;
} }
@ -117,7 +133,9 @@ public class CalendarEvent {
Objects.equals(this.getCalName(), e.getCalName()) && Objects.equals(this.getCalName(), e.getCalName()) &&
Objects.equals(this.getCalAccountName(), e.getCalAccountName()) && Objects.equals(this.getCalAccountName(), e.getCalAccountName()) &&
(this.getColor() == e.getColor()) && (this.getColor() == e.getColor()) &&
(this.isAllDay() == e.isAllDay()); (this.isAllDay() == e.isAllDay()) &&
Objects.equals(this.getOrganizer(), e.getOrganizer()) &&
Objects.equals(this.getRemindersAbsoluteTs(), e.getRemindersAbsoluteTs());
} else { } else {
return false; return false;
} }
@ -135,6 +153,8 @@ public class CalendarEvent {
result = 31 * result + Objects.hash(calAccountName); result = 31 * result + Objects.hash(calAccountName);
result = 31 * result + Integer.valueOf(color).hashCode(); result = 31 * result + Integer.valueOf(color).hashCode();
result = 31 * result + Boolean.valueOf(allDay).hashCode(); result = 31 * result + Boolean.valueOf(allDay).hashCode();
result = 31 * result + Objects.hash(organizer);
result = 31 * result + Objects.hash(remindersAbsoluteTs);
return result; return result;
} }
} }

View File

@ -36,7 +36,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
@ -60,10 +59,12 @@ public class CalendarManager {
Instances.TITLE, Instances.TITLE,
Instances.DESCRIPTION, Instances.DESCRIPTION,
Instances.EVENT_LOCATION, Instances.EVENT_LOCATION,
Instances.ORGANIZER,
Instances.CALENDAR_DISPLAY_NAME, Instances.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.ACCOUNT_NAME,
Instances.CALENDAR_COLOR, Instances.CALENDAR_COLOR,
Instances.ALL_DAY Instances.ALL_DAY,
Instances.EVENT_ID //needed for reminders
}; };
private static final int lookahead_days = 7; private static final int lookahead_days = 7;
@ -98,26 +99,54 @@ public class CalendarManager {
return calendarEventList; return calendarEventList;
} }
while (evtCursor.moveToNext()) { while (evtCursor.moveToNext()) {
long start = evtCursor.getLong(1); long start = evtCursor.getLong(evtCursor.getColumnIndexOrThrow(Instances.BEGIN));
long end = evtCursor.getLong(2); long end = evtCursor.getLong(evtCursor.getColumnIndexOrThrow(Instances.END));
if (end == 0) { if (end == 0) {
LOG.info("no end time, will parse duration string"); LOG.info("no end time, will parse duration string");
Time time = new Time(); //FIXME: deprecated FTW Time time = new Time(); //FIXME: deprecated FTW
time.parse(evtCursor.getString(3)); time.parse(evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.DURATION)));
end = start + time.toMillis(false); end = start + time.toMillis(false);
} }
CalendarEvent calEvent = new CalendarEvent( CalendarEvent calEvent = new CalendarEvent(
start, start,
end, end,
evtCursor.getLong(0), evtCursor.getLong(evtCursor.getColumnIndexOrThrow(Instances._ID)),
evtCursor.getString(4), evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.TITLE)),
evtCursor.getString(5), evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.DESCRIPTION)),
evtCursor.getString(6), evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.EVENT_LOCATION)),
evtCursor.getString(7), evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.CALENDAR_DISPLAY_NAME)),
evtCursor.getString(8), evtCursor.getString(evtCursor.getColumnIndexOrThrow(CalendarContract.Calendars.ACCOUNT_NAME)),
evtCursor.getInt(9), evtCursor.getInt(evtCursor.getColumnIndexOrThrow(Instances.CALENDAR_COLOR)),
!evtCursor.getString(10).equals("0") !evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.ALL_DAY)).equals("0"),
evtCursor.getString(evtCursor.getColumnIndexOrThrow(Instances.ORGANIZER))
); );
// Query reminders for this event
final Cursor reminderCursor = mContext.getContentResolver().query(
CalendarContract.Reminders.CONTENT_URI,
null,
CalendarContract.Reminders.EVENT_ID + " = ?",
new String[]{String.valueOf(evtCursor.getLong(evtCursor.getColumnIndexOrThrow(Instances.EVENT_ID)))},
null
);
if (reminderCursor != null && reminderCursor.getCount() > 0) {
final List<Long> reminders = new ArrayList<>();
while (reminderCursor.moveToNext()) {
int minutes = reminderCursor.getInt(reminderCursor.getColumnIndexOrThrow(CalendarContract.Reminders.MINUTES));
int method = reminderCursor.getInt(reminderCursor.getColumnIndexOrThrow(CalendarContract.Reminders.METHOD));
LOG.debug("Reminder Method: {}, Minutes: {}", method, minutes);
if (method == 1) //METHOD_ALERT
reminders.add(calEvent.getBegin() + minutes * 60 * 1000);
}
reminderCursor.close();
calEvent.setRemindersAbsoluteTs(reminders);
}
if (!calendarIsBlacklisted(calEvent.getUniqueCalName())) { if (!calendarIsBlacklisted(calEvent.getUniqueCalName())) {
calendarEventList.add(calEvent); calendarEventList.add(calEvent);
} else { } else {

View File

@ -25,22 +25,25 @@ public class CalendarEventTest extends TestBase {
@Test @Test
public void testHashCode() { public void testHashCode() {
CalendarEvent c1 = CalendarEvent c1 =
new CalendarEvent(BEGIN, END, ID_1, "something", null, null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false); new CalendarEvent(BEGIN, END, ID_1, "something", null, null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false, null);
CalendarEvent c2 = CalendarEvent c2 =
new CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false); new CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false, null);
CalendarEvent c3 = CalendarEvent c3 =
new CalendarEvent(BEGIN, END, ID_1, null, null, "something", CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false); new CalendarEvent(BEGIN, END, ID_1, null, null, "something", CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false, null);
CalendarEvent c4 =
new CalendarEvent(BEGIN, END, ID_1, null, null, "something", CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false, "some");
assertEquals(c1.hashCode(), c1.hashCode()); assertEquals(c1.hashCode(), c1.hashCode());
assertNotEquals(c1.hashCode(), c2.hashCode()); assertNotEquals(c1.hashCode(), c2.hashCode());
assertNotEquals(c2.hashCode(), c3.hashCode()); assertNotEquals(c2.hashCode(), c3.hashCode());
assertNotEquals(c3.hashCode(), c4.hashCode());
} }
@Test @Test
public void testSync() { public void testSync() {
List<CalendarEvent> eventList = new ArrayList<>(); List<CalendarEvent> eventList = new ArrayList<>();
eventList.add(new CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false)); eventList.add(new CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false, null));
GBDevice dummyGBDevice = createDummyGDevice("00:00:01:00:03"); GBDevice dummyGBDevice = createDummyGDevice("00:00:01:00:03");
dummyGBDevice.setState(GBDevice.State.INITIALIZED); dummyGBDevice.setState(GBDevice.State.INITIALIZED);
@ -49,7 +52,7 @@ public class CalendarEventTest extends TestBase {
testCR.syncCalendar(eventList); testCR.syncCalendar(eventList);
eventList.add(new CalendarEvent(BEGIN, END, ID_2, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false)); eventList.add(new CalendarEvent(BEGIN, END, ID_2, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false, null));
testCR.syncCalendar(eventList); testCR.syncCalendar(eventList);
CalendarSyncStateDao calendarSyncStateDao = daoSession.getCalendarSyncStateDao(); CalendarSyncStateDao calendarSyncStateDao = daoSession.getCalendarSyncStateDao();