From 68bd43fe6a9469412991df80a513d8a859d2519a Mon Sep 17 00:00:00 2001 From: rfc2822 Date: Sun, 27 Oct 2013 00:52:35 +0200 Subject: [PATCH] don't show calendars/address books as syncable in account when no calendars/address books were added; select only events from correct calendar when querying (fixes #37) --- res/xml/account_prefs.xml | 4 ++-- .../davdroid/resource/LocalAddressBook.java | 6 ++++++ .../davdroid/resource/LocalCalendar.java | 6 +++++- .../davdroid/resource/LocalCollection.java | 17 ++++++++++++++--- .../syncadapter/SelectCollectionsFragment.java | 8 ++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/res/xml/account_prefs.xml b/res/xml/account_prefs.xml index d91cf3c8..b23cf78e 100644 --- a/res/xml/account_prefs.xml +++ b/res/xml/account_prefs.xml @@ -4,7 +4,7 @@ + android:data="http://davdroid.bitfire.at/?pk_campaign=davdroid-app&pk_kwd=accounts" /> - \ No newline at end of file + diff --git a/src/at/bitfire/davdroid/resource/LocalAddressBook.java b/src/at/bitfire/davdroid/resource/LocalAddressBook.java index 770f9e15..6cb8e279 100644 --- a/src/at/bitfire/davdroid/resource/LocalAddressBook.java +++ b/src/at/bitfire/davdroid/resource/LocalAddressBook.java @@ -60,6 +60,7 @@ public class LocalAddressBook extends LocalCollection { protected String entryColumnAccountType() { return RawContacts.ACCOUNT_TYPE; } protected String entryColumnAccountName() { return RawContacts.ACCOUNT_NAME; } + protected String entryColumnParentID() { return null; /* maybe use RawContacts.DATA_SET some day? */ } protected String entryColumnID() { return RawContacts._ID; } protected String entryColumnRemoteName() { return RawContacts.SOURCE_ID; } protected String entryColumnETag() { return RawContacts.SYNC2; } @@ -79,6 +80,11 @@ public class LocalAddressBook extends LocalCollection { /* collection operations */ + @Override + public long getId() { + return -1; + } + @Override public String getCTag() { return accountManager.getUserData(account, Constants.ACCOUNT_KEY_ADDRESSBOOK_CTAG); diff --git a/src/at/bitfire/davdroid/resource/LocalCalendar.java b/src/at/bitfire/davdroid/resource/LocalCalendar.java index 9f454f5d..614815f9 100644 --- a/src/at/bitfire/davdroid/resource/LocalCalendar.java +++ b/src/at/bitfire/davdroid/resource/LocalCalendar.java @@ -11,6 +11,7 @@ import java.net.URISyntaxException; import java.text.ParseException; import java.util.LinkedList; import java.util.List; + import lombok.Getter; import net.fortuna.ical4j.model.Parameter; import net.fortuna.ical4j.model.ParameterList; @@ -25,7 +26,9 @@ import net.fortuna.ical4j.model.property.Organizer; import net.fortuna.ical4j.model.property.RDate; import net.fortuna.ical4j.model.property.RRule; import net.fortuna.ical4j.model.property.Status; + import org.apache.commons.lang.StringUtils; + import android.accounts.Account; import android.annotation.SuppressLint; import android.annotation.TargetApi; @@ -51,7 +54,7 @@ import at.bitfire.davdroid.syncadapter.ServerInfo; public class LocalCalendar extends LocalCollection { private static final String TAG = "davdroid.LocalCalendar"; - protected long id; + @Getter protected long id; @Getter protected String path, cTag; protected static String COLLECTION_COLUMN_CTAG = Calendars.CAL_SYNC1; @@ -67,6 +70,7 @@ public class LocalCalendar extends LocalCollection { protected String entryColumnAccountType() { return Events.ACCOUNT_TYPE; } protected String entryColumnAccountName() { return Events.ACCOUNT_NAME; } + protected String entryColumnParentID() { return Events.CALENDAR_ID; } protected String entryColumnID() { return Events._ID; } protected String entryColumnRemoteName() { return Events._SYNC_ID; } protected String entryColumnETag() { return Events.SYNC_DATA1; } diff --git a/src/at/bitfire/davdroid/resource/LocalCollection.java b/src/at/bitfire/davdroid/resource/LocalCollection.java index 0aa92b9c..b9bf6fb8 100644 --- a/src/at/bitfire/davdroid/resource/LocalCollection.java +++ b/src/at/bitfire/davdroid/resource/LocalCollection.java @@ -39,6 +39,7 @@ public abstract class LocalCollection { abstract protected String entryColumnAccountType(); abstract protected String entryColumnAccountName(); + abstract protected String entryColumnParentID(); abstract protected String entryColumnID(); abstract protected String entryColumnRemoteName(); abstract protected String entryColumnETag(); @@ -57,6 +58,7 @@ public abstract class LocalCollection { // collection operations + abstract public long getId(); abstract public String getCTag(); abstract public void setCTag(String cTag); @@ -64,9 +66,12 @@ public abstract class LocalCollection { // content provider (= database) querying public Resource[] findDirty() throws RemoteException { + String where = entryColumnDirty() + "=1"; + if (entryColumnParentID() != null) + where += " AND " + entryColumnParentID() + "=" + String.valueOf(getId()); Cursor cursor = providerClient.query(entriesURI(), new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() }, - entryColumnDirty() + "=1", null, null); + where, null, null); LinkedList dirty = new LinkedList(); while (cursor.moveToNext()) dirty.add(findById(cursor.getLong(0), cursor.getString(1), cursor.getString(2), true)); @@ -74,9 +79,12 @@ public abstract class LocalCollection { } public Resource[] findDeleted() throws RemoteException { + String where = entryColumnDeleted() + "=1"; + if (entryColumnParentID() != null) + where += " AND " + entryColumnParentID() + "=" + String.valueOf(getId()); Cursor cursor = providerClient.query(entriesURI(), new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() }, - entryColumnDeleted() + "=1", null, null); + where, null, null); LinkedList deleted = new LinkedList(); while (cursor.moveToNext()) deleted.add(findById(cursor.getLong(0), cursor.getString(1), cursor.getString(2), false)); @@ -84,9 +92,12 @@ public abstract class LocalCollection { } public Resource[] findNew() throws RemoteException { + String where = entryColumnDirty() + "=1 AND " + entryColumnRemoteName() + " IS NULL"; + if (entryColumnParentID() != null) + where += " AND " + entryColumnParentID() + "=" + String.valueOf(getId()); Cursor cursor = providerClient.query(entriesURI(), new String[] { entryColumnID() }, - entryColumnDirty() + "=1 AND " + entryColumnRemoteName() + " IS NULL", null, null); + where, null, null); LinkedList fresh = new LinkedList(); while (cursor.moveToNext()) { String uid = UUID.randomUUID().toString(), diff --git a/src/at/bitfire/davdroid/syncadapter/SelectCollectionsFragment.java b/src/at/bitfire/davdroid/syncadapter/SelectCollectionsFragment.java index e1e2d11a..87584ba2 100644 --- a/src/at/bitfire/davdroid/syncadapter/SelectCollectionsFragment.java +++ b/src/at/bitfire/davdroid/syncadapter/SelectCollectionsFragment.java @@ -141,16 +141,20 @@ public class SelectCollectionsFragment extends ListFragment { if (!addressBooks.isEmpty()) { userData.putString(Constants.ACCOUNT_KEY_ADDRESSBOOK_PATH, addressBooks.get(0).getPath()); + ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1); ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true); - } + } else + ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 0); if (accountManager.addAccountExplicitly(account, serverInfo.getPassword(), userData)) { // account created, now create calendars if (!calendars.isEmpty()) { for (ServerInfo.ResourceInfo calendar : calendars) LocalCalendar.create(account, getActivity().getContentResolver(), calendar); + ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1); ContentResolver.setSyncAutomatically(account, CalendarContract.AUTHORITY, true); - } + } else + ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 0); getActivity().finish(); } else