2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2017-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2018-02-26 14:27:32 +01:00
|
|
|
Gobbetti, Daniel Hauck
|
2017-04-16 12:34:37 +02:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-04-16 19:37:43 +02:00
|
|
|
import android.widget.Toast;
|
2017-04-16 12:34:37 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-04-19 17:44:02 +02:00
|
|
|
import java.util.Calendar;
|
2017-04-16 12:34:37 +02:00
|
|
|
import java.util.Enumeration;
|
2022-09-27 13:58:26 +02:00
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-04-16 12:34:37 +02:00
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-04-16 19:37:43 +02:00
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
2017-04-16 12:34:37 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2017-04-16 19:37:43 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.CalendarSyncState;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.CalendarSyncStateDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2017-04-16 12:34:37 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
2022-06-16 23:28:17 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.calendar.CalendarEvent;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.calendar.CalendarManager;
|
2017-04-16 19:37:43 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2017-04-16 12:34:37 +02:00
|
|
|
|
|
|
|
public class CalendarReceiver extends BroadcastReceiver {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(CalendarReceiver.class);
|
2017-04-16 19:37:43 +02:00
|
|
|
private Hashtable<Long, EventSyncState> eventState = new Hashtable<>();
|
|
|
|
|
|
|
|
private GBDevice mGBDevice;
|
2017-04-16 12:34:37 +02:00
|
|
|
|
|
|
|
private class EventSyncState {
|
2017-04-16 19:37:43 +02:00
|
|
|
private int state;
|
2022-06-16 23:28:17 +02:00
|
|
|
private CalendarEvent event;
|
2017-04-16 12:34:37 +02:00
|
|
|
|
2022-06-16 23:28:17 +02:00
|
|
|
EventSyncState(CalendarEvent event, int state) {
|
2017-04-16 12:34:37 +02:00
|
|
|
this.state = state;
|
|
|
|
this.event = event;
|
|
|
|
}
|
|
|
|
|
2017-04-16 19:37:43 +02:00
|
|
|
public int getState() {
|
2017-04-16 12:34:37 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-04-16 19:37:43 +02:00
|
|
|
public void setState(int state) {
|
2017-04-16 12:34:37 +02:00
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
2022-06-16 23:28:17 +02:00
|
|
|
public CalendarEvent getEvent() {
|
2017-04-16 12:34:37 +02:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2022-06-16 23:28:17 +02:00
|
|
|
public void setEvent(CalendarEvent event) {
|
2017-04-16 12:34:37 +02:00
|
|
|
this.event = event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 19:37:43 +02:00
|
|
|
private static class EventState {
|
|
|
|
private static final int NOT_SYNCED = 0;
|
|
|
|
private static final int SYNCED = 1;
|
|
|
|
private static final int NEEDS_UPDATE = 2;
|
|
|
|
private static final int NEEDS_DELETE = 3;
|
2017-04-16 12:34:37 +02:00
|
|
|
}
|
|
|
|
|
2017-04-16 19:37:43 +02:00
|
|
|
public CalendarReceiver(GBDevice gbDevice) {
|
2017-04-16 12:34:37 +02:00
|
|
|
LOG.info("Created calendar receiver.");
|
2017-04-16 19:37:43 +02:00
|
|
|
mGBDevice = gbDevice;
|
2017-04-24 13:50:40 +02:00
|
|
|
onReceive(GBApplication.getContext(), new Intent());
|
2017-04-16 12:34:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 17:29:40 +02:00
|
|
|
public GBDevice getGBDevice(){
|
|
|
|
return mGBDevice;
|
|
|
|
}
|
|
|
|
|
2017-04-16 12:34:37 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2017-04-17 21:09:29 +02:00
|
|
|
LOG.info("got calendar changed broadcast");
|
2022-06-16 23:28:17 +02:00
|
|
|
List<CalendarEvent> eventList = (new CalendarManager(context, mGBDevice.getAddress())).getCalendarEventList();
|
2017-04-24 13:50:40 +02:00
|
|
|
syncCalendar(eventList);
|
2017-04-16 19:37:43 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 23:28:17 +02:00
|
|
|
public void syncCalendar(List<CalendarEvent> eventList) {
|
2017-04-16 19:37:43 +02:00
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
DaoSession session = dbHandler.getDaoSession();
|
2017-04-27 07:52:11 +02:00
|
|
|
syncCalendar(eventList, session);
|
|
|
|
} catch (Exception e1) {
|
2020-05-05 14:11:46 +02:00
|
|
|
GB.toast("Database Error while syncing Calendar", Toast.LENGTH_SHORT, GB.ERROR, e1);
|
2017-04-27 07:52:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-16 12:34:37 +02:00
|
|
|
|
2022-06-16 23:28:17 +02:00
|
|
|
public void syncCalendar(List<CalendarEvent> eventList, DaoSession session) {
|
2017-04-27 07:52:11 +02:00
|
|
|
LOG.info("Syncing with calendar.");
|
2022-06-16 23:28:17 +02:00
|
|
|
Hashtable<Long, CalendarEvent> eventTable = new Hashtable<>();
|
2017-04-27 07:52:11 +02:00
|
|
|
Long deviceId = DBHelper.getDevice(mGBDevice, session).getId();
|
|
|
|
QueryBuilder<CalendarSyncState> qb = session.getCalendarSyncStateDao().queryBuilder();
|
2017-04-16 19:37:43 +02:00
|
|
|
|
|
|
|
|
2022-06-16 23:28:17 +02:00
|
|
|
for (CalendarEvent e : eventList) {
|
2017-04-27 07:52:11 +02:00
|
|
|
long id = e.getId();
|
|
|
|
eventTable.put(id, e);
|
|
|
|
if (!eventState.containsKey(e.getId())) {
|
|
|
|
qb = session.getCalendarSyncStateDao().queryBuilder();
|
2017-04-16 19:37:43 +02:00
|
|
|
|
2017-04-27 07:52:11 +02:00
|
|
|
CalendarSyncState calendarSyncState = qb.where(qb.and(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId), CalendarSyncStateDao.Properties.CalendarEntryId.eq(id)))
|
|
|
|
.build().unique();
|
|
|
|
if (calendarSyncState == null) {
|
|
|
|
eventState.put(id, new EventSyncState(e, EventState.NOT_SYNCED));
|
|
|
|
LOG.info("event id=" + id + " is yet unknown to device id=" + deviceId);
|
|
|
|
} else if (calendarSyncState.getHash() == e.hashCode()) {
|
|
|
|
eventState.put(id, new EventSyncState(e, EventState.SYNCED));
|
|
|
|
LOG.info("event id=" + id + " is up to date on device id=" + deviceId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eventState.put(id, new EventSyncState(e, EventState.NEEDS_UPDATE));
|
|
|
|
LOG.info("event id=" + id + " is not up to date on device id=" + deviceId);
|
2017-04-16 12:34:37 +02:00
|
|
|
}
|
2017-04-16 19:37:43 +02:00
|
|
|
}
|
2017-04-27 07:52:11 +02:00
|
|
|
}
|
2017-04-16 19:37:43 +02:00
|
|
|
|
2017-04-27 07:52:11 +02:00
|
|
|
// add all missing calendar ids on the device to sync status (so that they are deleted later)
|
|
|
|
List<CalendarSyncState> CalendarSyncStateList = qb.where(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId)).build().list();
|
|
|
|
for (CalendarSyncState CalendarSyncState : CalendarSyncStateList) {
|
|
|
|
if (!eventState.containsKey(CalendarSyncState.getCalendarEntryId())) {
|
|
|
|
eventState.put(CalendarSyncState.getCalendarEntryId(), new EventSyncState(null, EventState.NEEDS_DELETE));
|
2020-08-26 04:34:41 +02:00
|
|
|
LOG.info("insert null event for orphaned calendar id=" + CalendarSyncState.getCalendarEntryId() + " for device=" + mGBDevice.getName());
|
2017-04-16 19:37:43 +02:00
|
|
|
}
|
2017-04-27 07:52:11 +02:00
|
|
|
}
|
2017-04-16 19:37:43 +02:00
|
|
|
|
2017-04-27 07:52:11 +02:00
|
|
|
Enumeration<Long> ids = eventState.keys();
|
|
|
|
while (ids.hasMoreElements()) {
|
|
|
|
qb = session.getCalendarSyncStateDao().queryBuilder();
|
|
|
|
Long i = ids.nextElement();
|
|
|
|
EventSyncState es = eventState.get(i);
|
|
|
|
if (eventTable.containsKey(i)) {
|
|
|
|
if (es.getState() == EventState.SYNCED) {
|
|
|
|
if (!es.getEvent().equals(eventTable.get(i))) {
|
|
|
|
eventState.put(i, new EventSyncState(eventTable.get(i), EventState.NEEDS_UPDATE));
|
2017-04-16 19:37:43 +02:00
|
|
|
}
|
2017-04-27 07:52:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (es.getState() == EventState.NOT_SYNCED) {
|
|
|
|
// delete for current device only
|
|
|
|
qb.where(qb.and(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId), CalendarSyncStateDao.Properties.CalendarEntryId.eq(i)))
|
|
|
|
.buildDelete().executeDeleteWithoutDetachingEntities();
|
|
|
|
eventState.remove(i);
|
2017-04-16 12:34:37 +02:00
|
|
|
} else {
|
2017-04-27 07:52:11 +02:00
|
|
|
es.setState(EventState.NEEDS_DELETE);
|
|
|
|
eventState.put(i, es);
|
2017-04-16 12:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 07:52:11 +02:00
|
|
|
updateEvents(deviceId, session);
|
2017-04-16 12:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 19:37:43 +02:00
|
|
|
private void updateEvents(Long deviceId, DaoSession session) {
|
2017-04-16 12:34:37 +02:00
|
|
|
Enumeration<Long> ids = eventState.keys();
|
|
|
|
while (ids.hasMoreElements()) {
|
|
|
|
Long i = ids.nextElement();
|
|
|
|
EventSyncState es = eventState.get(i);
|
2017-04-16 19:37:43 +02:00
|
|
|
int syncState = es.getState();
|
|
|
|
if (syncState == EventState.NOT_SYNCED || syncState == EventState.NEEDS_UPDATE) {
|
2022-06-16 23:28:17 +02:00
|
|
|
CalendarEvent calendarEvent = es.getEvent();
|
2017-04-16 12:34:37 +02:00
|
|
|
CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
|
|
|
|
calendarEventSpec.id = i;
|
2017-04-16 21:08:49 +02:00
|
|
|
calendarEventSpec.title = calendarEvent.getTitle();
|
2017-04-19 17:44:02 +02:00
|
|
|
calendarEventSpec.allDay = calendarEvent.isAllDay();
|
2017-04-16 21:08:49 +02:00
|
|
|
calendarEventSpec.timestamp = calendarEvent.getBeginSeconds();
|
2017-04-20 10:40:28 +02:00
|
|
|
calendarEventSpec.durationInSeconds = calendarEvent.getDurationSeconds(); //FIXME: leads to problems right now
|
2017-04-19 17:44:02 +02:00
|
|
|
if (calendarEvent.isAllDay()) {
|
2022-09-27 13:58:26 +02:00
|
|
|
//force the all day events to begin at midnight and last N whole days
|
|
|
|
Calendar c = GregorianCalendar.getInstance();
|
|
|
|
int numDays = (int)TimeUnit.DAYS.convert(calendarEvent.getEnd()-calendarEvent.getBegin(),
|
|
|
|
TimeUnit.MILLISECONDS);
|
|
|
|
c.setTimeInMillis(calendarEvent.getBegin());
|
|
|
|
c.set(Calendar.HOUR_OF_DAY, 0);
|
2022-08-23 17:03:58 +02:00
|
|
|
//workaround for negative timezones
|
2022-09-27 13:58:26 +02:00
|
|
|
if(c.getTimeZone().getRawOffset()<0) c.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
|
calendarEventSpec.timestamp = (int) (c.getTimeInMillis() / 1000);
|
2022-09-19 12:08:33 +02:00
|
|
|
calendarEventSpec.durationInSeconds = 24 * 60 * 60 * numDays;
|
2017-04-19 17:44:02 +02:00
|
|
|
}
|
2017-04-16 21:08:49 +02:00
|
|
|
calendarEventSpec.description = calendarEvent.getDescription();
|
2017-04-19 17:44:02 +02:00
|
|
|
calendarEventSpec.location = calendarEvent.getLocation();
|
2017-04-16 12:34:37 +02:00
|
|
|
calendarEventSpec.type = CalendarEventSpec.TYPE_UNKNOWN;
|
2022-08-23 17:05:38 +02:00
|
|
|
calendarEventSpec.calName = calendarEvent.getUniqueCalName();
|
|
|
|
calendarEventSpec.color = calendarEvent.getColor();
|
2017-04-16 19:37:43 +02:00
|
|
|
if (syncState == EventState.NEEDS_UPDATE) {
|
2022-08-01 23:04:41 +02:00
|
|
|
GBApplication.deviceService(mGBDevice).onDeleteCalendarEvent(CalendarEventSpec.TYPE_UNKNOWN, i);
|
2017-04-16 19:37:43 +02:00
|
|
|
}
|
2022-08-01 23:04:41 +02:00
|
|
|
GBApplication.deviceService(mGBDevice).onAddCalendarEvent(calendarEventSpec);
|
2017-04-16 12:34:37 +02:00
|
|
|
es.setState(EventState.SYNCED);
|
|
|
|
eventState.put(i, es);
|
2017-04-16 19:37:43 +02:00
|
|
|
// update db
|
2017-04-19 14:52:07 +02:00
|
|
|
session.insertOrReplace(new CalendarSyncState(null, deviceId, i, es.event.hashCode()));
|
2017-04-16 19:37:43 +02:00
|
|
|
} else if (syncState == EventState.NEEDS_DELETE) {
|
2022-08-01 23:04:41 +02:00
|
|
|
GBApplication.deviceService(mGBDevice).onDeleteCalendarEvent(CalendarEventSpec.TYPE_UNKNOWN, i);
|
2017-04-16 12:34:37 +02:00
|
|
|
eventState.remove(i);
|
2017-04-16 19:37:43 +02:00
|
|
|
// delete from db for current device only
|
|
|
|
QueryBuilder<CalendarSyncState> qb = session.getCalendarSyncStateDao().queryBuilder();
|
|
|
|
qb.where(qb.and(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId), CalendarSyncStateDao.Properties.CalendarEntryId.eq(i)))
|
|
|
|
.buildDelete().executeDeleteWithoutDetachingEntities();
|
2017-04-16 12:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|