Snap for 8746144 from bd325fc0efd8f104f93e04969abd53e198df2d5f to mainline-ipsec-release

Change-Id: I7d9f4720f5131442bc819dbd1aabe4a6b406847d
diff --git a/src/java/com/android/ims/ImsCall.java b/src/java/com/android/ims/ImsCall.java
index a5813d8..06230a1 100755
--- a/src/java/com/android/ims/ImsCall.java
+++ b/src/java/com/android/ims/ImsCall.java
@@ -1762,7 +1762,12 @@
     public void sendRttMessage(String rttMessage) {
         synchronized(mLockObj) {
             if (mSession == null) {
-                loge("sendRttMessage::no session");
+                loge("sendRttMessage::no session, ignoring");
+                return;
+            }
+            if (mCallProfile == null || mCallProfile.mMediaProfile == null)  {
+                loge("sendRttMessage:: no valid call profile, ignoring");
+                return;
             }
             if (!mCallProfile.mMediaProfile.isRttCall()) {
                 logi("sendRttMessage::Not an rtt call, ignoring");
@@ -1781,7 +1786,12 @@
 
         synchronized(mLockObj) {
             if (mSession == null) {
-                loge("sendRttModifyRequest::no session");
+                loge("sendRttModifyRequest::no session, ignoring");
+                return;
+            }
+            if (mCallProfile == null || mCallProfile.mMediaProfile == null)  {
+                loge("sendRttModifyRequest:: no valid call profile, ignoring");
+                return;
             }
             if (rttOn && mCallProfile.mMediaProfile.isRttCall()) {
                 logi("sendRttModifyRequest::Already RTT call, ignoring request to turn on.");
@@ -1815,6 +1825,11 @@
         synchronized(mLockObj) {
             if (mSession == null) {
                 loge("sendRttModifyResponse::no session");
+                return;
+            }
+            if (mCallProfile == null || mCallProfile.mMediaProfile == null)  {
+                loge("sendRttModifyResponse:: no valid call profile, ignoring");
+                return;
             }
             if (mCallProfile.mMediaProfile.isRttCall()) {
                 logi("sendRttModifyResponse::Already RTT call, ignoring.");
diff --git a/src/java/com/android/ims/ImsManager.java b/src/java/com/android/ims/ImsManager.java
index cf95a20..c41426d 100644
--- a/src/java/com/android/ims/ImsManager.java
+++ b/src/java/com/android/ims/ImsManager.java
@@ -3035,7 +3035,8 @@
     public @MmTelFeature.ProcessCallResult int shouldProcessCall(boolean isEmergency,
             String[] numbers) throws ImsException {
         try {
-            return mMmTelConnectionRef.get().shouldProcessCall(isEmergency, numbers);
+            MmTelFeatureConnection c = getOrThrowExceptionIfServiceUnavailable();
+            return c.shouldProcessCall(isEmergency, numbers);
         } catch (RemoteException e) {
             throw new ImsException("shouldProcessCall()", e,
                     ImsReasonInfo.CODE_LOCAL_IMS_SERVICE_DOWN);
diff --git a/tests/src/com/android/ims/ImsManagerTest.java b/tests/src/com/android/ims/ImsManagerTest.java
index 42f110d..0653908 100644
--- a/tests/src/com/android/ims/ImsManagerTest.java
+++ b/tests/src/com/android/ims/ImsManagerTest.java
@@ -22,6 +22,7 @@
 import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_LTE;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.anyInt;
@@ -968,6 +969,21 @@
                 anyInt());
     }
 
+    @Test @SmallTest
+    public void testShouldProcessCall_ThrowsExceptionIfServiceIsStillInitializing() {
+        ImsManager imsManager = getImsManagerAndInitProvisionedValues();
+        doReturn(-1).when(mMmTelFeatureConnection).getSubId();
+        assertThrows(ImsException.class, () -> imsManager.shouldProcessCall(true, new String[1]));
+    }
+
+    @Test @SmallTest
+    public void testShouldProcessCall_DoesNotThrowExceptionWhenServiceInitialized()
+        throws Exception {
+        ImsManager imsManager = getImsManagerAndInitProvisionedValues();
+        int ret = imsManager.shouldProcessCall(true, new String[1]);
+        assertEquals(MmTelFeature.PROCESS_CALL_IMS, ret);
+    }
+
     /**
      * Tests the operation of setWfcRoamingSetting and ensures that the user setting for WFC roaming
      * and the ImsConfig setting are both called properly.