Merge "Support satellite config version for metrics" into main
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index a3d8baf..0548aa5 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -15017,4 +15017,25 @@
             Binder.restoreCallingIdentity(identity);
         }
     }
+
+    /**
+     * Get list of applications that are optimized for low bandwidth satellite data.
+     *
+     * @return List of Application Name with data optimized network property.
+     * {@link #PROPERTY_SATELLITE_DATA_OPTIMIZED}
+     */
+    @Override
+    public List<String> getSatelliteDataOptimizedApps() {
+        enforceSatelliteCommunicationPermission("getSatelliteDataOptimizedApps");
+        List<String> appNames = new ArrayList<>();
+        int userId = Binder.getCallingUserHandle().getIdentifier();
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            appNames = mSatelliteController.getSatelliteDataOptimizedApps(userId);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+
+        return appNames;
+    }
 }
diff --git a/src/com/android/phone/settings/RadioInfo.java b/src/com/android/phone/settings/RadioInfo.java
index ab7a3bf..5f0b2c1 100644
--- a/src/com/android/phone/settings/RadioInfo.java
+++ b/src/com/android/phone/settings/RadioInfo.java
@@ -2004,7 +2004,8 @@
     private static final int SATELLITE_CHANNEL = 8665;
     private final OnCheckedChangeListener mForceSatelliteChannelOnChangeListener =
             (buttonView, isChecked) -> {
-                if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
+
+                if (!isValidSubscription(mSubId)) {
                     loge("Force satellite channel invalid subId " + mSubId);
                     return;
                 }
@@ -2237,11 +2238,9 @@
                         dataMode);
                 overrideBundle.putBoolean(
                         KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL, false);
-                log("satData: mMockSatelliteDataListener: new " + overrideBundle);
-                if (SubscriptionManager.isValidSubscriptionId(mSubId)) {
+                if (isValidSubscription(mSubId)) {
                     getCarrierConfig().overrideConfig(mSubId, overrideBundle, false);
-                } else {
-                    Log.e(TAG, "SubscriptionId is not valid: " + mSubId);
+                    log("satData: mMockSatelliteDataListener: Updated new config" + overrideBundle);
                 }
             };
 
@@ -2271,7 +2270,7 @@
     }
 
     private void reloadCarrierConfigDefaults() {
-        if (mSatelliteDataOriginalBundle[mPhoneId] != null) {
+        if (mSatelliteDataOriginalBundle[mPhoneId] != null && isValidSubscription(mSubId)) {
             log("satData: Setting originalCarrierConfig = "
                     + mSatelliteDataOriginalBundle[mPhoneId]);
             getCarrierConfig().overrideConfig(mSubId, mSatelliteDataOriginalBundle[mPhoneId],
@@ -2281,24 +2280,40 @@
 
     private boolean isValidOperator(int subId) {
         String operatorNumeric = null;
-        if (SubscriptionManager.isValidSubscriptionId(subId)) {
-            operatorNumeric = mTelephonyManager
-                    .getNetworkOperatorForPhone(mPhoneId);
+        if (isValidSubscription(subId)) {
+            operatorNumeric = mTelephonyManager.getNetworkOperatorForPhone(mPhoneId);
             TelephonyManager tm;
-            if (TextUtils.isEmpty(operatorNumeric)
-                    && (tm = getSystemService(TelephonyManager.class)) != null) {
+            if (TextUtils.isEmpty(operatorNumeric) && (tm = getSystemService(
+                    TelephonyManager.class)) != null) {
                 operatorNumeric = tm.getSimOperatorNumericForPhone(mPhoneId);
             }
         }
         return !TextUtils.isEmpty(operatorNumeric);
     }
 
+    /**
+     * This method will do extra check to validate the subId.
+     * <p>
+     * In case user opens the radioInfo when sim is active and enable some checks and go to the
+     * SIM settings screen and disabled the screen. Upon return to radioInfo screen subId is still
+     * valid but not in active state any more.
+     */
+    private boolean isValidSubscription(int subId) {
+        boolean isValidSubId = false;
+        if (SubscriptionManager.isValidSubscriptionId(subId)) {
+            SubscriptionManager mSm = getSystemService(SubscriptionManager.class);
+            isValidSubId = mSm.isActiveSubscriptionId(subId);
+        }
+        log("isValidSubscription, subId [ " + subId + " ] = " + isValidSubId);
+        return isValidSubId;
+    }
+
     private final OnCheckedChangeListener mMockSatelliteListener =
             (buttonView, isChecked) -> {
                 int subId = mSubId;
                 int phoneId = mPhoneId;
                 if (SubscriptionManager.isValidPhoneId(phoneId)
-                        && SubscriptionManager.isValidSubscriptionId(subId)) {
+                        && isValidSubscription(subId)) {
                     if (getCarrierConfig() == null) return;
                     if (isChecked) {
                         if (!isValidOperator(subId)) {
diff --git a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
index ef6a02a..bbcb52b 100644
--- a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
+++ b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
@@ -46,6 +46,7 @@
 import android.platform.test.flag.junit.SetFlagsRule;
 import android.preference.PreferenceManager;
 import android.telephony.RadioAccessFamily;
+import android.telephony.Rlog;
 import android.telephony.TelephonyManager;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
@@ -74,6 +75,7 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.Collections;
+import java.util.List;
 import java.util.Locale;
 
 /**
@@ -85,6 +87,8 @@
     @Rule
     public TestRule compatChangeRule = new PlatformCompatChangeRule();
 
+    private static final String TAG = "PhoneInterfaceManagerTest";
+
     private PhoneInterfaceManager mPhoneInterfaceManager;
     private SharedPreferences mSharedPreferences;
     @Mock private IIntegerConsumer mIIntegerConsumer;
@@ -322,6 +326,10 @@
                 mPhoneInterfaceManager).getDefaultPhone();
     }
 
+    private static void loge(String message) {
+        Rlog.e(TAG, message);
+    }
+
     @Test
     public void setNullCipherNotificationsEnabled_allReqsMet_successfullyEnabled() {
         setModemSupportsNullCipherNotification(true);
@@ -550,4 +558,25 @@
         String packageName = mPhoneInterfaceManager.getCurrentPackageName();
         assertEquals(null, packageName);
     }
+
+    @Test
+    public void testGetSatelliteDataOptimizedApps() throws Exception {
+        doReturn(true).when(mFeatureFlags).carrierRoamingNbIotNtn();
+        mPhoneInterfaceManager.setFeatureFlags(mFeatureFlags);
+        loge("FeatureFlagApi is set to return true");
+
+        boolean containsCtsApp = false;
+        String ctsPackageName = "android.telephony.cts";
+        List<String> listSatelliteApplications =
+                mPhoneInterfaceManager.getSatelliteDataOptimizedApps();
+
+        for (String packageName : listSatelliteApplications) {
+            if (ctsPackageName.equals(packageName)) {
+                containsCtsApp = true;
+            }
+        }
+
+        assertFalse(containsCtsApp);
+    }
+
 }