shortening some code by importing more specific packages

This commit is contained in:
tibbi 2020-04-07 12:43:33 +02:00
parent 10bc1d1278
commit b00eea624c

View File

@ -5,7 +5,10 @@ import android.content.ContentValues
import android.content.Context import android.content.Context
import android.provider.ContactsContract import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds
import android.provider.ContactsContract.CommonDataKinds.Organization
import android.provider.ContactsContract.CommonDataKinds.StructuredName
import android.provider.Telephony import android.provider.Telephony
import android.provider.Telephony.Sms
import android.text.TextUtils import android.text.TextUtils
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
@ -15,29 +18,31 @@ import com.simplemobiletools.smsmessenger.helpers.Config
import com.simplemobiletools.smsmessenger.models.Contact import com.simplemobiletools.smsmessenger.models.Contact
import com.simplemobiletools.smsmessenger.models.Message import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.models.MessagingThread import com.simplemobiletools.smsmessenger.models.MessagingThread
import java.util.*
import kotlin.collections.ArrayList
val Context.config: Config get() = Config.newInstance(applicationContext) val Context.config: Config get() = Config.newInstance(applicationContext)
fun Context.getMessages(threadID: Int? = null): ArrayList<Message> { fun Context.getMessages(threadID: Int? = null): ArrayList<Message> {
val messages = ArrayList<Message>() val messages = ArrayList<Message>()
val hasContactsPermission = hasPermission(PERMISSION_READ_CONTACTS) val hasContactsPermission = hasPermission(PERMISSION_READ_CONTACTS)
val uri = Telephony.Sms.CONTENT_URI val uri = Sms.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
Telephony.Sms._ID, Sms._ID,
Telephony.Sms.SUBJECT, Sms.SUBJECT,
Telephony.Sms.BODY, Sms.BODY,
Telephony.Sms.TYPE, Sms.TYPE,
Telephony.Sms.ADDRESS, Sms.ADDRESS,
Telephony.Sms.PERSON, Sms.PERSON,
Telephony.Sms.DATE, Sms.DATE,
Telephony.Sms.READ, Sms.READ,
Telephony.Sms.THREAD_ID Sms.THREAD_ID
) )
val selection = if (threadID == null) { val selection = if (threadID == null) {
"1 == 1) GROUP BY (${Telephony.Sms.THREAD_ID}" "1 == 1) GROUP BY (${Sms.THREAD_ID}"
} else { } else {
"${Telephony.Sms.THREAD_ID} = ?" "${Sms.THREAD_ID} = ?"
} }
val selectionArgs = if (threadID == null) { val selectionArgs = if (threadID == null) {
@ -47,16 +52,16 @@ fun Context.getMessages(threadID: Int? = null): ArrayList<Message> {
} }
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor -> queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Telephony.Sms._ID) val id = cursor.getIntValue(Sms._ID)
val subject = cursor.getStringValue(Telephony.Sms.SUBJECT) ?: "" val subject = cursor.getStringValue(Sms.SUBJECT) ?: ""
val body = cursor.getStringValue(Telephony.Sms.BODY) val body = cursor.getStringValue(Sms.BODY)
val type = cursor.getIntValue(Telephony.Sms.TYPE) val type = cursor.getIntValue(Sms.TYPE)
var senderName = cursor.getStringValue(Telephony.Sms.ADDRESS) var senderName = cursor.getStringValue(Sms.ADDRESS)
val senderNumber = senderName val senderNumber = senderName
val date = (cursor.getLongValue(Telephony.Sms.DATE) / 1000).toInt() val date = (cursor.getLongValue(Sms.DATE) / 1000).toInt()
val read = cursor.getIntValue(Telephony.Sms.READ) == 1 val read = cursor.getIntValue(Sms.READ) == 1
val person = cursor.getIntValue(Telephony.Sms.PERSON) val person = cursor.getIntValue(Sms.PERSON)
val thread = cursor.getIntValue(Telephony.Sms.THREAD_ID) val thread = cursor.getIntValue(Sms.THREAD_ID)
if (hasContactsPermission) { if (hasContactsPermission) {
if (senderName != null && person != 0) { if (senderName != null && person != 0) {
@ -76,20 +81,20 @@ fun Context.getMessages(threadID: Int? = null): ArrayList<Message> {
} }
fun Context.getThreadInfo(id: Int): MessagingThread? { fun Context.getThreadInfo(id: Int): MessagingThread? {
val uri = Telephony.Sms.CONTENT_URI val uri = Sms.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
Telephony.Sms._ID, Sms._ID,
Telephony.Sms.ADDRESS, Sms.ADDRESS,
Telephony.Sms.PERSON Sms.PERSON
) )
val selection = "${Telephony.Sms.THREAD_ID} = ?" val selection = "${Sms.THREAD_ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
try { try {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null) val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use { cursor?.use {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
val person = cursor.getIntValue(Telephony.Sms.PERSON) val person = cursor.getIntValue(Sms.PERSON)
val address = cursor.getStringValue(Telephony.Sms.ADDRESS) val address = cursor.getStringValue(Sms.ADDRESS)
var title = address var title = address
if (title != null && person != 0) { if (title != null && person != 0) {
@ -112,21 +117,21 @@ fun Context.getThreadInfo(id: Int): MessagingThread? {
fun Context.getPersonsName(id: Int): String? { fun Context.getPersonsName(id: Int): String? {
val uri = ContactsContract.Data.CONTENT_URI val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
CommonDataKinds.StructuredName.PREFIX, StructuredName.PREFIX,
CommonDataKinds.StructuredName.GIVEN_NAME, StructuredName.GIVEN_NAME,
CommonDataKinds.StructuredName.MIDDLE_NAME, StructuredName.MIDDLE_NAME,
CommonDataKinds.StructuredName.FAMILY_NAME, StructuredName.FAMILY_NAME,
CommonDataKinds.StructuredName.SUFFIX, StructuredName.SUFFIX,
CommonDataKinds.Organization.COMPANY, Organization.COMPANY,
CommonDataKinds.Organization.TITLE, Organization.TITLE,
ContactsContract.Data.MIMETYPE ContactsContract.Data.MIMETYPE
) )
val selection = val selection =
"(${ContactsContract.Data.MIMETYPE} = ? OR ${ContactsContract.Data.MIMETYPE} = ?) AND ${ContactsContract.Data.CONTACT_ID} = ?" "(${ContactsContract.Data.MIMETYPE} = ? OR ${ContactsContract.Data.MIMETYPE} = ?) AND ${ContactsContract.Data.CONTACT_ID} = ?"
val selectionArgs = arrayOf( val selectionArgs = arrayOf(
CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, StructuredName.CONTENT_ITEM_TYPE,
CommonDataKinds.Organization.CONTENT_ITEM_TYPE, Organization.CONTENT_ITEM_TYPE,
id.toString() id.toString()
) )
@ -136,23 +141,23 @@ fun Context.getPersonsName(id: Int): String? {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE) val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
val isPerson = mimetype == CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE val isPerson = mimetype == StructuredName.CONTENT_ITEM_TYPE
if (isPerson) { if (isPerson) {
val prefix = cursor.getStringValue(CommonDataKinds.StructuredName.PREFIX) ?: "" val prefix = cursor.getStringValue(StructuredName.PREFIX) ?: ""
val firstName = cursor.getStringValue(CommonDataKinds.StructuredName.GIVEN_NAME) ?: "" val firstName = cursor.getStringValue(StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(CommonDataKinds.StructuredName.MIDDLE_NAME) ?: "" val middleName = cursor.getStringValue(StructuredName.MIDDLE_NAME) ?: ""
val familyName = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: "" val familyName = cursor.getStringValue(StructuredName.FAMILY_NAME) ?: ""
val suffix = cursor.getStringValue(CommonDataKinds.StructuredName.SUFFIX) ?: "" val suffix = cursor.getStringValue(StructuredName.SUFFIX) ?: ""
if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) { if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) {
val names = arrayOf(prefix, firstName, middleName, familyName, suffix).filter { it.isNotEmpty() } val names = arrayOf(prefix, firstName, middleName, familyName, suffix).filter { it.isNotEmpty() }
return TextUtils.join(" ", names) return TextUtils.join(" ", names)
} }
} }
val isOrganization = mimetype == CommonDataKinds.Organization.CONTENT_ITEM_TYPE val isOrganization = mimetype == Organization.CONTENT_ITEM_TYPE
if (isOrganization) { if (isOrganization) {
val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: "" val company = cursor.getStringValue(Organization.COMPANY) ?: ""
val jobTitle = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: "" val jobTitle = cursor.getStringValue(Organization.TITLE) ?: ""
if (company.isNotEmpty() || jobTitle.isNotEmpty()) { if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
return "$company $jobTitle".trim() return "$company $jobTitle".trim()
} }
@ -192,7 +197,7 @@ fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
it.phoneNumber.substring(startIndex) it.phoneNumber.substring(startIndex)
}.toMutableList() as ArrayList<Contact> }.toMutableList() as ArrayList<Contact>
allContacts.sortBy { it.name.normalizeString().toLowerCase() } allContacts.sortBy { it.name.normalizeString().toLowerCase(Locale.getDefault()) }
callback(allContacts) callback(allContacts)
} }
} }
@ -223,34 +228,34 @@ fun Context.getContactNames(): List<Contact> {
val uri = ContactsContract.Data.CONTENT_URI val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
ContactsContract.Data.CONTACT_ID, ContactsContract.Data.CONTACT_ID,
CommonDataKinds.StructuredName.PREFIX, StructuredName.PREFIX,
CommonDataKinds.StructuredName.GIVEN_NAME, StructuredName.GIVEN_NAME,
CommonDataKinds.StructuredName.MIDDLE_NAME, StructuredName.MIDDLE_NAME,
CommonDataKinds.StructuredName.FAMILY_NAME, StructuredName.FAMILY_NAME,
CommonDataKinds.StructuredName.SUFFIX, StructuredName.SUFFIX,
CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI, StructuredName.PHOTO_THUMBNAIL_URI,
CommonDataKinds.Organization.COMPANY, Organization.COMPANY,
CommonDataKinds.Organization.TITLE, Organization.TITLE,
ContactsContract.Data.MIMETYPE ContactsContract.Data.MIMETYPE
) )
val selection = "${ContactsContract.Data.MIMETYPE} = ? OR ${ContactsContract.Data.MIMETYPE} = ?" val selection = "${ContactsContract.Data.MIMETYPE} = ? OR ${ContactsContract.Data.MIMETYPE} = ?"
val selectionArgs = arrayOf( val selectionArgs = arrayOf(
CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, StructuredName.CONTENT_ITEM_TYPE,
CommonDataKinds.Organization.CONTENT_ITEM_TYPE Organization.CONTENT_ITEM_TYPE
) )
queryCursor(uri, projection, selection, selectionArgs) { cursor -> queryCursor(uri, projection, selection, selectionArgs) { cursor ->
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE) val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
val photoUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" val photoUri = cursor.getStringValue(StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
val isPerson = mimetype == CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE val isPerson = mimetype == StructuredName.CONTENT_ITEM_TYPE
if (isPerson) { if (isPerson) {
val prefix = cursor.getStringValue(CommonDataKinds.StructuredName.PREFIX) ?: "" val prefix = cursor.getStringValue(StructuredName.PREFIX) ?: ""
val firstName = cursor.getStringValue(CommonDataKinds.StructuredName.GIVEN_NAME) ?: "" val firstName = cursor.getStringValue(StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(CommonDataKinds.StructuredName.MIDDLE_NAME) ?: "" val middleName = cursor.getStringValue(StructuredName.MIDDLE_NAME) ?: ""
val familyName = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: "" val familyName = cursor.getStringValue(StructuredName.FAMILY_NAME) ?: ""
val suffix = cursor.getStringValue(CommonDataKinds.StructuredName.SUFFIX) ?: "" val suffix = cursor.getStringValue(StructuredName.SUFFIX) ?: ""
if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) { if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) {
val names = arrayOf(prefix, firstName, middleName, familyName, suffix).filter { it.isNotEmpty() } val names = arrayOf(prefix, firstName, middleName, familyName, suffix).filter { it.isNotEmpty() }
val fullName = TextUtils.join(" ", names) val fullName = TextUtils.join(" ", names)
@ -259,10 +264,10 @@ fun Context.getContactNames(): List<Contact> {
} }
} }
val isOrganization = mimetype == CommonDataKinds.Organization.CONTENT_ITEM_TYPE val isOrganization = mimetype == Organization.CONTENT_ITEM_TYPE
if (isOrganization) { if (isOrganization) {
val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: "" val company = cursor.getStringValue(Organization.COMPANY) ?: ""
val jobTitle = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: "" val jobTitle = cursor.getStringValue(Organization.TITLE) ?: ""
if (company.isNotEmpty() || jobTitle.isNotEmpty()) { if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
val fullName = "$company $jobTitle".trim() val fullName = "$company $jobTitle".trim()
val contact = Contact(id, fullName, photoUri, "", true) val contact = Contact(id, fullName, photoUri, "", true)
@ -293,40 +298,40 @@ fun Context.getContactPhoneNumbers(): ArrayList<Contact> {
} }
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int) { fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int) {
val uri = Telephony.Sms.CONTENT_URI val uri = Sms.CONTENT_URI
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(Telephony.Sms.ADDRESS, address) put(Sms.ADDRESS, address)
put(Telephony.Sms.SUBJECT, subject) put(Sms.SUBJECT, subject)
put(Telephony.Sms.BODY, body) put(Sms.BODY, body)
put(Telephony.Sms.DATE, date) put(Sms.DATE, date)
put(Telephony.Sms.READ, read) put(Sms.READ, read)
put(Telephony.Sms.THREAD_ID, threadId) put(Sms.THREAD_ID, threadId)
put(Telephony.Sms.TYPE, type) put(Sms.TYPE, type)
} }
contentResolver.insert(uri, contentValues) contentResolver.insert(uri, contentValues)
} }
fun Context.deleteThread(id: Int) { fun Context.deleteThread(id: Int) {
val uri = Telephony.Sms.CONTENT_URI val uri = Sms.CONTENT_URI
val selection = "${Telephony.Sms.THREAD_ID} = ?" val selection = "${Sms.THREAD_ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
contentResolver.delete(uri, selection, selectionArgs) contentResolver.delete(uri, selection, selectionArgs)
} }
fun Context.deleteMessage(id: Int) { fun Context.deleteMessage(id: Int) {
val uri = Telephony.Sms.CONTENT_URI val uri = Sms.CONTENT_URI
val selection = "${Telephony.Sms._ID} = ?" val selection = "${Sms._ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
contentResolver.delete(uri, selection, selectionArgs) contentResolver.delete(uri, selection, selectionArgs)
} }
fun Context.markSMSRead(id: Int) { fun Context.markSMSRead(id: Int) {
val uri = Telephony.Sms.CONTENT_URI val uri = Sms.CONTENT_URI
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(Telephony.Sms.READ, 1) put(Sms.READ, 1)
} }
val selection = "${Telephony.Sms._ID} = ? AND ${Telephony.Sms.READ} = ?" val selection = "${Sms._ID} = ? AND ${Sms.READ} = ?"
val selectionArgs = arrayOf(id.toString(), "0") val selectionArgs = arrayOf(id.toString(), "0")
contentResolver.update(uri, contentValues, selection, selectionArgs) contentResolver.update(uri, contentValues, selection, selectionArgs)
} }