2

You know when an unknown number calls you and instead of the number you see the name of the company which is actually calling you. As far as I know Android gets this information through some google services.

What should I do if I want to make an app, that does the same thing? If an unknown number calls you, I want Android to ask my app to provide info for this number.

My search found this in the documentation. Here is said:

The most important use case for Directories is search. A Directory provider is expected to support at least Contacts.CONTENT_FILTER_URI. If a Directory provider wants to participate in email and phone lookup functionalities, it should also implement CommonDataKinds.Email.CONTENT_FILTER_URI and CommonDataKinds.Phone.CONTENT_FILTER_URI.

As far as I understand, I need to implement a ContactsContract.Directory. I tried something but I can't get it to work.

Can you tell me what should I do, can you refer me to a nice explanation of how contact info providing works? A nice example for this Directory would be nice too.

EDIT: What I tried - I created a sample app, created my own provider and registered it in the manifest. I added the required meta data, as shown in the documentation:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.sample.contactprovidertest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <provider
            android:authorities="com.sample.contactprovidertest"
            android:name=".MyProvider"
            android:enabled="true"
            android:exported="true">
            <meta-data
                android:name="android.content.ContactDirectory"
                android:value="true"/>
        </provider>
    </application>

</manifest>

MyProvider.java

public class MyProvider extends ContentProvider {

    private static final String TAG = "MyProvider";

    @Override
    public boolean onCreate() {

        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri,
                        String[] projection,
                        String selection,
                        String[] selectionArgs,
                        String sortOrder) {
        // this is never called
        Toast.makeText(getContext(), "it works!", Toast.LENGTH_SHORT).show();
        if (ContactsContract.Directory.CONTENT_URI.equals(uri)) {
            Log.d(TAG, "query: " + uri);
        }

        return null;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}
dephinera
  • 3,703
  • 11
  • 41
  • 75
  • 1
    `I tried something but I can't get it to work.` - Can you post what you've tried? Maybe we can find the problem with it. – Michael Dodd Jan 25 '17 at 10:57
  • @MichaelDodd I added my sample. Even if we find the problem, an explanation about how this thing works would be good. Thank you for your reply. – dephinera Jan 25 '17 at 12:21

1 Answers1

0

From the docs you've linked to:

The client interacts with a directory via Contacts Provider by supplying an optional directory= query parameter.

When the Contacts Provider receives the request, it transforms the URI and forwards the request to the corresponding directory content provider

When the incoming-call app launches as a response to an incoming call, it queries the Contacts Provider for local contacts, and also might provide the directory= param for Google's Global Address List as a fallback search if not found locally.

I don't think there's a way to ask the incoming call app to ask for your directory as well.

WHAT YOU CAN DO:

What all caller-id apps do, show a popup dialog on top of the incoming call screen. See How to create always-top fullscreen overlay activity in Android

and read online about LayoutParams.TYPE_SYSTEM_ALERT

Community
  • 1
  • 1
marmor
  • 27,641
  • 11
  • 107
  • 150
  • In my phone app (on Nougat) I see options for "forward search", "people search" and "reverse lookup" providers; when selected, I get to pick providers from a set list. So it looks like it is possible to add more directories, I just have no idea how the existing ones got there. – retorquere Jun 10 '17 at 10:53