Fix a minor logic issue in ag/18176102 am: 6f7c9bb60a

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/opt/net/ims/+/18203222

Change-Id: I66945139426c26833ab62b9c4b6b819c1b827792
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/java/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfo.java b/src/java/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfo.java
index 16e905d..dc79433 100644
--- a/src/java/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfo.java
+++ b/src/java/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfo.java
@@ -274,19 +274,15 @@
     }
 
     /**
-     * Get the IMS associated URI. It will first get the uri of MMTEL if it is not empty, otherwise
-     * it will try to get the uri of RCS. The null will be returned if both MMTEL and RCS are empty.
+     * Get the first URI from the "p-associated-uri" header included in the IMS registration
+     * response.
+     * @param preferTelUri If {@code true}, prefer returning the first TEL URI. If no TEL
+     *                     URIs exist, this method will still return the preferred (first) SIP URI
+     *                     in the header. If {@code false}, we will return the first URI
+     *                     in the "p-associated-uri" header, independent of the URI scheme.
      */
-    public synchronized Uri getImsAssociatedUri(boolean perferTelUri) {
-        if (perferTelUri == false) {
-            if (!mRcsAssociatedUris.isEmpty()) {
-                return mRcsAssociatedUris.get(0);
-            } else if (!mMmtelAssociatedUris.isEmpty()) {
-                return mMmtelAssociatedUris.get(0);
-            } else {
-                return null;
-            }
-        } else {
+    public synchronized Uri getImsAssociatedUri(boolean preferTelUri) {
+        if (preferTelUri) {
             if (!mRcsAssociatedUris.isEmpty()) {
                 for (Uri rcsAssociatedUri : mRcsAssociatedUris) {
                     if (PhoneAccount.SCHEME_TEL.equalsIgnoreCase(rcsAssociatedUri.getScheme())) {
@@ -301,6 +297,15 @@
                     }
                 }
             }
+        }
+
+        // Either we have not found a TEL URI or we do not prefer TEL URIs. Get the first URI from
+        // p-associated-uri list.
+        if (!mRcsAssociatedUris.isEmpty()) {
+            return mRcsAssociatedUris.get(0);
+        } else if (!mMmtelAssociatedUris.isEmpty()) {
+            return mMmtelAssociatedUris.get(0);
+        } else {
             return null;
         }
     }
diff --git a/src/java/com/android/ims/rcs/uce/presence/publish/PublishUtils.java b/src/java/com/android/ims/rcs/uce/presence/publish/PublishUtils.java
index 7df6bde..1a67a40 100644
--- a/src/java/com/android/ims/rcs/uce/presence/publish/PublishUtils.java
+++ b/src/java/com/android/ims/rcs/uce/presence/publish/PublishUtils.java
@@ -43,26 +43,34 @@
     private static final String SCHEME_TEL = "tel";
     private static final String DOMAIN_SEPARATOR = "@";
 
+    /**
+     * @return the contact URI of this device for either a PRESENCE or OPTIONS capabilities request.
+     * We will first try to use the IMS service associated URIs from the p-associated-uri header
+     * in the IMS registration response. If this is not available, we will fall back to using the
+     * SIM card information to generate the URI.
+     */
     public static Uri getDeviceContactUri(Context context, int subId,
             DeviceCapabilityInfo deviceCap, boolean isForPresence) {
         boolean preferTelUri = false;
         if (isForPresence) {
             preferTelUri = UceUtils.isTelUriForPidfXmlEnabled(context, subId);
         }
-        // Get the uri from the IMS associated URI which is provided by the IMS service.
+        // Get the uri from the IMS p-associated-uri header which is provided by the IMS service.
         Uri contactUri = deviceCap.getImsAssociatedUri(preferTelUri);
         if (contactUri != null) {
-            Log.d(LOG_TAG, "getDeviceContactUri: ims associated uri");
+            Uri convertedUri = preferTelUri ? getConvertedTelUri(context, contactUri) : contactUri;
+            Log.d(LOG_TAG, "getDeviceContactUri: returning "
+                    + (contactUri.equals(convertedUri) ? "found" : "converted")
+                    + " ims associated uri");
             return contactUri;
         }
 
+        // No IMS service provided URIs, so generate the contact uri from ISIM.
         TelephonyManager telephonyManager = getTelephonyManager(context, subId);
         if (telephonyManager == null) {
             Log.w(LOG_TAG, "getDeviceContactUri: TelephonyManager is null");
             return null;
         }
-
-        // Get the contact uri from ISIM.
         contactUri = getContactUriFromIsim(telephonyManager);
         if (contactUri != null) {
             Log.d(LOG_TAG, "getDeviceContactUri: impu");
@@ -152,6 +160,10 @@
         }
     }
 
+    /**
+     * @return a TEL URI version of the contact URI if given a SIP URI. If given a TEL URI, this
+     * method will return the same value given.
+     */
     private static Uri getConvertedTelUri(Context context, Uri contactUri) {
         if (contactUri == null) {
             return null;
diff --git a/tests/src/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfoTest.java b/tests/src/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfoTest.java
index 72cd26f..c977a08 100644
--- a/tests/src/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfoTest.java
+++ b/tests/src/com/android/ims/rcs/uce/presence/publish/DeviceCapabilityInfoTest.java
@@ -123,7 +123,8 @@
 
         assertEquals(number, telNumber);
 
-        //If there is only SIP URI, check the return uri is null if preferTelUir is true.
+        // If there is only SIP URI, this method will still return a SIP URI, since there are no TEL
+        // URIs found in the list.
         deviceCapInfo = createDeviceCapabilityInfo();
 
         uris[0] = Uri.fromParts(PhoneAccount.SCHEME_SIP, telNumber, null);
@@ -132,7 +133,12 @@
         deviceCapInfo.updateRcsAssociatedUri(uris);
         outUri = deviceCapInfo.getImsAssociatedUri(true);
 
-        assertNull(outUri);
+        numbers = outUri.getSchemeSpecificPart();
+        numberParts = numbers.split("[@;:]");
+        number = numberParts[0];
+
+        assertEquals(number, telNumber);
+
     }
 
     private DeviceCapabilityInfo createDeviceCapabilityInfo() {