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;
|
|
|
|
|
import android.content.ContentProviderClient;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.os.RemoteException;
|
|
|
|
|
import android.util.Log;
|
2014-12-21 01:19:06 +00:00
|
|
|
|
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
import at.bitfire.davdroid.resource.CalDavCalendar;
|
|
|
|
|
import at.bitfire.davdroid.resource.LocalCalendar;
|
|
|
|
|
import at.bitfire.davdroid.resource.LocalCollection;
|
2015-08-02 06:57:03 +00:00
|
|
|
|
import at.bitfire.davdroid.resource.WebDavCollection;
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
public class CalendarsSyncAdapterService extends Service {
|
|
|
|
|
private static SyncAdapter syncAdapter;
|
2015-05-15 21:35:27 +00:00
|
|
|
|
|
2014-12-20 19:21:46 +00:00
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
if (syncAdapter == null)
|
|
|
|
|
syncAdapter = new SyncAdapter(getApplicationContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
syncAdapter.close();
|
|
|
|
|
syncAdapter = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return syncAdapter.getSyncAdapterBinder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class SyncAdapter extends DavSyncAdapter {
|
2015-05-22 01:06:30 +00:00
|
|
|
|
private final static String TAG = "davdroid.CalendarsSync";
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
private SyncAdapter(Context context) {
|
|
|
|
|
super(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-08-02 06:57:03 +00:00
|
|
|
|
protected Map<LocalCollection<?>, WebDavCollection<?>> getSyncPairs(Account account, ContentProviderClient provider) {
|
2014-12-20 19:21:46 +00:00
|
|
|
|
AccountSettings settings = new AccountSettings(getContext(), account);
|
|
|
|
|
String userName = settings.getUserName(),
|
|
|
|
|
password = settings.getPassword();
|
|
|
|
|
boolean preemptive = settings.getPreemptiveAuth();
|
|
|
|
|
|
|
|
|
|
try {
|
2015-08-02 06:57:03 +00:00
|
|
|
|
Map<LocalCollection<?>, WebDavCollection<?>> map = new HashMap<>();
|
2014-12-20 19:21:46 +00:00
|
|
|
|
|
|
|
|
|
for (LocalCalendar calendar : LocalCalendar.findAll(account, provider)) {
|
2015-08-02 06:57:03 +00:00
|
|
|
|
WebDavCollection<?> dav = new CalDavCalendar(httpClient, calendar.getUrl(), userName, password, preemptive);
|
2014-12-20 19:21:46 +00:00
|
|
|
|
map.put(calendar, dav);
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
} catch (RemoteException ex) {
|
|
|
|
|
Log.e(TAG, "Couldn't find local calendars", ex);
|
|
|
|
|
} catch (URISyntaxException ex) {
|
|
|
|
|
Log.e(TAG, "Couldn't build calendar URI", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|