From dcd86c7d8675a306152b9a14abe942ed0b379a04 Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Sat, 18 Jul 2015 15:15:36 +0200 Subject: [PATCH] Small refactoring --- .../davdroid/resource/LocalAddressBook.java | 18 --------- .../davdroid/resource/LocalCollection.java | 40 ++++++++++++------- .../davdroid/syncadapter/SyncManager.java | 2 +- 3 files changed, 27 insertions(+), 33 deletions(-) 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 c9318a4c..7f83bd04 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.java +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.java @@ -132,25 +132,7 @@ public class LocalAddressBook extends LocalCollection { c.setVCardVersion(accountSettings.getAddressBookVCardVersion()); return c; } - - public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException { - String where; - - if (remoteResources.length != 0) { - List sqlFileNames = new LinkedList<>(); - for (Resource res : remoteResources) - sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName())); - where = entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")"; - } else - where = entryColumnRemoteName() + " IS NOT NULL"; - try { - return providerClient.delete(entriesURI(), where, null); - } catch (RemoteException e) { - throw new LocalStorageException("Couldn't delete contacts locally", e); - } - } - @Override public int commit() throws LocalStorageException { int affected = super.commit(); 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 d8249a49..7d19f268 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalCollection.java +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalCollection.java @@ -269,7 +269,9 @@ public abstract class LocalCollection { * @return the new resource object */ abstract public T newResource(long localID, String resourceName, String eTag); - /** Enqueues adding the resource (including all data) to the local collection. */ + /** Adds the resource (including all data) to the local collection. + * @param resource Resource to be added + */ public void add(Resource resource) throws LocalStorageException { int idx = pendingOperations.size(); pendingOperations.add( @@ -281,8 +283,8 @@ public abstract class LocalCollection { commit(); } - /** Enqueues updating an existing resource in the local collection. The resource will be found by - * the remote file name and all data will be updated. Requires commit(). */ + /** Updates an existing resource in the local collection. The resource will be found by + * the remote file name and all data will be updated. */ public void updateByRemoteName(Resource remoteResource) throws LocalStorageException { T localResource = findByRemoteName(remoteResource.getName(), false); pendingOperations.add( @@ -296,7 +298,7 @@ public abstract class LocalCollection { commit(); } - /** Enqueues deleting a resource from the local collection. Requires commit(). */ + /** Enqueues deleting a resource from the local collection. Requires commit() to be effective! */ public void delete(Resource resource) { pendingOperations.add(ContentProviderOperation .newDelete(ContentUris.withAppendedId(entriesURI(), resource.getLocalID())) @@ -309,7 +311,8 @@ public abstract class LocalCollection { * @param remoteResources resources with these remote file names will be kept * @return number of deleted resources */ - public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException { + public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException + { final String where; if (remoteResources.length != 0) { @@ -322,13 +325,19 @@ public abstract class LocalCollection { // delete all entries where = entryColumnRemoteName() + " IS NOT NULL"; + Log.d(TAG, "deleteAllExceptRemoteNames: " + where); + try { - return providerClient.delete( - entriesURI(), - // restrict deletion to parent collection - entryColumnParentID() + "=? AND (" + where + ')', - new String[] { String.valueOf(getId()) } - ); + if (entryColumnParentID() != null) + // entries have a parent collection (for instance, events which have a calendar) + return providerClient.delete( + entriesURI(), + entryColumnParentID() + "=? AND (" + where + ')', // restrict deletion to parent collection + new String[] { String.valueOf(getId()) } + ); + else + // entries don't have a parent collection (contacts are stored directly and not within an address book) + return providerClient.delete(entriesURI(), null, null); } catch (RemoteException e) { throw new LocalStorageException("Couldn't delete local resources", e); } @@ -348,7 +357,7 @@ public abstract class LocalCollection { } } - /** Enqueues removing the dirty flag from a locally-stored resource. Requires commit(). */ + /** Enqueues removing the dirty flag from a locally-stored resource. Requires commit() to be effective! */ public void clearDirty(Resource resource) { pendingOperations.add(ContentProviderOperation .newUpdate(ContentUris.withAppendedId(entriesURI(), resource.getLocalID())) @@ -364,8 +373,11 @@ public abstract class LocalCollection { Log.d(TAG, "Committing " + pendingOperations.size() + " operations ..."); ContentProviderResult[] results = providerClient.applyBatch(pendingOperations); for (ContentProviderResult result : results) - if (result != null && result.count != null) - affected += result.count; + if (result != null) // will have either .uri or .count set + if (result.count != null) + affected += result.count; + else if (result.uri != null) + affected = 1; Log.d(TAG, "... " + affected + " row(s) affected"); pendingOperations.clear(); } catch(OperationApplicationException | RemoteException ex) { 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 9453bded..c92d1635 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/SyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/SyncManager.java @@ -87,7 +87,7 @@ public class SyncManager { syncResult.stats.numInserts = pullNew(remotelyAdded.toArray(new Resource[remotelyAdded.size()])); syncResult.stats.numUpdates = pullChanged(remotelyUpdated.toArray(new Resource[remotelyUpdated.size()])); - Log.i(TAG, "Removing non-dirty resources that are not present remotely anymore"); + Log.i(TAG, "Removing entries that are not present remotely anymore (retaining " + remoteResources.length + " entries)"); syncResult.stats.numDeletes = local.deleteAllExceptRemoteNames(remoteResources); syncResult.stats.numEntries = syncResult.stats.numInserts + syncResult.stats.numUpdates + syncResult.stats.numDeletes;