diff --git a/app/src/main/java/com/etesync/syncadapter/App.kt b/app/src/main/java/com/etesync/syncadapter/App.kt index 7492c8f7..0c21c6a7 100644 --- a/app/src/main/java/com/etesync/syncadapter/App.kt +++ b/app/src/main/java/com/etesync/syncadapter/App.kt @@ -67,6 +67,7 @@ import java.util.logging.Logger import javax.net.ssl.HostnameVerifier import at.bitfire.cert4android.CustomCertManager +import at.bitfire.ical4android.AndroidCalendar import at.bitfire.ical4android.CalendarStorageException import at.bitfire.vcard4android.ContactsStorageException import io.requery.Persistable @@ -294,8 +295,8 @@ class App : Application() { // Generate account settings to make sure account is migrated. AccountSettings(this, account) - val calendars = LocalCalendar.find(account, this.contentResolver.acquireContentProviderClient(CalendarContract.CONTENT_URI)!!, - LocalCalendar.Factory.INSTANCE, null, null) as Array + val calendars = AndroidCalendar.find(account, this.contentResolver.acquireContentProviderClient(CalendarContract.CONTENT_URI)!!, + LocalCalendar.Factory, null, null) for (calendar in calendars) { calendar.fixEtags() } diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt index 92586d38..09674279 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt @@ -65,7 +65,7 @@ class LocalAddressBook( val accountManager = AccountManager.get(context) val account = Account(accountName(mainAccount, info), App.addressBookAccountType) - if (!accountManager.addAccountExplicitly(account, null, initialUserData(mainAccount, info.url.toString()))) + if (!accountManager.addAccountExplicitly(account, null, initialUserData(mainAccount, info.uid!!))) throw ContactsStorageException("Couldn't create address book account") val addressBook = LocalAddressBook(context, account, provider) @@ -276,7 +276,7 @@ class LocalAddressBook( try { val cursor = provider?.query(syncAdapterURI(RawContacts.CONTENT_URI), null, null, null, null) try { - return cursor?.count.toLong() + return cursor?.count?.toLong()!! } finally { cursor?.close() } @@ -286,31 +286,6 @@ class LocalAddressBook( } - internal fun getByGroupMembership(groupID: Long): Array { - try { - val cursor = provider.query(syncAdapterURI(ContactsContract.Data.CONTENT_URI), - arrayOf(RawContacts.Data.RAW_CONTACT_ID), - "(" + GroupMembership.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?) OR (" + CachedGroupMembership.MIMETYPE + "=? AND " + CachedGroupMembership.GROUP_ID + "=?)", - arrayOf(GroupMembership.CONTENT_ITEM_TYPE, groupID.toString(), CachedGroupMembership.CONTENT_ITEM_TYPE, groupID.toString()), null) - - val ids = HashSet() - while (cursor != null && cursor.moveToNext()) - ids.add(cursor.getLong(0)) - - cursor!!.close() - - val contacts = arrayOfNulls(ids.size) - var i = 0 - for (id in ids) - contacts[i++] = LocalContact(this, id, null, null) - return contacts - } catch (e: RemoteException) { - throw ContactsStorageException("Couldn't query contacts", e) - } - - } - - fun deleteAll() { try { provider?.delete(syncAdapterURI(RawContacts.CONTENT_URI), null, null) @@ -322,6 +297,22 @@ class LocalAddressBook( } + /* special group operations */ + fun getByGroupMembership(groupID: Long): List { + val ids = HashSet() + provider!!.query(syncAdapterURI(ContactsContract.Data.CONTENT_URI), + arrayOf(RawContacts.Data.RAW_CONTACT_ID), + "(${GroupMembership.MIMETYPE}=? AND ${GroupMembership.GROUP_ROW_ID}=?) OR (${CachedGroupMembership.MIMETYPE}=? AND ${CachedGroupMembership.GROUP_ID}=?)", + arrayOf(GroupMembership.CONTENT_ITEM_TYPE, groupID.toString(), CachedGroupMembership.CONTENT_ITEM_TYPE, groupID.toString()), + null)?.use { cursor -> + while (cursor.moveToNext()) + ids += cursor.getLong(0) + } + + return ids.map { findContactByID(it) } + } + + /* special group operations */ /** @@ -356,7 +347,6 @@ class LocalAddressBook( /** Fix all of the etags of all of the non-dirty contacts to be non-null. * Currently set to all ones. */ - @Throws(ContactsStorageException::class) fun fixEtags() { val newEtag = "1111111111111111111111111111111111111111111111111111111111111111" val where = ContactsContract.RawContacts.DIRTY + "=0 AND " + AndroidContact.COLUMN_ETAG + " IS NULL" @@ -364,7 +354,7 @@ class LocalAddressBook( val values = ContentValues(1) values.put(AndroidContact.COLUMN_ETAG, newEtag) try { - val fixed = provider.update(syncAdapterURI(RawContacts.CONTENT_URI), + val fixed = provider?.update(syncAdapterURI(RawContacts.CONTENT_URI), values, where, null) App.log.info("Fixed entries: " + fixed.toString()) } catch (e: RemoteException) { diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.kt index 4218854a..a54c6022 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.kt @@ -201,7 +201,7 @@ class LocalCalendar private constructor( syncAdapterURI(Events.CONTENT_URI), null, Events.CALENDAR_ID + "=?", arrayOf(id.toString()), null) try { - return cursor?.count.toLong() + return cursor?.count?.toLong()!! } finally { cursor?.close() } diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalContact.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalContact.kt index da23b459..428add3e 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalContact.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalContact.kt @@ -117,7 +117,7 @@ class LocalContact : AndroidContact, LocalAddress { val values = ContentValues(2) values.put(AndroidContact.COLUMN_FILENAME, uid) values.put(AndroidContact.COLUMN_UID, uid) - addressBook.provider.update(rawContactSyncURI(), values, null, null) + addressBook.provider?.update(rawContactSyncURI(), values, null, null) fileName = uid } @@ -133,7 +133,7 @@ class LocalContact : AndroidContact, LocalAddress { override fun insertDataRows(batch: BatchOperation) { super.insertDataRows(batch) - if (contact.unknownProperties != null) { + if (contact?.unknownProperties != null) { val op: BatchOperation.Operation val builder = ContentProviderOperation.newInsert(dataSyncURI()) if (id == null) { @@ -143,7 +143,7 @@ class LocalContact : AndroidContact, LocalAddress { builder.withValue(UnknownProperties.RAW_CONTACT_ID, id) } builder.withValue(UnknownProperties.MIMETYPE, UnknownProperties.CONTENT_ITEM_TYPE) - .withValue(UnknownProperties.UNKNOWN_PROPERTIES, contact.unknownProperties) + .withValue(UnknownProperties.UNKNOWN_PROPERTIES, contact?.unknownProperties) batch.enqueue(op) } diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalEvent.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalEvent.kt index 83c8f85d..74b30fb1 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalEvent.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalEvent.kt @@ -79,7 +79,7 @@ class LocalEvent : AndroidEvent, LocalResource { eTag = row.getAsString(COLUMN_ETAG) event?.uid = row.getAsString(COLUMN_UID) - event.sequence = row.getAsInteger(COLUMN_SEQUENCE) + event?.sequence = row.getAsInteger(COLUMN_SEQUENCE) val isOrganizer = row.getAsInteger(Events.IS_ORGANIZER) weAreOrganizer = isOrganizer != null && isOrganizer != 0 } diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalResource.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalResource.kt index d4413fac..9ce2f184 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalResource.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalResource.kt @@ -12,7 +12,7 @@ import at.bitfire.ical4android.CalendarStorageException import at.bitfire.vcard4android.ContactsStorageException interface LocalResource { - val uuid: String? + val uuid: String /** True if doesn't exist on server yet, false otherwise. */ val isLocalOnly: Boolean diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalTask.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalTask.kt index 00e26e53..9ddd0b7a 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalTask.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalTask.kt @@ -66,8 +66,8 @@ class LocalTask : AndroidTask, LocalResource { override fun buildTask(builder: ContentProviderOperation.Builder, update: Boolean) { super.buildTask(builder, update) builder.withValue(TaskContract.Tasks._SYNC_ID, fileName) - .withValue(COLUMN_UID, task.uid) - .withValue(COLUMN_SEQUENCE, task.sequence) + .withValue(COLUMN_UID, task?.uid) + .withValue(COLUMN_SEQUENCE, task?.sequence) .withValue(COLUMN_ETAG, eTag) } diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalTaskList.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalTaskList.kt index 35b1adde..d9ef05e4 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalTaskList.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalTaskList.kt @@ -80,6 +80,9 @@ class LocalTaskList private constructor( return tasks } + override fun findAll(): List + = queryTasks(null, null) + override fun findWithoutFileName(): List = queryTasks(Tasks._SYNC_ID + " IS NULL", null) @@ -89,10 +92,10 @@ class LocalTaskList private constructor( override fun count(): Long { try { val cursor = provider.client.query( - TaskProvider.syncAdapterUri(provider.tasksUri()), null, + TaskProvider.syncAdapterUri(provider.tasksUri(), account), null, Tasks.LIST_ID + "=?", arrayOf(id.toString()), null) try { - return cursor?.count.toLong() + return cursor?.count?.toLong()!! } finally { cursor?.close() }