From a36505ebd41d4d036da04b6ae8e75477e31b56dc Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Sat, 2 Nov 2019 15:04:13 +0100 Subject: [PATCH] feat(Friends): Display contacts --- app/src/main/java/fr/plnech/dunbar/Friend.kt | 26 ++++++++++++++++++++++++++ app/src/main/java/fr/plnech/dunbar/Friends.kt | 5 ----- app/src/main/java/fr/plnech/dunbar/MainActivity.kt | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- app/src/main/res/drawable/user.jpeg | Bin 0 -> 570020 bytes app/src/main/res/layout/contact.xml | 29 ++++++++++++++++------------- app/src/main/res/layout/content_main.xml | 5 ++--- 6 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/fr/plnech/dunbar/Friend.kt delete mode 100644 app/src/main/java/fr/plnech/dunbar/Friends.kt create mode 100644 app/src/main/res/drawable/user.jpeg diff --git a/app/src/main/java/fr/plnech/dunbar/Friend.kt b/app/src/main/java/fr/plnech/dunbar/Friend.kt new file mode 100644 index 0000000..f6c6a28 --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/Friend.kt @@ -0,0 +1,26 @@ +package fr.plnech.dunbar + +import android.graphics.Bitmap +import android.provider.ContactsContract + +data class Friend(val map: MutableMap, val photo: Bitmap?) { + override fun toString(): String = "$id: $name ($timesContacted times, last $lastTime)" + + val name: String? + get() = map[ContactsContract.Contacts.DISPLAY_NAME] + + val id: String + get() = map[ContactsContract.Contacts._ID]!! + + val lastTime: String? + get() = map[ContactsContract.Contacts.LAST_TIME_CONTACTED] + + val timesContacted: String? + get() = map[ContactsContract.Contacts.TIMES_CONTACTED] + + val isYou: String? + get() = map[ContactsContract.Contacts.IS_USER_PROFILE] + + val visibleOutsideSearch: String? + get() = map[ContactsContract.Contacts.IN_DEFAULT_DIRECTORY] +} \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/Friends.kt b/app/src/main/java/fr/plnech/dunbar/Friends.kt deleted file mode 100644 index c9b26b0..0000000 --- a/app/src/main/java/fr/plnech/dunbar/Friends.kt +++ /dev/null @@ -1,5 +0,0 @@ -package fr.plnech.dunbar - -// * @see [contacts doc](https://developer.android.com/training/contacts-provider/retrieve-names) -class Friends { -} \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt index 483ffc0..3f6ca05 100644 --- a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt @@ -1,7 +1,11 @@ package fr.plnech.dunbar +import android.content.ContentUris +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.os.Bundle import android.provider.ContactsContract +import android.util.Log import android.view.Menu import android.view.MenuItem import android.widget.ArrayAdapter @@ -10,20 +14,18 @@ import androidx.appcompat.app.AppCompatActivity import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.content_main.* +import java.io.IOException class MainActivity : AppCompatActivity() { - private val CONTENT_VIEW_ID = 10101010 + private val friends = mutableListOf() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) - - - fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() @@ -49,6 +51,8 @@ class MainActivity : AppCompatActivity() { } fun fetchFriends() { + Toast.makeText(this@MainActivity, "Fetching friends...", Toast.LENGTH_SHORT).show() + contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, @@ -56,31 +60,65 @@ class MainActivity : AppCompatActivity() { null, null )?.let { - - val contacts = mutableListOf>>() while (it.moveToNext()) { - contacts.add(mutableListOf>().also { contact -> + mutableMapOf().also { map -> + + val indexID = it.getColumnIndex(ContactsContract.Contacts._ID) + val id = it.getLong(indexID) + val photo: Bitmap? = getPhoto(id) + it.columnNames.forEach { name -> - contact.add(Pair(name, it.getString(it.getColumnIndex(name)))) + val index = it.getColumnIndex(name) + map[name] = it.getString(index) } - }) + friends.add(Friend(map, photo)) + } } - friends.adapter = - ArrayAdapter(this, R.layout.contact, R.id.name, - contacts.map { contact -> - contact.filter { !it.second.isNullOrBlank() } - .joinToString { "${it.first}:${it.second}" } - }) + val adapter = ArrayAdapter( + this, R.layout.contact, R.id.name, + friends + ) - val nbFriends = friends.adapter.count + friendsList.adapter = adapter + friendsList.setOnItemClickListener { parent, view, position, id -> + val contact: String = adapter.getItem(position)?.toString() ?: "NONE" + +// Snackbar.make(friendsList, contact, Snackbar.LENGTH_INDEFINITE).show() + Toast.makeText(this, contact, Toast.LENGTH_LONG).show() + } + + val nbFriends = adapter.count welcomeTitle.text = "$nbFriends ${"friend".plural(nbFriends)} on Dunbar" Toast.makeText(this@MainActivity, "$nbFriends ", Toast.LENGTH_LONG).show() - it?.close() + it.close() } } + + private fun getPhoto(id: Long): Bitmap? { + var photo: Bitmap? = null + + try { + val inputStream = ContactsContract.Contacts.openContactPhotoInputStream( + contentResolver, ContentUris.withAppendedId( + ContactsContract.Contacts.CONTENT_URI, + java.lang.Long.valueOf(id) + ) + ) + + if (inputStream != null) { + photo = BitmapFactory.decodeStream(inputStream) + } + + inputStream?.close() + } catch (e: IOException) { + e.printStackTrace() + } + Log.d("--> ", photo.toString()) + return photo + } } diff --git a/app/src/main/res/drawable/user.jpeg b/app/src/main/res/drawable/user.jpeg new file mode 100644 index 0000000..139dc99 Binary files /dev/null and b/app/src/main/res/drawable/user.jpeg differ diff --git a/app/src/main/res/layout/contact.xml b/app/src/main/res/layout/contact.xml index e423727..bf8fbe4 100644 --- a/app/src/main/res/layout/contact.xml +++ b/app/src/main/res/layout/contact.xml @@ -14,30 +14,33 @@ + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" />