Skip to content

Commit 169ec43

Browse files
committed
fix: support secondary and teritary fields when importing contact (addresses #383)
1 parent c3c5e16 commit 169ec43

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

app/src/main/java/com/bnyro/contacts/ui/activities/MainActivity.kt

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.bnyro.contacts.ui.screens.MainAppContent
1616
import com.bnyro.contacts.ui.theme.ConnectYouTheme
1717
import com.bnyro.contacts.util.BackupHelper
1818
import com.bnyro.contacts.util.ContactsHelper
19+
import com.bnyro.contacts.util.IntentHelper
1920
import java.net.URLDecoder
2021

2122
class MainActivity : BaseActivity() {
@@ -49,34 +50,7 @@ class MainActivity : BaseActivity() {
4950
private fun getInsertContactData(): ContactData? {
5051
return when {
5152
intent?.action == Intent.ACTION_INSERT -> {
52-
val name = intent.getStringExtra(Intents.Insert.NAME)
53-
?: intent.getStringExtra(Intents.Insert.PHONETIC_NAME)
54-
ContactData(
55-
displayName = name,
56-
firstName = name?.split(" ")?.firstOrNull(),
57-
surName = name?.split(" ", limit = 2)?.lastOrNull(),
58-
organization = intent.getStringExtra(Intents.Insert.COMPANY),
59-
numbers = listOfNotNull(
60-
intent.getStringExtra(Intents.Insert.PHONE)?.let {
61-
ValueWithType(it, 0)
62-
}
63-
),
64-
emails = listOfNotNull(
65-
intent.getStringExtra(Intents.Insert.EMAIL)?.let {
66-
ValueWithType(it, 0)
67-
}
68-
),
69-
notes = listOfNotNull(
70-
intent.getStringExtra(Intents.Insert.NOTES)?.let {
71-
ValueWithType(it, 0)
72-
}
73-
),
74-
addresses = listOfNotNull(
75-
intent.getStringExtra(Intents.Insert.POSTAL)?.let {
76-
ValueWithType(it, 0)
77-
}
78-
)
79-
)
53+
IntentHelper.extractContactFromIntent(intent)
8054
}
8155

8256
intent?.getStringExtra("action") == "create" -> ContactData()

app/src/main/java/com/bnyro/contacts/util/IntentHelper.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import android.provider.ContactsContract
88
import androidx.core.net.toUri
99
import com.bnyro.contacts.R
1010
import com.bnyro.contacts.enums.IntentActionType
11+
import com.bnyro.contacts.obj.ContactData
12+
import com.bnyro.contacts.obj.TranslatedType
13+
import com.bnyro.contacts.obj.ValueWithType
1114

1215
object IntentHelper {
1316
fun launchAction(context: Context, type: IntentActionType, argument: String) {
@@ -67,4 +70,43 @@ object IntentHelper {
6770
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
6871
context.startActivity(intent)
6972
}
73+
74+
fun extractContactFromIntent(intent: Intent): ContactData {
75+
val name = intent.getStringExtra(ContactsContract.Intents.Insert.NAME)
76+
?: intent.getStringExtra(ContactsContract.Intents.Insert.PHONETIC_NAME)
77+
78+
return ContactData(
79+
displayName = name,
80+
firstName = name?.split(" ")?.firstOrNull(),
81+
surName = name?.split(" ", limit = 2)?.lastOrNull(),
82+
organization = intent.getStringExtra(ContactsContract.Intents.Insert.COMPANY),
83+
numbers = extractIntentValue(intent, ContactsContract.Intents.Insert.PHONE, ContactsContract.Intents.Insert.PHONE_TYPE) +
84+
extractIntentValue(intent, ContactsContract.Intents.Insert.SECONDARY_PHONE, ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE) +
85+
extractIntentValue(intent, ContactsContract.Intents.Insert.TERTIARY_PHONE, ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE),
86+
emails = extractIntentValue(intent, ContactsContract.Intents.Insert.EMAIL, ContactsContract.Intents.Insert.EMAIL_TYPE) +
87+
extractIntentValue(intent, ContactsContract.Intents.Insert.SECONDARY_EMAIL, ContactsContract.Intents.Insert.SECONDARY_EMAIL_TYPE) +
88+
extractIntentValue(intent, ContactsContract.Intents.Insert.TERTIARY_EMAIL, ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE),
89+
notes = extractIntentValue(intent, ContactsContract.Intents.Insert.NOTES),
90+
addresses = extractIntentValue(intent, ContactsContract.Intents.Insert.POSTAL)
91+
)
92+
}
93+
94+
private fun extractIntentValue(
95+
intent: Intent,
96+
key: String,
97+
typeKey: String? = null,
98+
types: List<TranslatedType> = emptyList()
99+
): List<ValueWithType> {
100+
val entry = intent.getStringExtra(key) ?: return emptyList()
101+
102+
val type = if (typeKey != null) {
103+
val typeIdentifier = intent.getStringExtra(typeKey)
104+
105+
types.firstOrNull {
106+
it.vcardType?.value?.uppercase() == typeIdentifier
107+
}?.id
108+
} else null
109+
110+
return listOf(ValueWithType(entry, type ?: 0))
111+
}
70112
}

0 commit comments

Comments
 (0)