2015-03-08 22:30:03 +00:00
|
|
|
|
/*
|
2015-05-27 08:48:27 +00:00
|
|
|
|
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
|
2014-12-20 19:21:46 +00:00
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
2015-03-08 22:30:03 +00:00
|
|
|
|
*/
|
2014-12-20 19:21:46 +00:00
|
|
|
|
package at.bitfire.davdroid.syncadapter;
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
|
import android.app.Service;
|
2015-10-14 11:38:18 +00:00
|
|
|
|
import android.content.AbstractThreadedSyncAdapter;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import android.content.ContentProviderClient;
|
2016-01-20 14:22:58 +00:00
|
|
|
|
import android.content.ContentValues;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
2015-10-14 11:38:18 +00:00
|
|
|
|
import android.content.SyncResult;
|
2016-01-20 14:22:58 +00:00
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.DatabaseUtils;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2015-10-14 11:38:18 +00:00
|
|
|
|
import android.os.Bundle;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import android.os.IBinder;
|
2015-10-14 19:45:19 +00:00
|
|
|
|
import android.provider.CalendarContract;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
2016-01-20 14:22:58 +00:00
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.Map;
|
2016-02-24 22:08:19 +00:00
|
|
|
|
import java.util.logging.Level;
|
2016-01-20 14:22:58 +00:00
|
|
|
|
|
2016-02-24 22:08:19 +00:00
|
|
|
|
import at.bitfire.davdroid.App;
|
2016-01-20 14:22:58 +00:00
|
|
|
|
import at.bitfire.davdroid.model.CollectionInfo;
|
|
|
|
|
import at.bitfire.davdroid.model.ServiceDB;
|
|
|
|
|
import at.bitfire.davdroid.model.ServiceDB.OpenHelper;
|
2015-10-14 16:19:59 +00:00
|
|
|
|
import at.bitfire.davdroid.resource.LocalCalendar;
|
|
|
|
|
import at.bitfire.ical4android.CalendarStorageException;
|
2016-01-20 14:22:58 +00:00
|
|
|
|
import lombok.Cleanup;
|
2015-10-14 16:19:59 +00:00
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
public class CalendarsSyncAdapterService extends Service {
|
2016-01-20 14:22:58 +00:00
|
|
|
|
private static SyncAdapter syncAdapter;
|
2016-01-22 23:04:48 +00:00
|
|
|
|
private OpenHelper dbHelper;
|
2015-05-15 21:35:27 +00:00
|
|
|
|
|
2016-01-20 14:22:58 +00:00
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
dbHelper = new OpenHelper(this);
|
2016-01-22 23:04:48 +00:00
|
|
|
|
syncAdapter = new SyncAdapter(this, dbHelper.getReadableDatabase());
|
2016-01-20 14:22:58 +00:00
|
|
|
|
}
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
2016-01-20 14:22:58 +00:00
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
dbHelper.close();
|
|
|
|
|
}
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return syncAdapter.getSyncAdapterBinder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-14 11:38:18 +00:00
|
|
|
|
private static class SyncAdapter extends AbstractThreadedSyncAdapter {
|
2016-01-20 14:22:58 +00:00
|
|
|
|
private final SQLiteDatabase db;
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
public SyncAdapter(Context context, SQLiteDatabase db) {
|
2015-10-14 11:38:18 +00:00
|
|
|
|
super(context, false);
|
2016-01-22 23:04:48 +00:00
|
|
|
|
this.db = db;
|
2015-10-14 11:38:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.info("Starting calendar sync (" + authority + ")");
|
2015-10-14 16:19:59 +00:00
|
|
|
|
|
2016-02-23 17:42:50 +00:00
|
|
|
|
// required for ical4j and dav4android (ServiceLoader)
|
|
|
|
|
Thread.currentThread().setContextClassLoader(getContext().getClassLoader());
|
|
|
|
|
|
2015-10-14 16:19:59 +00:00
|
|
|
|
try {
|
2016-01-20 14:22:58 +00:00
|
|
|
|
updateLocalCalendars(provider, account);
|
|
|
|
|
|
2015-10-14 19:45:19 +00:00
|
|
|
|
for (LocalCalendar calendar : (LocalCalendar[])LocalCalendar.find(account, provider, LocalCalendar.Factory.INSTANCE, CalendarContract.Calendars.SYNC_EVENTS + "!=0", null)) {
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.info("Synchronizing calendar #" + calendar.getId() + ", URL: " + calendar.getName());
|
2015-10-17 09:33:35 +00:00
|
|
|
|
CalendarSyncManager syncManager = new CalendarSyncManager(getContext(), account, extras, authority, syncResult, calendar);
|
2015-10-14 16:19:59 +00:00
|
|
|
|
syncManager.performSync();
|
|
|
|
|
}
|
|
|
|
|
} catch (CalendarStorageException e) {
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.log(Level.SEVERE, "Couldn't enumerate local calendars", e);
|
2015-10-14 16:19:59 +00:00
|
|
|
|
}
|
2015-10-14 11:38:18 +00:00
|
|
|
|
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.info("Calendar sync complete");
|
2015-10-14 11:38:18 +00:00
|
|
|
|
}
|
2016-01-20 14:22:58 +00:00
|
|
|
|
|
|
|
|
|
private void updateLocalCalendars(ContentProviderClient provider, Account account) throws CalendarStorageException {
|
|
|
|
|
long service = getService(account);
|
|
|
|
|
|
|
|
|
|
// enumerate remote and local calendars
|
|
|
|
|
Map<String, CollectionInfo> remote = remoteCalendars(service);
|
|
|
|
|
LocalCalendar[] local = (LocalCalendar[])LocalCalendar.find(account, provider, LocalCalendar.Factory.INSTANCE, null, null);
|
|
|
|
|
|
|
|
|
|
// delete obsolete local calendar
|
|
|
|
|
for (LocalCalendar calendar : local) {
|
|
|
|
|
String url = calendar.getName();
|
|
|
|
|
if (!remote.containsKey(url)) {
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.fine("Deleting obsolete local calendar " + url);
|
2016-01-20 14:22:58 +00:00
|
|
|
|
calendar.delete();
|
|
|
|
|
} else {
|
|
|
|
|
// remote CollectionInfo found for this local collection, update data
|
|
|
|
|
CollectionInfo info = remote.get(url);
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.fine("Updating local calendar " + url + " with " + info);
|
2016-01-20 14:22:58 +00:00
|
|
|
|
calendar.update(info);
|
|
|
|
|
// we already have a local calendar for this remote collection, don't take into consideration anymore
|
|
|
|
|
remote.remove(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create new local calendars
|
|
|
|
|
for (String url : remote.keySet()) {
|
|
|
|
|
CollectionInfo info = remote.get(url);
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.info("Adding local calendar list " + info);
|
2016-01-20 14:22:58 +00:00
|
|
|
|
LocalCalendar.create(account, provider, info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long getService(Account account) {
|
|
|
|
|
@Cleanup Cursor c = db.query(ServiceDB.Services._TABLE, new String[]{ServiceDB.Services.ID},
|
|
|
|
|
ServiceDB.Services.ACCOUNT_NAME + "=? AND " + ServiceDB.Services.SERVICE + "=?", new String[]{account.name, ServiceDB.Services.SERVICE_CALDAV}, null, null, null);
|
|
|
|
|
c.moveToNext();
|
|
|
|
|
return c.getLong(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, CollectionInfo> remoteCalendars(long service) {
|
|
|
|
|
Map<String, CollectionInfo> collections = new LinkedHashMap<>();
|
|
|
|
|
@Cleanup Cursor cursor = db.query(ServiceDB.Collections._TABLE, ServiceDB.Collections._COLUMNS,
|
2016-01-20 20:12:37 +00:00
|
|
|
|
ServiceDB.Collections.SERVICE_ID + "=? AND " + ServiceDB.Collections.SUPPORTS_VEVENT + "!=0 AND selected",
|
2016-01-20 14:22:58 +00:00
|
|
|
|
new String[] { String.valueOf(service) }, null, null, null);
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
DatabaseUtils.cursorRowToContentValues(cursor, values);
|
|
|
|
|
CollectionInfo info = CollectionInfo.fromDB(values);
|
|
|
|
|
collections.put(info.url, info);
|
|
|
|
|
}
|
|
|
|
|
return collections;
|
|
|
|
|
}
|
2015-10-14 11:38:18 +00:00
|
|
|
|
}
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
}
|