Merge pull request #689 from may-kumar/master

Add option to set ringtone for Groups
This commit is contained in:
Tibor Kaputa 2021-08-16 12:07:53 +02:00 committed by GitHub
commit b07edf7d2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 4 deletions

View file

@ -1,12 +1,13 @@
package com.simplemobiletools.contacts.pro.activities
import android.app.Activity
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.underlineText
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter
@ -27,6 +28,25 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
private var wasInit = false
lateinit var group: Group
protected val INTENT_SELECT_RINGTONE = 600
protected var contact: Contact? = null
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == INTENT_SELECT_RINGTONE && resultCode == Activity.RESULT_OK && resultData != null) {
val extras = resultData.extras
if (extras?.containsKey(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) == true) {
val uri = extras.getParcelable<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) ?: return
try {
setRingtoneOnSelected(uri)
} catch (e: Exception) {
showErrorToast(e)
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_group_contacts)
@ -64,6 +84,7 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
when (item.itemId) {
R.id.send_sms_to_group -> sendSMSToGroup()
R.id.send_email_to_group -> sendEmailToGroup()
R.id.assign_ringtone_to_group -> assignRingtoneToGroup()
else -> return super.onOptionsItemSelected(item)
}
return true
@ -100,6 +121,15 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
sendEmailToContacts(groupContacts)
}
private fun assignRingtoneToGroup() {
val ringtonePickerIntent = getRingtonePickerIntent()
try {
startActivityForResult(ringtonePickerIntent, INTENT_SELECT_RINGTONE)
} catch (e: Exception) {
toast(e.toString())
}
}
private fun updateContacts(contacts: ArrayList<Contact>) {
val currAdapter = group_contacts_list.adapter
if (currAdapter == null) {
@ -136,4 +166,25 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
}
}
}
private fun getDefaultRingtoneUri() = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE)
private fun getRingtonePickerIntent(): Intent {
val defaultRingtoneUri = getDefaultRingtoneUri()
return Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply {
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true)
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE)
putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, defaultRingtoneUri)
putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, defaultRingtoneUri)
}
}
private fun setRingtoneOnSelected(uri: Uri) {
groupContacts.forEach{
ContactsHelper(this).updateRingtone(it.contactId.toString(), uri.toString())
}
}
}

View file

@ -1,6 +1,11 @@
<?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/assign_ringtone_to_group"
android:icon="@drawable/ic_bell_vector"
android:title="@string/ringtone"
app:showAsAction="ifRoom" />
<item
android:id="@+id/send_sms_to_group"
android:icon="@drawable/ic_sms_vector"