Modern Apps git

← Modern-Apps.git

contacts: per-contact custom ringtone (#579)

Commit
f5018e6e856123af1ef489c48f62ce979f686b17
Author
vayun-mathur <[email protected]>
When
2026-08-22 01:06 UTC (3 months ago)
Parents
c891d3f

Diff

contacts/src/main/java/com/vayunmathur/contacts/data/Contact.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/data/Contact.kt b/contacts/src/main/java/com/vayunmathur/contacts/data/Contact.ktindex 3fa98fcd97..b03eb59779 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/data/Contact.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/data/Contact.kt@@ -15,6 +15,7 @@ import kotlinx.datetime.format import kotlinx.datetime.format.MonthNames import kotlinx.datetime.toLocalDateTime import com.vayunmathur.library.util.localizedMonthNames+import com.vayunmathur.library.ui.RINGTONE_SILENT import kotlinx.serialization.Serializable import kotlin.io.encoding.Base64 import kotlin.time.Clock@@ -230,7 +231,9 @@ data class Contact(     val accountType: String?,     val accountName: String?,     val isFavorite: Boolean,-    val details: ContactDetails+    val details: ContactDetails,+    /** `null` rings with the system default; [RINGTONE_SILENT] suppresses the ringtone. */+    val customRingtone: String? = null ) {     val name: Name         get() = details.names.first()@@ -256,14 +259,16 @@ data class Contact(             ops += ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)                 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)                 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)+                .withValue(ContactsContract.RawContacts.CUSTOM_RINGTONE, ringtoneToProvider(customRingtone))                 .build()              ops += details.all().map { createInsertOperation(it) }         } else {-            // Favorite+            // Favorite and ringtone             ops += ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)                 .withSelection("${ContactsContract.RawContacts._ID} = ?", arrayOf(id.toString()))                 .withValue(ContactsContract.RawContacts.STARRED, if (isFavorite) 1 else 0)+                .withValue(ContactsContract.RawContacts.CUSTOM_RINGTONE, ringtoneToProvider(customRingtone))                 .build()              // details@@ -409,9 +414,20 @@ data class Contact(             val displayName: String?,             val isFavorite: Boolean,             val accountName: String?,-            val accountType: String?+            val accountType: String?,+            val customRingtone: String?         ) +        /**+         * The provider spells "do not ring" as an empty CUSTOM_RINGTONE and "system default"+         * as NULL, so translate between that and the shared [RINGTONE_SILENT] sentinel.+         */+        private fun ringtoneFromProvider(stored: String?): String? =+            if (stored != null && stored.isEmpty()) RINGTONE_SILENT else stored++        private fun ringtoneToProvider(value: String?): String? =+            if (value == RINGTONE_SILENT) "" else value+         private fun getContacts(context: Context, contactId: Long?): List<Contact> {             val contentResolver = context.contentResolver             val projection = arrayOf(@@ -420,6 +436,7 @@ data class Contact(                 ContactsContract.RawContacts.STARRED,                 ContactsContract.RawContacts.ACCOUNT_NAME,                 ContactsContract.RawContacts.ACCOUNT_TYPE,+                ContactsContract.RawContacts.CUSTOM_RINGTONE,             )              val rawContacts = mutableListOf<RawContactInfo>()@@ -435,6 +452,7 @@ data class Contact(                     val starredIdx = cursor.getColumnIndexOrThrow(ContactsContract.RawContacts.STARRED)                     val accountNameIdx = cursor.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_NAME)                     val accountTypeIdx = cursor.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE)+                    val ringtoneIdx = cursor.getColumnIndexOrThrow(ContactsContract.RawContacts.CUSTOM_RINGTONE)                      while (cursor.moveToNext()) {                         rawContacts += RawContactInfo(@@ -442,7 +460,8 @@ data class Contact(                             displayName = cursor.getStringOrNull(nameIdx),                             isFavorite = cursor.getInt(starredIdx) == 1,                             accountName = cursor.getStringOrNull(accountNameIdx),-                            accountType = cursor.getStringOrNull(accountTypeIdx)+                            accountType = cursor.getStringOrNull(accountTypeIdx),+                            customRingtone = ringtoneFromProvider(cursor.getStringOrNull(ringtoneIdx))                         )                     }                 }@@ -455,7 +474,7 @@ data class Contact(             val allDetails = getDetailsInternal(context, contactId)             return rawContacts.mapNotNull { raw ->                 val details = processDetails(allDetails[raw.id] ?: ContactDetails.empty(), raw.displayName) ?: return@mapNotNull null-                Contact(raw.id, raw.accountType, raw.accountName, raw.isFavorite, details)+                Contact(raw.id, raw.accountType, raw.accountName, raw.isFavorite, details, raw.customRingtone)             }         } 
contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.ktindex 75becbfc54..d057521348 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt@@ -6,6 +6,7 @@ import android.content.ContentUris import android.content.Intent import android.content.pm.PackageManager import android.graphics.Bitmap+import android.media.RingtoneManager import android.net.Uri import android.provider.ContactsContract import android.os.Bundle@@ -44,6 +45,8 @@ import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Surface import com.vayunmathur.library.ui.Text import androidx.compose.runtime.Composable+import androidx.activity.compose.rememberLauncherForActivityResult+import androidx.activity.result.contract.ActivityResultContracts import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf@@ -70,6 +73,7 @@ import com.google.i18n.phonenumbers.PhoneNumberUtil import com.vayunmathur.contacts.data.CDKEvent import com.vayunmathur.contacts.data.Contact import com.vayunmathur.contacts.data.formatDisplay+import com.vayunmathur.contacts.data.isSimAccountType import com.vayunmathur.contacts.util.ContactDetailsUiState import com.vayunmathur.contacts.util.ContactPlatforms import com.vayunmathur.contacts.util.ContactViewModel@@ -93,9 +97,13 @@ import com.vayunmathur.library.ui.IconSms import com.vayunmathur.library.ui.IconStar import com.vayunmathur.library.ui.IconStarBorder import com.vayunmathur.library.ui.IconVideoCamera+import com.vayunmathur.library.ui.IconVolumeUp import com.vayunmathur.library.ui.OverflowMenu import com.vayunmathur.library.ui.rememberMessenger import com.vayunmathur.library.ui.appBarScrollBehavior+import com.vayunmathur.library.ui.ringtonePickerIntent+import com.vayunmathur.library.ui.ringtonePickerResult+import com.vayunmathur.library.ui.ringtoneTitle import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.filterNotNull@@ -152,6 +160,14 @@ fun ContactDetailsPage(      val scope = rememberCoroutineScope()     val shareContactLabel = stringResource(R.string.share_contact)+    val ringtonePickerTitle = stringResource(R.string.select_ringtone)+    val ringtoneLauncher = rememberLauncherForActivityResult(+        ActivityResultContracts.StartActivityForResult()+    ) { result ->+        if (result.resultCode == android.app.Activity.RESULT_OK) {+            viewModel.saveContact(contact.copy(customRingtone = ringtonePickerResult(result.data)))+        }+    }      ContactDetailsScreen(         state = ContactDetailsUiState(@@ -170,6 +186,16 @@ fun ContactDetailsPage(             override fun shareContacts(contacts: List<Contact>, filename: String) {                 shareContactsAsVcf(scope, context, contacts, filename, shareContactLabel)             }++            override fun pickRingtone(contact: Contact) {+                ringtoneLauncher.launch(+                    ringtonePickerIntent(+                        contact.customRingtone,+                        RingtoneManager.TYPE_RINGTONE,+                        ringtonePickerTitle,+                    )+                )+            }         },         showBackButton = showBackButton,     )@@ -402,6 +428,18 @@ fun ContactDetailsScreen(                     }                 }             }++            // A SIM's address book has nowhere to keep a ringtone.+            if (!isSimAccountType(contact.accountType)) {+                item {+                    DetailItem(+                        icon = { IconVolumeUp() },+                        data = ringtoneTitle(context, contact.customRingtone),+                        label = stringResource(R.string.ringtone),+                        onClick = { actions.pickRingtone(contact) },+                    )+                }+            }         } } 
contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.ktindex 08853cd3dc..914dd18489 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt@@ -56,6 +56,8 @@ interface ContactsActions {     /** Asks for confirmation before deleting — the plain [deleteContact] does not. */     fun confirmDeleteContact(contact: Contact) {}     fun closeContact() {}+    /** Opens the system ringtone picker; the result is saved straight onto [contact]. */+    fun pickRingtone(contact: Contact) {}     fun addToGroup(contactIds: List<Long>) {}      /** Exports [contacts] to a VCF named [filename] and opens a share chooser. */
contacts/src/main/res/values-ja/strings.xml
diff --git a/contacts/src/main/res/values-ja/strings.xml b/contacts/src/main/res/values-ja/strings.xmlindex 1fe2f9ba61..bf1b3fee51 100644--- a/contacts/src/main/res/values-ja/strings.xml+++ b/contacts/src/main/res/values-ja/strings.xml@@ -8,6 +8,13 @@     <string name="about_name"><xliff:g id="first_name" example="John">%1$s</xliff:g> について</string>     <string name="birthday">誕生日</string>     <string name="note">ノート</string>+    <string name="ringtone">着信音</string>+    <string name="select_ringtone">着信音を選択</string>+    <!-- Japanese overrides for the shared :library:ui ringtone labels. They cannot live in+         :library:ui because a values-ja there would add Japanese to every app's localeConfig. -->+    <string name="ringtone_default">デフォルト</string>+    <string name="ringtone_silent">無音</string>+    <string name="ringtone_custom">カスタム</string>     <string name="favorites">お気に入り</string>     <string name="export_contacts">全ての連絡先をエクスポート</string>     <string name="on_device">デバイス上</string>
contacts/src/main/res/values/strings.xml
diff --git a/contacts/src/main/res/values/strings.xml b/contacts/src/main/res/values/strings.xmlindex ec287ff5ac..8f7ed7a86f 100644--- a/contacts/src/main/res/values/strings.xml+++ b/contacts/src/main/res/values/strings.xml@@ -17,6 +17,10 @@     <string name="birthday">Birthday</string>     <!-- Section header and field label for freeform notes -->     <string name="note">Note</string>+    <!-- Field label for the row showing which ringtone plays for calls from this contact -->+    <string name="ringtone">Ringtone</string>+    <!-- Title of the system ringtone picker opened from the ringtone row -->+    <string name="select_ringtone">Select ringtone</string>     <!-- ContactList: section header above starred contacts -->     <string name="favorites">Favorites</string>     <!-- Settings: button to export all contacts to VCF file -->