feat(Messages): Fetch, although not needed now

parent 0543837d
......@@ -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<Friend>()
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
}
}
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<Map<String, String?>> = mutableListOf()
var sent: MutableList<Map<String, String?>> = mutableListOf()
fun fetchAll() {
inbox = fetchMessages(Telephony.Sms.Inbox.CONTENT_URI)
sent = fetchMessages(Telephony.Sms.Sent.CONTENT_URI)
}
private fun fetchMessages(uri: Uri): MutableList<Map<String, String?>> {
val messages = mutableListOf<Map<String, String?>>()
contentResolver.query(uri, null, null, null, null).let {
if (it != null) {
if (it.moveToFirst()) {
while (it.moveToNext()) {
println("Processing message.")
val msg = mutableMapOf<String, String?>()
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
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