Calendar Sync: detect changed events by hash code

This commit is contained in:
Andreas Shimokawa 2017-04-19 14:52:07 +02:00
parent 087f5879b0
commit 546b68ad2d
2 changed files with 9 additions and 12 deletions

View File

@ -282,7 +282,7 @@ public class GBDaoGenerator {
indexUnique.makeUnique(); indexUnique.makeUnique();
calendarSyncState.addIndex(indexUnique); calendarSyncState.addIndex(indexUnique);
calendarSyncState.addToOne(device, deviceId); calendarSyncState.addToOne(device, deviceId);
calendarSyncState.addIntProperty("syncState").notNull(); calendarSyncState.addIntProperty("hash").notNull();
} }
private static Property findProperty(Entity entity, String propertyName) { private static Property findProperty(Entity entity, String propertyName) {

View File

@ -113,8 +113,11 @@ public class CalendarReceiver extends BroadcastReceiver {
.build().unique(); .build().unique();
if (calendarSyncState == null) { if (calendarSyncState == null) {
eventState.put(e.getId(), new EventSyncState(e, EventState.NOT_SYNCED)); eventState.put(e.getId(), new EventSyncState(e, EventState.NOT_SYNCED));
} else { } else if (calendarSyncState.getHash() == e.hashCode()) {
eventState.put(e.getId(), new EventSyncState(e, calendarSyncState.getSyncState())); eventState.put(e.getId(), new EventSyncState(e, EventState.NEEDS_UPDATE));
}
else {
eventState.put(e.getId(), new EventSyncState(e, EventState.SYNCED));
} }
} }
} }
@ -123,7 +126,7 @@ public class CalendarReceiver extends BroadcastReceiver {
List<CalendarSyncState> CalendarSyncStateList = qb.where(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId)).build().list(); List<CalendarSyncState> CalendarSyncStateList = qb.where(CalendarSyncStateDao.Properties.DeviceId.eq(deviceId)).build().list();
for (CalendarSyncState CalendarSyncState : CalendarSyncStateList) { for (CalendarSyncState CalendarSyncState : CalendarSyncStateList) {
if (!eventState.containsKey(CalendarSyncState.getCalendarEntryId())) { if (!eventState.containsKey(CalendarSyncState.getCalendarEntryId())) {
eventState.put(CalendarSyncState.getCalendarEntryId(), new EventSyncState(null, CalendarSyncState.getSyncState())); eventState.put(CalendarSyncState.getCalendarEntryId(), new EventSyncState(null, EventState.NEEDS_DELETE));
LOG.info("insert null event for orphanded calendar id=" + CalendarSyncState.getCalendarEntryId() + " for device=" + mGBDevice.getName()); LOG.info("insert null event for orphanded calendar id=" + CalendarSyncState.getCalendarEntryId() + " for device=" + mGBDevice.getName());
} }
} }
@ -137,12 +140,6 @@ public class CalendarReceiver extends BroadcastReceiver {
if (es.getState() == EventState.SYNCED) { if (es.getState() == EventState.SYNCED) {
if (!es.getEvent().equals(eventTable.get(i))) { if (!es.getEvent().equals(eventTable.get(i))) {
eventState.put(i, new EventSyncState(eventTable.get(i), EventState.NEEDS_UPDATE)); eventState.put(i, new EventSyncState(eventTable.get(i), EventState.NEEDS_UPDATE));
// update sync status of that Calendar entry in DB for all devices
CalendarSyncStateList = qb.where(CalendarSyncStateDao.Properties.CalendarEntryId.eq(i)).build().list();
for (CalendarSyncState CalendarSyncState : CalendarSyncStateList) {
CalendarSyncState.setSyncState(EventState.NEEDS_UPDATE);
CalendarSyncState.update();
}
} }
} }
} else { } else {
@ -160,7 +157,7 @@ public class CalendarReceiver extends BroadcastReceiver {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
GB.toast("Datebase Error while syncing Calendar", Toast.LENGTH_SHORT, GB.ERROR); GB.toast("Database Error while syncing Calendar", Toast.LENGTH_SHORT, GB.ERROR);
} }
} }
@ -187,7 +184,7 @@ public class CalendarReceiver extends BroadcastReceiver {
es.setState(EventState.SYNCED); es.setState(EventState.SYNCED);
eventState.put(i, es); eventState.put(i, es);
// update db // update db
session.insertOrReplace(new CalendarSyncState(null, deviceId, i, EventState.SYNCED)); session.insertOrReplace(new CalendarSyncState(null, deviceId, i, es.event.hashCode()));
} else if (syncState == EventState.NEEDS_DELETE) { } else if (syncState == EventState.NEEDS_DELETE) {
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_UNKNOWN, i); GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_UNKNOWN, i);
eventState.remove(i); eventState.remove(i);