When oppt profile is disabled, make sure to enable the modem stack.

Whenever there's a oppt subscription change, check and enable modem
stack if oppt subscription is no longer active on that slot.

Bug: 136022701
Test: manual
Change-Id: I1a5fd0fc26c84a31ca024ed9c441a2a46c8d9533
Merged-In: I1a5fd0fc26c84a31ca024ed9c441a2a46c8d9533
diff --git a/src/com/android/ons/ONSProfileSelector.java b/src/com/android/ons/ONSProfileSelector.java
index 1869f37..9b9a172 100644
--- a/src/com/android/ons/ONSProfileSelector.java
+++ b/src/com/android/ons/ONSProfileSelector.java
@@ -165,6 +165,7 @@
             new SubscriptionManager.OnOpportunisticSubscriptionsChangedListener() {
                 @Override
                 public void onOpportunisticSubscriptionsChanged() {
+                    logDebug("onOpportunisticSubscriptionsChanged.");
                     mHandler.sendEmptyMessage(MSG_PROFILE_UPDATE);
                 }
             };
@@ -586,7 +587,7 @@
     }
 
     private boolean enableModem(int subId, boolean enable) {
-        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+        if (!mSubscriptionManager.isActiveSubId(subId)) {
             return false;
         }
 
@@ -724,6 +725,24 @@
         }
     }
 
+    private void enableModemStackForNonOpportunisticSlots() {
+        int phoneCount = mTelephonyManager.getPhoneCount();
+        // Do nothing in single SIM mode.
+        if (phoneCount < 2) return;
+
+        for (int i = 0; i < phoneCount; i++) {
+            boolean hasActiveOpptProfile = false;
+            for (SubscriptionInfo info : mOppSubscriptionInfos) {
+                if (info.getSimSlotIndex() == i) {
+                    hasActiveOpptProfile = true;
+                }
+            }
+            // If the slot doesn't have active opportunistic profile anymore, it's back to
+            // DSDS use-case. Make sure the the modem stack is enabled.
+            if (!hasActiveOpptProfile) mTelephonyManager.enableModemForSlot(i, true);
+        }
+    }
+
     @VisibleForTesting
     protected void init(Context c, ONSProfileSelectionCallback profileSelectionCallback) {
         mContext = c;
@@ -748,6 +767,7 @@
                     case MSG_PROFILE_UPDATE:
                         synchronized (mLock) {
                             updateOpportunisticSubscriptions();
+                            enableModemStackForNonOpportunisticSlots();
                         }
                         break;
                     case MSG_START_PROFILE_SELECTION: