FPII-447:show the contact photo if exists in recipients bar

[APP-Messaging]The contact’s profile cannot be displayed in recipients bar in messaging.

Change-Id: I37facfdf5f5dff15ca2283784f54b0d1f22c5730
diff --git a/src/com/android/ex/chips/DefaultPhotoManager.java b/src/com/android/ex/chips/DefaultPhotoManager.java
index b9c001d..344fe1e 100644
--- a/src/com/android/ex/chips/DefaultPhotoManager.java
+++ b/src/com/android/ex/chips/DefaultPhotoManager.java
@@ -21,6 +21,8 @@
 import android.net.Uri;
 import android.os.AsyncTask;
 import android.provider.ContactsContract;
+import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.RawContacts;
 import android.support.v4.util.LruCache;
 import android.util.Log;
 
@@ -64,7 +66,21 @@
     public void populatePhotoBytesAsync(RecipientEntry entry, PhotoManagerCallback callback) {
         final Uri photoThumbnailUri = entry.getPhotoThumbnailUri();
         if (photoThumbnailUri != null) {
-            final byte[] photoBytes = mPhotoCacheMap.get(photoThumbnailUri);
+            getPhotoAsync(entry, callback, photoThumbnailUri);
+        } else {
+            long contactId = findRawContactIdOfContact(entry.getDestination(), entry.getDisplayName());
+            if ( contactId != -1 && entry.getContactId() != RecipientEntry.INVALID_CONTACT) {
+                Uri contructedUri = Uri.parse("content://com.android.contacts/contacts/"
+                    +String.valueOf(contactId)+"/photo");
+                getPhotoAsync(entry, callback, contructedUri);
+            } else if (callback != null) {
+                callback.onPhotoBytesAsyncLoadFailed();
+            }
+        }
+    }
+
+    private void getPhotoAsync(RecipientEntry entry, PhotoManagerCallback callback, Uri uri) {
+        final byte[] photoBytes = mPhotoCacheMap.get(uri);
             if (photoBytes != null) {
                 entry.setPhotoBytes(photoBytes);
                 if (callback != null) {
@@ -75,11 +91,39 @@
                     Log.d(TAG, "No photo cache for " + entry.getDisplayName()
                             + ". Fetch one asynchronously");
                 }
-                fetchPhotoAsync(entry, photoThumbnailUri, callback);
+                fetchPhotoAsync(entry, uri, callback);
             }
-        } else if (callback != null) {
-            callback.onPhotoBytesAsyncLoadFailed();
+    }
+
+    private long findRawContactIdOfContact(String destination, String displayname) {
+        final Cursor cursor = mContentResolver.query(Data.CONTENT_URI,
+                new String[] { RawContacts.CONTACT_ID, Data.DATA1 },
+                Data.DATA1 + "=? OR "+Data.DATA2+"=?",
+                new String[] { destination, displayname },
+                RawContacts._ID + " asc");
+
+        long contactId1 = -1;
+        long contactId2 = -1;
+        try {
+            while (cursor.moveToNext()) {
+                if (cursor.getString(1).equals(displayname)) {
+                    contactId1 = cursor.getLong(0);
+                    if (contactId2 != -1) break;
+                }
+                if (cursor.getString(1).equals(destination)) {
+                    contactId2 = cursor.getLong(0);
+                    if (contactId1 != -1) break;
+                }
+            }
+
+            if (contactId1 == contactId2 && contactId1 != -1) {
+                return contactId1;
+            }
+        } finally {
+            cursor.close();
         }
+
+        return -1;
     }
 
     private void fetchPhotoAsync(final RecipientEntry entry, final Uri photoThumbnailUri,