add handling for some third party intents

This commit is contained in:
tibbi 2018-01-02 14:29:23 +01:00
parent cbf781dfea
commit 2cfcdba0f9
3 changed files with 125 additions and 6 deletions

View File

@ -50,7 +50,71 @@
<activity
android:name=".activities.ContactActivity"
android:label="@string/new_contact"
android:parentActivityName=".activities.MainActivity"/>
android:parentActivityName=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.INSERT_OR_EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.item/person"/>
<data android:mimeType="vnd.android.cursor.item/contact"/>
<data android:mimeType="vnd.android.cursor.item/raw_contact"/>
</intent-filter>
<intent-filter android:label="@string/edit_contact">
<action android:name="android.intent.action.EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data
android:host="contacts"
android:mimeType="vnd.android.cursor.item/person"/>
<data
android:host="com.android.contacts"
android:mimeType="vnd.android.cursor.item/contact"/>
<data
android:host="com.android.contacts"
android:mimeType="vnd.android.cursor.item/raw_contact"/>
</intent-filter>
<intent-filter android:label="@string/new_contact">
<action android:name="android.intent.action.INSERT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/person"/>
<data android:mimeType="vnd.android.cursor.dir/contact"/>
<data android:mimeType="vnd.android.cursor.dir/raw_contact"/>
</intent-filter>
<intent-filter android:label="@string/edit_contact">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/person"/>
<data android:mimeType="vnd.android.cursor.dir/contact"/>
<data android:mimeType="vnd.android.cursor.item/group"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.item/group"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.INSERT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/group"/>
</intent-filter>
<intent-filter android:label="@string/edit_contact">
<action android:name="com.android.contacts.action.QUICK_CONTACT"/>
<action android:name="android.provider.action.QUICK_CONTACT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.item/contact"/>
<data android:mimeType="vnd.android.cursor.item/person"/>
</intent-filter>
</activity>
<activity
android:name="com.simplemobiletools.commons.activities.AboutActivity"

View File

@ -33,10 +33,7 @@ import com.simplemobiletools.commons.helpers.getDateFormats
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.BuildConfig
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.sendEmailIntent
import com.simplemobiletools.contacts.extensions.sendSMSIntent
import com.simplemobiletools.contacts.extensions.tryStartCall
import com.simplemobiletools.contacts.extensions.*
import com.simplemobiletools.contacts.helpers.*
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Email
@ -95,7 +92,17 @@ class ContactActivity : SimpleActivity() {
}
private fun initContact() {
val contactId = intent.getIntExtra(CONTACT_ID, 0)
var contactId = intent.getIntExtra(CONTACT_ID, 0)
if (contactId == 0 && intent.action == "android.provider.action.QUICK_CONTACT") {
val data = intent.data
if (data != null) {
val rawId = getContactRawId(data) ?: -1
if (rawId != -1) {
contactId = rawId
}
}
}
if (contactId != 0) {
contact = ContactsHelper(this).getContactWithId(contactId)
if (contact == null) {

View File

@ -1,9 +1,15 @@
package com.simplemobiletools.contacts.extensions
import android.annotation.TargetApi
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.ContactsContract
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.isLollipopPlus
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.contacts.activities.ContactActivity
import com.simplemobiletools.contacts.helpers.CONTACT_ID
@ -40,3 +46,45 @@ fun Context.sendSMSIntent(recipient: String) {
}
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
fun Context.getContactRawId(dataUri: Uri): Int? {
val lookupKey = getLookupKeyFromUri(dataUri)
if (lookupKey != null && isLollipopPlus()) {
val uri = lookupContactUri(lookupKey, this)
val projection = arrayOf(ContactsContract.Contacts.NAME_RAW_CONTACT_ID)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, null, null, null)
if (cursor.moveToFirst()) {
return cursor.getIntValue(ContactsContract.Contacts.NAME_RAW_CONTACT_ID)
}
} finally {
cursor?.close()
}
}
return -1
}
// from https://android.googlesource.com/platform/packages/apps/Dialer/+/68038172793ee0e2ab3e2e56ddfbeb82879d1f58/java/com/android/contacts/common/util/UriUtils.java
fun getLookupKeyFromUri(lookupUri: Uri): String? {
return if (!isEncodedContactUri(lookupUri)) {
val segments = lookupUri.pathSegments
if (segments.size < 3) null else Uri.encode(segments[2])
} else {
null
}
}
fun isEncodedContactUri(uri: Uri?): Boolean {
if (uri == null) {
return false
}
val lastPathSegment = uri.lastPathSegment ?: return false
return lastPathSegment == "encoded"
}
fun lookupContactUri(lookup: String, context: Context): Uri {
val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup)
return ContactsContract.Contacts.lookupContact(context.contentResolver, lookupUri)
}