handle PICK contact intent

This commit is contained in:
tibbi 2018-01-02 16:49:03 +01:00
parent b57884ab8e
commit 59837806be
7 changed files with 175 additions and 17 deletions

View File

@ -42,6 +42,22 @@
</intent-filter>
</activity>
<activity
android:name=".activities.SelectContactActivity"
android:label="@string/select_contact">
<intent-filter>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/contact"/>
<data android:mimeType="vnd.android.cursor.dir/person"/>
<data android:mimeType="vnd.android.cursor.dir/phone_v2"/>
<data android:mimeType="vnd.android.cursor.dir/phone"/>
<data android:mimeType="vnd.android.cursor.dir/email_v2"/>
</intent-filter>
</activity>
<activity
android:name=".activities.SettingsActivity"
android:label="@string/settings"

View File

@ -0,0 +1,111 @@
package com.simplemobiletools.contacts.activities
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.appLaunched
import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.isActivityDestroyed
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
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.models.Contact
import kotlinx.android.synthetic.main.layout_select_contact.*
class SelectContactActivity : SimpleActivity() {
private var skipFavorites = false
private var allowMultiple = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_select_contact)
appLaunched()
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
handlePermission(PERMISSION_WRITE_CONTACTS) {
if (it) {
initContacts()
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_select_contact, menu)
menu.findItem(R.id.confirm).isVisible = allowMultiple
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.confirm -> confirmSelection()
else -> return super.onOptionsItemSelected(item)
}
return true
}
private fun initContacts() {
skipFavorites = intent.getBooleanExtra("extraSkipFavorites", false)
allowMultiple = intent.getBooleanExtra("isPickMultiContactsWithoutProfile", false)
if (allowMultiple) {
title = getString(R.string.select_contacts)
invalidateOptionsMenu()
}
ContactsHelper(this).getContacts {
var contacts = it
if (isActivityDestroyed()) {
return@getContacts
}
val contactSources = config.displayContactSources
if (!config.showAllContacts()) {
contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
if (skipFavorites) {
val favorites = config.favorites
contacts = contacts.filter { !favorites.contains(it.id.toString()) } as ArrayList<Contact>
}
Contact.sorting = config.sorting
contacts.sort()
runOnUiThread {
select_contact_list.adapter = SelectContactsAdapter(this, contacts, HashSet())
select_contact_fastscroller.allowBubbleDisplay = baseConfig.showInfoBubble
select_contact_fastscroller.setViews(select_contact_list) {
select_contact_fastscroller.updateBubbleText(contacts[it].getBubbleText())
}
}
}
}
private fun confirmSelection() {
val selectedItems = (select_contact_list.adapter as SelectContactsAdapter).getSelectedItemsSet()
val lookupKey = ContactsHelper(this).getContactLookupKey(selectedItems.first())
val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
Intent().apply {
data = lookupUri
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
setResult(RESULT_OK, this)
}
finish()
}
}

View File

@ -20,8 +20,8 @@ import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.item_add_favorite_with_number.view.*
import java.util.*
class AddFavoritesAdapter(val activity: SimpleActivity, val contacts: List<Contact>, val selectedContacts: Set<String>)
: RecyclerView.Adapter<AddFavoritesAdapter.ViewHolder>() {
class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Contact>, val selectedContacts: Set<String>)
: RecyclerView.Adapter<SelectContactsAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
private val config = activity.config

View File

@ -5,37 +5,38 @@ import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.AddFavoritesAdapter
import com.simplemobiletools.contacts.adapters.SelectContactsAdapter
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.dialog_add_favorites.view.*
import kotlinx.android.synthetic.main.layout_select_contact.view.*
import java.util.HashSet
import kotlin.collections.ArrayList
class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit) {
private var dialog: AlertDialog? = null
private var view = activity.layoutInflater.inflate(R.layout.dialog_add_favorites, null)
private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null)
private val config = activity.config
private var allContacts = ArrayList<Contact>()
init {
ContactsHelper(activity).getContacts {
allContacts = it
Contact.sorting = config.sorting
allContacts.sort()
val contactSources = config.displayContactSources
if (!activity.config.showAllContacts()) {
allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
Contact.sorting = config.sorting
allContacts.sort()
activity.runOnUiThread {
view.apply {
add_favorites_list.adapter = AddFavoritesAdapter(activity, allContacts, config.favorites)
add_favorites_fastscroller.allowBubbleDisplay = activity.baseConfig.showInfoBubble
add_favorites_fastscroller.setViews(add_favorites_list) {
add_favorites_fastscroller.updateBubbleText(allContacts[it].getBubbleText())
select_contact_list.adapter = SelectContactsAdapter(activity, allContacts, config.favorites)
select_contact_fastscroller.allowBubbleDisplay = activity.baseConfig.showInfoBubble
select_contact_fastscroller.setViews(select_contact_list) {
select_contact_fastscroller.updateBubbleText(allContacts[it].getBubbleText())
}
}
}
@ -52,7 +53,7 @@ class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit)
private fun dialogConfirmed() {
val allDisplayedIDs = ArrayList<String>()
allContacts.mapTo(allDisplayedIDs, { it.id.toString() })
val selectedItems = (view.add_favorites_list.adapter as AddFavoritesAdapter).getSelectedItemsSet()
val selectedItems = (view.select_contact_list.adapter as SelectContactsAdapter).getSelectedItemsSet()
config.addFavorites(selectedItems)
allDisplayedIDs.removeAll(selectedItems)

View File

@ -521,6 +521,25 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
}
fun getContactLookupKey(contactId: String): String {
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(ContactsContract.Data.CONTACT_ID, ContactsContract.Data.LOOKUP_KEY)
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val lookupKey = cursor.getStringValue(ContactsContract.Data.LOOKUP_KEY)
return "$lookupKey/$id"
}
} finally {
cursor?.close()
}
return ""
}
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
fun deleteContacts(contacts: ArrayList<Contact>) {

View File

@ -2,25 +2,26 @@
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/add_favorites_holder"
android:id="@+id/select_contact_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/add_favorites_list"
android:id="@+id/select_contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<com.simplemobiletools.commons.views.FastScroller
android:id="@+id/add_favorites_fastscroller"
android:id="@+id/select_contact_fastscroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/add_favorites_list"
android:layout_alignBottom="@+id/select_contact_list"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/add_favorites_list"
android:layout_alignTop="@+id/select_contact_list"
android:paddingLeft="@dimen/normal_margin"
android:paddingStart="@dimen/normal_margin">

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/confirm"
android:icon="@drawable/ic_check"
android:title="@string/confirm_selection"
app:showAsAction="ifRoom"/>
</menu>