feat(Friends): Display contacts

parent e803bf28
package fr.plnech.dunbar
import android.graphics.Bitmap
import android.provider.ContactsContract
data class Friend(val map: MutableMap<String, String?>, 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
package fr.plnech.dunbar
// * @see [contacts doc](https://developer.android.com/training/contacts-provider/retrieve-names)
class Friends {
}
\ No newline at end of file
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<Friend>()
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<List<Pair<String, String>>>()
while (it.moveToNext()) {
contacts.add(mutableListOf<Pair<String, String>>().also { contact ->
mutableMapOf<String, String?>().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))
}
}
val adapter = ArrayAdapter<Friend>(
this, R.layout.contact, R.id.name,
friends
)
friends.adapter =
ArrayAdapter<String>(this, R.layout.contact, R.id.name,
contacts.map { contact ->
contact.filter { !it.second.isNullOrBlank() }
.joinToString { "${it.first}:${it.second}" }
})
friendsList.adapter = adapter
friendsList.setOnItemClickListener { parent, view, position, id ->
val contact: String = adapter.getItem(position)?.toString() ?: "NONE"
val nbFriends = friends.adapter.count
// 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
}
}
......@@ -14,30 +14,33 @@
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="5dp"
android:height="150dp"
android:src="@android:drawable/"
android:contentDescription="TODO"
android:elevation="2dp"
android:src="@drawable/user"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/name"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/avatars"
android:contentDescription="TODO" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:clickable="true"
android:focusable="true"
android:maxLines="2"
android:layout_margin="15dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="end"
android:maxLines="3"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textSize="32sp"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@id/pic"
app:layout_constraintRight_toLeftOf="@id/name"
app:layout_constraintTop_toTopOf="parent"
......
......@@ -15,15 +15,14 @@
android:layout_height="wrap_content"
tools:text="N friends on Dunbar"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/friends"
app:layout_constraintBottom_toTopOf="@id/friendsList"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/friends"
android:id="@+id/friendsList"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment