Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Android contacts with type-to-filter functionality, restricted to a specific account

I'm trying to:

  • Display a list of contacts
  • Let the user search through them by typing a query
  • Limit search results only to a specific Google/Gmail account.

This is how I build the URI for the cursor:

// User is searching for 'jo'
String query = "jo";
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(query));

// Restrict the query to contacts from '[email protected]'
Uri.Builder builder = uri.buildUpon();
builder.appendQueryParameter(
    ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.DEFAULT));
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, "[email protected]");
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");

uri = builder.build();

This is the final URI:

content://com.android.contacts/contacts/filter/jo?directory=0&account_name=example%40gmail.com&account_type=com.google

Currently, this shows search results from all accounts on the phone.


NOTE: If I use Contacts.CONTENT_URI instead of Contacts.CONTENT_FILTER_URI, then specifying the directory/account works as expected, but I can no longer use 'type-to-filter' style search.

The documentation does state:

The most important use case for Directories is search. A Directory provider is expected to support at least Contacts.CONTENT_FILTER_URI.

Could anyone help point out what I might be doing wrong?

like image 499
Gautam Avatar asked Jun 11 '15 14:06

Gautam


1 Answers

I added your code in Google's example for contact retrieving, and with a couple of changes it worked perfectly with my Google for Work account.

The changes I made were:

  • remove the line with DIRECTORY_PARAM_KEY, as I didn't find it to make any difference
  • removed ContactsQuery.SELECTION from the return statement, because that constant prevents "invisible" contacts from being displayed.

The changes were made to ContactsListFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // If this is the loader for finding contacts in the Contacts Provider
    // (the only one supported)
    if (id == ContactsQuery.QUERY_ID) {
        Uri contentUri;

        // There are two types of searches, one which displays all contacts and
        // one which filters contacts by a search query. If mSearchTerm is set
        // then a search query has been entered and the latter should be used.

        if (mSearchTerm == null) {
            // Since there's no search string, use the content URI that searches the entire
            // Contacts table
            contentUri = ContactsQuery.CONTENT_URI;
        } else {
            // Since there's a search string, use the special content Uri that searches the
            // Contacts table. The URI consists of a base Uri and the search string.
            contentUri = Uri.withAppendedPath(ContactsQuery.FILTER_URI, Uri.encode(mSearchTerm));
        }

        // HERE COMES YOUR CODE (except the DIRECTORY_PARAM_KEY line)
        Uri.Builder builder = contentUri.buildUpon();
        builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, "[email protected]");
        builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
        contentUri = builder.build();

        // Returns a new CursorLoader for querying the Contacts table. No arguments are used
        // for the selection clause. The search string is either encoded onto the content URI,
        // or no contacts search string is used. The other search criteria are constants. See
        // the ContactsQuery interface.

        return new CursorLoader(getActivity(),
                contentUri,
                ContactsQuery.PROJECTION,
                null, // I REMOVED SELECTION HERE
                null,
                ContactsQuery.SORT_ORDER);
    }

    Log.e(TAG, "onCreateLoader - incorrect ID provided (" + id + ")");
    return null;
}
like image 119
Leo supports Monica Cellio Avatar answered Oct 21 '22 06:10

Leo supports Monica Cellio