Different results when add new contact on Google devices (Nexus 6, Pixel )

74 views Asked by At

I have strange issue with Nexus 6 (Android 7.0) that when adding new contact via intent, Uri as result as rawContacts not lookup as usual.

Here is my intent:

public static Intent getNewContactIntent() {
    Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
    intent.putExtra("finishActivityOnSaveCompleted", true);
    return intent;
}

Result onActivityResult:

Uri contactUri = data.getData();

On Nexus 6 (Api 24) device:

content://com.android.contacts/raw_contacts/1376

While on other devices including Nexus 6 on Emulator:

content://com.android.contacts/contacts/lookup/2883i3c5a4b238cc57aad/1376

How to manage to get same result on all Android devices including Google's ?

Extra Info:

This weird behaviour can be reproduced on Pixel (Android 8) too.

Data retrieved with uri (.../raw_contacts/1376):

+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
|           column            |                                                                    value                                                                    |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
| sort_key                    | Maher1                                                                                                                                      |
| send_to_voicemail           | 0                                                                                                                                           |
| pinned                      | 0                                                                                                                                           |
| display_name                | Maher1                                                                                                                                      |
| metadata_dirty              | 0                                                                                                                                           |
| phonebook_label_alt         | M                                                                                                                                           |
| phonebook_bucket            | 13                                                                                                                                          |
| version                     | 7                                                                                                                                           |
| custom_ringtone             | null                                                                                                                                        |
| _id                         | 1376                                                                                                                                        |
| times_contacted             | 0                                                                                                                                           |
| account_type_and_data_set   | com.google                                                                                                                                  |
| sync4                       | null                                                                                                                                        |
| dirty                       | 0                                                                                                                                           |
| sync2                       | Rn48fTVSLi17ImA9XBZXEUUJQwI.""                                                                                                              |
| contact_id                  | 1376                                                                                                                                        |
| raw_contact_is_user_profile | 0                                                                                                                                           |
| aggregation_mode            | 0                                                                                                                                           |
| data_set                    | null                                                                                                                                        |
| phonebook_label             | M                                                                                                                                           |
| account_type                | com.google                                                                                                                                  |
| sync3                       | 2017-08-31T12:44:27.075Z                                                                                                                    |
| display_name_alt            | Maher1                                                                                                                                      |
| phonetic_name               | null                                                                                                                                        |
| last_time_contacted         | null                                                                                                                                        |
| display_name_source         | 40                                                                                                                                          |
| backup_id                   | 3c5a4b238cc57aad                                                                                                                            |
| phonebook_bucket_alt        | 13                                                                                                                                          |
| sort_key_alt                | Maher1                                                                                                                                      |
| starred                     | 0                                                                                                                                           |
| deleted                     | 0                                                                                                                                           |
| account_name                | maher*********@gmail.com                                                                                                                    |
| sourceid                    | 3c5a4b238cc57aad                                                                                                                            |
| sync1                       | https://www.google.com/m8/feeds/contacts/maher*********%40gmail.com/base2_property-android_linksto-gprofiles_highresphotos/3c5a4b238cc57aad |
| phonetic_name_style         | 0                                                                                                                                           |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+

While data that could be retrieved with expected uri (.../contacts/lookup/2883i3c5a4b238cc57aad/1376):

+--------------------------------+-----------------------+
|             column             |         value         |
+--------------------------------+-----------------------+
| sort_key                       | Maher1                |
| photo_uri                      | null                  |
| send_to_voicemail              | 0                     |
| contact_status                 | null                  |
| contact_status_label           | null                  |
| pinned                         | 0                     |
| display_name                   | Maher1                |
| phonebook_label_alt            | M                     |
| phonebook_bucket               | 13                    |
| contact_status_res_package     | null                  |
| in_default_directory           | 1                     |
| photo_id                       | null                  |
| custom_ringtone                | null                  |
| _id                            | 1376                  |
| times_contacted                | 0                     |
| phonebook_label                | M                     |
| lookup                         | 2883i3c5a4b238cc57aad |
| display_name_alt               | Maher1                |
| phonetic_name                  | null                  |
| last_time_contacted            | 0                     |
| contact_last_updated_timestamp | 1504183466085         |
| has_phone_number               | 0                     |
| in_visible_group               | 1                     |
| photo_file_id                  | null                  |
| display_name_source            | 40                    |
| is_user_profile                | 0                     |
| contact_status_ts              | null                  |
| sort_key_alt                   | Maher1                |
| phonebook_bucket_alt           | 13                    |
| contact_presence               | null                  |
| starred                        | 0                     |
| photo_thumb_uri                | null                  |
| contact_status_icon            | null                  |
| contact_chat_capability        | null                  |
| name_raw_contact_id            | 1376                  |
| phonetic_name_style            | 0                     |
+--------------------------------+-----------------------+
1

There are 1 answers

0
Maher Abuthraa On

The best solution I found:

public static Uri getLookupURI(Intent data, ContentResolver resolver) {
    Uri contactData = data.getData();
    if (isRawContactsUri(contactData)) {
        contactData = ContactsContract.RawContacts.getContactLookupUri(resolver, contactData);
    }
    return contactData;
}

public static boolean isRawContactsUri(Uri contactUri) {
    return null != contactUri.getPathSegments() && !contactUri.getPathSegments().isEmpty() && "raw_contacts".equals(contactUri.getPathSegments().get(0));
}

Checks if result has uri with rawContacts then extract LookupUri.