diff --git a/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.java b/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.java index e9e1e859..e63e714c 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.java +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.java @@ -18,6 +18,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Parcel; import android.os.RemoteException; +import android.provider.CalendarContract; import android.provider.ContactsContract; import android.provider.ContactsContract.Groups; import android.provider.ContactsContract.RawContacts; @@ -30,6 +31,7 @@ import java.util.List; import java.util.logging.Level; import at.bitfire.davdroid.App; +import at.bitfire.ical4android.CalendarStorageException; import at.bitfire.vcard4android.AndroidAddressBook; import at.bitfire.vcard4android.AndroidContact; import at.bitfire.vcard4android.AndroidGroup; @@ -149,6 +151,15 @@ public class LocalAddressBook extends AndroidAddressBook implements LocalCollect return nameless.toArray(new LocalResource[nameless.size()]); } + @Override + public LocalResource getByUid(String uid) throws ContactsStorageException { + LocalContact[] ret = (LocalContact[]) queryContacts(AndroidContact.COLUMN_FILENAME + " =? ", new String[]{uid}); + if (ret != null && ret.length > 0) { + return ret[0]; + } + return null; + } + public void deleteAll() throws ContactsStorageException { try { provider.delete(syncAdapterURI(RawContacts.CONTENT_URI), null, null); diff --git a/app/src/main/java/at/bitfire/davdroid/resource/LocalCalendar.java b/app/src/main/java/at/bitfire/davdroid/resource/LocalCalendar.java index 31d89d67..d70fbc44 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalCalendar.java +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalCalendar.java @@ -125,6 +125,15 @@ public class LocalCalendar extends AndroidCalendar implements LocalCollection { return (LocalEvent[])queryEvents(Events._SYNC_ID + " IS NULL AND " + Events.ORIGINAL_ID + " IS NULL", null); } + @Override + public LocalEvent getByUid(String uid) throws CalendarStorageException { + LocalEvent[] ret = (LocalEvent[]) queryEvents(Events._SYNC_ID + " =? ", new String[]{uid}); + if (ret != null && ret.length > 0) { + return ret[0]; + } + return null; + } + @Override public LocalResource[] getDirty() throws CalendarStorageException, FileNotFoundException { List dirty = new LinkedList<>(); diff --git a/app/src/main/java/at/bitfire/davdroid/resource/LocalCollection.java b/app/src/main/java/at/bitfire/davdroid/resource/LocalCollection.java index bb97dbae..4b43d073 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalCollection.java +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalCollection.java @@ -21,6 +21,7 @@ public interface LocalCollection { LocalResource[] getDirty() throws CalendarStorageException, ContactsStorageException, FileNotFoundException; LocalResource[] getAll() throws CalendarStorageException, ContactsStorageException; + LocalResource getByUid(String uid) throws CalendarStorageException, ContactsStorageException; String getCTag() throws CalendarStorageException, ContactsStorageException; void setCTag(String cTag) throws CalendarStorageException, ContactsStorageException; diff --git a/app/src/main/java/at/bitfire/davdroid/resource/LocalTaskList.java b/app/src/main/java/at/bitfire/davdroid/resource/LocalTaskList.java index e08a264d..6755a34a 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalTaskList.java +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalTaskList.java @@ -17,6 +17,7 @@ import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.RemoteException; +import android.provider.CalendarContract; import android.support.annotation.NonNull; import org.dmfs.provider.tasks.TaskContract.TaskLists; @@ -93,6 +94,15 @@ public class LocalTaskList extends AndroidTaskList implements LocalCollection { return (LocalTask[])queryTasks(Tasks._SYNC_ID + " IS NULL", null); } + @Override + public LocalTask getByUid(String uid) throws CalendarStorageException { + LocalTask[] ret = (LocalTask[]) queryTasks(Tasks._SYNC_ID + " =? ", new String[]{uid}); + if (ret != null && ret.length > 0) { + return ret[0]; + } + return null; + } + @Override public LocalResource[] getDirty() throws CalendarStorageException, FileNotFoundException { LocalTask[] tasks = (LocalTask[])queryTasks(Tasks._DIRTY + "!=0 AND " + Tasks._DELETED + "== 0", null); diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java index 28b81073..9c87c102 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java @@ -96,25 +96,18 @@ public class CalendarSyncManager extends SyncManager { App.log.warning("Received multiple VCALs, using first one"); Event event = events[0]; + LocalEvent local = (LocalEvent) localCollection.getByUid(event.uid); if (cEntry.isAction(SyncEntry.Actions.ADD) || cEntry.isAction(SyncEntry.Actions.CHANGE)) { - LocalResource local = processEvent(event); - - if (local != null) { - localResources.put(local.getUuid(), local); - } - + processEvent(event, local); } else { - LocalResource local = localResources.get(event.uid); App.log.info("Removing local record #" + local.getId() + " which has been deleted on the server"); - localResources.remove(local.getUuid()); local.delete(); } } - private LocalResource processEvent(final Event newData) throws IOException, ContactsStorageException, CalendarStorageException { + private LocalResource processEvent(final Event newData, LocalEvent localEvent) throws IOException, ContactsStorageException, CalendarStorageException { // delete local event, if it exists - LocalEvent localEvent = (LocalEvent) localResources.get(newData.uid); if (localEvent != null) { App.log.info("Updating " + newData.uid + " in local calendar"); localEvent.setETag(newData.uid); diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java index 280a1c76..a24678e0 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java @@ -149,26 +149,20 @@ public class ContactsSyncManager extends SyncManager { App.log.warning("Received multiple VCards, using first one"); Contact contact = contacts[0]; + LocalResource local = (LocalResource) localCollection.getByUid(contact.uid); + if (cEntry.isAction(SyncEntry.Actions.ADD) || cEntry.isAction(SyncEntry.Actions.CHANGE)) { - LocalResource local = processContact(contact); - - if (local != null) { - localResources.put(local.getUuid(), local); - } - + processContact(contact, local); } else { - LocalResource local = localResources.get(contact.uid); App.log.info("Removing local record #" + local.getId() + " which has been deleted on the server"); - localResources.remove(local.getUuid()); local.delete(); } } - private LocalResource processContact(final Contact newData) throws IOException, ContactsStorageException { + private LocalResource processContact(final Contact newData, LocalResource local) throws IOException, ContactsStorageException { String uuid = newData.uid; // update local contact, if it exists - LocalResource local = localResources.get(uuid); if (local != null) { App.log.log(Level.INFO, "Updating " + uuid + " in local address book", newData); diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/SyncManager.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/SyncManager.java index 0f01a71a..20fbbb5c 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/SyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/SyncManager.java @@ -92,11 +92,6 @@ abstract public class SyncManager { */ protected List remoteEntries; - /** - * sync-able resources in the local collection, as enumerated by {@link #prepareLocal()} - */ - protected Map localResources; - public SyncManager(Context context, Account account, AccountSettings settings, Bundle extras, String authority, SyncResult syncResult, String uniqueCollectionId, CollectionInfo.Type serviceType) throws InvalidAccountException { this.context = context; this.account = account; @@ -311,19 +306,10 @@ abstract public class SyncManager { } /** - * Lists all local resources which should be taken into account for synchronization into {@link #localResources}. */ protected void prepareLocal() throws CalendarStorageException, ContactsStorageException { prepareDirty(); - // fetch list of local contacts and build hash table to index file name - LocalResource[] localList = localCollection.getAll(); - localResources = new HashMap<>(localList.length); - for (LocalResource resource : localList) { - App.log.fine("Found local resource: " + resource.getUuid()); - localResources.put(resource.getUuid(), resource); - } - remoteCTag = localCollection.getCTag(); }