Ignore connectionReady with invalid subscription

While SIM is disabled and enabled again,
if a clients register a callback before IMS service initializes the MMTEL feature connection,
invalid IMS connection state is notified to the client temporarily.

Bug: 227525553
Test: atest TeleServiceTests:ImsStateCallbackControllerTest
Change-Id: I537e63b0f9d401c9e6121fc53fb0287c2cc41aee
diff --git a/src/com/android/phone/ImsStateCallbackController.java b/src/com/android/phone/ImsStateCallbackController.java
index a7caab0..57c1787 100644
--- a/src/com/android/phone/ImsStateCallbackController.java
+++ b/src/com/android/phone/ImsStateCallbackController.java
@@ -296,6 +296,8 @@
             logd(mLogPrefix + "connectionReady " + subId);
 
             mSubId = subId;
+            if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) return;
+
             mState = STATE_READY;
             mReason = AVAILABLE;
             mHasConfig = true;
@@ -439,6 +441,8 @@
             logd(mLogPrefix + "connectionReady " + subId);
 
             mSubId = subId;
+            if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) return;
+
             mState = STATE_READY;
             mReason = AVAILABLE;
             mHasConfig = true;
@@ -642,7 +646,7 @@
         }
 
         void notifyInactive() {
-            if (VDBG) logv("CallbackWrapper notifyInactive subId=" + mSubId);
+            logd("CallbackWrapper notifyInactive subId=" + mSubId);
 
             try {
                 mCallback.onUnavailable(REASON_SUBSCRIPTION_INACTIVE);
diff --git a/tests/src/com/android/phone/ImsStateCallbackControllerTest.java b/tests/src/com/android/phone/ImsStateCallbackControllerTest.java
index cbd6ceb..cb4321c 100644
--- a/tests/src/com/android/phone/ImsStateCallbackControllerTest.java
+++ b/tests/src/com/android/phone/ImsStateCallbackControllerTest.java
@@ -822,6 +822,58 @@
         verify(mRcsFeatureConnectorSlot0, times(0)).disconnect();
     }
 
+    @Test
+    @SmallTest
+    public void testMmTelConnectionReadyWhenReEnableSim() throws Exception {
+        createController(1);
+
+        // MMTEL feature
+        mMmTelConnectorListenerSlot0.getValue().connectionReady(null, SLOT_0_SUB_ID);
+        processAllMessages();
+        mMmTelConnectorListenerSlot0.getValue()
+                .connectionUnavailable(UNAVAILABLE_REASON_DISCONNECTED);
+        processAllMessages();
+        mMmTelConnectorListenerSlot0.getValue().connectionReady(null,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        processAllMessages();
+        mImsStateCallbackController
+                .registerImsStateCallback(SLOT_0_SUB_ID, FEATURE_MMTEL, mCallback0, "callback0");
+        processAllMessages();
+
+        assertTrue(mImsStateCallbackController.isRegistered(mCallback0));
+        verify(mCallback0, times(1)).onUnavailable(REASON_IMS_SERVICE_DISCONNECTED);
+        verify(mCallback0, times(0)).onAvailable();
+
+        mImsStateCallbackController.unregisterImsStateCallback(mCallback0);
+        processAllMessages();
+        assertFalse(mImsStateCallbackController.isRegistered(mCallback0));
+
+        // RCS feature
+        // TelephonyRcsService notifying active features
+        mImsStateCallbackController.notifyExternalRcsStateChanged(SLOT_0, false, true);
+        processAllMessages();
+        // RcsFeatureController notifying STATE_READY
+        mImsStateCallbackController.notifyExternalRcsStateChanged(SLOT_0, true, true);
+        processAllMessages();
+        mRcsConnectorListenerSlot0.getValue()
+                .connectionUnavailable(UNAVAILABLE_REASON_DISCONNECTED);
+        processAllMessages();
+        mRcsConnectorListenerSlot0.getValue().connectionReady(null,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        processAllMessages();
+        mImsStateCallbackController
+                .registerImsStateCallback(SLOT_0_SUB_ID, FEATURE_RCS, mCallback1, "callback1");
+        processAllMessages();
+
+        assertTrue(mImsStateCallbackController.isRegistered(mCallback1));
+        verify(mCallback1, times(1)).onUnavailable(REASON_IMS_SERVICE_DISCONNECTED);
+        verify(mCallback1, times(0)).onAvailable();
+
+        mImsStateCallbackController.unregisterImsStateCallback(mCallback1);
+        processAllMessages();
+        assertFalse(mImsStateCallbackController.isRegistered(mCallback1));
+    }
+
     private void createController(int slotCount) throws Exception {
         if (Looper.myLooper() == null) {
             Looper.prepare();