Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e7ea93d5 authored by linyuh's avatar linyuh Committed by Copybara-Service
Browse files

Refactor CallerInfo and ContactInfoHelper to improve readability.

Test: Existing tests
PiperOrigin-RevId: 178862915
Change-Id: I2dd58db10d546c062b0f43dad80f74d588cb65e1
parent a4745bdd
Loading
Loading
Loading
Loading
+14 −16
Original line number Diff line number Diff line
@@ -337,30 +337,28 @@ public class ContactInfoHelper {
      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;
    }
    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();
    }
  }

+118 −120
Original line number Diff line number Diff line
@@ -198,19 +198,24 @@ public class CallerInfo {
   */
  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()) {
    if (cursor == null || !cursor.moveToFirst()) {
      return info;
    }

    // TODO: photo_id is always available but not taken
    // care of here. Maybe we should store it in the
    // CallerInfo object as well.
@@ -221,7 +226,7 @@ public class CallerInfo {
    // Look for the name
    columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
    if (columnIndex != -1) {
          info.name = cursor.getString(columnIndex);
      info.name = normalize(cursor.getString(columnIndex));
    }

    // Look for the number
@@ -261,8 +266,7 @@ public class CallerInfo {
      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))) {
          && (VERSION.SDK_INT >= VERSION_CODES.N || !Contacts.isEnterpriseContactId(contactId))) {
        info.contactIdOrZero = contactId;
        Log.v(TAG, "==> got info.contactIdOrZero: " + info.contactIdOrZero);
      }
@@ -320,13 +324,7 @@ public class CallerInfo {
    info.nameAlternative =
        ContactInfoHelper.lookUpDisplayNameAlternative(
            context, info.lookupKeyOrNull, info.userType, directoryId);
      }
    cursor.close();
    }

    info.needUpdate = false;
    info.name = normalize(info.name);
    info.contactRefUri = contactRef;

    return info;
  }