fix #137, allow sending SMS and emails to whole groups

This commit is contained in:
tibbi 2018-04-18 15:50:37 +02:00
parent 387030e50e
commit a035b21c01
4 changed files with 90 additions and 52 deletions

View File

@ -1,6 +1,8 @@
package com.simplemobiletools.contacts.activities package com.simplemobiletools.contacts.activities
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.adapters.ContactsAdapter import com.simplemobiletools.contacts.adapters.ContactsAdapter
@ -46,6 +48,20 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
refreshContacts() 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() { private fun fabClicked() {
SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts -> SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts ->
Thread { 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<Contact>) { private fun updateContacts(contacts: ArrayList<Contact>) {
val currAdapter = group_contacts_list.adapter val currAdapter = group_contacts_list.adapter
if (currAdapter == null) { if (currAdapter == null) {

View File

@ -1,8 +1,6 @@
package com.simplemobiletools.contacts.adapters package com.simplemobiletools.contacts.adapters
import android.content.Intent
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri
import android.view.Menu import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup 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.beVisibleIf
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.extensions.isActivityDestroyed import com.simplemobiletools.commons.extensions.isActivityDestroyed
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.dialogs.CreateNewGroupDialog import com.simplemobiletools.contacts.dialogs.CreateNewGroupDialog
import com.simplemobiletools.contacts.extensions.addContactsToGroup import com.simplemobiletools.contacts.extensions.*
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.editContact
import com.simplemobiletools.contacts.extensions.shareContacts
import com.simplemobiletools.contacts.helpers.* import com.simplemobiletools.contacts.helpers.*
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.interfaces.RemoveFromGroupListener import com.simplemobiletools.contacts.interfaces.RemoveFromGroupListener
@ -181,21 +175,13 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
} }
private fun addToFavorites() { private fun addToFavorites() {
val newFavorites = ArrayList<Contact>() ContactsHelper(activity).addFavorites(getSelectedContacts())
selectedPositions.forEach {
newFavorites.add(contactItems[it])
}
ContactsHelper(activity).addFavorites(newFavorites)
refreshListener?.refreshContacts(FAVORITES_TAB_MASK) refreshListener?.refreshContacts(FAVORITES_TAB_MASK)
finishActMode() finishActMode()
} }
private fun addToGroup() { private fun addToGroup() {
val selectedContacts = ArrayList<Contact>() val selectedContacts = getSelectedContacts()
selectedPositions.forEach {
selectedContacts.add(contactItems[it])
}
val NEW_GROUP_ID = -1 val NEW_GROUP_ID = -1
val items = ArrayList<RadioItem>() val items = ArrayList<RadioItem>()
ContactsHelper(activity).getStoredGroups().forEach { ContactsHelper(activity).getStoredGroups().forEach {
@ -233,46 +219,19 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
} }
private fun sendSMSToContacts() { private fun sendSMSToContacts() {
val numbers = StringBuilder() activity.sendSMSToContacts(getSelectedContacts())
selectedPositions.forEach {
val contact = contactItems[it]
contact.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(activity.packageManager) != null) {
activity.startActivity(this)
} else {
activity.toast(R.string.no_app_found)
}
}
} }
private fun sendEmailToContacts() { private fun sendEmailToContacts() {
val emails = ArrayList<String>() activity.sendEmailToContacts(getSelectedContacts())
selectedPositions.forEach { }
val contact = contactItems[it]
contact.emails.forEach {
if (it.value.isNotEmpty()) {
emails.add(it.value)
}
}
}
Intent(Intent.ACTION_SEND_MULTIPLE).apply { private fun getSelectedContacts(): ArrayList<Contact> {
type = "message/rfc822" val contacts = ArrayList<Contact>()
putExtra(Intent.EXTRA_EMAIL, emails.toTypedArray()) selectedPositions.forEach {
if (resolveActivity(activity.packageManager) != null) { contacts.add(contactItems[it])
activity.startActivity(this)
} else {
activity.toast(R.string.no_app_found)
}
} }
return contacts
} }
override fun onViewRecycled(holder: ViewHolder) { override fun onViewRecycled(holder: ViewHolder) {

View File

@ -99,6 +99,47 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList<Contact>) {
} }
} }
fun BaseSimpleActivity.sendSMSToContacts(contacts: ArrayList<Contact>) {
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<Contact>) {
val emails = ArrayList<String>()
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? { fun BaseSimpleActivity.getTempFile(): File? {
val folder = File(cacheDir, "contacts") val folder = File(cacheDir, "contacts")
if (!folder.exists()) { if (!folder.exists()) {

View File

@ -0,0 +1,14 @@
<?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/send_sms_to_group"
android:icon="@drawable/ic_sms"
android:title="@string/send_sms_to_group"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/send_email_to_group"
android:icon="@drawable/ic_email"
android:title="@string/send_email_to_group"
app:showAsAction="ifRoom"/>
</menu>