Calendar Synchronisation - avinotec/Stundenplaner.FHE_EAH GitHub Wiki

Since spring 2023, the events of My Schedule can be synchronized to a calendar on the user's phone.

PreferenceScreen

User can access the settings of the calendar synchronization and enable it in the settings of My Schedule. For this reason, the settings were turned into a PreferenceScreen. Further information about preferences, preference categories, etc.: Android Developer. The preferences are organized in MySchedulePreferenceFragment which also holds the setup and initialization of all calendar sync related buttons.

CalendarModel

For the setup the following instructions from Android Developer were followed: https://developer.android.com/guide/topics/providers/calendar-provider

The CalendarModel holds methods for creating, updating and deleting calendars and calendar entries. For that a query, update or insert is executed at a Content Provider which stores data like a relational database. A calendar entry is created from a MyScheduleEventVo. This value object also holds the id of the calendar entry after it has been created and a variable changedSinceLastCalSync to track whether the calendar entry of the MyScheduleEventVo needs to be created or updated. Example for updating an calendar entry:

//get properties of the calendar entry from the MyScheduleEventVo
ContentValues values = getCalendarEntryValues(myScheduleEventVo, calId);
long calEventId = scheduleEvent.getCalEventId(); //the id of the calendar event
final Uri updateUri = ContentUris.withAppendedId(Events.CONTENT_URI, calEventId);
final ContentResolver cr = Main.getAppContext().getContentResolver();
//update
cr.update(updateUri, values, null, null);
//set changedSinceLastCalSync to false
scheduleEvent.setChangedSinceLastCalSync(false);

Method syncMySchedule()

The method iterates of every MyScheduleEventVo of every subscribed event series. The id of the calendar chosen for synchronization is loaded from the Shared Preferences. In syncEventSeries() each MyScheduleEventVo with changedSinceLastCalSync == true is synchronized. If it already holds a calendar event id, this event is updated. If not, the calendar entry is created. The method syncMySchedule() needs to be synchronized to avoid threads executing the synchronization in parallel. Parallel execution has not been considered while developing and thus is not save.

CalendarModel as EventDispatcher

The CalendarModel class extends EventDispatcher to enable it to receive and handle events. If the calendar that has been chosen for synchronisation is deleted (because the user used the delete-a-calendar-button/functionality of the app) a corresponding event is fired and processed by the listener specified in MyPreferenceFragment. Leading to certain buttons or switches and their descriptions beeing updated.

CalendarSynchronisationBackgroundTask

CalendarSynchronisationBackgroundTask is a Runnable to run CalendarModel.syncMySchedule(). To run it periodically, a Future instance is scheduled and executed via a ScheduledExecutorService.