fix #78, properly return email at Get email intent

This commit is contained in:
tibbi 2018-02-22 20:33:24 +01:00
parent 6cc148b816
commit 2c31cf5bc9
2 changed files with 42 additions and 6 deletions

View File

@ -14,10 +14,13 @@ import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.adapters.SelectContactsAdapter
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.SMT_PRIVATE
import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.layout_select_contact.*
class SelectContactActivity : SimpleActivity() {
private var isGetEmailIntent = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_select_contact)
@ -41,12 +44,20 @@ class SelectContactActivity : SimpleActivity() {
}
private fun initContacts() {
ContactsHelper(this).getContacts {
var contacts = it
isGetEmailIntent = intent.data == ContactsContract.CommonDataKinds.Email.CONTENT_URI
ContactsHelper(this).getContacts(true) {
if (isActivityDestroyed()) {
return@getContacts
}
var contacts = it.filter {
if (isGetEmailIntent) {
(it.source != SMT_PRIVATE && it.emails.isNotEmpty())
} else {
true
}
} as ArrayList<Contact>
val contactSources = config.displayContactSources
if (!config.showAllContacts()) {
contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
@ -68,14 +79,21 @@ class SelectContactActivity : SimpleActivity() {
}
private fun confirmSelection(contact: Contact) {
val lookupKey = ContactsHelper(this).getContactLookupKey(contact.id.toString())
val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
Intent().apply {
data = lookupUri
data = getResultUri(contact)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
setResult(RESULT_OK, this)
}
finish()
}
private fun getResultUri(contact: Contact): Uri {
return if (isGetEmailIntent) {
val emailID = ContactsHelper(this).getContactDataId(contact.id.toString())
Uri.withAppendedPath(ContactsContract.Data.CONTENT_URI, emailID)
} else {
val lookupKey = ContactsHelper(this).getContactLookupKey(contact.id.toString())
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
}
}
}

View File

@ -757,6 +757,24 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return ""
}
fun getContactDataId(contactId: String): String {
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(ContactsContract.Data._ID, ContactsContract.Data.RAW_CONTACT_ID, ContactsContract.Data.MIMETYPE)
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(CommonDataKinds.Email.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
return cursor.getStringValue(ContactsContract.Data._ID)
}
} finally {
cursor?.close()
}
return ""
}
fun addFavorites(contacts: ArrayList<Contact>) {
toggleLocalFavorites(contacts, true)
toggleFavorites(contacts, true)