From a9319600523804b96470e606127825f64cf865b3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 10 Dec 2017 13:28:44 +0100 Subject: [PATCH] adding an initial implementation of a Contacts fetcher --- app/build.gradle | 2 +- .../contacts/activities/MainActivity.kt | 3 ++ .../contacts/helpers/Constants.kt | 1 + .../contacts/helpers/ContactsHelper.kt | 44 ++++++++++++++++ .../contacts/models/Contact.kt | 3 ++ app/src/main/res/layout/item_contact.xml | 50 +++++++++++++++++++ 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/simplemobiletools/contacts/helpers/Constants.kt create mode 100644 app/src/main/java/com/simplemobiletools/contacts/helpers/ContactsHelper.kt create mode 100644 app/src/main/java/com/simplemobiletools/contacts/models/Contact.kt create mode 100644 app/src/main/res/layout/item_contact.xml diff --git a/app/build.gradle b/app/build.gradle index e1cfa2f7..06bfed73 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,5 +32,5 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:3.2.4' + implementation 'com.simplemobiletools:commons:3.2.5' } diff --git a/app/src/main/java/com/simplemobiletools/contacts/activities/MainActivity.kt b/app/src/main/java/com/simplemobiletools/contacts/activities/MainActivity.kt index a30a64dc..a5709603 100644 --- a/app/src/main/java/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/java/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -13,6 +13,7 @@ import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS import com.simplemobiletools.contacts.BuildConfig import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.extensions.config +import com.simplemobiletools.contacts.helpers.ContactsHelper class MainActivity : SimpleActivity() { var storedUseEnglish = false @@ -68,6 +69,8 @@ class MainActivity : SimpleActivity() { } private fun initContacts() { + ContactsHelper(this).getContacts { + } } } diff --git a/app/src/main/java/com/simplemobiletools/contacts/helpers/Constants.kt b/app/src/main/java/com/simplemobiletools/contacts/helpers/Constants.kt new file mode 100644 index 00000000..a2c4049e --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/contacts/helpers/Constants.kt @@ -0,0 +1 @@ +package com.simplemobiletools.contacts.helpers diff --git a/app/src/main/java/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/java/com/simplemobiletools/contacts/helpers/ContactsHelper.kt new file mode 100644 index 00000000..203a76d4 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -0,0 +1,44 @@ +package com.simplemobiletools.contacts.helpers + +import android.database.Cursor +import android.provider.ContactsContract +import com.simplemobiletools.commons.extensions.getIntValue +import com.simplemobiletools.commons.extensions.getStringValue +import com.simplemobiletools.commons.extensions.showErrorToast +import com.simplemobiletools.contacts.activities.SimpleActivity +import com.simplemobiletools.contacts.models.Contact + +class ContactsHelper(val activity: SimpleActivity) { + fun getContacts(callback: (ArrayList) -> Unit) { + val contacts = ArrayList() + Thread { + val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI + val projection = arrayOf( + ContactsContract.CommonDataKinds.Phone.CONTACT_ID, + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, + ContactsContract.CommonDataKinds.Phone.NUMBER, + ContactsContract.CommonDataKinds.Phone.PHOTO_URI + ) + + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID) + val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: continue + val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: continue + val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" + val contact = Contact(id, name, number, photoUri) + contacts.add(contact) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + callback(contacts) + }.start() + } +} diff --git a/app/src/main/java/com/simplemobiletools/contacts/models/Contact.kt b/app/src/main/java/com/simplemobiletools/contacts/models/Contact.kt new file mode 100644 index 00000000..b1162d38 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/contacts/models/Contact.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.contacts.models + +data class Contact(val id: Int, var name: String, var number: String, var photoUri: String) diff --git a/app/src/main/res/layout/item_contact.xml b/app/src/main/res/layout/item_contact.xml new file mode 100644 index 00000000..1948e1a6 --- /dev/null +++ b/app/src/main/res/layout/item_contact.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + +