1
0
mirror of https://github.com/etesync/android synced 2024-11-15 12:28:57 +00:00

Fix NPE for synchronization while CalDAV/CardDAV services is not available in DB

This commit is contained in:
Ricki Hirner 2016-03-30 11:16:34 +02:00
parent f1ccd01708
commit bb95a25b91
4 changed files with 48 additions and 33 deletions

View File

@ -10,7 +10,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "at.bitfire.davdroid"

View File

@ -21,6 +21,8 @@ import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.CalendarContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.LinkedHashMap;
import java.util.Map;
@ -74,9 +76,8 @@ public class CalendarsSyncAdapterService extends SyncAdapterService {
}
private void updateLocalCalendars(ContentProviderClient provider, Account account) throws CalendarStorageException {
long service = getService(account);
// enumerate remote and local calendars
Long service = getService(account);
Map<String, CollectionInfo> remote = remoteCalendars(service);
LocalCalendar[] local = (LocalCalendar[])LocalCalendar.find(account, provider, LocalCalendar.Factory.INSTANCE, null, null);
@ -104,23 +105,29 @@ public class CalendarsSyncAdapterService extends SyncAdapterService {
}
}
long getService(Account account) {
@Cleanup Cursor c = db.query(Services._TABLE, new String[]{ Services.ID },
Services.ACCOUNT_NAME + "=? AND " + Services.SERVICE + "=?", new String[]{account.name, Services.SERVICE_CALDAV}, null, null, null);
c.moveToNext();
return c.getLong(0);
@Nullable
Long getService(Account account) {
@Cleanup Cursor c = db.query(Services._TABLE, new String[] { Services.ID },
Services.ACCOUNT_NAME + "=? AND " + Services.SERVICE + "=?", new String[] { account.name, Services.SERVICE_CALDAV }, null, null, null);
if (c.moveToNext())
return c.getLong(0);
else
return null;
}
private Map<String, CollectionInfo> remoteCalendars(long service) {
@NonNull
private Map<String, CollectionInfo> remoteCalendars(Long service) {
Map<String, CollectionInfo> collections = new LinkedHashMap<>();
@Cleanup Cursor cursor = db.query(Collections._TABLE, null,
Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VEVENT + "!=0 AND " + Collections.SYNC,
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);
if (service != null) {
@Cleanup Cursor cursor = db.query(Collections._TABLE, null,
Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VEVENT + "!=0 AND " + Collections.SYNC,
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;
}

View File

@ -66,7 +66,8 @@ public class ContactsSyncAdapterService extends SyncAdapterService {
}
else
App.log.info("No address book collection selected for synchronization");
}
} else
App.log.info("No CardDAV service found in DB");
App.log.info("Address book sync complete");
}

View File

@ -20,6 +20,8 @@ import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.LinkedHashMap;
import java.util.Map;
@ -79,9 +81,8 @@ public class TasksSyncAdapterService extends SyncAdapterService {
}
private void updateLocalTaskLists(TaskProvider provider, Account account) throws CalendarStorageException {
long service = getService(account);
// enumerate remote and local task lists
Long service = getService(account);
Map<String, CollectionInfo> remote = remoteTaskLists(service);
LocalTaskList[] local = (LocalTaskList[])LocalTaskList.find(account, provider, LocalTaskList.Factory.INSTANCE, null, null);
@ -109,23 +110,29 @@ public class TasksSyncAdapterService extends SyncAdapterService {
}
}
long getService(Account account) {
@Cleanup Cursor c = db.query(Services._TABLE, new String[]{ Services.ID },
@Nullable
Long getService(Account account) {
@Cleanup Cursor c = db.query(Services._TABLE, new String[] { Services.ID },
Services.ACCOUNT_NAME + "=? AND " + Services.SERVICE + "=?", new String[] { account.name, Services.SERVICE_CALDAV }, null, null, null);
c.moveToNext();
return c.getLong(0);
if (c.moveToNext())
return c.getLong(0);
else
return null;
}
private Map<String, CollectionInfo> remoteTaskLists(long service) {
@NonNull
private Map<String, CollectionInfo> remoteTaskLists(Long service) {
Map<String, CollectionInfo> collections = new LinkedHashMap<>();
@Cleanup Cursor cursor = db.query(Collections._TABLE, null,
Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VTODO + "!=0 AND " + Collections.SYNC,
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);
if (service != null) {
@Cleanup Cursor cursor = db.query(Collections._TABLE, null,
Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VTODO + "!=0 AND " + Collections.SYNC,
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;
}