From af7b46f1b797a502b45393f922d4ea4c1ff23fd9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 14 Feb 2021 19:18:49 +0100 Subject: [PATCH] allow changing contact ringtones from the View screen too --- .../pro/activities/ViewContactActivity.kt | 16 ++++++++++++++++ .../contacts/pro/helpers/ContactsHelper.kt | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt index b28d498c..67d645f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.pro.activities import android.content.ContentUris import android.content.Intent +import android.media.AudioManager import android.media.RingtoneManager import android.net.Uri import android.os.Bundle @@ -10,6 +11,7 @@ import android.view.View import android.view.WindowManager import android.widget.RelativeLayout import com.simplemobiletools.commons.dialogs.ConfirmationDialog +import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.contacts.pro.R @@ -589,6 +591,20 @@ class ViewContactActivity : ContactActivity() { } contact_ringtone.copyOnLongClick(contact_ringtone.text.toString()) + + contact_ringtone.setOnClickListener { + val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri + SelectAlarmSoundDialog(this, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true, + onAlarmPicked = { + contact!!.ringtone = it?.uri + contact_ringtone.text = it?.title + + ensureBackgroundThread { + ContactsHelper(this).updateRingtone(contact!!.contactId.toString(), it?.uri ?: "") + } + }, onAlarmSoundDeleted = {} + ) + } } else { contact_ringtone_image.beGone() contact_ringtone.beGone() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index d180804e..6e3d5bcf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -1424,6 +1424,21 @@ class ContactsHelper(val context: Context) { LocalContactsHelper(context).toggleFavorites(localContacts, addToFavorites) } + fun updateRingtone(contactId: String, newUri: String) { + try { + val operations = ArrayList() + val uri = Uri.withAppendedPath(Contacts.CONTENT_URI, contactId) + ContentProviderOperation.newUpdate(uri).apply { + withValue(Contacts.CUSTOM_RINGTONE, newUri) + operations.add(build()) + } + + context.contentResolver.applyBatch(AUTHORITY, operations) + } catch (e: Exception) { + context.showErrorToast(e) + } + } + fun deleteContact(originalContact: Contact, deleteClones: Boolean = false, callback: (success: Boolean) -> Unit) { ensureBackgroundThread { if (deleteClones) {