request READ_SMS permission and fetch the available messages

This commit is contained in:
tibbi 2020-04-03 11:57:30 +02:00
parent 1350ce420a
commit b239227794
4 changed files with 59 additions and 3 deletions

View File

@ -36,5 +36,5 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.24.10' implementation 'com.simplemobiletools:commons:5.24.11'
} }

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.simplemobiletools.smsmessenger"> package="com.simplemobiletools.smsmessenger">
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission <uses-permission
android:name="android.permission.USE_FINGERPRINT" android:name="android.permission.USE_FINGERPRINT"
tools:node="remove" /> tools:node="remove" />

View File

@ -1,14 +1,17 @@
package com.simplemobiletools.smsmessenger.activities package com.simplemobiletools.smsmessenger.activities
import android.content.Intent import android.content.Intent
import android.database.Cursor
import android.os.Bundle import android.os.Bundle
import android.provider.Telephony
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.commons.extensions.appLaunched import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.checkAppSideloading import com.simplemobiletools.commons.helpers.PERMISSION_READ_SMS
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.smsmessenger.BuildConfig import com.simplemobiletools.smsmessenger.BuildConfig
import com.simplemobiletools.smsmessenger.R import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.models.SMS
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
@ -20,6 +23,14 @@ class MainActivity : SimpleActivity() {
if (checkAppSideloading()) { if (checkAppSideloading()) {
return return
} }
handlePermission(PERMISSION_READ_SMS) {
if (it) {
initMessenger()
} else {
finish()
}
}
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -36,6 +47,47 @@ class MainActivity : SimpleActivity() {
return true return true
} }
private fun initMessenger() {
val sms = getSMSs()
}
private fun getSMSs(): ArrayList<SMS> {
val smss = ArrayList<SMS>()
val uri = Telephony.Sms.CONTENT_URI
val projection = arrayOf(
Telephony.Sms._ID,
Telephony.Sms.SUBJECT,
Telephony.Sms.BODY,
Telephony.Sms.TYPE,
Telephony.Sms.ADDRESS,
Telephony.Sms.DATE,
Telephony.Sms.READ
)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(Telephony.Sms._ID)
val subject = cursor.getStringValue(Telephony.Sms.SUBJECT) ?: ""
val body = cursor.getStringValue(Telephony.Sms.BODY)
val type = cursor.getIntValue(Telephony.Sms.TYPE)
val address = cursor.getStringValue(Telephony.Sms.ADDRESS)
val date = (cursor.getLongValue(Telephony.Sms.DATE) / 1000).toInt()
val read = cursor.getIntValue(Telephony.Sms.READ) == 1
val sms = SMS(id, subject, body, type, address, date, read)
smss.add(sms)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
cursor?.close()
}
return smss
}
private fun launchSettings() { private fun launchSettings() {
startActivity(Intent(applicationContext, SettingsActivity::class.java)) startActivity(Intent(applicationContext, SettingsActivity::class.java))
} }

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.smsmessenger.models
data class SMS(val id: Int, val subject: String, val body: String, val type: Int, val address: String, val date: Int, val read: Boolean)