From f7f464bd57c29697f0f0d77cb2958be37339e3a8 Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Sat, 2 Nov 2019 16:39:41 +0100 Subject: [PATCH] feat(Messages): Fetch, although not needed now --- app/src/main/java/fr/plnech/dunbar/MainActivity.kt | 20 ++++++++++++++------ app/src/main/java/fr/plnech/dunbar/Messages.kt | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/fr/plnech/dunbar/Messages.kt diff --git a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt index c4becbf..8060628 100644 --- a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt @@ -5,7 +5,6 @@ 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.Toast @@ -18,6 +17,7 @@ import java.io.IOException class MainActivity : AppCompatActivity() { + private lateinit var messages: Messages private val friends = mutableListOf() override fun onCreate(savedInstanceState: Bundle?) { @@ -25,6 +25,8 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) setSupportActionBar(toolbar) +// messages = Messages(applicationContext) + fab.setOnClickListener { view -> reloadFriends() } @@ -51,8 +53,15 @@ class MainActivity : AppCompatActivity() { private fun reloadFriends() { fetchFriends() displayFriends() +// fetchMessages() + } - Toast.makeText(this, "Friends reloaded.", Toast.LENGTH_SHORT).show() + private fun fetchMessages() { + messages.fetchAll() + Toast.makeText( + this, "${messages.sent.size} messages sent, " + + "${messages.inbox.size} messages received.", Toast.LENGTH_SHORT + ).show() } private fun fetchFriends() { @@ -102,8 +111,6 @@ class MainActivity : AppCompatActivity() { val nbFriends = adapter.itemCount welcomeTitle.text = "$nbFriends ${"friend".plural(nbFriends)} on Dunbar" - - Toast.makeText(this@MainActivity, "$nbFriends ", Toast.LENGTH_LONG).show() } private fun getPhoto(id: Long): Bitmap? { @@ -111,7 +118,8 @@ class MainActivity : AppCompatActivity() { try { val inputStream = ContactsContract.Contacts.openContactPhotoInputStream( - contentResolver, ContentUris.withAppendedId( + contentResolver, + ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, java.lang.Long.valueOf(id) ) @@ -125,7 +133,7 @@ class MainActivity : AppCompatActivity() { } catch (e: IOException) { e.printStackTrace() } - Log.d("--> ", photo.toString()) + println("Photo for $id: $photo") return photo } } diff --git a/app/src/main/java/fr/plnech/dunbar/Messages.kt b/app/src/main/java/fr/plnech/dunbar/Messages.kt new file mode 100644 index 0000000..bccd691 --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/Messages.kt @@ -0,0 +1,48 @@ +package fr.plnech.dunbar + +import android.content.Context +import android.net.Uri +import android.provider.Telephony + + +class Messages(context: Context) { + + private val contentResolver = context.contentResolver + var inbox: MutableList> = mutableListOf() + var sent: MutableList> = mutableListOf() + + fun fetchAll() { + inbox = fetchMessages(Telephony.Sms.Inbox.CONTENT_URI) + sent = fetchMessages(Telephony.Sms.Sent.CONTENT_URI) + } + + private fun fetchMessages(uri: Uri): MutableList> { + val messages = mutableListOf>() + + contentResolver.query(uri, null, null, null, null).let { + if (it != null) { + if (it.moveToFirst()) { + while (it.moveToNext()) { + + println("Processing message.") + val msg = mutableMapOf() + for (i in 0 until it.count) { + for (col in it.columnNames) { + val index = it.getColumnIndex(col) + msg[col] = it.getString(index) + } + } + messages.add(msg) + } + } else { + println("No messages in ${uri.toString().replace("content://sms/", "")}.") + } + it.close() + } else { + println("Null cursor.") + } + + } + return messages + } +} \ No newline at end of file -- libgit2 0.27.0