DRSchlaubi / contact_picker

A Flutter plugin for picking a contact from the address book.
https://pub.dev/packages/fluttercontactpicker
BSD 3-Clause "New" or "Revised" License
18 stars 37 forks source link

Support Custom Relationship Type #33

Closed alkebuware closed 3 years ago

alkebuware commented 3 years ago

The plugin crashes when a user selects a contact with a custom relation type. Newer devices don't allow their users to set a relationship type on a contact, but older phones did. If type is 0 the number for custom, the app will crash.

We should add the ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM, and return the LABEL column value as indicated at https://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Relation

The crash occurs in ContactPicker.kt's buildRelation method

    private fun buildRelation(cursor: Cursor): Map<String, String> {
        val name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.NAME))
        val type = when (val typeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE))) {
            ContactsContract.CommonDataKinds.Relation.TYPE_ASSISTANT -> "assistant"
            ContactsContract.CommonDataKinds.Relation.TYPE_BROTHER -> "brother"
            ContactsContract.CommonDataKinds.Relation.TYPE_CHILD -> "child"
            ContactsContract.CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER -> "domestic_partner"
            ContactsContract.CommonDataKinds.Relation.TYPE_FATHER -> "father"
            ContactsContract.CommonDataKinds.Relation.TYPE_FRIEND -> "friend"
            ContactsContract.CommonDataKinds.Relation.TYPE_MANAGER -> "manager"
            ContactsContract.CommonDataKinds.Relation.TYPE_MOTHER -> "mother"
            ContactsContract.CommonDataKinds.Relation.TYPE_PARENT -> "parent"
            ContactsContract.CommonDataKinds.Relation.TYPE_PARTNER -> "partner"
            ContactsContract.CommonDataKinds.Relation.TYPE_REFERRED_BY -> "referred_by"
            ContactsContract.CommonDataKinds.Relation.TYPE_RELATIVE -> "relative"
            ContactsContract.CommonDataKinds.Relation.TYPE_SISTER -> "sister"
            ContactsContract.CommonDataKinds.Relation.TYPE_SPOUSE -> "spouse"
            else -> error("Unknown type: $typeInt")
        }

        return mapOf("name" to name, "type" to type)
    }

Below is a stacktrace off the exception. java.lang.RuntimeException: at android.app.ActivityThread.deliverResults (ActivityThread.java:5009) at android.app.ActivityThread.handleSendResult (ActivityThread.java:5050) at android.app.servertransaction.ActivityResultItem.execute (ActivityResultItem.java:51) at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2066) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:223) at android.app.ActivityThread.main (ActivityThread.java:7660) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947) Caused by: java.lang.IllegalStateException: at me.schlaubi.fluttercontactpicker.ContactPicker.access$buildPhoneNumber (ContactPicker.java:46) or .buildPhoneNumber (ContactPicker.java:46) or .buildRelation (ContactPicker.java:46) at me.schlaubi.fluttercontactpicker.ContactPicker.access$buildContact (ContactPicker.java:151) or .access$buildDisplayNamed (ContactPicker.java:151) or .access$getPickContext$p (ContactPicker.java:151) or .buildAddress (ContactPicker.java:151) or .buildContact (ContactPicker.java:151) or .buildCustomField (ContactPicker.java:151) or .buildDisplayNamed (ContactPicker.java:151) or .buildLabeledItem (ContactPicker.java:151) or .buildPhoto (ContactPicker.java:151) or .label (ContactPicker.java:151) or .onActivityResult (ContactPicker.java:151) or .processContact (ContactPicker.java:151) or .processDisplayNamed (ContactPicker.java:151) or .processInput (ContactPicker.java:151) or .requestPicker (ContactPicker.java:151) at me.schlaubi.fluttercontactpicker.ContactPicker.access$buildContact (ContactPicker.java) or .access$buildDisplayNamed (ContactPicker.java) or .access$getPickContext$p (ContactPicker.java) or .buildAddress (ContactPicker.java) or .buildContact (ContactPicker.java) or .buildCustomField (ContactPicker.java) or .buildDisplayNamed (ContactPicker.java) or .buildLabeledItem (ContactPicker.java) or .buildPhoto (ContactPicker.java) or .label (ContactPicker.java) or .onActivityResult (ContactPicker.java) or .processContact (ContactPicker.java) or .processDisplayNamed (ContactPicker.java) or .processInput (ContactPicker.java) or .requestPicker (ContactPicker.java) at me.schlaubi.fluttercontactpicker.ContactPicker$onActivityResult$3.invoke (ContactPicker.java:19) at me.schlaubi.fluttercontactpicker.ContactPicker$onActivityResult$3.invoke (ContactPicker.java:6) at me.schlaubi.fluttercontactpicker.ContactPicker$processContact$1.invoke (ContactPicker.java:40) at me.schlaubi.fluttercontactpicker.ContactPicker$processContact$1.invoke (ContactPicker.java:2) at me.schlaubi.fluttercontactpicker.ContactPicker.access$buildContact (ContactPicker.java:26) or .access$buildDisplayNamed (ContactPicker.java:26) or .access$getPickContext$p (ContactPicker.java:26) or .buildAddress (ContactPicker.java:26) or .buildContact (ContactPicker.java:26) or .buildCustomField (ContactPicker.java:26) or .buildDisplayNamed (ContactPicker.java:26) or .buildLabeledItem (ContactPicker.java:26) or .buildPhoto (ContactPicker.java:26) or .label (ContactPicker.java:26) or .onActivityResult (ContactPicker.java:26) or .processContact (ContactPicker.java:26) or .processDisplayNamed (ContactPicker.java:26) or .processInput (ContactPicker.java:26) or .requestPicker (ContactPicker.java:26) at me.schlaubi.fluttercontactpicker.ContactPicker.access$buildContact (ContactPicker.java:5) or .access$buildDisplayNamed (ContactPicker.java:5) or .access$getPickContext$p (ContactPicker.java:5) or .buildAddress (ContactPicker.java:5) or .buildContact (ContactPicker.java:5) or .buildCustomField (ContactPicker.java:5) or .buildDisplayNamed (ContactPicker.java:5) or .buildLabeledItem (ContactPicker.java:5) or .buildPhoto (ContactPicker.java:5) or .label (ContactPicker.java:5) or .onActivityResult (ContactPicker.java:5) or .processContact (ContactPicker.java:5) or .processDisplayNamed (ContactPicker.java:5) or .processInput (ContactPicker.java:5) or .requestPicker (ContactPicker.java:5) at me.schlaubi.fluttercontactpicker.ContactPicker.access$buildContact (ContactPicker.java:19) or .access$buildDisplayNamed (ContactPicker.java:19) or .access$getPickContext$p (ContactPicker.java:19) or .buildAddress (ContactPicker.java:19) or .buildContact (ContactPicker.java:19) or .buildCustomField (ContactPicker.java:19) or .buildDisplayNamed (ContactPicker.java:19) or .buildLabeledItem (ContactPicker.java:19) or .buildPhoto (ContactPicker.java:19) or .label (ContactPicker.java:19) or .onActivityResult (ContactPicker.java:19) or .processContact (ContactPicker.java:19) or .processDisplayNamed (ContactPicker.java:19) or .processInput (ContactPicker.java:19) or .requestPicker (ContactPicker.java:19) at me.schlaubi.fluttercontactpicker.AbstractPickContext.addActivityResultListener (AbstractPickContext.java:30) or .onActivityResult (AbstractPickContext.java:30) at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.addActivityResultListener (FlutterEngineConnectionRegistry.java:25) or .addOnNewIntentListener (FlutterEngineConnectionRegistry.java:25) or .addRequestPermissionsResultListener (FlutterEngineConnectionRegistry.java:25) or .getLifecycle (FlutterEngineConnectionRegistry.java:25) or .onActivityResult (FlutterEngineConnectionRegistry.java:25) or .onNewIntent (FlutterEngineConnectionRegistry.java:25) or .onRequestPermissionsResult (FlutterEngineConnectionRegistry.java:25) or .onRestoreInstanceState (FlutterEngineConnectionRegistry.java:25) at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.add (FlutterEngineConnectionRegistry.java:15) or .attachToActivity (FlutterEngineConnectionRegistry.java:15) or .attachToActivityInternal (FlutterEngineConnectionRegistry.java:15) or .has (FlutterEngineConnectionRegistry.java:15) or .onActivityResult (FlutterEngineConnectionRegistry.java:15) or .onSaveInstanceState (FlutterEngineConnectionRegistry.java:15) or .onUserLeaveHint (FlutterEngineConnectionRegistry.java:15) or .remove (FlutterEngineConnectionRegistry.java:15) at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.access$000 (FlutterActivityAndFragmentDelegate.java:51) or .getFlutterEngine (FlutterActivityAndFragmentDelegate.java:51) or .onActivityResult (FlutterActivityAndFragmentDelegate.java:51) or .onAttach (FlutterActivityAndFragmentDelegate.java:51) or .onCreateView (FlutterActivityAndFragmentDelegate.java:51) or .onNewIntent (FlutterActivityAndFragmentDelegate.java:51) or .onRequestPermissionsResult (FlutterActivityAndFragmentDelegate.java:51) or .onRestoreInstanceState (FlutterActivityAndFragmentDelegate.java:51) or .onTrimMemory (FlutterActivityAndFragmentDelegate.java:51) at io.flutter.embedding.android.FlutterActivity.onActivityResult (FlutterActivity.java:10) at android.app.Activity.dispatchActivityResult (Activity.java:8310) at android.app.ActivityThread.deliverResults (ActivityThread.java:5002) at android.app.ActivityThread.handleSendResult (ActivityThread.java:5050) at android.app.servertransaction.ActivityResultItem.execute (ActivityResultItem.java:51) at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2066) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:223) at android.app.ActivityThread.main (ActivityThread.java:7660) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947)

DRSchlaubi commented 3 years ago

Will take a look at this tmr

alkebuware commented 3 years ago

This was merged #34