Merge "Query lookup uri with work lookup key directly is not allowed" into nyc-dev am: ebfbfa10d9
am: 769f26fe74

* commit '769f26fe7488415cb677d4980540bbd7dcfc3ac0':
  Query lookup uri with work lookup key directly is not allowed
diff --git a/InCallUI/src/com/android/incallui/CallerInfo.java b/InCallUI/src/com/android/incallui/CallerInfo.java
index a638e11..f270678 100644
--- a/InCallUI/src/com/android/incallui/CallerInfo.java
+++ b/InCallUI/src/com/android/incallui/CallerInfo.java
@@ -237,12 +237,6 @@
                     info.name = cursor.getString(columnIndex);
                 }
 
-                columnIndex = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY);
-                if (columnIndex != -1) {
-                    info.nameAlternative = ContactInfoHelper.lookUpDisplayNameAlternative(
-                            context, cursor.getString(columnIndex));
-                }
-
                 // Look for the number
                 columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
                 if (columnIndex != -1) {
@@ -326,6 +320,9 @@
                         : contactRef.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
                 final Long directoryId = directory == null ? null : Longs.tryParse(directory);
                 info.userType = ContactsUtils.determineUserType(directoryId, contactId);
+
+                info.nameAlternative = ContactInfoHelper.lookUpDisplayNameAlternative(
+                        context, info.lookupKeyOrNull, info.userType);
             }
             cursor.close();
         }
diff --git a/InCallUI/src/com/android/incallui/StatusBarNotifier.java b/InCallUI/src/com/android/incallui/StatusBarNotifier.java
index 173fe42..7d212aa 100644
--- a/InCallUI/src/com/android/incallui/StatusBarNotifier.java
+++ b/InCallUI/src/com/android/incallui/StatusBarNotifier.java
@@ -36,6 +36,7 @@
 import android.graphics.drawable.BitmapDrawable;
 import android.media.AudioAttributes;
 import android.net.Uri;
+import android.provider.ContactsContract.Contacts;
 import android.support.annotation.Nullable;
 import android.telecom.Call.Details;
 import android.telecom.PhoneAccount;
@@ -229,7 +230,6 @@
      * Sets up the main Ui for the notification
      */
     private void buildAndSendNotification(Call originalCall, ContactCacheEntry contactInfo) {
-
         // This can get called to update an existing notification after contact information has come
         // back. However, it can happen much later. Before we continue, we need to make sure that
         // the call being passed in is still the one we want to show in the notification.
@@ -440,11 +440,14 @@
 
     private void addPersonReference(Notification.Builder builder, ContactCacheEntry contactInfo,
             Call call) {
-        if (contactInfo.lookupUri != null) {
+        // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
+        // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
+        // NotificationManager using it.
+        if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
             builder.addPerson(contactInfo.lookupUri.toString());
         } else if (!TextUtils.isEmpty(call.getNumber())) {
             builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL,
-                            call.getNumber(), null).toString());
+                    call.getNumber(), null).toString());
         }
     }
 
diff --git a/src/com/android/dialer/calllog/ContactInfoHelper.java b/src/com/android/dialer/calllog/ContactInfoHelper.java
index a9c7651..ff7a3f6 100644
--- a/src/com/android/dialer/calllog/ContactInfoHelper.java
+++ b/src/com/android/dialer/calllog/ContactInfoHelper.java
@@ -31,6 +31,7 @@
 import android.util.Log;
 
 import com.android.contacts.common.ContactsUtils;
+import com.android.contacts.common.ContactsUtils.UserType;
 import com.android.contacts.common.util.Constants;
 import com.android.contacts.common.util.PermissionsUtil;
 import com.android.contacts.common.util.PhoneNumberHelper;
@@ -181,7 +182,8 @@
             }
             String lookupKey = phoneLookupCursor.getString(PhoneQuery.LOOKUP_KEY);
             ContactInfo contactInfo = createPhoneLookupContactInfo(phoneLookupCursor, lookupKey);
-            contactInfo.nameAlternative = lookUpDisplayNameAlternative(mContext, lookupKey);
+            contactInfo.nameAlternative = lookUpDisplayNameAlternative(mContext, lookupKey,
+                    contactInfo.userType);
             return contactInfo;
         } finally {
             phoneLookupCursor.close();
@@ -207,13 +209,13 @@
         return info;
     }
 
-    public static String lookUpDisplayNameAlternative(Context context, String lookupKey) {
-        if (lookupKey == null) {
+    public static String lookUpDisplayNameAlternative(Context context, String lookupKey,
+            @UserType long userType) {
+        // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
+        if (lookupKey == null || userType == ContactsUtils.USER_TYPE_WORK) {
             return null;
         }
-
         final Uri uri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
-
         Cursor cursor = null;
         try {
             cursor = context.getContentResolver().query(uri,
@@ -222,9 +224,6 @@
             if (cursor != null && cursor.moveToFirst()) {
                 return cursor.getString(PhoneQuery.NAME_ALTERNATIVE);
             }
-        } catch (IllegalArgumentException e) {
-            // Thrown for work profile queries. For those, we don't support
-            // alternative display names.
         } finally {
             if (cursor != null) {
                 cursor.close();
diff --git a/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java b/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java
index 680afb1..e53eb78 100644
--- a/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java
+++ b/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java
@@ -22,6 +22,7 @@
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.MediumTest;
 
+import com.android.contacts.common.ContactsUtils;
 import com.android.contacts.common.test.mocks.ContactsMockContext;
 import com.android.contacts.common.test.mocks.MockContentProvider.Query;
 
@@ -99,13 +100,15 @@
     }
 
     public void testLookupDisplayNameAlternative_NullLookup() {
-        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, null));
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, null,
+                ContactsUtils.USER_TYPE_CURRENT));
     }
 
     public void testLookupDisplayNameAlternative_NoResults() {
         setUpQueryExpectations(displayNameAlternativeUri,
                 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION);
-        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY));
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext,
+                TEST_LOOKUP_KEY, ContactsUtils.USER_TYPE_CURRENT));
         mContext.verify();
     }
 
@@ -113,7 +116,8 @@
         setUpQueryExpectations(displayNameAlternativeUri,
                 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION, TEST_DISPLAY_NAME_ALTERNATIVE_ROW);
         Assert.assertEquals(TEST_DISPLAY_NAME_ALTERNATIVE,
-                mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY));
+                mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY
+                , ContactsUtils.USER_TYPE_CURRENT));
         mContext.verify();
     }