1
0
mirror of https://github.com/etesync/android synced 2024-11-14 03:39:52 +00:00

add DURATION iCal->Android, collection refactoring

This commit is contained in:
rfc2822 2013-11-26 22:20:00 +01:00
parent 4f9bd5bebd
commit 8f7b48748b
8 changed files with 54 additions and 72 deletions

View File

@ -333,7 +333,9 @@ public class LocalAddressBook extends LocalCollection<Contact> {
/* content builder methods */
@Override
protected Builder buildEntry(Builder builder, Contact contact) {
protected Builder buildEntry(Builder builder, Resource resource) {
Contact contact = (Contact)resource;
return builder
.withValue(RawContacts.ACCOUNT_NAME, account.name)
.withValue(RawContacts.ACCOUNT_TYPE, account.type)
@ -345,7 +347,9 @@ public class LocalAddressBook extends LocalCollection<Contact> {
@Override
protected void addDataRows(Contact contact, long localID, int backrefIdx) {
protected void addDataRows(Resource resource, long localID, int backrefIdx) {
Contact contact = (Contact)resource;
pendingOperations.add(buildStructuredName(newDataInsertBuilder(localID, backrefIdx), contact).build());
if (contact.getNickNames() != null)
@ -375,10 +379,10 @@ public class LocalAddressBook extends LocalCollection<Contact> {
}
@Override
protected void removeDataRows(Contact contact) {
protected void removeDataRows(Resource resource) {
pendingOperations.add(ContentProviderOperation.newDelete(dataURI())
.withSelection(Data.RAW_CONTACT_ID + "=?",
new String[] { String.valueOf(contact.getLocalID()) }).build());
new String[] { String.valueOf(resource.getLocalID()) }).build());
}

View File

@ -417,7 +417,9 @@ public class LocalCalendar extends LocalCollection<Event> {
/* content builder methods */
@Override
protected Builder buildEntry(Builder builder, Event event) {
protected Builder buildEntry(Builder builder, Resource resource) {
Event event = (Event)resource;
builder = builder
.withValue(Events.CALENDAR_ID, id)
.withValue(entryColumnRemoteName(), event.getName())
@ -431,6 +433,9 @@ public class LocalCalendar extends LocalCollection<Event> {
if (event.getDtEndTzID() != null)
builder = builder.withValue(Events.EVENT_END_TIMEZONE, event.getDtEndTzID());
if (event.getDuration() != null)
builder = builder.withValue(Events.DURATION, event.getDuration().getValue());
if (event.getRrule() != null)
builder = builder.withValue(Events.RRULE, event.getRrule().getValue());
@ -466,7 +471,8 @@ public class LocalCalendar extends LocalCollection<Event> {
@Override
protected void addDataRows(Event event, long localID, int backrefIdx) {
protected void addDataRows(Resource resource, long localID, int backrefIdx) {
Event event = (Event)resource;
for (Attendee attendee : event.getAttendees())
pendingOperations.add(buildAttendee(newDataInsertBuilder(Attendees.CONTENT_URI, Attendees.EVENT_ID, localID, backrefIdx), attendee).build());
for (VAlarm alarm : event.getAlarms())
@ -474,7 +480,8 @@ public class LocalCalendar extends LocalCollection<Event> {
}
@Override
protected void removeDataRows(Event event) {
protected void removeDataRows(Resource resource) {
Event event = (Event)resource;
pendingOperations.add(ContentProviderOperation.newDelete(syncAdapterURI(Attendees.CONTENT_URI))
.withSelection(Attendees.EVENT_ID + "=?",
new String[] { String.valueOf(event.getLocalID()) }).build());

View File

@ -24,7 +24,7 @@ import android.os.RemoteException;
import android.provider.CalendarContract;
import android.util.Log;
public abstract class LocalCollection<ResourceType extends Resource> {
public abstract class LocalCollection<T extends Resource> {
private static final String TAG = "davdroid.LocalCollection";
protected Account account;
@ -72,7 +72,7 @@ public abstract class LocalCollection<ResourceType extends Resource> {
Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() },
where, null, null);
LinkedList<Resource> dirty = new LinkedList<Resource>();
LinkedList<T> dirty = new LinkedList<T>();
while (cursor != null && cursor.moveToNext())
dirty.add(findById(cursor.getLong(0), cursor.getString(1), cursor.getString(2), true));
return dirty.toArray(new Resource[0]);
@ -85,7 +85,7 @@ public abstract class LocalCollection<ResourceType extends Resource> {
Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID(), entryColumnRemoteName(), entryColumnETag() },
where, null, null);
LinkedList<Resource> deleted = new LinkedList<Resource>();
LinkedList<T> deleted = new LinkedList<T>();
while (cursor != null && cursor.moveToNext())
deleted.add(findById(cursor.getLong(0), cursor.getString(1), cursor.getString(2), false));
return deleted.toArray(new Resource[0]);
@ -98,11 +98,11 @@ public abstract class LocalCollection<ResourceType extends Resource> {
Cursor cursor = providerClient.query(entriesURI(),
new String[] { entryColumnID() },
where, null, null);
LinkedList<Resource> fresh = new LinkedList<Resource>();
LinkedList<T> fresh = new LinkedList<T>();
while (cursor != null && cursor.moveToNext()) {
String uid = UUID.randomUUID().toString(),
resourceName = uid + fileExtension();
Resource resource = findById(cursor.getLong(0), resourceName, null, true); //new Event(cursor.getLong(0), resourceName, null);
T resource = findById(cursor.getLong(0), resourceName, null, true); //new Event(cursor.getLong(0), resourceName, null);
resource.setUid(uid);
// new record: set generated resource name in database
@ -116,15 +116,15 @@ public abstract class LocalCollection<ResourceType extends Resource> {
return fresh.toArray(new Resource[0]);
}
abstract public Resource findById(long localID, String resourceName, String eTag, boolean populate) throws RemoteException;
abstract public ResourceType findByRemoteName(String name) throws RemoteException;
abstract public T findById(long localID, String resourceName, String eTag, boolean populate) throws RemoteException;
abstract public T findByRemoteName(String name) throws RemoteException;
public abstract void populate(Resource record) throws RemoteException;
// create/update/delete
public void add(ResourceType resource) throws ValidationException {
public void add(Resource resource) throws ValidationException {
resource.validate();
int idx = pendingOperations.size();
@ -136,8 +136,8 @@ public abstract class LocalCollection<ResourceType extends Resource> {
addDataRows(resource, -1, idx);
}
public void updateByRemoteName(ResourceType remoteResource) throws RemoteException, ValidationException {
ResourceType localResource = findByRemoteName(remoteResource.getName());
public void updateByRemoteName(Resource remoteResource) throws RemoteException, ValidationException {
T localResource = findByRemoteName(remoteResource.getName());
remoteResource.validate();
pendingOperations.add(
@ -197,8 +197,8 @@ public abstract class LocalCollection<ResourceType extends Resource> {
// content builders
protected abstract Builder buildEntry(Builder builder, ResourceType resource);
protected abstract Builder buildEntry(Builder builder, Resource resource);
protected abstract void addDataRows(ResourceType resource, long localID, int backrefIdx);
protected abstract void removeDataRows(ResourceType resource);
protected abstract void addDataRows(Resource resource, long localID, int backrefIdx);
protected abstract void removeDataRows(Resource resource);
}

View File

@ -27,14 +27,14 @@ import at.bitfire.davdroid.webdav.WebDavResource;
import at.bitfire.davdroid.webdav.WebDavResource.MultigetType;
import at.bitfire.davdroid.webdav.WebDavResource.PutMode;
public abstract class RemoteCollection<ResourceType extends Resource> {
public abstract class RemoteCollection<T extends Resource> {
private static final String TAG = "davdroid.RemoteCollection";
@Getter WebDavResource collection;
abstract protected String memberContentType();
abstract protected MultigetType multiGetType();
abstract protected ResourceType newResourceSkeleton(String name, String ETag);
abstract protected T newResourceSkeleton(String name, String ETag);
public RemoteCollection(String baseURL, String user, String password, boolean preemptiveAuth) throws URISyntaxException {
collection = new WebDavResource(new URI(baseURL), user, password, preemptiveAuth, true);
@ -56,7 +56,7 @@ public abstract class RemoteCollection<ResourceType extends Resource> {
public Resource[] getMemberETags() throws IOException, InvalidDavResponseException, HttpException {
collection.propfind(HttpPropfind.Mode.MEMBERS_ETAG);
List<ResourceType> resources = new LinkedList<ResourceType>();
List<T> resources = new LinkedList<T>();
if (collection.getMembers() != null) {
for (WebDavResource member : collection.getMembers())
resources.add(newResourceSkeleton(member.getName(), member.getETag()));
@ -66,23 +66,23 @@ public abstract class RemoteCollection<ResourceType extends Resource> {
}
@SuppressWarnings("unchecked")
public Resource[] multiGet(ResourceType[] resources) throws IOException, InvalidDavResponseException, HttpException {
public Resource[] multiGet(Resource[] resources) throws IOException, InvalidDavResponseException, HttpException {
try {
if (resources.length == 1) {
Resource resource = get(resources[0]);
return (resource != null) ? (ResourceType[]) new Resource[] { resource } : null;
return (resource != null) ? (T[]) new Resource[] { resource } : null;
}
LinkedList<String> names = new LinkedList<String>();
for (ResourceType resource : resources)
for (Resource resource : resources)
names.add(resource.getName());
collection.multiGet(names.toArray(new String[0]), multiGetType());
LinkedList<ResourceType> foundResources = new LinkedList<ResourceType>();
LinkedList<T> foundResources = new LinkedList<T>();
if (collection.getMembers() != null)
for (WebDavResource member : collection.getMembers()) {
ResourceType resource = newResourceSkeleton(member.getName(), member.getETag());
T resource = newResourceSkeleton(member.getName(), member.getETag());
try {
InputStream is = member.getContent();
if (is != null) {
@ -108,11 +108,11 @@ public abstract class RemoteCollection<ResourceType extends Resource> {
/* internal member operations */
public ResourceType get(ResourceType resource) throws IOException, HttpException, ParserException {
WebDavResource member = new WebDavResource(collection, resource.getName());
public Resource get(Resource resources) throws IOException, HttpException, ParserException {
WebDavResource member = new WebDavResource(collection, resources.getName());
member.get();
resource.parseEntity(member.getContent());
return resource;
resources.parseEntity(member.getContent());
return resources;
}
public void add(Resource res) throws IOException, HttpException, ValidationException {

View File

@ -7,39 +7,25 @@
******************************************************************************/
package at.bitfire.davdroid.syncadapter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import lombok.Synchronized;
import net.fortuna.ical4j.data.ParserException;
import org.apache.http.HttpException;
import org.apache.http.auth.AuthenticationException;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.content.SyncResult;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.resource.CalDavCalendar;
import at.bitfire.davdroid.resource.IncapableResourceException;
import at.bitfire.davdroid.resource.LocalCalendar;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.RemoteCollection;
import at.bitfire.davdroid.webdav.WebDavResource;
public class CalendarsSyncAdapterService extends Service {
private static SyncAdapter syncAdapter;
@ -63,13 +49,13 @@ public class CalendarsSyncAdapterService extends Service {
}
@Override
protected Map<LocalCollection, RemoteCollection> getSyncPairs(Account account, ContentProviderClient provider) {
protected Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
try {
Map<LocalCollection, RemoteCollection> map = new HashMap<LocalCollection, RemoteCollection>();
Map<LocalCollection<?>, RemoteCollection<?>> map = new HashMap<LocalCollection<?>, RemoteCollection<?>>();
for (LocalCalendar calendar : LocalCalendar.findAll(account, provider)) {
URI uri = new URI(accountManager.getUserData(account, Constants.ACCOUNT_KEY_BASE_URL)).resolve(calendar.getPath());
RemoteCollection dav = new CalDavCalendar(uri.toString(),
RemoteCollection<?> dav = new CalDavCalendar(uri.toString(),
accountManager.getUserData(account, Constants.ACCOUNT_KEY_USERNAME),
accountManager.getPassword(account),
Boolean.parseBoolean(accountManager.getUserData(account, Constants.ACCOUNT_KEY_AUTH_PREEMPTIVE)));

View File

@ -7,36 +7,21 @@
******************************************************************************/
package at.bitfire.davdroid.syncadapter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.NonNull;
import lombok.Synchronized;
import net.fortuna.ical4j.data.ParserException;
import org.apache.http.HttpException;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.content.SyncResult;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.resource.CardDavAddressBook;
import at.bitfire.davdroid.resource.IncapableResourceException;
import at.bitfire.davdroid.resource.LocalAddressBook;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.RemoteCollection;
@ -63,22 +48,22 @@ public class ContactsSyncAdapterService extends Service {
}
@Override
protected Map<LocalCollection, RemoteCollection> getSyncPairs(Account account, ContentProviderClient provider) {
protected Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
String addressBookPath = accountManager.getUserData(account, Constants.ACCOUNT_KEY_ADDRESSBOOK_PATH);
if (addressBookPath == null)
return null;
try {
LocalCollection database = new LocalAddressBook(account, provider, accountManager);
LocalCollection<?> database = new LocalAddressBook(account, provider, accountManager);
URI uri = new URI(accountManager.getUserData(account, Constants.ACCOUNT_KEY_BASE_URL)).resolve(addressBookPath);
RemoteCollection dav = new CardDavAddressBook(
RemoteCollection<?> dav = new CardDavAddressBook(
uri.toString(),
accountManager.getUserData(account, Constants.ACCOUNT_KEY_USERNAME),
accountManager.getPassword(account),
Boolean.parseBoolean(accountManager.getUserData(account, Constants.ACCOUNT_KEY_AUTH_PREEMPTIVE)));
Map<LocalCollection, RemoteCollection> map = new HashMap<LocalCollection, RemoteCollection>();
Map<LocalCollection<?>, RemoteCollection<?>> map = new HashMap<LocalCollection<?>, RemoteCollection<?>>();
map.put(database, dav);
return map;

View File

@ -33,7 +33,7 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter {
}
protected abstract Map<LocalCollection, RemoteCollection> getSyncPairs(Account account, ContentProviderClient provider);
protected abstract Map<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider);
@Override
@ -45,12 +45,12 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter {
SyncManager syncManager = new SyncManager(account, accountManager);
Map<LocalCollection, RemoteCollection> syncCollections = getSyncPairs(account, provider);
Map<LocalCollection<?>, RemoteCollection<?>> 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<?>, RemoteCollection<?>> entry : syncCollections.entrySet())
syncManager.synchronize(entry.getKey(), entry.getValue(), extras.containsKey(ContentResolver.SYNC_EXTRAS_MANUAL), syncResult);
} catch (AuthenticationException ex) {

View File

@ -40,7 +40,7 @@ public class SyncManager {
this.accountManager = accountManager;
}
public void synchronize(LocalCollection local, RemoteCollection dav, boolean manualSync, SyncResult syncResult) throws RemoteException, OperationApplicationException, IOException, HttpException {
public void synchronize(LocalCollection<? extends Resource> local, RemoteCollection<? extends Resource> dav, boolean manualSync, SyncResult syncResult) throws RemoteException, OperationApplicationException, IOException, HttpException {
boolean fetchCollection = false;
// PHASE 1: UPLOAD LOCALLY-CHANGED RESOURCES