forked from upstream-mirrors/quik
Clean up methods for retreiving contacts
This commit is contained in:
@@ -44,40 +44,35 @@ class ContactRepository @Inject constructor(val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getContactBlocking(address: String): Contact? {
|
fun getContactBlocking(address: String): Contact? {
|
||||||
return getContactFromRealm(address) ?: getContactFromDb(address)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getContactFromRealm(address: String): Contact? {
|
|
||||||
val realm = Realm.getDefaultInstance()
|
|
||||||
val results = realm.where(Contact::class.java)
|
|
||||||
.equalTo("numbers.address", address)
|
|
||||||
.findAll()
|
|
||||||
|
|
||||||
realm.close()
|
|
||||||
return results.getOrNull(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getContactFromDb(address: String): Contact? {
|
|
||||||
if (address.isEmpty()) return null
|
if (address.isEmpty()) return null
|
||||||
|
|
||||||
val contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address))
|
val realm = Realm.getDefaultInstance()
|
||||||
val projection = arrayOf(BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.LOOKUP_KEY)
|
var contact = realm.where(Contact::class.java)
|
||||||
try {
|
.equalTo("numbers.address", address)
|
||||||
context.contentResolver.query(contactUri, projection, null, null, null).use { contactCursor ->
|
.findFirst()
|
||||||
if (contactCursor.moveToFirst()) {
|
|
||||||
return Contact().apply {
|
realm.close()
|
||||||
numbers.add(PhoneNumber().apply { this.address = address })
|
|
||||||
name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).orEmpty()
|
if (contact == null) {
|
||||||
lookupKey = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY)).orEmpty()
|
val contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address))
|
||||||
lastUpdate = System.currentTimeMillis()
|
val projection = arrayOf(BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.LOOKUP_KEY)
|
||||||
|
try {
|
||||||
|
context.contentResolver.query(contactUri, projection, null, null, null).use { contactCursor ->
|
||||||
|
if (contactCursor.moveToFirst()) {
|
||||||
|
contact = Contact().apply {
|
||||||
|
numbers.add(PhoneNumber().apply { this.address = address })
|
||||||
|
name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).orEmpty()
|
||||||
|
lookupKey = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY)).orEmpty()
|
||||||
|
lastUpdate = System.currentTimeMillis()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.w(e)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.w(e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return contact
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user