From a035b21c01a7f826649dcc8c9b840b7d6a690d47 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 18 Apr 2018 15:50:37 +0200 Subject: [PATCH] fix #137, allow sending SMS and emails to whole groups --- .../activities/GroupContactsActivity.kt | 24 +++++++ .../contacts/adapters/ContactsAdapter.kt | 63 ++++--------------- .../contacts/extensions/Activity.kt | 41 ++++++++++++ app/src/main/res/menu/menu_group.xml | 14 +++++ 4 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 app/src/main/res/menu/menu_group.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt index 2ee2ab73..c81d309b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt @@ -1,6 +1,8 @@ package com.simplemobiletools.contacts.activities import android.os.Bundle +import android.view.Menu +import android.view.MenuItem import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.adapters.ContactsAdapter @@ -46,6 +48,20 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh refreshContacts() } + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.menu_group, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.send_sms_to_group -> sendSMSToGroup() + R.id.send_email_to_group -> sendEmailToGroup() + else -> return super.onOptionsItemSelected(item) + } + return true + } + private fun fabClicked() { SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts -> Thread { @@ -74,6 +90,14 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh } } + private fun sendSMSToGroup() { + sendSMSToContacts(groupContacts) + } + + private fun sendEmailToGroup() { + sendEmailToContacts(groupContacts) + } + private fun updateContacts(contacts: ArrayList) { val currAdapter = group_contacts_list.adapter if (currAdapter == null) { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt index e4de64d5..2bbc6d2b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt @@ -1,8 +1,6 @@ package com.simplemobiletools.contacts.adapters -import android.content.Intent import android.graphics.drawable.Drawable -import android.net.Uri import android.view.Menu import android.view.View import android.view.ViewGroup @@ -17,17 +15,13 @@ import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.isActivityDestroyed -import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.dialogs.CreateNewGroupDialog -import com.simplemobiletools.contacts.extensions.addContactsToGroup -import com.simplemobiletools.contacts.extensions.config -import com.simplemobiletools.contacts.extensions.editContact -import com.simplemobiletools.contacts.extensions.shareContacts +import com.simplemobiletools.contacts.extensions.* import com.simplemobiletools.contacts.helpers.* import com.simplemobiletools.contacts.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.interfaces.RemoveFromGroupListener @@ -181,21 +175,13 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList() - selectedPositions.forEach { - newFavorites.add(contactItems[it]) - } - ContactsHelper(activity).addFavorites(newFavorites) + ContactsHelper(activity).addFavorites(getSelectedContacts()) refreshListener?.refreshContacts(FAVORITES_TAB_MASK) finishActMode() } private fun addToGroup() { - val selectedContacts = ArrayList() - selectedPositions.forEach { - selectedContacts.add(contactItems[it]) - } - + val selectedContacts = getSelectedContacts() val NEW_GROUP_ID = -1 val items = ArrayList() ContactsHelper(activity).getStoredGroups().forEach { @@ -233,46 +219,19 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList() - selectedPositions.forEach { - val contact = contactItems[it] - contact.emails.forEach { - if (it.value.isNotEmpty()) { - emails.add(it.value) - } - } - } + activity.sendEmailToContacts(getSelectedContacts()) + } - Intent(Intent.ACTION_SEND_MULTIPLE).apply { - type = "message/rfc822" - putExtra(Intent.EXTRA_EMAIL, emails.toTypedArray()) - if (resolveActivity(activity.packageManager) != null) { - activity.startActivity(this) - } else { - activity.toast(R.string.no_app_found) - } + private fun getSelectedContacts(): ArrayList { + val contacts = ArrayList() + selectedPositions.forEach { + contacts.add(contactItems[it]) } + return contacts } override fun onViewRecycled(holder: ViewHolder) { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt index 34b74393..a02e7101 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt @@ -99,6 +99,47 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList) { } } +fun BaseSimpleActivity.sendSMSToContacts(contacts: ArrayList) { + val numbers = StringBuilder() + contacts.forEach { + it.phoneNumbers.forEach { + if (it.value.isNotEmpty()) { + numbers.append("${it.value};") + } + } + + val uriString = "smsto:${numbers.toString().trimEnd(';')}" + Intent(Intent.ACTION_SENDTO, Uri.parse(uriString)).apply { + if (resolveActivity(packageManager) != null) { + startActivity(this) + } else { + toast(R.string.no_app_found) + } + } + } +} + +fun BaseSimpleActivity.sendEmailToContacts(contacts: ArrayList) { + val emails = ArrayList() + contacts.forEach { + it.emails.forEach { + if (it.value.isNotEmpty()) { + emails.add(it.value) + } + } + } + + Intent(Intent.ACTION_SEND_MULTIPLE).apply { + type = "message/rfc822" + putExtra(Intent.EXTRA_EMAIL, emails.toTypedArray()) + if (resolveActivity(packageManager) != null) { + startActivity(this) + } else { + toast(R.string.no_app_found) + } + } +} + fun BaseSimpleActivity.getTempFile(): File? { val folder = File(cacheDir, "contacts") if (!folder.exists()) { diff --git a/app/src/main/res/menu/menu_group.xml b/app/src/main/res/menu/menu_group.xml new file mode 100644 index 00000000..c8073523 --- /dev/null +++ b/app/src/main/res/menu/menu_group.xml @@ -0,0 +1,14 @@ + + + + +