2016-01-19 12:51:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package at.bitfire.davdroid;
|
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import android.accounts.Account;
|
2016-01-19 23:39:10 +00:00
|
|
|
|
import android.accounts.AccountManager;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import android.app.Service;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import android.content.ContentValues;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import android.content.Intent;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.DatabaseUtils;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import android.os.Binder;
|
|
|
|
|
import android.os.IBinder;
|
2016-01-19 23:39:10 +00:00
|
|
|
|
import android.text.TextUtils;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import java.io.IOException;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import java.util.HashSet;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.LinkedHashSet;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import java.util.Map;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import at.bitfire.dav4android.DavResource;
|
|
|
|
|
import at.bitfire.dav4android.UrlUtils;
|
|
|
|
|
import at.bitfire.dav4android.exception.DavException;
|
|
|
|
|
import at.bitfire.dav4android.exception.HttpException;
|
|
|
|
|
import at.bitfire.dav4android.exception.NotFoundException;
|
|
|
|
|
import at.bitfire.dav4android.property.AddressbookHomeSet;
|
|
|
|
|
import at.bitfire.dav4android.property.CalendarHomeSet;
|
2016-01-22 23:04:48 +00:00
|
|
|
|
import at.bitfire.dav4android.property.GroupMembership;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import at.bitfire.davdroid.model.CollectionInfo;
|
|
|
|
|
import at.bitfire.davdroid.model.ServiceDB.*;
|
|
|
|
|
import at.bitfire.davdroid.syncadapter.AccountSettings;
|
|
|
|
|
import lombok.Cleanup;
|
|
|
|
|
import okhttp3.HttpUrl;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
|
2016-01-19 12:51:52 +00:00
|
|
|
|
public class DavService extends Service {
|
|
|
|
|
|
|
|
|
|
public static final String
|
2016-01-19 23:39:10 +00:00
|
|
|
|
ACTION_ACCOUNTS_UPDATED = "accountsUpdated",
|
2016-01-19 12:51:52 +00:00
|
|
|
|
ACTION_REFRESH_COLLECTIONS = "refreshCollections",
|
|
|
|
|
EXTRA_DAV_SERVICE_ID = "davServiceID";
|
|
|
|
|
|
|
|
|
|
private final IBinder binder = new InfoBinder();
|
|
|
|
|
|
|
|
|
|
private final Set<Long> runningRefresh = new HashSet<>();
|
|
|
|
|
private final List<RefreshingStatusListener> refreshingStatusListeners = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
long id = intent.getLongExtra(EXTRA_DAV_SERVICE_ID, -1);
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
2016-01-19 23:39:10 +00:00
|
|
|
|
case ACTION_ACCOUNTS_UPDATED:
|
|
|
|
|
cleanupAccounts();
|
|
|
|
|
break;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
case ACTION_REFRESH_COLLECTIONS:
|
|
|
|
|
if (runningRefresh.add(id)) {
|
|
|
|
|
new Thread(new RefreshCollections(id)).start();
|
|
|
|
|
for (RefreshingStatusListener listener : refreshingStatusListeners)
|
|
|
|
|
listener.onDavRefreshStatusChanged(id, true);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* BOUND SERVICE PART
|
|
|
|
|
for communicating with the activities
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return binder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface RefreshingStatusListener {
|
|
|
|
|
void onDavRefreshStatusChanged(long id, boolean refreshing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InfoBinder extends Binder {
|
|
|
|
|
public boolean isRefreshing(long id) {
|
|
|
|
|
return runningRefresh.contains(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addRefreshingStatusListener(RefreshingStatusListener listener, boolean callImmediate) {
|
|
|
|
|
refreshingStatusListeners.add(listener);
|
|
|
|
|
if (callImmediate)
|
|
|
|
|
for (long id : runningRefresh)
|
|
|
|
|
listener.onDavRefreshStatusChanged(id, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeRefreshingStatusListener(RefreshingStatusListener listener) {
|
|
|
|
|
refreshingStatusListeners.remove(listener);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ACTION RUNNABLES
|
|
|
|
|
which actually do the work
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-19 23:39:10 +00:00
|
|
|
|
void cleanupAccounts() {
|
|
|
|
|
Constants.log.info("[DavService] Cleaning up orphaned accounts");
|
|
|
|
|
|
|
|
|
|
final OpenHelper dbHelper = new OpenHelper(this);
|
|
|
|
|
try {
|
|
|
|
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
|
|
List<String> sqlAccountNames = new LinkedList<>();
|
|
|
|
|
AccountManager am = AccountManager.get(this);
|
|
|
|
|
for (Account account : am.getAccountsByType(Constants.ACCOUNT_TYPE))
|
|
|
|
|
sqlAccountNames.add(DatabaseUtils.sqlEscapeString(account.name));
|
|
|
|
|
|
|
|
|
|
if (sqlAccountNames.isEmpty())
|
|
|
|
|
db.delete(Services._TABLE, null, null);
|
|
|
|
|
else
|
|
|
|
|
db.delete(Services._TABLE, Services.ACCOUNT_NAME + " NOT IN (" + TextUtils.join(",", sqlAccountNames) + ")", null);
|
|
|
|
|
} finally {
|
|
|
|
|
dbHelper.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 12:51:52 +00:00
|
|
|
|
private class RefreshCollections implements Runnable {
|
2016-01-19 19:04:25 +00:00
|
|
|
|
final long service;
|
|
|
|
|
final OpenHelper dbHelper;
|
|
|
|
|
SQLiteDatabase db;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
|
|
|
|
|
RefreshCollections(long davServiceId) {
|
2016-01-19 19:04:25 +00:00
|
|
|
|
this.service = davServiceId;
|
|
|
|
|
dbHelper = new OpenHelper(DavService.this);
|
2016-01-19 12:51:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
2016-01-19 19:04:25 +00:00
|
|
|
|
db = dbHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
|
|
String serviceType = serviceType();
|
|
|
|
|
Constants.log.info("[DavService {}] Refreshing {} collections", service, serviceType);
|
|
|
|
|
|
|
|
|
|
// create authenticating OkHttpClient (credentials taken from account settings)
|
|
|
|
|
OkHttpClient httpClient = httpClient();
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
// refresh home sets: principal
|
2016-01-19 19:04:25 +00:00
|
|
|
|
Set<HttpUrl> homeSets = readHomeSets();
|
|
|
|
|
HttpUrl principal = readPrincipal();
|
|
|
|
|
if (principal != null) {
|
|
|
|
|
Constants.log.debug("[DavService {}] Querying principal for home sets", service);
|
|
|
|
|
DavResource dav = new DavResource(null, httpClient, principal);
|
2016-01-22 23:04:48 +00:00
|
|
|
|
queryHomeSets(serviceType, httpClient, principal, homeSets);
|
|
|
|
|
|
|
|
|
|
// refresh home sets: direct group memberships
|
|
|
|
|
GroupMembership groupMembership = (GroupMembership)dav.properties.get(GroupMembership.NAME);
|
|
|
|
|
if (groupMembership != null)
|
|
|
|
|
for (String href : groupMembership.hrefs) {
|
|
|
|
|
Constants.log.debug("[DavService {}] Querying member group {} for home sets", href);
|
|
|
|
|
queryHomeSets(serviceType, httpClient, dav.location.resolve(href), homeSets);
|
|
|
|
|
}
|
2016-01-19 19:04:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
// now refresh collections (taken from home sets)
|
2016-01-19 19:04:25 +00:00
|
|
|
|
Map<HttpUrl, CollectionInfo> collections = readCollections();
|
2016-01-22 23:04:48 +00:00
|
|
|
|
|
|
|
|
|
// (remember selections before)
|
|
|
|
|
Set<HttpUrl> selectedCollections = new HashSet<>();
|
|
|
|
|
for (CollectionInfo info : collections.values())
|
|
|
|
|
if (info.selected)
|
|
|
|
|
selectedCollections.add(HttpUrl.parse(info.url));
|
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
for (Iterator<HttpUrl> iterator = homeSets.iterator(); iterator.hasNext();) {
|
|
|
|
|
HttpUrl homeSet = iterator.next();
|
|
|
|
|
Constants.log.debug("[DavService {}] Listing home set {}", service, homeSet);
|
|
|
|
|
|
|
|
|
|
DavResource dav = new DavResource(null, httpClient, homeSet);
|
|
|
|
|
try {
|
|
|
|
|
dav.propfind(1, CollectionInfo.DAV_PROPERTIES);
|
|
|
|
|
for (DavResource member : dav.members) {
|
|
|
|
|
CollectionInfo info = CollectionInfo.fromDavResource(member);
|
|
|
|
|
info.confirmed = true;
|
|
|
|
|
Constants.log.debug("[DavService {}] Found collection {}", service, info);
|
|
|
|
|
|
|
|
|
|
if ((serviceType.equals(Services.SERVICE_CARDDAV) && info.type == CollectionInfo.Type.ADDRESS_BOOK) ||
|
|
|
|
|
(serviceType.equals(Services.SERVICE_CALDAV) && info.type == CollectionInfo.Type.CALENDAR))
|
|
|
|
|
collections.put(member.location, info);
|
|
|
|
|
}
|
|
|
|
|
} catch(NotFoundException e) {
|
|
|
|
|
// 404, remove home set
|
|
|
|
|
iterator.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check/refresh unconfirmed collections
|
|
|
|
|
for (Iterator<Map.Entry<HttpUrl, CollectionInfo>> iterator = collections.entrySet().iterator(); iterator.hasNext();) {
|
|
|
|
|
Map.Entry<HttpUrl, CollectionInfo> entry = iterator.next();
|
|
|
|
|
HttpUrl url = entry.getKey();
|
|
|
|
|
CollectionInfo info = entry.getValue();
|
|
|
|
|
|
|
|
|
|
if (!info.confirmed)
|
|
|
|
|
try {
|
|
|
|
|
DavResource dav = new DavResource(null, httpClient, url);
|
|
|
|
|
dav.propfind(0, CollectionInfo.DAV_PROPERTIES);
|
|
|
|
|
info = CollectionInfo.fromDavResource(dav);
|
|
|
|
|
info.confirmed = true;
|
|
|
|
|
|
|
|
|
|
// remove unusable collections
|
|
|
|
|
if ((serviceType.equals(Services.SERVICE_CARDDAV) && info.type != CollectionInfo.Type.ADDRESS_BOOK) ||
|
|
|
|
|
(serviceType.equals(Services.SERVICE_CALDAV) && info.type != CollectionInfo.Type.CALENDAR))
|
|
|
|
|
iterator.remove();
|
|
|
|
|
} catch(NotFoundException e) {
|
|
|
|
|
// 404, remove collection
|
|
|
|
|
iterator.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
// restore selections
|
|
|
|
|
for (HttpUrl url : selectedCollections) {
|
|
|
|
|
CollectionInfo info = collections.get(url);
|
|
|
|
|
if (info != null)
|
|
|
|
|
info.selected = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 23:39:10 +00:00
|
|
|
|
try {
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
saveHomeSets(homeSets);
|
|
|
|
|
saveCollections(collections.values());
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IOException|HttpException|DavException e) {
|
2016-01-19 19:04:25 +00:00
|
|
|
|
Constants.log.error("Couldn't refresh collection list", e);
|
2016-01-19 12:51:52 +00:00
|
|
|
|
} finally {
|
2016-01-19 19:04:25 +00:00
|
|
|
|
dbHelper.close();
|
|
|
|
|
|
|
|
|
|
runningRefresh.remove(service);
|
2016-01-19 12:51:52 +00:00
|
|
|
|
for (RefreshingStatusListener listener : refreshingStatusListeners)
|
2016-01-19 19:04:25 +00:00
|
|
|
|
listener.onDavRefreshStatusChanged(service, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
private void queryHomeSets(String serviceType, OkHttpClient client, HttpUrl url, Set<HttpUrl> homeSets) throws IOException, HttpException, DavException {
|
|
|
|
|
DavResource dav = new DavResource(null, client, url);
|
|
|
|
|
if (Services.SERVICE_CARDDAV.equals(serviceType)) {
|
|
|
|
|
dav.propfind(0, AddressbookHomeSet.NAME, GroupMembership.NAME);
|
|
|
|
|
AddressbookHomeSet addressbookHomeSet = (AddressbookHomeSet)dav.properties.get(AddressbookHomeSet.NAME);
|
|
|
|
|
if (addressbookHomeSet != null)
|
|
|
|
|
for (String href : addressbookHomeSet.hrefs)
|
|
|
|
|
homeSets.add(UrlUtils.withTrailingSlash(dav.location.resolve(href)));
|
|
|
|
|
} else if (Services.SERVICE_CALDAV.equals(serviceType)) {
|
|
|
|
|
dav.propfind(0, CalendarHomeSet.NAME, GroupMembership.NAME);
|
|
|
|
|
CalendarHomeSet calendarHomeSet = (CalendarHomeSet)dav.properties.get(CalendarHomeSet.NAME);
|
|
|
|
|
if (calendarHomeSet != null)
|
|
|
|
|
for (String href : calendarHomeSet.hrefs)
|
|
|
|
|
homeSets.add(UrlUtils.withTrailingSlash(dav.location.resolve(href)));
|
|
|
|
|
}
|
2016-01-19 19:04:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
private OkHttpClient httpClient() {
|
2016-01-22 23:04:48 +00:00
|
|
|
|
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[]{Services.ACCOUNT_NAME}, Services.ID + "=?", new String[] { String.valueOf(service) }, null, null, null);
|
2016-01-19 19:04:25 +00:00
|
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
|
Account account = new Account(cursor.getString(0), Constants.ACCOUNT_TYPE);
|
|
|
|
|
AccountSettings settings = new AccountSettings(DavService.this, account);
|
|
|
|
|
|
|
|
|
|
OkHttpClient httpClient = HttpClient.create(DavService.this);
|
|
|
|
|
httpClient = HttpClient.addAuthentication(httpClient, settings.username(), settings.password(), settings.preemptiveAuth());
|
|
|
|
|
return httpClient;
|
|
|
|
|
} else
|
|
|
|
|
throw new IllegalArgumentException("Service not found");
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 23:04:48 +00:00
|
|
|
|
private String serviceType() {
|
|
|
|
|
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[]{Services.SERVICE}, Services.ID + "=?", new String[] { String.valueOf(service) }, null, null, null);
|
|
|
|
|
if (cursor.moveToNext())
|
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
else
|
|
|
|
|
throw new IllegalArgumentException("Service not found");
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
private HttpUrl readPrincipal() {
|
|
|
|
|
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[]{Services.PRINCIPAL}, Services.ID + "=?", new String[]{String.valueOf(service)}, null, null, null);
|
|
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
|
String principal = cursor.getString(0);
|
|
|
|
|
if (principal != null)
|
|
|
|
|
return HttpUrl.parse(cursor.getString(0));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Set<HttpUrl> readHomeSets() {
|
|
|
|
|
Set<HttpUrl> homeSets = new LinkedHashSet<>();
|
|
|
|
|
@Cleanup Cursor cursor = db.query(HomeSets._TABLE, new String[]{HomeSets.URL}, HomeSets.SERVICE_ID + "=?", new String[]{String.valueOf(service)}, null, null, null);
|
|
|
|
|
while (cursor.moveToNext())
|
|
|
|
|
homeSets.add(HttpUrl.parse(cursor.getString(0)));
|
|
|
|
|
return homeSets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveHomeSets(Set<HttpUrl> homeSets) {
|
|
|
|
|
db.delete(HomeSets._TABLE, HomeSets.SERVICE_ID + "=?", new String[]{String.valueOf(service)});
|
|
|
|
|
for (HttpUrl homeSet : homeSets) {
|
|
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
|
values.put(HomeSets.SERVICE_ID, service);
|
|
|
|
|
values.put(HomeSets.URL, homeSet.toString());
|
|
|
|
|
db.insertOrThrow(HomeSets._TABLE, null, values);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<HttpUrl, CollectionInfo> readCollections() {
|
|
|
|
|
Map<HttpUrl, CollectionInfo> collections = new LinkedHashMap<>();
|
|
|
|
|
@Cleanup Cursor cursor = db.query(Collections._TABLE, Collections._COLUMNS, Collections.SERVICE_ID + "=?", new String[]{String.valueOf(service)}, null, null, null);
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
DatabaseUtils.cursorRowToContentValues(cursor, values);
|
|
|
|
|
collections.put(HttpUrl.parse(values.getAsString(Collections.URL)), CollectionInfo.fromDB(values));
|
|
|
|
|
}
|
|
|
|
|
return collections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveCollections(Iterable<CollectionInfo> collections) {
|
|
|
|
|
db.delete(Collections._TABLE, HomeSets.SERVICE_ID + "=?", new String[]{String.valueOf(service)});
|
|
|
|
|
for (CollectionInfo collection : collections) {
|
|
|
|
|
ContentValues values = collection.toDB();
|
|
|
|
|
Constants.log.debug("Saving collection: {}", values);
|
|
|
|
|
values.put(Collections.SERVICE_ID, service);
|
|
|
|
|
db.insertWithOnConflict(Collections._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
|
2016-01-19 12:51:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|