mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
add a helper function for getting the called contact at the Dialer
This commit is contained in:
@ -205,7 +205,15 @@
|
|||||||
<activity
|
<activity
|
||||||
android:name=".activities.DialerActivity"
|
android:name=".activities.DialerActivity"
|
||||||
android:label="@string/dialer"
|
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
|
<provider
|
||||||
android:name="androidx.core.content.FileProvider"
|
android:name="androidx.core.content.FileProvider"
|
||||||
|
@ -1,14 +1,24 @@
|
|||||||
package com.simplemobiletools.contacts.pro.activities
|
package com.simplemobiletools.contacts.pro.activities
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||||
import com.simplemobiletools.contacts.pro.R
|
import com.simplemobiletools.contacts.pro.R
|
||||||
|
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
||||||
import kotlinx.android.synthetic.main.activity_dialer.*
|
import kotlinx.android.synthetic.main.activity_dialer.*
|
||||||
|
|
||||||
class DialerActivity : SimpleActivity() {
|
class DialerActivity : SimpleActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_dialer)
|
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() {
|
override fun onResume() {
|
||||||
|
@ -92,6 +92,31 @@ class ContactsHelper(val activity: Activity) {
|
|||||||
}.start()
|
}.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> {
|
private fun getContentResolverAccounts(): HashSet<ContactSource> {
|
||||||
val uri = ContactsContract.Data.CONTENT_URI
|
val uri = ContactsContract.Data.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
|
@ -13,6 +13,15 @@ class LocalContactsHelper(val activity: Activity) {
|
|||||||
|
|
||||||
fun getContactWithId(id: Int) = convertLocalContactToContact(activity.contactsDB.getContactWithId(id))
|
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 {
|
fun insertOrUpdateContact(contact: Contact): Boolean {
|
||||||
val localContact = convertContactToLocalContact(contact)
|
val localContact = convertContactToLocalContact(contact)
|
||||||
return activity.contactsDB.insertOrUpdate(localContact) > 0
|
return activity.contactsDB.insertOrUpdate(localContact) > 0
|
||||||
|
@ -27,7 +27,7 @@ data class LocalContact(
|
|||||||
@ColumnInfo(name = "websites") var websites: ArrayList<String>,
|
@ColumnInfo(name = "websites") var websites: ArrayList<String>,
|
||||||
@ColumnInfo(name = "ims") var IMs: ArrayList<IM>) {
|
@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
|
override fun hashCode() = id ?: 0
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user