title: Android contacts: How does the lookup key works?
body: <p>On top of contacts <strong>id</strong>, Android also got <a href="https://developer.android.com/reference/android/provider/ContactsContract.ContactsColumns.html#LOOKUP_KEY" rel="noreferrer">LOOK_UP</a> key. Since <strong>id</strong> of contact can change, you can obtain user uri, using <strong>LOOK_UP</strong> key.</p>
  
  <pre><code>public static Uri lookupContactUri(String lookup, Context context){
      ContentResolver contentResolver = context.getContentResolver();
      Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup);
      return ContactsContract.Contacts.lookupContact(contentResolver, lookupUri);
  }
  </code></pre>
  
  <p>But how does it work? The source code of the <code>Contacts.lookupContact</code> doesn't tell much about the actual implementation. So can anyone explain how does they manage to pull this up?</p>
  
  <pre><code>    /**
       * Computes a content URI (see {@link #CONTENT_URI}) given a lookup URI.
       * &lt;p&gt;
       * Returns null if the contact cannot be found.
       */
      public static Uri lookupContact(ContentResolver resolver, Uri lookupUri) {
          if (lookupUri == null) {
              return null;
          }
  
          Cursor c = resolver.query(lookupUri, new String[]{Contacts._ID}, null, null, null);
          if (c == null) {
              return null;
          }
  
          try {
              if (c.moveToFirst()) {
                  long contactId = c.getLong(0);
                  return ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
              }
          } finally {
              c.close();
          }
          return null;
      }
  </code></pre>
  
  <p>Another thing I tested, is merging two contacts using <a href="https://developer.android.com/reference/android/provider/ContactsContract.AggregationExceptions.html" rel="noreferrer">ContactsContract.AggregationExceptions</a> and then quarrying for contact uri. Both of the <strong>LOOK_UP</strong> keys yield with the same contact uri as expected.</p>
  
  <p>So how are they doing it?</p>