Use Contact URIs instead of contact ids.

Contact ids are not guaranteed to be stable.

Instead use a contact URI (which contains both a lookup key and a
contact id, used only as a hint).

This change is needed for caching, since we do not what to cache the
unstable contact ids.

Bug: 5101753
Change-Id: I34814c8935e706ee898adcedc39c4baeea147d67
diff --git a/src/com/android/contacts/CallDetailActivity.java b/src/com/android/contacts/CallDetailActivity.java
index 4941121..d7881a2 100644
--- a/src/com/android/contacts/CallDetailActivity.java
+++ b/src/com/android/contacts/CallDetailActivity.java
@@ -139,6 +139,7 @@
         PhoneLookup.NUMBER,
         PhoneLookup.NORMALIZED_NUMBER,
         PhoneLookup.PHOTO_URI,
+        PhoneLookup.LOOKUP_KEY,
     };
     static final int COLUMN_INDEX_ID = 0;
     static final int COLUMN_INDEX_NAME = 1;
@@ -147,6 +148,7 @@
     static final int COLUMN_INDEX_NUMBER = 4;
     static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
     static final int COLUMN_INDEX_PHOTO_URI = 6;
+    static final int COLUMN_INDEX_LOOKUP_KEY = 7;
 
     private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
         @Override
@@ -328,7 +330,7 @@
                 // first.
                 PhoneCallDetails firstDetails = details[0];
                 mNumber = firstDetails.number.toString();
-                final long personId = firstDetails.personId;
+                final Uri contactUri = firstDetails.contactUri;
                 final Uri photoUri = firstDetails.photoUri;
 
                 // Set the details header, based on the first phone call.
@@ -353,9 +355,8 @@
                     nameOrNumber = firstDetails.number;
                 }
 
-                if (firstDetails.personId != -1) {
-                    Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
-                    mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
+                if (contactUri != null) {
+                    mainActionIntent = new Intent(Intent.ACTION_VIEW, contactUri);
                     mainActionIcon = R.drawable.ic_contacts_holo_dark;
                     mainActionDescription =
                             getString(R.string.description_view_contact, nameOrNumber);
@@ -498,8 +499,8 @@
             CharSequence nameText = "";
             int numberType = 0;
             CharSequence numberLabel = "";
-            long personId = -1L;
             Uri photoUri = null;
+            Uri contactUri = null;
             // If this is not a regular number, there is no point in looking it up in the contacts.
             if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
                 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
@@ -511,7 +512,6 @@
                 String candidateNumberText = number;
                 try {
                     if (phonesCursor != null && phonesCursor.moveToFirst()) {
-                        personId = phonesCursor.getLong(COLUMN_INDEX_ID);
                         nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
                         String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
                         photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
@@ -521,6 +521,11 @@
                                 countryIso);
                         numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
                         numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
+                        long personId = phonesCursor.getLong(COLUMN_INDEX_ID);
+                        if (personId > 0) {
+                            contactUri = Contacts.getLookupUri(personId,
+                                    phonesCursor.getString(COLUMN_INDEX_LOOKUP_KEY));
+                        }
                     } else {
                         // We could not find this contact in the contacts, just format the phone
                         // number as best as we can. All the other fields will have their default
@@ -535,7 +540,7 @@
             }
             return new PhoneCallDetails(number, numberText, countryIso, geocode,
                     new int[]{ callType }, date, duration,
-                    nameText, numberType, numberLabel, personId, photoUri);
+                    nameText, numberType, numberLabel, contactUri, photoUri);
         } finally {
             if (callCursor != null) {
                 callCursor.close();
diff --git a/src/com/android/contacts/PhoneCallDetails.java b/src/com/android/contacts/PhoneCallDetails.java
index 5718091..78ac9b3 100644
--- a/src/com/android/contacts/PhoneCallDetails.java
+++ b/src/com/android/contacts/PhoneCallDetails.java
@@ -48,10 +48,10 @@
     public final int numberType;
     /** The custom label associated with the phone number in the contact, or the empty string. */
     public final CharSequence numberLabel;
-    /** The id of the contact associated with this phone call. */
-    public final long personId;
+    /** The URI of the contact associated with this phone call. */
+    public final Uri contactUri;
     /**
-     * The photo uri of the picture of the contact that is associated with this phone call or
+     * The photo URI of the picture of the contact that is associated with this phone call or
      * null if there is none.
      */
     public final Uri photoUri;
@@ -60,13 +60,13 @@
     public PhoneCallDetails(CharSequence number, CharSequence formattedNumber,
             String countryIso, String geocode, int[] callTypes, long date, long duration) {
         this(number, formattedNumber, countryIso, geocode, callTypes, date, duration, "", 0, "",
-                -1L, null);
+                null, null);
     }
 
     /** Create the details for a call with a number associated with a contact. */
     public PhoneCallDetails(CharSequence number, CharSequence formattedNumber,
             String countryIso, String geocode, int[] callTypes, long date, long duration,
-            CharSequence name, int numberType, CharSequence numberLabel, long personId,
+            CharSequence name, int numberType, CharSequence numberLabel, Uri contactUri,
             Uri photoUri) {
         this.number = number;
         this.formattedNumber = formattedNumber;
@@ -78,7 +78,7 @@
         this.name = name;
         this.numberType = numberType;
         this.numberLabel = numberLabel;
-        this.personId = personId;
+        this.contactUri = contactUri;
         this.photoUri = photoUri;
     }
 }
diff --git a/src/com/android/contacts/calllog/CallLogAdapter.java b/src/com/android/contacts/calllog/CallLogAdapter.java
index 28e2e90..bed721a 100644
--- a/src/com/android/contacts/calllog/CallLogAdapter.java
+++ b/src/com/android/contacts/calllog/CallLogAdapter.java
@@ -313,8 +313,11 @@
                 // Note the Data.CONTACT_ID column here is
                 // equivalent to the PERSON_ID_COLUMN_INDEX column
                 // we use with "phonesCursor" below.
-                info.personId = dataTableCursor.getLong(
+                long contactId = dataTableCursor.getLong(
                         dataTableCursor.getColumnIndex(Data.CONTACT_ID));
+                String lookupKey = dataTableCursor.getString(
+                        dataTableCursor.getColumnIndex(Data.LOOKUP_KEY));
+                info.contactUri = Contacts.getLookupUri(contactId, lookupKey);
                 info.name = dataTableCursor.getString(
                         dataTableCursor.getColumnIndex(Data.DISPLAY_NAME));
                 // "type" and "label" are currently unused for SIP addresses
@@ -331,8 +334,6 @@
                 info.thumbnailUri = thumbnailUriString == null
                         ? null
                         : Uri.parse(thumbnailUriString);
-                info.lookupKey = dataTableCursor.getString(
-                        dataTableCursor.getColumnIndex(Data.LOOKUP_KEY));
             } else {
                 info = ContactInfo.EMPTY;
             }
@@ -366,7 +367,9 @@
         if (phonesCursor != null) {
             if (phonesCursor.moveToFirst()) {
                 info = new ContactInfo();
-                info.personId = phonesCursor.getLong(PhoneQuery.PERSON_ID);
+                long contactId = phonesCursor.getLong(PhoneQuery.PERSON_ID);
+                String lookupKey = phonesCursor.getString(PhoneQuery.LOOKUP_KEY);
+                info.contactUri = Contacts.getLookupUri(contactId, lookupKey);
                 info.name = phonesCursor.getString(PhoneQuery.NAME);
                 info.type = phonesCursor.getInt(PhoneQuery.PHONE_TYPE);
                 info.label = phonesCursor.getString(PhoneQuery.LABEL);
@@ -379,7 +382,6 @@
                 info.thumbnailUri = thumbnailUriString == null
                         ? null
                         : Uri.parse(thumbnailUriString);
-                info.lookupKey = phonesCursor.getString(PhoneQuery.LOOKUP_KEY);
             } else {
                 info = ContactInfo.EMPTY;
             }
@@ -629,12 +631,11 @@
             info = cachedContactInfo;
         }
 
-        final long personId = info.personId;
+        final Uri contactUri = info.contactUri;
         final String name = info.name;
         final int ntype = info.type;
         final String label = info.label;
         final Uri thumbnailUri = info.thumbnailUri;
-        final String lookupKey = info.lookupKey;
         final int[] callTypes = getCallTypes(c, count);
         final String geocode = c.getString(CallLogQuery.GEOCODED_LOCATION);
         final PhoneCallDetails details;
@@ -643,14 +644,14 @@
                     callTypes, date, duration);
         } else {
             details = new PhoneCallDetails(number, formattedNumber, countryIso, geocode,
-                    callTypes, date, duration, name, ntype, label, personId, thumbnailUri);
+                    callTypes, date, duration, name, ntype, label, contactUri , thumbnailUri);
         }
 
         final boolean isNew = CallLogQuery.isNewSection(c);
         // New items also use the highlighted version of the text.
         final boolean isHighlighted = isNew;
         mCallLogViewsHelper.setPhoneCallDetails(views, details, isHighlighted);
-        setPhoto(views, thumbnailUri, personId, lookupKey);
+        setPhoto(views, thumbnailUri, contactUri);
 
         // Listen for the first draw
         if (mPreDrawListener == null) {
@@ -709,7 +710,7 @@
     /** Returns the contact information as stored in the call log. */
     private ContactInfo getContactInfoFromCallLog(Cursor c) {
         ContactInfo info = new ContactInfo();
-        info.personId = -1;
+        info.contactUri = null;
         info.name = c.getString(CallLogQuery.CACHED_NAME);
         info.type = c.getInt(CallLogQuery.CACHED_NUMBER_TYPE);
         info.label = c.getString(CallLogQuery.CACHED_NUMBER_LABEL);
@@ -718,7 +719,6 @@
         info.formattedNumber = info.number;
         info.normalizedNumber = info.number;
         info.thumbnailUri = null;
-        info.lookupKey = null;
         return info;
     }
 
@@ -740,10 +740,8 @@
         return callTypes;
     }
 
-    private void setPhoto(CallLogListItemViews views, Uri thumbnailUri, long contactId,
-            String lookupKey) {
-        views.quickContactView.assignContactUri(contactId == -1 ? null :
-                Contacts.getLookupUri(contactId, lookupKey));
+    private void setPhoto(CallLogListItemViews views, Uri thumbnailUri, Uri contactUri) {
+        views.quickContactView.assignContactUri(contactUri);
         mContactPhotoManager.loadPhoto(views.quickContactView, thumbnailUri);
     }
 
diff --git a/src/com/android/contacts/calllog/CallLogFragment.java b/src/com/android/contacts/calllog/CallLogFragment.java
index 215fd7b..83f44d0 100644
--- a/src/com/android/contacts/calllog/CallLogFragment.java
+++ b/src/com/android/contacts/calllog/CallLogFragment.java
@@ -18,7 +18,6 @@
 
 import com.android.contacts.ContactsUtils;
 import com.android.contacts.R;
-import com.android.contacts.activities.DialtactsActivity;
 import com.android.contacts.activities.DialtactsActivity.ViewPagerVisibilityListener;
 import com.android.contacts.test.NeededForTesting;
 import com.android.contacts.voicemail.VoicemailStatusHelper;
diff --git a/src/com/android/contacts/calllog/ContactInfo.java b/src/com/android/contacts/calllog/ContactInfo.java
index 1f106e4..58c5f6a 100644
--- a/src/com/android/contacts/calllog/ContactInfo.java
+++ b/src/com/android/contacts/calllog/ContactInfo.java
@@ -16,6 +16,8 @@
 
 package com.android.contacts.calllog;
 
+import com.android.contacts.util.UriUtils;
+
 import android.net.Uri;
 import android.text.TextUtils;
 
@@ -23,7 +25,7 @@
  * Information for a contact as needed by the Call Log.
  */
 public final class ContactInfo {
-    public long personId = -1;
+    public Uri contactUri;
     public String name;
     public int type;
     public String label;
@@ -31,18 +33,17 @@
     public String formattedNumber;
     public String normalizedNumber;
     public Uri thumbnailUri;
-    public String lookupKey;
 
     public static ContactInfo EMPTY = new ContactInfo();
 
     @Override
     public int hashCode() {
-        // Uses only name and personId to determine hashcode.
+        // Uses only name and contactUri to determine hashcode.
         // This should be sufficient to have a reasonable distribution of hash codes.
-        // Moreover, there should be no two people with the same personId.
+        // Moreover, there should be no two people with the same contactUri.
         final int prime = 31;
         int result = 1;
-        result = prime * result + (int) (personId ^ (personId >>> 32));
+        result = prime * result + ((contactUri == null) ? 0 : contactUri.hashCode());
         result = prime * result + ((name == null) ? 0 : name.hashCode());
         return result;
     }
@@ -53,21 +54,14 @@
         if (obj == null) return false;
         if (getClass() != obj.getClass()) return false;
         ContactInfo other = (ContactInfo) obj;
-        if (personId != other.personId) return false;
+        if (!UriUtils.areEqual(contactUri, other.contactUri)) return false;
         if (!TextUtils.equals(name, other.name)) return false;
         if (type != other.type) return false;
         if (!TextUtils.equals(label, other.label)) return false;
         if (!TextUtils.equals(number, other.number)) return false;
         // Ignore formatted number.
         if (!TextUtils.equals(normalizedNumber, other.normalizedNumber)) return false;
-        if (!uriEquals(thumbnailUri, other.thumbnailUri)) return false;
-        if (!TextUtils.equals(lookupKey, other.lookupKey)) return false;
+        if (!UriUtils.areEqual(thumbnailUri, other.thumbnailUri)) return false;
         return true;
     }
-
-    private static boolean uriEquals(Uri thumbnailUri1, Uri thumbnailUri2) {
-        if (thumbnailUri1 == thumbnailUri2) return true;
-        if (thumbnailUri1 == null) return false;
-        return thumbnailUri1.equals(thumbnailUri2);
-    }
 }
\ No newline at end of file
diff --git a/src/com/android/contacts/util/UriUtils.java b/src/com/android/contacts/util/UriUtils.java
new file mode 100644
index 0000000..28874f2
--- /dev/null
+++ b/src/com/android/contacts/util/UriUtils.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.util;
+
+import android.net.Uri;
+
+/**
+ * Utility methods for dealing with URIs.
+ */
+public class UriUtils {
+    /** Static helper, not instantiable. */
+    private UriUtils() {}
+
+    /** Checks whether two URI are equal, taking care of the case where either is null. */
+    public static boolean areEqual(Uri uri1, Uri uri2) {
+        if (uri1 == null && uri2 == null) {
+            return true;
+        }
+        if (uri1 == null || uri2 == null) {
+            return false;
+        }
+        return uri1.equals(uri2);
+    }
+}