request READ_SMS permission and fetch the available messages
This commit is contained in:
parent
1350ce420a
commit
b239227794
|
@ -36,5 +36,5 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.simplemobiletools:commons:5.24.10'
|
||||
implementation 'com.simplemobiletools:commons:5.24.11'
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.simplemobiletools.smsmessenger">
|
||||
|
||||
<uses-permission android:name="android.permission.READ_SMS" />
|
||||
<uses-permission
|
||||
android:name="android.permission.USE_FINGERPRINT"
|
||||
tools:node="remove" />
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package com.simplemobiletools.smsmessenger.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.database.Cursor
|
||||
import android.os.Bundle
|
||||
import android.provider.Telephony
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import com.simplemobiletools.commons.extensions.appLaunched
|
||||
import com.simplemobiletools.commons.extensions.checkAppSideloading
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_SMS
|
||||
import com.simplemobiletools.commons.models.FAQItem
|
||||
import com.simplemobiletools.smsmessenger.BuildConfig
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.models.SMS
|
||||
|
||||
class MainActivity : SimpleActivity() {
|
||||
|
||||
|
@ -20,6 +23,14 @@ class MainActivity : SimpleActivity() {
|
|||
if (checkAppSideloading()) {
|
||||
return
|
||||
}
|
||||
|
||||
handlePermission(PERMISSION_READ_SMS) {
|
||||
if (it) {
|
||||
initMessenger()
|
||||
} else {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
|
@ -36,6 +47,47 @@ class MainActivity : SimpleActivity() {
|
|||
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() {
|
||||
startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
||||
}
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue