Change app name to carrier name

Slice purchase notification shows the carrier name instead of the
application name.
Don't allow purchases when roaming.

Test: manual, unit, CTS
Bug: 263429235
Change-Id: I6586c6b132983514343f7e4081beaa5d855b1fa4
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index c1288e5..194b8f7 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -490,13 +490,11 @@
 
     private static final class PurchasePremiumCapabilityArgument {
         public @TelephonyManager.PremiumCapability int capability;
-        public @NonNull String appName;
         public @NonNull IIntegerConsumer callback;
 
         PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
-                @NonNull String appName, @NonNull IIntegerConsumer callback) {
+                @NonNull IIntegerConsumer callback) {
             this.capability = capability;
-            this.appName = appName;
             this.callback = callback;
         }
     }
@@ -2178,7 +2176,7 @@
                     PurchasePremiumCapabilityArgument arg =
                             (PurchasePremiumCapabilityArgument) request.argument;
                     SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
-                            arg.capability, arg.appName, onCompleted);
+                            arg.capability, onCompleted);
                     break;
                 }
 
@@ -11507,8 +11505,7 @@
         }
 
         sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
-                new PurchasePremiumCapabilityArgument(capability, callingProcess, callback), phone,
-                null);
+                new PurchasePremiumCapabilityArgument(capability, callback), phone, null);
     }
 
     /**
diff --git a/src/com/android/phone/slice/SlicePurchaseController.java b/src/com/android/phone/slice/SlicePurchaseController.java
index 3864119..9298d55 100644
--- a/src/com/android/phone/slice/SlicePurchaseController.java
+++ b/src/com/android/phone/slice/SlicePurchaseController.java
@@ -40,6 +40,7 @@
 import android.os.Message;
 import android.os.PersistableBundle;
 import android.provider.DeviceConfig;
+import android.sysprop.TelephonyProperties;
 import android.telephony.AnomalyReporter;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
@@ -204,11 +205,9 @@
     public static final String EXTRA_FAILURE_REASON =
             "com.android.phone.slice.extra.FAILURE_REASON";
     /**
-     * Extra for the application name requesting to purchase the premium capability
-     * from the slice purchase application.
+     * Extra for the user's carrier.
      */
-    public static final String EXTRA_REQUESTING_APP_NAME =
-            "com.android.phone.slice.extra.REQUESTING_APP_NAME";
+    public static final String EXTRA_CARRIER = "com.android.phone.slice.extra.CARRIER";
     /**
      * Extra for the canceled PendingIntent that the slice purchase application can send as a
      * response if the performance boost notification or WebView was canceled by the user.
@@ -497,11 +496,10 @@
                 break;
             }
             case EVENT_START_SLICE_PURCHASE_APP: {
-                int capability = msg.arg1;
-                String appName = (String) msg.obj;
-                logd("EVENT_START_SLICE_PURCHASE_APP: " + appName + " requests capability "
+                int capability = (int) msg.obj;
+                logd("EVENT_START_SLICE_PURCHASE_APP: "
                         + TelephonyManager.convertPremiumCapabilityToString(capability));
-                onStartSlicePurchaseApplication(capability, appName);
+                onStartSlicePurchaseApplication(capability);
                 break;
             }
             case EVENT_PURCHASE_TIMEOUT: {
@@ -565,13 +563,11 @@
      * Purchase the given premium capability from the carrier.
      *
      * @param capability The premium capability to purchase.
-     * @param appName The name of the application requesting premium capabilities.
      * @param onComplete The callback message to send when the purchase request is complete.
      */
     public synchronized void purchasePremiumCapability(
-            @TelephonyManager.PremiumCapability int capability, @NonNull String appName,
-            @NonNull Message onComplete) {
-        logd("purchasePremiumCapability: " + appName + " requests capability "
+            @TelephonyManager.PremiumCapability int capability, @NonNull Message onComplete) {
+        logd("purchasePremiumCapability: "
                 + TelephonyManager.convertPremiumCapabilityToString(capability));
         // Check whether the premium capability can be purchased.
         if (!arePremiumCapabilitiesSupportedByDevice()) {
@@ -627,8 +623,7 @@
         // All state checks passed. Mark purchase pending and start the slice purchase application.
         // Process through the handler since this method is synchronized.
         mPendingPurchaseCapabilities.put(capability, onComplete);
-        sendMessage(obtainMessage(EVENT_START_SLICE_PURCHASE_APP, capability, 0 /* unused */,
-                appName));
+        sendMessage(obtainMessage(EVENT_START_SLICE_PURCHASE_APP, capability));
     }
 
     private void sendPurchaseResult(@TelephonyManager.PremiumCapability int capability,
@@ -687,8 +682,8 @@
         }
     }
 
-    private void onStartSlicePurchaseApplication(@TelephonyManager.PremiumCapability int capability,
-            @NonNull String appName) {
+    private void onStartSlicePurchaseApplication(
+            @TelephonyManager.PremiumCapability int capability) {
         PremiumNetworkEntitlementResponse premiumNetworkEntitlementResponse =
                 mPremiumNetworkEntitlementApi.checkEntitlementStatus(capability);
 
@@ -721,7 +716,8 @@
         }
 
         String purchaseUrl = getPurchaseUrl(premiumNetworkEntitlementResponse);
-        if (TextUtils.isEmpty(purchaseUrl)) {
+        String carrier = getSimOperator();
+        if (TextUtils.isEmpty(purchaseUrl) || TextUtils.isEmpty(carrier)) {
             handlePurchaseResult(capability,
                     PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED, false);
             return;
@@ -753,7 +749,7 @@
         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_CARRIER, carrier);
         intent.putExtra(EXTRA_INTENT_CANCELED, createPendingIntent(
                 ACTION_SLICE_PURCHASE_APP_RESPONSE_CANCELED, capability, false));
         intent.putExtra(EXTRA_INTENT_CARRIER_ERROR, createPendingIntent(
@@ -805,6 +801,20 @@
     }
 
     /**
+     * Get the SIM operator. This is the carrier name from the SIM rather than from the network,
+     * which will be the same regardless of whether the user is roaming or not.
+     *
+     * @return The operator name from the SIM.
+     */
+    @VisibleForTesting
+    @Nullable public String getSimOperator() {
+        if (mPhone.getPhoneId() < TelephonyProperties.icc_operator_alpha().size()) {
+            return TelephonyProperties.icc_operator_alpha().get(mPhone.getPhoneId());
+        }
+        return null;
+    }
+
+    /**
      * Create the PendingIntent to allow the slice purchase application to send back responses.
      *
      * @param action The action that will be sent for this PendingIntent
@@ -1028,6 +1038,11 @@
     }
 
     private boolean isNetworkAvailable() {
+        if (mPhone.getServiceState().getDataRoaming()) {
+            logd("Network unavailable because it is roaming.");
+            return false;
+        }
+
         // TODO (b/251558673): Create a listener for data network type changed to dismiss
         //  notification and activity when the network is no longer available.
         switch (mPhone.getServiceState().getDataNetworkType()) {