Refactor CallerInfo and ContactInfoHelper to improve readability.

Test: Existing tests
PiperOrigin-RevId: 178862915
Change-Id: I2dd58db10d546c062b0f43dad80f74d588cb65e1
diff --git a/java/com/android/dialer/phonenumbercache/ContactInfoHelper.java b/java/com/android/dialer/phonenumbercache/ContactInfoHelper.java
index c3f5b69..39e3866 100644
--- a/java/com/android/dialer/phonenumbercache/ContactInfoHelper.java
+++ b/java/com/android/dialer/phonenumbercache/ContactInfoHelper.java
@@ -337,30 +337,28 @@
       return ContactInfo.EMPTY;
     }
 
-    Cursor phoneLookupCursor = null;
-    try {
-      String[] projection = PhoneQuery.getPhoneLookupProjection(uri);
-      phoneLookupCursor = mContext.getContentResolver().query(uri, projection, null, null, null);
-    } catch (NullPointerException e) {
-      LogUtil.e("ContactInfoHelper.lookupContactFromUri", "phone lookup", e);
-      // Trap NPE from pre-N CP2
-      return null;
-    }
-    if (phoneLookupCursor == null) {
-      LogUtil.d("ContactInfoHelper.lookupContactFromUri", "phoneLookupCursor is null");
-      return null;
-    }
+    try (Cursor phoneLookupCursor =
+        mContext
+            .getContentResolver()
+            .query(
+                uri,
+                PhoneQuery.getPhoneLookupProjection(uri),
+                null /* selection */,
+                null /* selectionArgs */,
+                null /* sortOrder */)) {
+      if (phoneLookupCursor == null) {
+        LogUtil.d("ContactInfoHelper.lookupContactFromUri", "phoneLookupCursor is null");
+        return null;
+      }
 
-    try {
       if (!phoneLookupCursor.moveToFirst()) {
         return ContactInfo.EMPTY;
       }
+
       String lookupKey = phoneLookupCursor.getString(PhoneQuery.LOOKUP_KEY);
       ContactInfo contactInfo = createPhoneLookupContactInfo(phoneLookupCursor, lookupKey);
       fillAdditionalContactInfo(mContext, contactInfo);
       return contactInfo;
-    } finally {
-      phoneLookupCursor.close();
     }
   }
 
diff --git a/java/com/android/incallui/CallerInfo.java b/java/com/android/incallui/CallerInfo.java
index 6f8d192..5c43b4f 100644
--- a/java/com/android/incallui/CallerInfo.java
+++ b/java/com/android/incallui/CallerInfo.java
@@ -198,135 +198,133 @@
    */
   public static CallerInfo getCallerInfo(Context context, Uri contactRef, Cursor cursor) {
     CallerInfo info = new CallerInfo();
-    info.photoResource = 0;
-    info.phoneLabel = null;
-    info.numberType = 0;
-    info.numberLabel = null;
     info.cachedPhoto = null;
-    info.isCachedPhotoCurrent = false;
     info.contactExists = false;
+    info.contactRefUri = contactRef;
+    info.isCachedPhotoCurrent = false;
+    info.name = null;
+    info.needUpdate = false;
+    info.numberLabel = null;
+    info.numberType = 0;
+    info.phoneLabel = null;
+    info.photoResource = 0;
     info.userType = ContactsUtils.USER_TYPE_CURRENT;
 
     Log.v(TAG, "getCallerInfo() based on cursor...");
 
-    if (cursor != null) {
-      if (cursor.moveToFirst()) {
-        // TODO: photo_id is always available but not taken
-        // care of here. Maybe we should store it in the
-        // CallerInfo object as well.
-
-        long contactId = 0L;
-        int columnIndex;
-
-        // Look for the name
-        columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
-        if (columnIndex != -1) {
-          info.name = cursor.getString(columnIndex);
-        }
-
-        // Look for the number
-        columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
-        if (columnIndex != -1) {
-          info.phoneNumber = cursor.getString(columnIndex);
-        }
-
-        // Look for the normalized number
-        columnIndex = cursor.getColumnIndex(PhoneLookup.NORMALIZED_NUMBER);
-        if (columnIndex != -1) {
-          info.normalizedNumber = cursor.getString(columnIndex);
-        }
-
-        // Look for the label/type combo
-        columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL);
-        if (columnIndex != -1) {
-          int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE);
-          if (typeColumnIndex != -1) {
-            info.numberType = cursor.getInt(typeColumnIndex);
-            info.numberLabel = cursor.getString(columnIndex);
-            info.phoneLabel =
-                Phone.getTypeLabel(context.getResources(), info.numberType, info.numberLabel)
-                    .toString();
-          }
-        }
-
-        // cache the lookup key for later use to create lookup URIs
-        columnIndex = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY);
-        if (columnIndex != -1) {
-          info.lookupKeyOrNull = cursor.getString(columnIndex);
-        }
-
-        // Look for the person_id.
-        columnIndex = getColumnIndexForPersonId(contactRef, cursor);
-        if (columnIndex != -1) {
-          contactId = cursor.getLong(columnIndex);
-          // QuickContacts in M doesn't support enterprise contact id
-          if (contactId != 0
-              && (VERSION.SDK_INT >= VERSION_CODES.N
-                  || !Contacts.isEnterpriseContactId(contactId))) {
-            info.contactIdOrZero = contactId;
-            Log.v(TAG, "==> got info.contactIdOrZero: " + info.contactIdOrZero);
-          }
-        } else {
-          // No valid columnIndex, so we can't look up person_id.
-          Log.v(TAG, "Couldn't find contactId column for " + contactRef);
-          // Watch out: this means that anything that depends on
-          // person_id will be broken (like contact photo lookups in
-          // the in-call UI, for example.)
-        }
-
-        // Display photo URI.
-        columnIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
-        if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) {
-          info.contactDisplayPhotoUri = Uri.parse(cursor.getString(columnIndex));
-        } else {
-          info.contactDisplayPhotoUri = null;
-        }
-
-        // look for the custom ringtone, create from the string stored
-        // in the database.
-        columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE);
-        if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) {
-          if (TextUtils.isEmpty(cursor.getString(columnIndex))) {
-            // make it consistent with frameworks/base/.../CallerInfo.java
-            info.contactRingtoneUri = Uri.EMPTY;
-          } else {
-            info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex));
-          }
-        } else {
-          info.contactRingtoneUri = null;
-        }
-
-        // look for the send to voicemail flag, set it to true only
-        // under certain circumstances.
-        columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL);
-        info.shouldSendToVoicemail = (columnIndex != -1) && ((cursor.getInt(columnIndex)) == 1);
-        info.contactExists = true;
-
-        // Determine userType by directoryId and contactId
-        final String directory =
-            contactRef == null
-                ? null
-                : contactRef.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
-        Long directoryId = null;
-        if (directory != null) {
-          try {
-            directoryId = Long.parseLong(directory);
-          } catch (NumberFormatException e) {
-            // do nothing
-          }
-        }
-        info.userType = ContactsUtils.determineUserType(directoryId, contactId);
-
-        info.nameAlternative =
-            ContactInfoHelper.lookUpDisplayNameAlternative(
-                context, info.lookupKeyOrNull, info.userType, directoryId);
-      }
-      cursor.close();
+    if (cursor == null || !cursor.moveToFirst()) {
+      return info;
     }
 
-    info.needUpdate = false;
-    info.name = normalize(info.name);
-    info.contactRefUri = contactRef;
+    // TODO: photo_id is always available but not taken
+    // care of here. Maybe we should store it in the
+    // CallerInfo object as well.
+
+    long contactId = 0L;
+    int columnIndex;
+
+    // Look for the name
+    columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
+    if (columnIndex != -1) {
+      info.name = normalize(cursor.getString(columnIndex));
+    }
+
+    // Look for the number
+    columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
+    if (columnIndex != -1) {
+      info.phoneNumber = cursor.getString(columnIndex);
+    }
+
+    // Look for the normalized number
+    columnIndex = cursor.getColumnIndex(PhoneLookup.NORMALIZED_NUMBER);
+    if (columnIndex != -1) {
+      info.normalizedNumber = cursor.getString(columnIndex);
+    }
+
+    // Look for the label/type combo
+    columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL);
+    if (columnIndex != -1) {
+      int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE);
+      if (typeColumnIndex != -1) {
+        info.numberType = cursor.getInt(typeColumnIndex);
+        info.numberLabel = cursor.getString(columnIndex);
+        info.phoneLabel =
+            Phone.getTypeLabel(context.getResources(), info.numberType, info.numberLabel)
+                .toString();
+      }
+    }
+
+    // cache the lookup key for later use to create lookup URIs
+    columnIndex = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY);
+    if (columnIndex != -1) {
+      info.lookupKeyOrNull = cursor.getString(columnIndex);
+    }
+
+    // Look for the person_id.
+    columnIndex = getColumnIndexForPersonId(contactRef, cursor);
+    if (columnIndex != -1) {
+      contactId = cursor.getLong(columnIndex);
+      // QuickContacts in M doesn't support enterprise contact id
+      if (contactId != 0
+          && (VERSION.SDK_INT >= VERSION_CODES.N || !Contacts.isEnterpriseContactId(contactId))) {
+        info.contactIdOrZero = contactId;
+        Log.v(TAG, "==> got info.contactIdOrZero: " + info.contactIdOrZero);
+      }
+    } else {
+      // No valid columnIndex, so we can't look up person_id.
+      Log.v(TAG, "Couldn't find contactId column for " + contactRef);
+      // Watch out: this means that anything that depends on
+      // person_id will be broken (like contact photo lookups in
+      // the in-call UI, for example.)
+    }
+
+    // Display photo URI.
+    columnIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
+    if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) {
+      info.contactDisplayPhotoUri = Uri.parse(cursor.getString(columnIndex));
+    } else {
+      info.contactDisplayPhotoUri = null;
+    }
+
+    // look for the custom ringtone, create from the string stored
+    // in the database.
+    columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE);
+    if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) {
+      if (TextUtils.isEmpty(cursor.getString(columnIndex))) {
+        // make it consistent with frameworks/base/.../CallerInfo.java
+        info.contactRingtoneUri = Uri.EMPTY;
+      } else {
+        info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex));
+      }
+    } else {
+      info.contactRingtoneUri = null;
+    }
+
+    // look for the send to voicemail flag, set it to true only
+    // under certain circumstances.
+    columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL);
+    info.shouldSendToVoicemail = (columnIndex != -1) && ((cursor.getInt(columnIndex)) == 1);
+    info.contactExists = true;
+
+    // Determine userType by directoryId and contactId
+    final String directory =
+        contactRef == null
+            ? null
+            : contactRef.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
+    Long directoryId = null;
+    if (directory != null) {
+      try {
+        directoryId = Long.parseLong(directory);
+      } catch (NumberFormatException e) {
+        // do nothing
+      }
+    }
+    info.userType = ContactsUtils.determineUserType(directoryId, contactId);
+
+    info.nameAlternative =
+        ContactInfoHelper.lookUpDisplayNameAlternative(
+            context, info.lookupKeyOrNull, info.userType, directoryId);
+    cursor.close();
 
     return info;
   }