Not using the PhoneNumberService API for reverse lookups anymore

Change-Id: I75c2a4f5241a5bef5fd78610758629f7543bc4da
diff --git a/src/com/android/incallui/ContactInfoCache.java b/src/com/android/incallui/ContactInfoCache.java
index 448de7f..55c774a 100644
--- a/src/com/android/incallui/ContactInfoCache.java
+++ b/src/com/android/incallui/ContactInfoCache.java
@@ -28,6 +28,8 @@
 import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
 
+import com.android.dialer.calllog.ContactInfo;
+import com.android.dialer.lookup.ReverseLookupThread;
 import com.android.incallui.service.PhoneNumberService;
 import com.android.incalluibind.ServiceFactory;
 import com.android.services.telephony.common.Call;
@@ -169,12 +171,16 @@
         sendInfoNotifications(callId, cacheEntry);
 
         if (didLocalLookup) {
-            if (!callerInfo.contactExists && cacheEntry.name == null &&
-                    mPhoneNumberService != null) {
+            if (!callerInfo.contactExists && cacheEntry.name == null) {
                 Log.d(TAG, "Contact lookup. Local contacts miss, checking remote");
-                final PhoneNumberServiceListener listener = new PhoneNumberServiceListener(callId);
-                mPhoneNumberService.getPhoneNumberInfo(cacheEntry.number, listener, listener,
-                        isIncoming);
+                if (mPhoneNumberService != null) {
+                    final PhoneNumberServiceListener listener = new PhoneNumberServiceListener(callId);
+                    mPhoneNumberService.getPhoneNumberInfo(cacheEntry.number, listener, listener,
+                            isIncoming);
+                } else {
+                    final ReverseLookupListener listener = new ReverseLookupListener(callId);
+                    ReverseLookupThread.performLookup(mContext, cacheEntry.number, listener);
+                }
             } else if (cacheEntry.personUri != null) {
                 Log.d(TAG, "Contact lookup. Local contact found, starting image load");
                 // Load the image with a callback to update the image state.
@@ -195,6 +201,57 @@
         }
     }
 
+    public class ReverseLookupListener {
+        private final int mCallId;
+
+        ReverseLookupListener(int callId) {
+            mCallId = callId;
+        }
+
+        public void onLookupComplete(final ContactInfo info) {
+            if (info == null) {
+                Log.d(TAG, "Reverse lookup returned no result.");
+                clearCallbacks(mCallId);
+                return;
+            }
+
+            ContactCacheEntry entry = new ContactCacheEntry();
+            entry.name = info.name;
+            entry.number = info.number;
+            if (info.type == Phone.TYPE_CUSTOM) {
+                entry.label = info.label;
+            } else {
+                final CharSequence typeStr = Phone.getTypeLabel(
+                        mContext.getResources(), info.type, info.label);
+                entry.label = typeStr == null ? null : typeStr.toString();
+            }
+
+            final ContactCacheEntry oldEntry = mInfoMap.get(mCallId);
+            if (oldEntry != null) {
+                // Location is only obtained from local lookup so persist
+                // the value for remote lookups. Once we have a name this
+                // field is no longer used; it is persisted here in case
+                // the UI is ever changed to use it.
+                entry.location = oldEntry.location;
+            }
+
+            // Add the contact info to the cache.
+            mInfoMap.put(mCallId, entry);
+            sendInfoNotifications(mCallId, entry);
+
+            // If there is no image then we should not expect another callback.
+            if (info.photoUri == null) {
+                // We're done, so clear callbacks
+                clearCallbacks(mCallId);
+            }
+        }
+
+        public void onImageFetchComplete(Bitmap bitmap) {
+            onImageLoadComplete(TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, null,
+                    bitmap, (Integer) mCallId);
+        }
+    }
+
     class PhoneNumberServiceListener implements PhoneNumberService.NumberLookupListener,
                                      PhoneNumberService.ImageLookupListener {
         private final int mCallId;