From 544121fb45b6ac75abb0cd50c9ae9e475cea4a65 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 21 Dec 2017 14:22:14 +0100 Subject: [PATCH] allow changing contact source --- .../contacts/activities/ContactActivity.kt | 18 ++++++++++++++++++ .../contacts/helpers/ContactsHelper.kt | 15 ++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt index bb880f45..d324de05 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -273,6 +273,7 @@ class ContactActivity : SimpleActivity() { surname = contact_surname.value phoneNumbers = getFilledPhoneNumbers() emails = getFilledEmails() + source = contact_source.value /*if (ContactsHelper(this@ContactActivity).updateContact(this)) { finish() @@ -332,7 +333,24 @@ class ContactActivity : SimpleActivity() { } private fun showAccountSourcePicker() { + ContactsHelper(this).getContactSources { + val items = ArrayList() + val sources = it + val currentSource = contact_source.value + var currentSourceIndex = -1 + sources.forEachIndexed { index, account -> + items.add(RadioItem(index, account)) + if (account == currentSource) { + currentSourceIndex = index + } + } + runOnUiThread { + RadioGroupDialog(this, items, currentSourceIndex) { + contact_source.text = sources[it as Int] + } + } + } } private fun trySendSMS() { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 2e3a04b1..83ae3d09 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -24,21 +24,26 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val accounts = HashSet() Thread { val uri = ContactsContract.RawContacts.CONTENT_URI - val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_NAME) + val projection = arrayOf( + ContactsContract.RawContacts.ACCOUNT_NAME + ) + + val selection = "${ContactsContract.RawContacts.DIRTY} = ?" + val selectionArgs = arrayOf("0") var cursor: Cursor? = null try { - cursor = activity.contentResolver.query(uri, projection, null, null, null) + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) if (cursor?.moveToFirst() == true) { do { - accounts.add(cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)) + val name = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) + accounts.add(name) } while (cursor.moveToNext()) } } finally { cursor?.close() } - val sourcesWithContacts = ArrayList(accounts).filter { doesSourceContainContacts(it) } as ArrayList - callback(sourcesWithContacts) + callback(ArrayList(accounts)) }.start() }