6

Background

Not sure how it's called, but when you open the contact-details screen of the contacts app, you see various apps there that you can click to perform actions on (such as calling and sending messages via Viber and WhatsApp) as such:

enter image description here

The problem

I don't know how those actions are called, so I can't find out how to investigate them. I tried searching for each social network, how to use it, but this seems like a lot of effort that might not even work well in the future.

I wish to query those actions, show them, and handle them, for all of the apps that are shown on the native contacts app.

What I've tried

I tried to investigate the intents that are being used, and found that for Viber, this is what can be used for messages:

    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/"+id));
    intent.setPackage("com.viber.voip");

However, I don't know what this "id" is, only that it works because I've tested it with real data. I also tried to actually print all of the contacts database, to find the correct value to use (and the mapping), but I didn't find it.

Also, I can't find how this information should have been found. My guess is that it should probably include a query of the available mimetypes, and check them on the specified contact (probably using contact id).

The question

Given a contact (id or phone number), how can I show and perform the operation as shown on contact-details screen of the contacts app ?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Please check http://stackoverflow.com/questions/26999096/android-how-to-add-custom-field-action-to-contact-detail and http://stackoverflow.com/questions/17066444/android-contacts-custom-field/17164257 – Blehi Mar 16 '16 at 10:30
  • @Blehi This is the opposite of what I'm talking about. I want to create views based on what the contacts data already has: viber, Whatsapp, etc... For example, if you create a contacts app, you should show it there. I don't want to create new ones. – android developer Mar 16 '16 at 12:11
  • Bumped this and put a bounty on it. Would love to see a generic solution. – Yoker Feb 01 '17 at 23:37
  • I am not sure what you are trying to do... Do you want to show the list of contacts and, based on the apps installed, show diferent actions? – Leandro Borges Ferreira Feb 08 '17 at 15:02
  • Leandro. The person in the question wants to recreate the default contacts app by Google. And handle the clicks the exact same way. With the exact same icons. – Yoker Feb 08 '17 at 19:32
  • @Yoker Well the icons are of the app, which has the account, but I wonder how "WhatsApp Call" and "Message" appear, and how can I get the numbers there and what they are supposed to do upon clicking on them. – android developer Feb 09 '17 at 20:28
  • http://stackoverflow.com/questions/35448250/how-to-get-whatsapp-contact-from-android It's all in the data table, I just don't know how to get the individual icons. – Yoker Feb 14 '17 at 18:31
  • 1
    @Yoker If you can find the app that created the data, you can get its icon. But what is the query to get the items I've shown on the screenshot, and their intent? Where do you get the strings "WhatsApp Call", "Message" from? – android developer Feb 14 '17 at 18:36

1 Answers1

0

If you query for all that contact's info from the ContactsContract.Data.CONTENT_URI table, and dump it to log, you'll see raw-contacts in accounts like com.whatsapp or com.viber, that have data rows with mimetypes that begin with vnd.android.cursor.item.

For example a Whatsapp Data row might look like this:

_id: 247
account_type: com.whatsapp
mimetype: vnd.android.cursor.item/vnd.com.whatsapp.profile
display_name: Bob
raw_contact_id: 62
data1: 1123456789@s.whatsapp.net
data2: WhatsApp
data3: Message +1 123-456-789
// other info ...

So when your code sees such Data rows, it should display to the user the app icon of the app com.whatsapp (account_type) with the text Message +1 123-456-789 (data3) and you can also display other info like app name Whatsapp (data2).

When that action is clicked, you need to create an intent like this:

Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, 247);
Intent i = new Intent(Intent.ACTION_VIEW, uri);
i.setType("vnd.android.cursor.item/vnd.com.whatsapp.profile");

The app should have an Activity that registered to that mimetype, that will query the Data.CONTENT_URI table for the 247 row-id, get the profile id from data1 and perform the action requested.

The specific fields (which one is the visible text, etc.) are defined in a ContactsDataKind object in the app, but it's not that easily read by external apps, but in my experience most such apps use the same fields for the same behavior (e.g. data3 is the user displayed action text)

P.S.
To get the resource of an app that is not yours, you can use this:

Drawable icon = getPackageManager().getApplicationIcon( PACKAGE_NAME );

EDIT

Each app that syncs contacts and wish to be presented by Contacts apps with app-specific actions will need to create a contacts.xml file under res/xml/contacts.xml that app needs to be accessible via getPackageManager() (or other means) so the contact presenting app will be able to read it and recognize the app's specific MIMETYPES and field mappings.

For example, Whatsapp contacts.xml looks like this:

<ContactsSource
  xmlns:android="http://schemas.android.com/apk/res/android">
    <ContactsDataKind android:icon="@mipmap/icon" android:mimeType="vnd.android.cursor.item/vnd.com.whatsapp.profile" android:summaryColumn="data2" android:detailColumn="data3" android:detailSocialSummary="true" />
    <ContactsDataKind android:icon="@mipmap/icon" android:mimeType="vnd.android.cursor.item/vnd.com.whatsapp.voip.call" android:summaryColumn="data2" android:detailColumn="data3" />
    <ContactsDataKind android:icon="@mipmap/icon" android:mimeType="vnd.android.cursor.item/vnd.com.whatsapp.video.call" android:summaryColumn="data2" android:detailColumn="data3" />
</ContactsSource>

And Google Duo contacts.xml file is:

<ContactsAccountType
  xmlns:android="http://schemas.android.com/apk/res/android">
    <ContactsDataKind android:icon="@drawable/product_logo_duo_color_48" android:mimeType="vnd.android.cursor.item/com.google.android.apps.tachyon.phone" android:summaryColumn="data4" android:detailColumn="data5" android:detailSocialSummary="true" />
    <ContactsDataKind android:icon="@drawable/duo_audio_icon_vector" android:mimeType="vnd.android.cursor.item/com.google.android.apps.tachyon.phone.audio" android:summaryColumn="data4" android:detailColumn="data5" android:detailSocialSummary="true" />
</ContactsAccountType>

See official docs here: https://developer.android.com/guide/topics/providers/contacts-provider#ContactsFile

However, if you're not planning on supporting EVERY single app in the world, rather just some hand-picked list of apps like Whatsapp / Duo, it would be way easier just to map those fields manually in your app

marmor
  • 27,641
  • 11
  • 107
  • 150
  • @yoker what happens when you run the intent? do you see the WhatsApp app opens? are you getting "no activity found to handle this intent"? – marmor Feb 03 '17 at 17:17
  • How do I know which app to use for the icon? Can't apps put there (in "account_type" ) whatever they want? – android developer Feb 04 '17 at 09:59
  • I haven't seen a case where account_type is not the package name, do you have an example that shows otherwise? – marmor Feb 04 '17 at 11:12
  • The intent stuff code doesn't work. Please run your own code before posting it. Also - the package manager icon IS NOT THE SAME as the icons described in the question. As you can see in the question, the viber icons are all different for all the different actions. – Yoker Feb 04 '17 at 19:08
  • Where does the "WhatsApp call" come from? – android developer Feb 09 '17 at 20:38
  • I've shown as an example a data row of mimetype `vnd.android.cursor.item/vnd.com.whatsapp.profile`, there's another row for the `Whatsapp Call` item, there may be multiple such data rows for each app – marmor Feb 10 '17 at 09:04
  • @marmor For Google Duo, `DATA5` is the user displayed action text. How do we identify such text columns? – Karthik Jul 14 '20 at 05:06
  • @androiddeveloper I'm having the same issue. Have you figured out a way for this? – Karthik Jul 14 '20 at 05:07
  • @Karthik No idea. But it has to be there somewhere.... – android developer Jul 14 '20 at 08:45
  • @Karthik please create a different question on SO so i can answer this – marmor Jul 14 '20 at 08:52
  • @marmor Why not answer here? This way I could also set it as the right answer... Please show how the Contacts app fetches this information that I've shown on the screenshot. – android developer Jul 14 '20 at 10:22
  • how to get only phone number, not whats app and duo – Tippu Fisal Sheriff Oct 16 '22 at 06:14