1
0
mirror of https://github.com/etesync/android synced 2025-02-16 17:42:03 +00:00
This commit is contained in:
Tom Hacohen 2019-01-06 11:33:36 +00:00
parent 88dad0a538
commit d8d5591f52
8 changed files with 35 additions and 41 deletions

View File

@ -67,6 +67,7 @@ import java.util.logging.Logger
import javax.net.ssl.HostnameVerifier import javax.net.ssl.HostnameVerifier
import at.bitfire.cert4android.CustomCertManager import at.bitfire.cert4android.CustomCertManager
import at.bitfire.ical4android.AndroidCalendar
import at.bitfire.ical4android.CalendarStorageException import at.bitfire.ical4android.CalendarStorageException
import at.bitfire.vcard4android.ContactsStorageException import at.bitfire.vcard4android.ContactsStorageException
import io.requery.Persistable import io.requery.Persistable
@ -294,8 +295,8 @@ class App : Application() {
// Generate account settings to make sure account is migrated. // Generate account settings to make sure account is migrated.
AccountSettings(this, account) AccountSettings(this, account)
val calendars = LocalCalendar.find(account, this.contentResolver.acquireContentProviderClient(CalendarContract.CONTENT_URI)!!, val calendars = AndroidCalendar.find(account, this.contentResolver.acquireContentProviderClient(CalendarContract.CONTENT_URI)!!,
LocalCalendar.Factory.INSTANCE, null, null) as Array<LocalCalendar> LocalCalendar.Factory, null, null)
for (calendar in calendars) { for (calendar in calendars) {
calendar.fixEtags() calendar.fixEtags()
} }

View File

@ -65,7 +65,7 @@ class LocalAddressBook(
val accountManager = AccountManager.get(context) val accountManager = AccountManager.get(context)
val account = Account(accountName(mainAccount, info), App.addressBookAccountType) 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") throw ContactsStorageException("Couldn't create address book account")
val addressBook = LocalAddressBook(context, account, provider) val addressBook = LocalAddressBook(context, account, provider)
@ -276,7 +276,7 @@ class LocalAddressBook(
try { try {
val cursor = provider?.query(syncAdapterURI(RawContacts.CONTENT_URI), null, null, null, null) val cursor = provider?.query(syncAdapterURI(RawContacts.CONTENT_URI), null, null, null, null)
try { try {
return cursor?.count.toLong() return cursor?.count?.toLong()!!
} finally { } finally {
cursor?.close() cursor?.close()
} }
@ -286,31 +286,6 @@ class LocalAddressBook(
} }
internal fun getByGroupMembership(groupID: Long): Array<LocalContact> {
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<Long>()
while (cursor != null && cursor.moveToNext())
ids.add(cursor.getLong(0))
cursor!!.close()
val contacts = arrayOfNulls<LocalContact>(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() { fun deleteAll() {
try { try {
provider?.delete(syncAdapterURI(RawContacts.CONTENT_URI), null, null) provider?.delete(syncAdapterURI(RawContacts.CONTENT_URI), null, null)
@ -322,6 +297,22 @@ class LocalAddressBook(
} }
/* special group operations */
fun getByGroupMembership(groupID: Long): List<LocalContact> {
val ids = HashSet<Long>()
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 */ /* 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. /** Fix all of the etags of all of the non-dirty contacts to be non-null.
* Currently set to all ones. */ * Currently set to all ones. */
@Throws(ContactsStorageException::class)
fun fixEtags() { fun fixEtags() {
val newEtag = "1111111111111111111111111111111111111111111111111111111111111111" val newEtag = "1111111111111111111111111111111111111111111111111111111111111111"
val where = ContactsContract.RawContacts.DIRTY + "=0 AND " + AndroidContact.COLUMN_ETAG + " IS NULL" val where = ContactsContract.RawContacts.DIRTY + "=0 AND " + AndroidContact.COLUMN_ETAG + " IS NULL"
@ -364,7 +354,7 @@ class LocalAddressBook(
val values = ContentValues(1) val values = ContentValues(1)
values.put(AndroidContact.COLUMN_ETAG, newEtag) values.put(AndroidContact.COLUMN_ETAG, newEtag)
try { try {
val fixed = provider.update(syncAdapterURI(RawContacts.CONTENT_URI), val fixed = provider?.update(syncAdapterURI(RawContacts.CONTENT_URI),
values, where, null) values, where, null)
App.log.info("Fixed entries: " + fixed.toString()) App.log.info("Fixed entries: " + fixed.toString())
} catch (e: RemoteException) { } catch (e: RemoteException) {

View File

@ -201,7 +201,7 @@ class LocalCalendar private constructor(
syncAdapterURI(Events.CONTENT_URI), null, syncAdapterURI(Events.CONTENT_URI), null,
Events.CALENDAR_ID + "=?", arrayOf(id.toString()), null) Events.CALENDAR_ID + "=?", arrayOf(id.toString()), null)
try { try {
return cursor?.count.toLong() return cursor?.count?.toLong()!!
} finally { } finally {
cursor?.close() cursor?.close()
} }

View File

@ -117,7 +117,7 @@ class LocalContact : AndroidContact, LocalAddress {
val values = ContentValues(2) val values = ContentValues(2)
values.put(AndroidContact.COLUMN_FILENAME, uid) values.put(AndroidContact.COLUMN_FILENAME, uid)
values.put(AndroidContact.COLUMN_UID, uid) values.put(AndroidContact.COLUMN_UID, uid)
addressBook.provider.update(rawContactSyncURI(), values, null, null) addressBook.provider?.update(rawContactSyncURI(), values, null, null)
fileName = uid fileName = uid
} }
@ -133,7 +133,7 @@ class LocalContact : AndroidContact, LocalAddress {
override fun insertDataRows(batch: BatchOperation) { override fun insertDataRows(batch: BatchOperation) {
super.insertDataRows(batch) super.insertDataRows(batch)
if (contact.unknownProperties != null) { if (contact?.unknownProperties != null) {
val op: BatchOperation.Operation val op: BatchOperation.Operation
val builder = ContentProviderOperation.newInsert(dataSyncURI()) val builder = ContentProviderOperation.newInsert(dataSyncURI())
if (id == null) { if (id == null) {
@ -143,7 +143,7 @@ class LocalContact : AndroidContact, LocalAddress {
builder.withValue(UnknownProperties.RAW_CONTACT_ID, id) builder.withValue(UnknownProperties.RAW_CONTACT_ID, id)
} }
builder.withValue(UnknownProperties.MIMETYPE, UnknownProperties.CONTENT_ITEM_TYPE) builder.withValue(UnknownProperties.MIMETYPE, UnknownProperties.CONTENT_ITEM_TYPE)
.withValue(UnknownProperties.UNKNOWN_PROPERTIES, contact.unknownProperties) .withValue(UnknownProperties.UNKNOWN_PROPERTIES, contact?.unknownProperties)
batch.enqueue(op) batch.enqueue(op)
} }

View File

@ -79,7 +79,7 @@ class LocalEvent : AndroidEvent, LocalResource<Event> {
eTag = row.getAsString(COLUMN_ETAG) eTag = row.getAsString(COLUMN_ETAG)
event?.uid = row.getAsString(COLUMN_UID) 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) val isOrganizer = row.getAsInteger(Events.IS_ORGANIZER)
weAreOrganizer = isOrganizer != null && isOrganizer != 0 weAreOrganizer = isOrganizer != null && isOrganizer != 0
} }

View File

@ -12,7 +12,7 @@ import at.bitfire.ical4android.CalendarStorageException
import at.bitfire.vcard4android.ContactsStorageException import at.bitfire.vcard4android.ContactsStorageException
interface LocalResource<in TData: Any> { interface LocalResource<in TData: Any> {
val uuid: String? val uuid: String
/** True if doesn't exist on server yet, false otherwise. */ /** True if doesn't exist on server yet, false otherwise. */
val isLocalOnly: Boolean val isLocalOnly: Boolean

View File

@ -66,8 +66,8 @@ class LocalTask : AndroidTask, LocalResource<Task> {
override fun buildTask(builder: ContentProviderOperation.Builder, update: Boolean) { override fun buildTask(builder: ContentProviderOperation.Builder, update: Boolean) {
super.buildTask(builder, update) super.buildTask(builder, update)
builder.withValue(TaskContract.Tasks._SYNC_ID, fileName) builder.withValue(TaskContract.Tasks._SYNC_ID, fileName)
.withValue(COLUMN_UID, task.uid) .withValue(COLUMN_UID, task?.uid)
.withValue(COLUMN_SEQUENCE, task.sequence) .withValue(COLUMN_SEQUENCE, task?.sequence)
.withValue(COLUMN_ETAG, eTag) .withValue(COLUMN_ETAG, eTag)
} }

View File

@ -80,6 +80,9 @@ class LocalTaskList private constructor(
return tasks return tasks
} }
override fun findAll(): List<LocalTask>
= queryTasks(null, null)
override fun findWithoutFileName(): List<LocalTask> override fun findWithoutFileName(): List<LocalTask>
= queryTasks(Tasks._SYNC_ID + " IS NULL", null) = queryTasks(Tasks._SYNC_ID + " IS NULL", null)
@ -89,10 +92,10 @@ class LocalTaskList private constructor(
override fun count(): Long { override fun count(): Long {
try { try {
val cursor = provider.client.query( val cursor = provider.client.query(
TaskProvider.syncAdapterUri(provider.tasksUri()), null, TaskProvider.syncAdapterUri(provider.tasksUri(), account), null,
Tasks.LIST_ID + "=?", arrayOf(id.toString()), null) Tasks.LIST_ID + "=?", arrayOf(id.toString()), null)
try { try {
return cursor?.count.toLong() return cursor?.count?.toLong()!!
} finally { } finally {
cursor?.close() cursor?.close()
} }