mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-02-17 03:51:03 +01:00
add a helper function for getting the called contact at the Dialer
This commit is contained in:
parent
945c5c8e9a
commit
cb629354c4
@ -205,7 +205,15 @@
|
||||
<activity
|
||||
android:name=".activities.DialerActivity"
|
||||
android:label="@string/dialer"
|
||||
android:parentActivityName=".activities.MainActivity"/>
|
||||
android:parentActivityName=".activities.MainActivity">
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.CALL"/>
|
||||
<category android:name="android.intent.category.DEFAULT"/>
|
||||
|
||||
<data android:scheme="tel"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
|
@ -1,14 +1,24 @@
|
||||
package com.simplemobiletools.contacts.pro.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
||||
import kotlinx.android.synthetic.main.activity_dialer.*
|
||||
|
||||
class DialerActivity : SimpleActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_dialer)
|
||||
|
||||
if (intent.action == Intent.ACTION_CALL && intent.data != null && intent.dataString?.contains("tel:") == true) {
|
||||
val number = Uri.decode(intent.dataString).substringAfter("tel:")
|
||||
ContactsHelper(this).getContactWithNumber(number) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
@ -92,6 +92,31 @@ class ContactsHelper(val activity: Activity) {
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun getContactWithNumber(number: String, callback: (contact: Contact?) -> Unit) {
|
||||
Thread {
|
||||
val uri = CommonDataKinds.Phone.CONTENT_URI
|
||||
val projection = arrayOf(ContactsContract.Data.RAW_CONTACT_ID)
|
||||
val selection = "${CommonDataKinds.Phone.NUMBER} = ?"
|
||||
val selectionArgs = arrayOf(number)
|
||||
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
|
||||
callback(getContactWithId(id, false))
|
||||
return@Thread
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
activity.showErrorToast(e)
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
callback(LocalContactsHelper(activity).getContactWithNumber(number))
|
||||
}.start()
|
||||
}
|
||||
|
||||
private fun getContentResolverAccounts(): HashSet<ContactSource> {
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
|
@ -13,6 +13,15 @@ class LocalContactsHelper(val activity: Activity) {
|
||||
|
||||
fun getContactWithId(id: Int) = convertLocalContactToContact(activity.contactsDB.getContactWithId(id))
|
||||
|
||||
fun getContactWithNumber(number: String): Contact? {
|
||||
activity.contactsDB.getContacts().forEach {
|
||||
if (it.phoneNumbers.map { it.value }.contains(number)) {
|
||||
return convertLocalContactToContact(it)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun insertOrUpdateContact(contact: Contact): Boolean {
|
||||
val localContact = convertContactToLocalContact(contact)
|
||||
return activity.contactsDB.insertOrUpdate(localContact) > 0
|
||||
|
@ -27,7 +27,7 @@ data class LocalContact(
|
||||
@ColumnInfo(name = "websites") var websites: ArrayList<String>,
|
||||
@ColumnInfo(name = "ims") var IMs: ArrayList<IM>) {
|
||||
|
||||
override fun equals(other: Any?) = this.id == (other as? LocalContact?)?.id
|
||||
override fun equals(other: Any?) = id == (other as? LocalContact?)?.id
|
||||
|
||||
override fun hashCode() = id ?: 0
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user