Skip to content

Commit

Permalink
fix: contact picker (closes you-apps#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Mar 29, 2024
1 parent 40868ab commit c3c5e16
Showing 1 changed file with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package com.bnyro.contacts.ui.activities

import android.app.Activity
import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds.Email
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.provider.ContactsContract.Data
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -33,10 +38,18 @@ import com.bnyro.contacts.ui.theme.ConnectYouTheme

class PickContactActivity : BaseActivity() {

private var specialMimeType: String? = null

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

specialMimeType = when (intent.data) {
Email.CONTENT_URI -> Email.CONTENT_ITEM_TYPE
Phone.CONTENT_URI -> Phone.CONTENT_ITEM_TYPE
else -> null
}

setContent {
ConnectYouTheme(themeModel.themeMode) {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
Expand Down Expand Up @@ -94,14 +107,48 @@ class PickContactActivity : BaseActivity() {
if (contact == null) {
setResult(Activity.RESULT_CANCELED, Intent())
} else {
val intent = Intent().apply {
data = ContentUris.withAppendedId(
val uri = when {
specialMimeType != null -> {
val contactId = getContactMimeTypeId(
this,
contact.contactId.toString(),
specialMimeType!!
)
Uri.withAppendedPath(Data.CONTENT_URI, contactId)
}

else -> ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contact.contactId
)
}
val intent = Intent().apply {
data = uri
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
setResult(Activity.RESULT_OK, intent)
}
finish()
}

private fun getContactMimeTypeId(
context: Context,
contactId: String,
mimeType: String
): String {
val uri = Data.CONTENT_URI
val projection = arrayOf(Data._ID, Data.RAW_CONTACT_ID, Data.MIMETYPE)
val selection = "${Data.MIMETYPE} = ? AND ${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(mimeType, contactId)

val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (it.moveToFirst()) {
val index = it.getColumnIndex(Data._ID)
return it.getString(index)
}
}
return ""
}

}

0 comments on commit c3c5e16

Please sign in to comment.