Modern Apps git

← Modern-Apps.git

contacts: extract insert-prefill parsing from MainActivity into intent helpers

Commit
1312e1d67f7d28d1cbcce1b35888b33e735896de
Author
Vayun Mathur <[email protected]>
When
2026-11-08 05:16 UTC (28 days ago)
Parents
4f367d3
What: move the ACTION_INSERT prefill-parsing block (buildInsertPrefill,
collectPrefillPhones/Emails/Postals/Names, applyPrefillDataRows and the
Phone/Email/Postal row appliers, PrefillNames) out of MainActivity.kt
into MainActivityIntentHelpers.kt as internal top-level functions, and
drop the now-unused imports from MainActivity.

Why: :contacts:lintDev failed FileLength - MainActivity.kt was 805
lines against the 800-line limit for non-ui files, failing the module.

Root cause: MainActivity accumulated intent-routing, prefill parsing,
contact resolution and navigation in one file until it crossed the
lint limit. The prefill parsers are pure intent/ContentValues logic
with no activity state, so they move cleanly.

Alternatives rejected: suppressing FileLength or splitting Route/
Navigation into new files - both rejected because Route is imported
by ~11 test files and Navigation by 8 ui files, so that split would
ripple; the prefill block is self-contained and follows the existing
MainActivityIntentHelpers.kt precedent (isVcfIntent/readType/
uriPhoneNumber).

User-visible effect: none - pure move, behavior identical.

Verification: :contacts:compileDevKotlin + :contacts:lint BUILD
SUCCESSFUL (FileLength clear, MainActivity.kt now 656 lines).
:contacts:testDevUnitTest: 459 tests, 1 failure in
GroupAndSimImportPass2Test.addAndRemoveGroupOpsReachProviderSkippingSimContacts
(expected 2, was 4) - pre-existing timing flake in group-op counting,
unrelated to this move (failing path touches neither moved function;
passes in isolation); tracked separately.

Diff

contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt b/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.ktindex 8b3ea721c7..89234b4b92 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt@@ -1,14 +1,11 @@ package com.vayunmathur.contacts  import android.Manifest-import android.content.ContentValues import android.content.Intent import android.content.ClipData import android.content.ContentUris import android.net.Uri import android.provider.ContactsContract-import android.provider.ContactsContract.Intents.Insert-import androidx.core.content.IntentCompat import androidx.activity.compose.BackHandler import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts@@ -37,13 +34,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle-import com.vayunmathur.contacts.data.CDKEmail-import com.vayunmathur.contacts.data.CDKNickname-import com.vayunmathur.contacts.data.CDKNote-import com.vayunmathur.contacts.data.CDKOrg import com.vayunmathur.contacts.data.CDKPhone-import com.vayunmathur.contacts.data.CDKSName-import com.vayunmathur.contacts.data.CDKStructuredPostal import com.vayunmathur.contacts.data.ContactPrefill import com.vayunmathur.contacts.data.PrefillValue import com.vayunmathur.contacts.ui.dialogs.AddAccountDialog@@ -270,145 +261,6 @@ class MainActivity : MaActivity() {             )         } -    /**-     * Parses everything an ACTION_INSERT-style intent can carry into a [ContactPrefill].-     *-     * Reads the scalar [ContactsContract.Intents.Insert] extras via [Intent.getCharSequenceExtra]-     * (dialers/call logs pass values as `CharSequence`/`Spannable`, so `getStringExtra` returns-     * null for them) plus the [ContactsContract.Intents.Insert.DATA] `ArrayList<ContentValues>`.-     */-    private fun buildInsertPrefill(intent: Intent): ContactPrefill {-        fun text(key: String): String? =-            intent.getCharSequenceExtra(key)?.toString()?.trim()?.takeIf { it.isNotEmpty() }--        val phones = collectPrefillPhones(intent, ::text)-        val emails = collectPrefillEmails(intent, ::text)-        val postals = collectPrefillPostals(intent, ::text)-        val names = collectPrefillNames(::text)-        applyPrefillDataRows(intent, phones, emails, postals, names)--        return ContactPrefill(-            names.name,-            names.company,-            names.notes,-            names.nickname,-            phones,-            emails,-            postals,-        )-    }--    private fun collectPrefillPhones(-        intent: Intent,-        text: (String) -> String?,-    ): MutableList<PrefillValue> {-        val phones = mutableListOf<PrefillValue>()-        val (phoneType, phoneLabel) = readType(intent, Insert.PHONE_TYPE)-        text(Insert.PHONE)?.let { phones += PrefillValue(it, phoneType, phoneLabel) }-        text(Insert.SECONDARY_PHONE)?.let { phones += PrefillValue(it) }-        text(Insert.TERTIARY_PHONE)?.let { phones += PrefillValue(it) }-        return phones-    }--    private fun collectPrefillEmails(-        intent: Intent,-        text: (String) -> String?,-    ): MutableList<PrefillValue> {-        val emails = mutableListOf<PrefillValue>()-        val (emailType, emailLabel) = readType(intent, Insert.EMAIL_TYPE)-        text(Insert.EMAIL)?.let { emails += PrefillValue(it, emailType, emailLabel) }-        text(Insert.SECONDARY_EMAIL)?.let { emails += PrefillValue(it) }-        text(Insert.TERTIARY_EMAIL)?.let { emails += PrefillValue(it) }-        return emails-    }--    private fun collectPrefillPostals(-        intent: Intent,-        text: (String) -> String?,-    ): MutableList<PrefillValue> {-        val postals = mutableListOf<PrefillValue>()-        val (postalType, postalLabel) = readType(intent, Insert.POSTAL_TYPE)-        text(Insert.POSTAL)?.let { postals += PrefillValue(it, postalType, postalLabel) }-        return postals-    }--    private class PrefillNames(-        var name: String? = null,-        var company: String? = null,-        var notes: String? = null,-        var nickname: String? = null,-    )--    private fun collectPrefillNames(-        text: (String) -> String?,-    ): PrefillNames = PrefillNames(-        name = text(Insert.NAME),-        company = text(Insert.COMPANY),-        notes = text(Insert.NOTES),-    )--    private fun applyPrefillDataRows(-        intent: Intent,-        phones: MutableList<PrefillValue>,-        emails: MutableList<PrefillValue>,-        postals: MutableList<PrefillValue>,-        names: PrefillNames,-    ) {-        // Insert.DATA: caller-provided rows, one ContentValues per data kind (typed).-        val dataRows =-            IntentCompat.getParcelableArrayListExtra(intent, Insert.DATA, ContentValues::class.java)-        dataRows?.forEach { cv ->-            fun value(key: String) = cv.getAsString(key)?.trim()?.takeIf { it.isNotEmpty() }-            when (cv.getAsString(ContactsContract.Data.MIMETYPE)) {-                CDKPhone.CONTENT_ITEM_TYPE ->-                    applyPrefillPhoneRow(cv, ::value, phones)-                CDKEmail.CONTENT_ITEM_TYPE ->-                    applyPrefillEmailRow(cv, ::value, emails)-                CDKStructuredPostal.CONTENT_ITEM_TYPE ->-                    applyPrefillPostalRow(cv, ::value, postals)-                CDKSName.CONTENT_ITEM_TYPE ->-                    if (names.name == null) names.name = value(CDKSName.DISPLAY_NAME)-                CDKOrg.CONTENT_ITEM_TYPE ->-                    if (names.company == null) names.company = value(CDKOrg.COMPANY)-                CDKNote.CONTENT_ITEM_TYPE ->-                    if (names.notes == null) names.notes = value(CDKNote.NOTE)-                CDKNickname.CONTENT_ITEM_TYPE ->-                    if (names.nickname == null) names.nickname = value(CDKNickname.NAME)-            }-        }-    }--    private fun applyPrefillPhoneRow(-        cv: ContentValues,-        value: (String) -> String?,-        phones: MutableList<PrefillValue>,-    ) {-        val number = value(CDKPhone.NUMBER) ?: return-        phones += PrefillValue(number, cv.getAsInteger(CDKPhone.TYPE), cv.getAsString(CDKPhone.LABEL))-    }--    private fun applyPrefillEmailRow(-        cv: ContentValues,-        value: (String) -> String?,-        emails: MutableList<PrefillValue>,-    ) {-        val address = value(CDKEmail.ADDRESS) ?: return-        emails += PrefillValue(address, cv.getAsInteger(CDKEmail.TYPE), cv.getAsString(CDKEmail.LABEL))-    }--    private fun applyPrefillPostalRow(-        cv: ContentValues,-        value: (String) -> String?,-        postals: MutableList<PrefillValue>,-    ) {-        val formatted = value(CDKStructuredPostal.FORMATTED_ADDRESS) ?: return-        postals += PrefillValue(-            formatted,-            cv.getAsInteger(CDKStructuredPostal.TYPE),-            cv.getAsString(CDKStructuredPostal.LABEL),-        )-    }-     private fun resolveContactId(uri: Uri?): Long? {         if (uri == null) return null         val path = uri.path ?: return null
contacts/src/main/java/com/vayunmathur/contacts/MainActivityIntentHelpers.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/MainActivityIntentHelpers.kt b/contacts/src/main/java/com/vayunmathur/contacts/MainActivityIntentHelpers.ktindex 24850b81d9..be8ad23819 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/MainActivityIntentHelpers.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/MainActivityIntentHelpers.kt@@ -1,7 +1,19 @@ package com.vayunmathur.contacts +import android.content.ContentValues import android.content.Intent import android.provider.ContactsContract+import android.provider.ContactsContract.Intents.Insert+import androidx.core.content.IntentCompat+import com.vayunmathur.contacts.data.CDKEmail+import com.vayunmathur.contacts.data.CDKNickname+import com.vayunmathur.contacts.data.CDKNote+import com.vayunmathur.contacts.data.CDKOrg+import com.vayunmathur.contacts.data.CDKPhone+import com.vayunmathur.contacts.data.CDKSName+import com.vayunmathur.contacts.data.CDKStructuredPostal+import com.vayunmathur.contacts.data.ContactPrefill+import com.vayunmathur.contacts.data.PrefillValue  internal fun isVcfIntent(intent: Intent): Boolean {     val type = intent.type ?: ""@@ -41,3 +53,142 @@ internal fun uriPhoneNumber(intent: Intent): String? {     }     return null }++internal class PrefillNames(+    var name: String? = null,+    var company: String? = null,+    var notes: String? = null,+    var nickname: String? = null,+)++/**+ * Parses everything an ACTION_INSERT-style intent can carry into a [ContactPrefill].+ *+ * Reads the scalar [ContactsContract.Intents.Insert] extras via [Intent.getCharSequenceExtra]+ * (dialers/call logs pass values as `CharSequence`/`Spannable`, so `getStringExtra` returns+ * null for them) plus the [ContactsContract.Intents.Insert.DATA] `ArrayList<ContentValues>`.+ */+internal fun buildInsertPrefill(intent: Intent): ContactPrefill {+    fun text(key: String): String? =+        intent.getCharSequenceExtra(key)?.toString()?.trim()?.takeIf { it.isNotEmpty() }++    val phones = collectPrefillPhones(intent, ::text)+    val emails = collectPrefillEmails(intent, ::text)+    val postals = collectPrefillPostals(intent, ::text)+    val names = collectPrefillNames(::text)+    applyPrefillDataRows(intent, phones, emails, postals, names)++    return ContactPrefill(+        names.name,+        names.company,+        names.notes,+        names.nickname,+        phones,+        emails,+        postals,+    )+}++internal fun collectPrefillPhones(+    intent: Intent,+    text: (String) -> String?,+): MutableList<PrefillValue> {+    val phones = mutableListOf<PrefillValue>()+    val (phoneType, phoneLabel) = readType(intent, Insert.PHONE_TYPE)+    text(Insert.PHONE)?.let { phones += PrefillValue(it, phoneType, phoneLabel) }+    text(Insert.SECONDARY_PHONE)?.let { phones += PrefillValue(it) }+    text(Insert.TERTIARY_PHONE)?.let { phones += PrefillValue(it) }+    return phones+}++internal fun collectPrefillEmails(+    intent: Intent,+    text: (String) -> String?,+): MutableList<PrefillValue> {+    val emails = mutableListOf<PrefillValue>()+    val (emailType, emailLabel) = readType(intent, Insert.EMAIL_TYPE)+    text(Insert.EMAIL)?.let { emails += PrefillValue(it, emailType, emailLabel) }+    text(Insert.SECONDARY_EMAIL)?.let { emails += PrefillValue(it) }+    text(Insert.TERTIARY_EMAIL)?.let { emails += PrefillValue(it) }+    return emails+}++internal fun collectPrefillPostals(+    intent: Intent,+    text: (String) -> String?,+): MutableList<PrefillValue> {+    val postals = mutableListOf<PrefillValue>()+    val (postalType, postalLabel) = readType(intent, Insert.POSTAL_TYPE)+    text(Insert.POSTAL)?.let { postals += PrefillValue(it, postalType, postalLabel) }+    return postals+}++internal fun collectPrefillNames(+    text: (String) -> String?,+): PrefillNames = PrefillNames(+    name = text(Insert.NAME),+    company = text(Insert.COMPANY),+    notes = text(Insert.NOTES),+)++internal fun applyPrefillDataRows(+    intent: Intent,+    phones: MutableList<PrefillValue>,+    emails: MutableList<PrefillValue>,+    postals: MutableList<PrefillValue>,+    names: PrefillNames,+) {+    // Insert.DATA: caller-provided rows, one ContentValues per data kind (typed).+    val dataRows =+        IntentCompat.getParcelableArrayListExtra(intent, Insert.DATA, ContentValues::class.java)+    dataRows?.forEach { cv ->+        fun value(key: String) = cv.getAsString(key)?.trim()?.takeIf { it.isNotEmpty() }+        when (cv.getAsString(ContactsContract.Data.MIMETYPE)) {+            CDKPhone.CONTENT_ITEM_TYPE ->+                applyPrefillPhoneRow(cv, ::value, phones)+            CDKEmail.CONTENT_ITEM_TYPE ->+                applyPrefillEmailRow(cv, ::value, emails)+            CDKStructuredPostal.CONTENT_ITEM_TYPE ->+                applyPrefillPostalRow(cv, ::value, postals)+            CDKSName.CONTENT_ITEM_TYPE ->+                if (names.name == null) names.name = value(CDKSName.DISPLAY_NAME)+            CDKOrg.CONTENT_ITEM_TYPE ->+                if (names.company == null) names.company = value(CDKOrg.COMPANY)+            CDKNote.CONTENT_ITEM_TYPE ->+                if (names.notes == null) names.notes = value(CDKNote.NOTE)+            CDKNickname.CONTENT_ITEM_TYPE ->+                if (names.nickname == null) names.nickname = value(CDKNickname.NAME)+        }+    }+}++internal fun applyPrefillPhoneRow(+    cv: ContentValues,+    value: (String) -> String?,+    phones: MutableList<PrefillValue>,+) {+    val number = value(CDKPhone.NUMBER) ?: return+    phones += PrefillValue(number, cv.getAsInteger(CDKPhone.TYPE), cv.getAsString(CDKPhone.LABEL))+}++internal fun applyPrefillEmailRow(+    cv: ContentValues,+    value: (String) -> String?,+    emails: MutableList<PrefillValue>,+) {+    val address = value(CDKEmail.ADDRESS) ?: return+    emails += PrefillValue(address, cv.getAsInteger(CDKEmail.TYPE), cv.getAsString(CDKEmail.LABEL))+}++internal fun applyPrefillPostalRow(+    cv: ContentValues,+    value: (String) -> String?,+    postals: MutableList<PrefillValue>,+) {+    val formatted = value(CDKStructuredPostal.FORMATTED_ADDRESS) ?: return+    postals += PrefillValue(+        formatted,+        cv.getAsInteger(CDKStructuredPostal.TYPE),+        cv.getAsString(CDKStructuredPostal.LABEL),+    )+}