Use photo id when available.

Photo id is a long that uniquely identifies a photo associated with a
contact. It only handles low-res photos, but that's what's used in the
call log anyway.

This commit uses photo ids instead of thumbnail URIs if the photo id is
available, since that's more efficient for storage purposes. In fact, if
a photo id is available it does not even bother storing the URI.

This will make a significant difference in terms of storage when I will
use the call log table to store cached contact information, including
the photo id and thumbnail URI.

Bug: 5101753
Change-Id: Ia60e3ce33763fe3744208763befbaf4489d60bdd
diff --git a/src/com/android/contacts/PhoneCallDetails.java b/src/com/android/contacts/PhoneCallDetails.java
index 78ac9b3..547695c 100644
--- a/src/com/android/contacts/PhoneCallDetails.java
+++ b/src/com/android/contacts/PhoneCallDetails.java
@@ -53,6 +53,8 @@
     /**
      * The photo URI of the picture of the contact that is associated with this phone call or
      * null if there is none.
+     * <p>
+     * This is meant to store the high-res photo only.
      */
     public final Uri photoUri;
 
diff --git a/src/com/android/contacts/calllog/CallLogAdapter.java b/src/com/android/contacts/calllog/CallLogAdapter.java
index bed721a..7cd73a5 100644
--- a/src/com/android/contacts/calllog/CallLogAdapter.java
+++ b/src/com/android/contacts/calllog/CallLogAdapter.java
@@ -329,11 +329,8 @@
                 info.number = dataTableCursor.getString(
                         dataTableCursor.getColumnIndex(Data.DATA1));
                 info.normalizedNumber = null;  // meaningless for SIP addresses
-                final String thumbnailUriString = dataTableCursor.getString(
-                        dataTableCursor.getColumnIndex(Data.PHOTO_THUMBNAIL_URI));
-                info.thumbnailUri = thumbnailUriString == null
-                        ? null
-                        : Uri.parse(thumbnailUriString);
+                info.photoId = dataTableCursor.getLong(
+                        dataTableCursor.getColumnIndex(Data.PHOTO_ID));
             } else {
                 info = ContactInfo.EMPTY;
             }
@@ -377,11 +374,7 @@
                         .getString(PhoneQuery.MATCHED_NUMBER);
                 info.normalizedNumber = phonesCursor
                         .getString(PhoneQuery.NORMALIZED_NUMBER);
-                final String thumbnailUriString = phonesCursor.getString(
-                        PhoneQuery.THUMBNAIL_URI);
-                info.thumbnailUri = thumbnailUriString == null
-                        ? null
-                        : Uri.parse(thumbnailUriString);
+                info.photoId = phonesCursor.getLong(PhoneQuery.PHOTO_ID);
             } else {
                 info = ContactInfo.EMPTY;
             }
@@ -635,7 +628,7 @@
         final String name = info.name;
         final int ntype = info.type;
         final String label = info.label;
-        final Uri thumbnailUri = info.thumbnailUri;
+        final long photoId = info.photoId;
         final int[] callTypes = getCallTypes(c, count);
         final String geocode = c.getString(CallLogQuery.GEOCODED_LOCATION);
         final PhoneCallDetails details;
@@ -643,15 +636,16 @@
             details = new PhoneCallDetails(number, formattedNumber, countryIso, geocode,
                     callTypes, date, duration);
         } else {
+            // We do not pass a photo id since we do not need the high-res picture.
             details = new PhoneCallDetails(number, formattedNumber, countryIso, geocode,
-                    callTypes, date, duration, name, ntype, label, contactUri , thumbnailUri);
+                    callTypes, date, duration, name, ntype, label, contactUri, null);
         }
 
         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, contactUri);
+        setPhoto(views, photoId, contactUri);
 
         // Listen for the first draw
         if (mPreDrawListener == null) {
@@ -718,7 +712,7 @@
         info.number = c.getString(CallLogQuery.NUMBER);
         info.formattedNumber = info.number;
         info.normalizedNumber = info.number;
-        info.thumbnailUri = null;
+        info.photoId = 0;
         return info;
     }
 
@@ -740,9 +734,9 @@
         return callTypes;
     }
 
-    private void setPhoto(CallLogListItemViews views, Uri thumbnailUri, Uri contactUri) {
+    private void setPhoto(CallLogListItemViews views, long photoId, Uri contactUri) {
         views.quickContactView.assignContactUri(contactUri);
-        mContactPhotoManager.loadPhoto(views.quickContactView, thumbnailUri);
+        mContactPhotoManager.loadPhoto(views.quickContactView, photoId);
     }
 
     /**
diff --git a/src/com/android/contacts/calllog/ContactInfo.java b/src/com/android/contacts/calllog/ContactInfo.java
index 58c5f6a..c28018c 100644
--- a/src/com/android/contacts/calllog/ContactInfo.java
+++ b/src/com/android/contacts/calllog/ContactInfo.java
@@ -32,7 +32,8 @@
     public String number;
     public String formattedNumber;
     public String normalizedNumber;
-    public Uri thumbnailUri;
+    /** The photo for the contact, if available. */
+    public long photoId;
 
     public static ContactInfo EMPTY = new ContactInfo();
 
@@ -61,7 +62,7 @@
         if (!TextUtils.equals(number, other.number)) return false;
         // Ignore formatted number.
         if (!TextUtils.equals(normalizedNumber, other.normalizedNumber)) return false;
-        if (!UriUtils.areEqual(thumbnailUri, other.thumbnailUri)) return false;
+        if (photoId != other.photoId) return false;
         return true;
     }
 }
\ No newline at end of file
diff --git a/src/com/android/contacts/calllog/PhoneQuery.java b/src/com/android/contacts/calllog/PhoneQuery.java
index 52faa8b..a53e5c8 100644
--- a/src/com/android/contacts/calllog/PhoneQuery.java
+++ b/src/com/android/contacts/calllog/PhoneQuery.java
@@ -29,7 +29,7 @@
             PhoneLookup.LABEL,
             PhoneLookup.NUMBER,
             PhoneLookup.NORMALIZED_NUMBER,
-            PhoneLookup.PHOTO_THUMBNAIL_URI,
+            PhoneLookup.PHOTO_ID,
             PhoneLookup.LOOKUP_KEY};
 
     public static final int PERSON_ID = 0;
@@ -38,6 +38,6 @@
     public static final int LABEL = 3;
     public static final int MATCHED_NUMBER = 4;
     public static final int NORMALIZED_NUMBER = 5;
-    public static final int THUMBNAIL_URI = 6;
+    public static final int PHOTO_ID = 6;
     public static final int LOOKUP_KEY = 7;
 }
\ No newline at end of file
diff --git a/src/com/android/contacts/util/UriUtils.java b/src/com/android/contacts/util/UriUtils.java
index 28874f2..7ef8786 100644
--- a/src/com/android/contacts/util/UriUtils.java
+++ b/src/com/android/contacts/util/UriUtils.java
@@ -35,4 +35,12 @@
         }
         return uri1.equals(uri2);
     }
+
+    /** Parses a string into a URI and returns null if the given string is null. */
+    public static Uri parseUriOrNull(String uriString) {
+        if (uriString == null) {
+            return null;
+        }
+        return Uri.parse(uriString);
+    }
 }