-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show search suggestions when dialing
- Loading branch information
1 parent
86ff047
commit 8d39324
Showing
9 changed files
with
315 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/bnyro/contacts/domain/model/BasicContactData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.bnyro.contacts.domain.model | ||
|
||
import android.net.Uri | ||
|
||
/** | ||
* Data class containing contact name, number and thumbnail | ||
* @property number phone number without any formatting | ||
* @property name contact display name | ||
* @property thumbnail contact thumbnail [Uri] | ||
*/ | ||
data class BasicContactData( | ||
val number: String, | ||
val name: String? = null, | ||
val thumbnail: Uri? = null | ||
) |
128 changes: 128 additions & 0 deletions
128
app/src/main/java/com/bnyro/contacts/domain/repositories/PhoneLookupRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.bnyro.contacts.domain.repositories | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import android.provider.ContactsContract.CommonDataKinds.Phone | ||
import android.provider.ContactsContract.PhoneLookup | ||
import androidx.core.net.toUri | ||
import com.bnyro.contacts.domain.model.BasicContactData | ||
import com.bnyro.contacts.util.extension.stringValue | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class PhoneLookupRepository(private val context: Context) { | ||
/** | ||
* Get all contacts with a number equal to or containing a given number. | ||
* @param number The number to search for | ||
* @return A list containing simple contact data | ||
*/ | ||
suspend fun getContactsWithNumber(number: String): List<BasicContactData> = | ||
withContext(Dispatchers.IO) { | ||
val contacts = mutableListOf<BasicContactData>() | ||
val uri = Phone.CONTENT_URI | ||
val projection = arrayOf( | ||
Phone.PHOTO_THUMBNAIL_URI, | ||
Phone.DISPLAY_NAME, | ||
Phone.NUMBER | ||
) | ||
val selection = "${Phone.NUMBER} LIKE ?" | ||
val selectionArgs = arrayOf("%$number%") | ||
|
||
val cursor = | ||
context.contentResolver.query(uri, projection, selection, selectionArgs, null) | ||
|
||
cursor?.use { | ||
while (it.moveToNext()) { | ||
val name = it.stringValue(Phone.DISPLAY_NAME) | ||
val phoneNumber = it.stringValue(Phone.NUMBER) | ||
val photoUri = it.stringValue(Phone.PHOTO_THUMBNAIL_URI)?.toUri() | ||
|
||
if (phoneNumber != null) { | ||
contacts.add( | ||
BasicContactData( | ||
number = phoneNumber, | ||
name = name, | ||
thumbnail = photoUri | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
contacts | ||
} | ||
|
||
/** | ||
* Get all contacts with a name containing the given query | ||
* @param nameQuery The name to search for | ||
* @return A list containing simple contact data | ||
*/ | ||
suspend fun getContactsWithName(nameQuery: String): List<BasicContactData> = | ||
withContext(Dispatchers.IO) { | ||
val contacts = mutableListOf<BasicContactData>() | ||
val uri = Phone.CONTENT_URI | ||
val projection = arrayOf( | ||
Phone.PHOTO_THUMBNAIL_URI, | ||
Phone.DISPLAY_NAME, | ||
Phone.NUMBER | ||
) | ||
val selection = "${Phone.DISPLAY_NAME} LIKE ?" | ||
val selectionArgs = arrayOf("%$nameQuery%") | ||
|
||
val cursor = | ||
context.contentResolver.query(uri, projection, selection, selectionArgs, null) | ||
|
||
cursor?.use { | ||
while (it.moveToNext()) { | ||
val name = it.stringValue(Phone.DISPLAY_NAME) | ||
val phoneNumber = it.stringValue(Phone.NUMBER) | ||
val photoUri = it.stringValue(Phone.PHOTO_THUMBNAIL_URI)?.toUri() | ||
|
||
if (phoneNumber != null) { | ||
contacts.add( | ||
BasicContactData( | ||
number = phoneNumber, | ||
name = name, | ||
thumbnail = photoUri | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
contacts | ||
} | ||
|
||
/**Get caller id from a number | ||
* @param phoneNumber The number of the caller | ||
* @return A simple contact data containing name phone and thumbnail | ||
*/ | ||
suspend fun getContactByNumber(phoneNumber: String): BasicContactData = | ||
withContext(Dispatchers.IO) { | ||
val uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)) | ||
val query = | ||
context.contentResolver.query( | ||
uri, | ||
arrayOf(PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_THUMBNAIL_URI), | ||
null, | ||
null, | ||
null | ||
) | ||
|
||
query?.use { cursor -> | ||
if (cursor.moveToFirst()) { | ||
BasicContactData( | ||
number = phoneNumber, | ||
name = cursor.stringValue( | ||
PhoneLookup.DISPLAY_NAME | ||
), | ||
thumbnail = cursor.stringValue(PhoneLookup.PHOTO_THUMBNAIL_URI)?.toUri() | ||
) | ||
|
||
} | ||
} | ||
BasicContactData( | ||
number = phoneNumber | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.