1
0
mirror of https://github.com/etesync/android synced 2024-11-17 13:30:00 +00:00

Don't load all resources when syncing, only as needed.

This commit is contained in:
Tom Hacohen 2017-02-16 18:18:44 +00:00
parent 8040ee7d9f
commit d6864f5062
7 changed files with 38 additions and 34 deletions

View File

@ -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);

View File

@ -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<LocalResource> dirty = new LinkedList<>();

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -92,11 +92,6 @@ abstract public class SyncManager {
*/
protected List<JournalEntryManager.Entry> remoteEntries;
/**
* sync-able resources in the local collection, as enumerated by {@link #prepareLocal()}
*/
protected Map<String, LocalResource> 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();
}