1
0
mirror of https://github.com/etesync/android synced 2024-11-26 01:48:34 +00:00

Update collection properties (name, color) on every sync

This commit is contained in:
Ricki Hirner 2015-08-02 08:57:03 +02:00
parent 5b7947034a
commit abf04e14d2
15 changed files with 100 additions and 52 deletions

View File

@ -8,10 +8,14 @@
package at.bitfire.davdroid;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DAVUtils {
private static final String TAG = "davdroid.DAVutils";
public static int CalDAVtoARGBColor(String davColor) {
int color = 0xFFC3EA6E; // fallback: "DAVdroid green"
if (davColor != null) {
@ -21,8 +25,10 @@ public class DAVUtils {
int color_rgb = Integer.parseInt(m.group(1), 16);
int color_alpha = m.group(2) != null ? (Integer.parseInt(m.group(2), 16) & 0xFF) : 0xFF;
color = (color_alpha << 24) | color_rgb;
}
} else
Log.w(TAG, "Couldn't parse color " + davColor + ", using DAVdroid green");
}
return color;
}
}

View File

@ -22,7 +22,7 @@ import at.bitfire.davdroid.webdav.DavFilter;
import at.bitfire.davdroid.webdav.DavMultiget;
import at.bitfire.davdroid.webdav.DavProp;
public class CalDavCalendar extends RemoteCollection<Event> {
public class CalDavCalendar extends WebDavCollection<Event> {
private final static String TAG = "davdroid.CalDAVCalendar";
public CalDavCalendar(CloseableHttpClient httpClient, String baseURL, String user, String password, boolean preemptiveAuth) throws URISyntaxException {

View File

@ -23,7 +23,7 @@ import at.bitfire.davdroid.webdav.DavFilter;
import at.bitfire.davdroid.webdav.DavMultiget;
import at.bitfire.davdroid.webdav.DavProp;
public class CalDavTaskList extends RemoteCollection<Task> {
public class CalDavTaskList extends WebDavCollection<Task> {
private final static String TAG = "davdroid.CalDAVTaskList";
public CalDavTaskList(CloseableHttpClient httpClient, String baseURL, String user, String password, boolean preemptiveAuth) throws URISyntaxException {

View File

@ -15,7 +15,7 @@ import at.bitfire.davdroid.syncadapter.AccountSettings;
import at.bitfire.davdroid.webdav.DavMultiget;
import ezvcard.VCardVersion;
public class CardDavAddressBook extends RemoteCollection<Contact> {
public class CardDavAddressBook extends WebDavCollection<Contact> {
AccountSettings accountSettings;
@Override

View File

@ -124,6 +124,12 @@ public class LocalAddressBook extends LocalCollection<Contact> {
accountSettings.setAddressBookCTag(cTag);
}
@Override
public void updateMetaData(String displayName, String color)
{
// address books don't have a display name or color in Android
}
/* create/update/delete */

View File

@ -192,6 +192,21 @@ public class LocalCalendar extends LocalCollection<Event> {
}
}
@Override
public void updateMetaData(String displayName, String color) throws LocalStorageException {
ContentValues values = new ContentValues();
if (displayName != null)
values.put(Calendars.CALENDAR_DISPLAY_NAME, displayName);
if (color != null)
values.put(Calendars.CALENDAR_COLOR, DAVUtils.CalDAVtoARGBColor(color));
try {
if (values.size() > 0)
providerClient.update(ContentUris.withAppendedId(calendarsURI(), id), values, null, null);
} catch(RemoteException e) {
throw new LocalStorageException(e);
}
}
@Override
public long[] findUpdated() throws LocalStorageException {
// mark (recurring) events with changed/deleted exceptions as dirty

View File

@ -91,10 +91,12 @@ public abstract class LocalCollection<T extends Resource> {
/** gets the ID if the collection (for instance, ID of the Android calendar) */
abstract public long getId();
/** sets local stored CTag */
abstract public void setCTag(String cTag) throws LocalStorageException;
/** gets the CTag of the collection */
abstract public String getCTag() throws LocalStorageException;
/** sets the CTag of the collection */
abstract public void setCTag(String cTag) throws LocalStorageException;
/** update locally stored collection properties */
abstract public void updateMetaData(String displayName, String color) throws LocalStorageException;
// content provider (= database) querying

View File

@ -90,7 +90,7 @@ public class LocalTaskList extends LocalCollection<Task> {
public static LocalTaskList[] findAll(Account account, ContentProviderClient providerClient) throws RemoteException {
@Cleanup Cursor cursor = providerClient.query(taskListsURI(account),
new String[] { TaskContract.TaskLists._ID, TaskContract.TaskLists._SYNC_ID },
new String[]{TaskContract.TaskLists._ID, TaskContract.TaskLists._SYNC_ID},
null, null, null);
LinkedList<LocalTaskList> taskList = new LinkedList<>();
@ -131,6 +131,21 @@ public class LocalTaskList extends LocalCollection<Task> {
}
}
@Override
public void updateMetaData(String displayName, String color) throws LocalStorageException {
ContentValues values = new ContentValues();
if (displayName != null)
values.put(TaskContract.TaskLists.LIST_NAME, displayName);
if (color != null)
values.put(TaskContract.TaskLists.LIST_COLOR, DAVUtils.CalDAVtoARGBColor(color));
try {
if (values.size() > 0)
providerClient.update(ContentUris.withAppendedId(taskListsURI(account), id), values, null, null);
} catch(RemoteException e) {
throw new LocalStorageException(e);
}
}
@Override
public Task newResource(long localID, String resourceName, String eTag) {
return new Task(localID, resourceName, eTag);

View File

@ -39,7 +39,7 @@ import lombok.Getter;
*
* @param <T> Subtype of Resource that can be stored in the collection
*/
public abstract class RemoteCollection<T extends Resource> {
public abstract class WebDavCollection<T extends Resource> {
private static final String TAG = "davdroid.resource";
URI baseURI;
@ -50,7 +50,7 @@ public abstract class RemoteCollection<T extends Resource> {
abstract protected T newResourceSkeleton(String name, String ETag);
public RemoteCollection(CloseableHttpClient httpClient, String baseURL, String user, String password, boolean preemptiveAuth) throws URISyntaxException {
public WebDavCollection(CloseableHttpClient httpClient, String baseURL, String user, String password, boolean preemptiveAuth) throws URISyntaxException {
baseURI = URIUtils.parseURI(baseURL, false);
collection = new WebDavResource(httpClient, baseURI, user, password, preemptiveAuth);
}
@ -58,14 +58,8 @@ public abstract class RemoteCollection<T extends Resource> {
/* collection operations */
public String getCTag() throws URISyntaxException, IOException, HttpException {
try {
if (collection.getCTag() == null && collection.getMembers() == null) // not already fetched
collection.propfind(HttpPropfind.Mode.COLLECTION_CTAG);
} catch (DavException e) {
return null;
}
return collection.getCTag();
public void getProperties() throws URISyntaxException, IOException, HttpException, DavException {
collection.propfind(HttpPropfind.Mode.COLLECTION_PROPERTIES);
}

View File

@ -23,7 +23,7 @@ import java.util.Map;
import at.bitfire.davdroid.resource.CalDavCalendar;
import at.bitfire.davdroid.resource.LocalCalendar;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.RemoteCollection;
import at.bitfire.davdroid.resource.WebDavCollection;
public class CalendarsSyncAdapterService extends Service {
private static SyncAdapter syncAdapter;
@ -54,17 +54,17 @@ public class CalendarsSyncAdapterService extends Service {
}
@Override
protected Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
protected Map<LocalCollection<?>, WebDavCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
AccountSettings settings = new AccountSettings(getContext(), account);
String userName = settings.getUserName(),
password = settings.getPassword();
boolean preemptive = settings.getPreemptiveAuth();
try {
Map<LocalCollection<?>, RemoteCollection<?>> map = new HashMap<>();
Map<LocalCollection<?>, WebDavCollection<?>> map = new HashMap<>();
for (LocalCalendar calendar : LocalCalendar.findAll(account, provider)) {
RemoteCollection<?> dav = new CalDavCalendar(httpClient, calendar.getUrl(), userName, password, preemptive);
WebDavCollection<?> dav = new CalDavCalendar(httpClient, calendar.getUrl(), userName, password, preemptive);
map.put(calendar, dav);
}
return map;

View File

@ -22,7 +22,7 @@ import java.util.Map;
import at.bitfire.davdroid.resource.CardDavAddressBook;
import at.bitfire.davdroid.resource.LocalAddressBook;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.RemoteCollection;
import at.bitfire.davdroid.resource.WebDavCollection;
public class ContactsSyncAdapterService extends Service {
private static ContactsSyncAdapter syncAdapter;
@ -53,7 +53,7 @@ public class ContactsSyncAdapterService extends Service {
}
@Override
protected Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
protected Map<LocalCollection<?>, WebDavCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
AccountSettings settings = new AccountSettings(getContext(), account);
String userName = settings.getUserName(),
password = settings.getPassword();
@ -65,9 +65,9 @@ public class ContactsSyncAdapterService extends Service {
try {
LocalCollection<?> database = new LocalAddressBook(account, provider, settings);
RemoteCollection<?> dav = new CardDavAddressBook(settings, httpClient, addressBookURL, userName, password, preemptive);
WebDavCollection<?> dav = new CardDavAddressBook(settings, httpClient, addressBookURL, userName, password, preemptive);
Map<LocalCollection<?>, RemoteCollection<?>> map = new HashMap<>();
Map<LocalCollection<?>, WebDavCollection<?>> map = new HashMap<>();
map.put(database, dav);
return map;

View File

@ -41,7 +41,7 @@ import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.R;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.LocalStorageException;
import at.bitfire.davdroid.resource.RemoteCollection;
import at.bitfire.davdroid.resource.WebDavCollection;
import at.bitfire.davdroid.ui.settings.AccountActivity;
import at.bitfire.davdroid.webdav.DavException;
import at.bitfire.davdroid.webdav.DavHttpClient;
@ -102,7 +102,7 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
}.execute();
}
protected abstract Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider);
protected abstract Map<LocalCollection<?>, WebDavCollection<?>> getSyncPairs(Account account, ContentProviderClient provider);
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
@ -128,12 +128,12 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter impleme
Intent exceptionIntent = null; // what shall happen when clicking on the exception notification
try {
// get local <-> remote collection pairs
Map<LocalCollection<?>, RemoteCollection<?>> syncCollections = getSyncPairs(account, provider);
Map<LocalCollection<?>, WebDavCollection<?>> syncCollections = getSyncPairs(account, provider);
if (syncCollections == null)
Log.i(TAG, "Nothing to synchronize");
else
try {
for (Map.Entry<LocalCollection<?>, RemoteCollection<?>> entry : syncCollections.entrySet())
for (Map.Entry<LocalCollection<?>, WebDavCollection<?>> entry : syncCollections.entrySet())
new SyncManager(entry.getKey(), entry.getValue()).synchronize(extras.containsKey(ContentResolver.SYNC_EXTRAS_MANUAL), syncResult);
} catch (DavException ex) {

View File

@ -19,13 +19,14 @@ import at.bitfire.davdroid.ArrayUtils;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.LocalStorageException;
import at.bitfire.davdroid.resource.RecordNotFoundException;
import at.bitfire.davdroid.resource.RemoteCollection;
import at.bitfire.davdroid.resource.WebDavCollection;
import at.bitfire.davdroid.resource.Resource;
import at.bitfire.davdroid.webdav.ConflictException;
import at.bitfire.davdroid.webdav.DavException;
import at.bitfire.davdroid.webdav.HttpException;
import at.bitfire.davdroid.webdav.NotFoundException;
import at.bitfire.davdroid.webdav.PreconditionFailedException;
import at.bitfire.davdroid.webdav.WebDavResource;
public class SyncManager {
private static final String TAG = "davdroid.SyncManager";
@ -33,41 +34,47 @@ public class SyncManager {
private static final int MAX_MULTIGET_RESOURCES = 35;
final protected LocalCollection<? extends Resource> local;
final protected RemoteCollection<? extends Resource> remote;
final protected WebDavCollection<? extends Resource> remote;
public SyncManager(LocalCollection<? extends Resource> local, RemoteCollection<? extends Resource> remote) {
public SyncManager(LocalCollection<? extends Resource> local, WebDavCollection<? extends Resource> remote) {
this.local = local;
this.remote = remote;
}
public void synchronize(boolean manualSync, SyncResult syncResult) throws URISyntaxException, LocalStorageException, IOException, HttpException, DavException {
// PHASE 1: push local changes to server
// PHASE 1: fetch collection properties
remote.getProperties();
final WebDavResource collectionResource = remote.getCollection();
local.updateMetaData(collectionResource.getDisplayName(), collectionResource.getColor());
// PHASE 2: push local changes to server
int deletedRemotely = pushDeleted(),
addedRemotely = pushNew(),
updatedRemotely = pushDirty();
// PHASE 2A: check if there's a reason to do a sync with remote (= forced sync or remote CTag changed)
boolean fetchCollection = (deletedRemotely + addedRemotely + updatedRemotely) > 0;
// PHASE 3A: check if there's a reason to do a sync with remote (= forced sync or remote CTag changed)
boolean syncMembers = (deletedRemotely + addedRemotely + updatedRemotely) > 0;
if (manualSync) {
Log.i(TAG, "Synchronization forced");
fetchCollection = true;
Log.i(TAG, "Full synchronization forced");
syncMembers = true;
}
if (!fetchCollection) {
String currentCTag = remote.getCTag(),
if (!syncMembers) {
final String
currentCTag = collectionResource.getCTag(),
lastCTag = local.getCTag();
Log.d(TAG, "Last local CTag = " + lastCTag + "; current remote CTag = " + currentCTag);
if (currentCTag == null || !currentCTag.equals(lastCTag))
fetchCollection = true;
syncMembers = true;
}
if (!fetchCollection) {
if (!syncMembers) {
Log.i(TAG, "No local changes and CTags match, no need to sync");
return;
}
// PHASE 2B: detect details of remote changes
// PHASE 3B: detect details of remote changes
Log.i(TAG, "Fetching remote resource list");
Set<Resource> remotelyAdded = new HashSet<>(),
remotelyUpdated = new HashSet<>();
@ -83,7 +90,7 @@ public class SyncManager {
}
}
// PHASE 3: pull remote changes from server
// PHASE 4: pull remote changes from server
syncResult.stats.numInserts = pullNew(remotelyAdded.toArray(new Resource[remotelyAdded.size()]));
syncResult.stats.numUpdates = pullChanged(remotelyUpdated.toArray(new Resource[remotelyUpdated.size()]));
@ -94,7 +101,7 @@ public class SyncManager {
// update collection CTag
Log.i(TAG, "Sync complete, fetching new CTag");
local.setCTag(remote.getCTag());
local.setCTag(collectionResource.getCTag());
}

View File

@ -23,7 +23,7 @@ import java.util.Map;
import at.bitfire.davdroid.resource.CalDavTaskList;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.LocalTaskList;
import at.bitfire.davdroid.resource.RemoteCollection;
import at.bitfire.davdroid.resource.WebDavCollection;
public class TasksSyncAdapterService extends Service {
private static SyncAdapter syncAdapter;
@ -54,17 +54,17 @@ public class TasksSyncAdapterService extends Service {
}
@Override
protected Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
protected Map<LocalCollection<?>, WebDavCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
AccountSettings settings = new AccountSettings(getContext(), account);
String userName = settings.getUserName(),
password = settings.getPassword();
boolean preemptive = settings.getPreemptiveAuth();
try {
Map<LocalCollection<?>, RemoteCollection<?>> map = new HashMap<>();
Map<LocalCollection<?>, WebDavCollection<?>> map = new HashMap<>();
for (LocalTaskList calendar : LocalTaskList.findAll(account, provider)) {
RemoteCollection<?> dav = new CalDavTaskList(httpClient, calendar.getUrl(), userName, password, preemptive);
WebDavCollection<?> dav = new CalDavTaskList(httpClient, calendar.getUrl(), userName, password, preemptive);
map.put(calendar, dav);
}
return map;

View File

@ -28,7 +28,7 @@ public class HttpPropfind extends HttpEntityEnclosingRequestBaseHC4 {
HOME_SETS,
CARDDAV_COLLECTIONS,
CALDAV_COLLECTIONS,
COLLECTION_CTAG,
COLLECTION_PROPERTIES,
MEMBERS_ETAG
}
@ -70,8 +70,11 @@ public class HttpPropfind extends HttpEntityEnclosingRequestBaseHC4 {
propfind.prop.calendarTimezone = new DavProp.CalendarTimezone();
propfind.prop.supportedCalendarComponentSet = new LinkedList<>();
break;
case COLLECTION_CTAG:
case COLLECTION_PROPERTIES:
propfind.prop.getctag = new DavProp.GetCTag();
propfind.prop.resourcetype = new DavProp.ResourceType();
propfind.prop.displayname = new DavProp.DisplayName();
propfind.prop.calendarColor = new DavProp.CalendarColor();
break;
case MEMBERS_ETAG:
depth = 1;