Merge "SlicePurchaseController get purchase URL from TS43 response"
diff --git a/src/com/android/phone/slice/SlicePurchaseController.java b/src/com/android/phone/slice/SlicePurchaseController.java
index 64261e1..6209c40 100644
--- a/src/com/android/phone/slice/SlicePurchaseController.java
+++ b/src/com/android/phone/slice/SlicePurchaseController.java
@@ -187,6 +187,8 @@
      */
     public static final String EXTRA_PREMIUM_CAPABILITY =
             "com.android.phone.slice.extra.PREMIUM_CAPABILITY";
+    /** Extra for the carrier URL to display to the user to allow premium capability purchase. */
+    public static final String EXTRA_PURCHASE_URL = "com.android.phone.slice.extra.PURCHASE_URL";
     /** Extra for the duration of the purchased premium capability. */
     public static final String EXTRA_PURCHASE_DURATION =
             "com.android.phone.slice.extra.PURCHASE_DURATION";
@@ -689,6 +691,13 @@
             return;
         }
 
+        String purchaseUrl = getPurchaseUrl(premiumNetworkEntitlementResponse);
+        if (TextUtils.isEmpty(purchaseUrl)) {
+            handlePurchaseResult(capability,
+                    PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED, false);
+            return;
+        }
+
         updateNotificationCounts();
         if (mMonthlyCount >= getCarrierConfigs().getInt(
                 CarrierConfigManager.KEY_PREMIUM_CAPABILITY_MAXIMUM_MONTHLY_NOTIFICATION_COUNT_INT)
@@ -714,6 +723,7 @@
         intent.putExtra(EXTRA_PHONE_ID, mPhone.getPhoneId());
         intent.putExtra(EXTRA_SUB_ID, mPhone.getSubId());
         intent.putExtra(EXTRA_PREMIUM_CAPABILITY, capability);
+        intent.putExtra(EXTRA_PURCHASE_URL, purchaseUrl);
         intent.putExtra(EXTRA_REQUESTING_APP_NAME, appName);
         intent.putExtra(EXTRA_INTENT_CANCELED, createPendingIntent(
                 ACTION_SLICE_PURCHASE_APP_RESPONSE_CANCELED, capability, false));
@@ -746,6 +756,26 @@
     }
 
     /**
+     * Get a valid purchase URL from either entitlement response or carrier configs, if one exists.
+     *
+     * @param entitlementResponse The entitlement response to get the purchase URL from.
+     * @return A valid purchase URL or an empty string if one doesn't exist.
+     */
+    @VisibleForTesting
+    @NonNull public String getPurchaseUrl(
+            @NonNull PremiumNetworkEntitlementResponse entitlementResponse) {
+        String purchaseUrl = entitlementResponse.mServiceFlowURL;
+        if (!isUrlValid(purchaseUrl)) {
+            purchaseUrl = getCarrierConfigs().getString(
+                    CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING);
+            if (!isUrlValid(purchaseUrl)) {
+                purchaseUrl = "";
+            }
+        }
+        return purchaseUrl;
+    }
+
+    /**
      * Create the PendingIntent to allow the slice purchase application to send back responses.
      *
      * @param action The action that will be sent for this PendingIntent
@@ -906,11 +936,6 @@
 
     private boolean isPremiumCapabilitySupportedByCarrier(
             @TelephonyManager.PremiumCapability int capability) {
-        String url = getCarrierConfigs().getString(
-                CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING);
-        if (!isUrlValid(url)) {
-            return false;
-        }
         int[] supportedCapabilities = getCarrierConfigs().getIntArray(
                 CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY);
         if (supportedCapabilities == null) {
diff --git a/tests/src/com/android/phone/slice/SlicePurchaseControllerTest.java b/tests/src/com/android/phone/slice/SlicePurchaseControllerTest.java
index 8320807..11a13dd 100644
--- a/tests/src/com/android/phone/slice/SlicePurchaseControllerTest.java
+++ b/tests/src/com/android/phone/slice/SlicePurchaseControllerTest.java
@@ -192,13 +192,16 @@
     }
 
     @Test
-    public void testIsUrlValid() {
-        // all other conditions met
-        doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone)
-                .getCachedAllowedNetworkTypesBitmask();
-        mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY,
-                new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY});
-        doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId();
+    public void testGetPurchaseURL() {
+        mEntitlementResponse.mServiceFlowURL = SlicePurchaseController.SLICE_PURCHASE_TEST_FILE;
+        String purchaseUrl = mSlicePurchaseController.getPurchaseUrl(mEntitlementResponse);
+        assertEquals(purchaseUrl, SlicePurchaseController.SLICE_PURCHASE_TEST_FILE);
+
+        mEntitlementResponse.mServiceFlowURL = null;
+        mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING,
+                SlicePurchaseController.SLICE_PURCHASE_TEST_FILE);
+        purchaseUrl = mSlicePurchaseController.getPurchaseUrl(mEntitlementResponse);
+        assertEquals(purchaseUrl, SlicePurchaseController.SLICE_PURCHASE_TEST_FILE);
 
         String[] invalidUrls = new String[] {
                 null,
@@ -212,14 +215,8 @@
         };
         for (String url : invalidUrls) {
             mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, url);
-            assertFalse(mSlicePurchaseController.isPremiumCapabilityAvailableForPurchase(
-                    TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY));
+            assertEquals("", mSlicePurchaseController.getPurchaseUrl(mEntitlementResponse));
         }
-
-        mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING,
-                SlicePurchaseController.SLICE_PURCHASE_TEST_FILE);
-        assertTrue(mSlicePurchaseController.isPremiumCapabilityAvailableForPurchase(
-                TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY));
     }
 
     @Test