Account for removing primary subs within a group.

In the DSDS case where there are two subscriptions belonging in the same
group and then one gets removed, we can simply assume that the remaining
sub should become the default without prompting the user.

Test: atest FrameworksTelephonyTests:MultiSimSettingControllerTest
Test: manual
Bug: 142092510
Change-Id: Ibeeec8d42c78059778415f5275bd78f224990135
Merged-In: Ibeeec8d42c78059778415f5275bd78f224990135
diff --git a/src/java/com/android/internal/telephony/MultiSimSettingController.java b/src/java/com/android/internal/telephony/MultiSimSettingController.java
index e973f07..61d873b 100644
--- a/src/java/com/android/internal/telephony/MultiSimSettingController.java
+++ b/src/java/com/android/internal/telephony/MultiSimSettingController.java
@@ -92,7 +92,8 @@
                     PRIMARY_SUB_SWAPPED,
                     PRIMARY_SUB_SWAPPED_IN_GROUP,
                     PRIMARY_SUB_MARKED_OPPT,
-                    PRIMARY_SUB_INITIALIZED
+                    PRIMARY_SUB_INITIALIZED,
+                    PRIMARY_SUB_REMOVED_IN_GROUP
     })
     private @interface PrimarySubChangeType {}
 
@@ -110,6 +111,9 @@
     private static final int PRIMARY_SUB_MARKED_OPPT            = 5;
     // Subscription information is initially loaded.
     private static final int PRIMARY_SUB_INITIALIZED            = 6;
+    // One or more primary subscriptions are deactivated but within the same group as another active
+    // sub.
+    private static final int PRIMARY_SUB_REMOVED_IN_GROUP       = 7;
 
     protected final Context mContext;
     protected final SubscriptionController mSubController;
@@ -600,7 +604,14 @@
             // any previous primary subscription becomes inactive, we consider it
             for (int subId : prevPrimarySubList) {
                 if (mPrimarySubList.contains(subId)) continue;
-                if (!mSubController.isActiveSubId(subId)) return PRIMARY_SUB_REMOVED;
+                if (!mSubController.isActiveSubId(subId)) {
+                    for (int currentSubId : mPrimarySubList) {
+                        if (areSubscriptionsInSameGroup(currentSubId, subId)) {
+                            return PRIMARY_SUB_REMOVED_IN_GROUP;
+                        }
+                    }
+                    return PRIMARY_SUB_REMOVED;
+                }
                 if (!mSubController.isOpportunistic(subId)) {
                     // Should never happen.
                     loge("[updatePrimarySubListAndGetChangeType]: missing active primary subId "
diff --git a/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java
index e340033..18887dd 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java
@@ -513,6 +513,46 @@
         verifyDismissIntentSent();
     }
 
+    @Test
+    @SmallTest
+    public void testGroupedPrimaryRemoved() throws Exception {
+        // Create subscription grouping of subs 1 and 2.
+        replaceInstance(SubscriptionInfo.class, "mGroupUUID", mSubInfo1, mGroupUuid1);
+        doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(1);
+        doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(2);
+        doReturn(Arrays.asList(mSubInfo1, mSubInfo2)).when(mSubControllerMock)
+                .getSubscriptionsInGroup(any(), anyString(), nullable(String.class));
+
+        mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
+        mMultiSimSettingControllerUT.notifySubscriptionGroupChanged(mGroupUuid1);
+        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
+        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        processAllMessages();
+
+        // Defaults not touched, sub 1 is already default.
+        verify(mSubControllerMock, never()).setDefaultDataSubId(anyInt());
+
+        // Take out SIM 1.
+        clearInvocations(mSubControllerMock);
+        doReturn(false).when(mSubControllerMock).isActiveSubId(1);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(1);
+        doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock1).getSubId();
+        List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo2);
+        doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
+                nullable(String.class));
+        doReturn(new int[]{2}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
+        mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
+        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(
+                0, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        processAllMessages();
+
+        // Sub 2 should be made the default sub silently.
+        verify(mSubControllerMock).setDefaultDataSubId(2);
+        verify(mSubControllerMock).setDefaultVoiceSubId(2);
+        verify(mSubControllerMock).setDefaultSmsSubId(2);
+        verifyDismissIntentSent();
+    }
+
     private Intent captureBroadcastIntent() {
         ArgumentCaptor<Intent> intentCapture = ArgumentCaptor.forClass(Intent.class);
         verify(mContext).sendBroadcast(intentCapture.capture());