Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f35bce45 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Adding a column to Contacts, which will be used as an additional "soft" identity.

The main consumer of this feature is shortcuts.

The LOOKUP KEY column will contain an encoded concatenation of the contact's row id
as well as sync_ids of all constituent raw contacts. It goes with the "contacts/lookup/*/#" URI.

When we get such a URI, we will first try to load the
contact with the specified _id and lookup_key.  If we cannot find the contact
that way, we will go scout for the contact that contains most of the sync_ids
we found in the lookup key.

We will need to make sure that the contact picker returns the lookup-style URIs.
parent 66360af8
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.accounts.Account;
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
@@ -215,6 +216,12 @@ public final class ContactsContract {
         * <P>Type: INTEGER</P>
         */
        public static final String HAS_PHONE_NUMBER = "has_phone_number";

        /**
         * An opaque value that contains hints on how to find the contact if
         * its row id changed as a result of a sync or aggregation.
         */
        public static final String LOOKUP_KEY = "lookup";
    }

    /**
@@ -233,6 +240,54 @@ public final class ContactsContract {
         */
        public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "contacts");

        /**
         * A content:// style URI for this table that should be used to create
         * shortcuts or otherwise create long-term links to contacts. This URI
         * should always be followed by a "/" and the contact's {@link #LOOKUP_KEY}.
         * It can optionally also have a "/" and last known contact ID appended after
         * that. This "complete" format is an important optimization and is highly recommended.
         * <p>
         * As long as the contact's row ID remains the same, this URI is
         * equivalent to {@link #CONTENT_URI}. If the contact's row ID changes
         * as a result of a sync or aggregation, this URI will look up the
         * contact using indirect information (sync IDs or constituent raw
         * contacts).
         * <p>
         * Lookup key should be appended unencoded - it is stored in the encoded
         * form, ready for use in a URI.
         */
        public static final Uri CONTENT_LOOKUP_URI = Uri.withAppendedPath(CONTENT_URI,
                "lookup");

        /**
         * Computes a complete lookup URI (see {@link #CONTENT_LOOKUP_URI}).
         * Pass either a basic content URI with a contact ID to obtain an
         * equivalent lookup URI. Pass a possibly stale lookup URI to get a fresh
         * lookup URI for the same contact.
         * <p>
         * Returns null if the contact cannot be found.
         */
        public static Uri getLookupUri(ContentResolver resolver, Uri contentUri) {
            Cursor c = resolver.query(contentUri,
                    new String[]{Contacts.LOOKUP_KEY, Contacts._ID}, null, null, null);
            if (c == null) {
                return null;
            }

            try {
                if (c.moveToFirst()) {
                    String lookupKey = c.getString(0);
                    long contactId = c.getLong(1);
                    return ContentUris.withAppendedId(
                            Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey),
                            contactId);
                }
            } finally {
                c.close();
            }
            return null;
        }

        /**
         * The content:// style URI for this table joined with useful data from
         * {@link Data}.