Merge "Fix call drop when cross SIM calling is enabled."
diff --git a/src/com/android/services/telephony/domainselection/NormalCallDomainSelector.java b/src/com/android/services/telephony/domainselection/NormalCallDomainSelector.java
index 82057d3..146874c 100644
--- a/src/com/android/services/telephony/domainselection/NormalCallDomainSelector.java
+++ b/src/com/android/services/telephony/domainselection/NormalCallDomainSelector.java
@@ -215,6 +215,12 @@
         }
     }
 
+    private boolean isOutOfService() {
+        return (mServiceState.getState() == ServiceState.STATE_OUT_OF_SERVICE
+                || mServiceState.getState() == ServiceState.STATE_POWER_OFF
+                || mServiceState.getState() == ServiceState.STATE_EMERGENCY_ONLY);
+    }
+
     private synchronized void selectDomain() {
         if (mStopDomainSelection || mSelectionAttributes == null
                 || mTransportSelectorCallback == null) {
@@ -225,12 +231,6 @@
         if (mServiceState == null) {
             logd("Waiting for ServiceState callback.");
             return;
-        } else if (mServiceState.getState() == ServiceState.STATE_OUT_OF_SERVICE
-                || mServiceState.getState() == ServiceState.STATE_POWER_OFF
-                || mServiceState.getState() == ServiceState.STATE_EMERGENCY_ONLY) {
-            loge("Cannot place call in current ServiceState: " + mServiceState.getState());
-            notifySelectionTerminated(DisconnectCause.OUT_OF_SERVICE);
-            return;
         }
 
         // Check if this is a re-dial scenario
@@ -240,8 +240,13 @@
             logd("PsDisconnectCause:" + imsReasonInfo.mCode);
             mReselectDomain = false;
             if (imsReasonInfo.mCode == ImsReasonInfo.CODE_LOCAL_CALL_CS_RETRY_REQUIRED) {
-                logd("Redialing over CS");
-                notifyCsSelected();
+                if (isOutOfService()) {
+                    loge("Cannot place call in current ServiceState: " + mServiceState.getState());
+                    notifySelectionTerminated(DisconnectCause.OUT_OF_SERVICE);
+                } else {
+                    logd("Redialing over CS");
+                    notifyCsSelected();
+                }
                 return;
             } else {
                 logd("Redialing cancelled.");
@@ -269,7 +274,12 @@
 
             if (!mImsStateTracker.isImsRegistered()) {
                 logd("IMS is NOT registered");
-                notifyCsSelected();
+                if (isOutOfService()) {
+                    loge("Cannot place call in current ServiceState: " + mServiceState.getState());
+                    notifySelectionTerminated(DisconnectCause.OUT_OF_SERVICE);
+                } else {
+                    notifyCsSelected();
+                }
                 return;
             }
 
@@ -289,11 +299,21 @@
             } else {
                 logd("IMS is not voice capable");
                 // Voice call CS fallback
-                notifyCsSelected();
+                if (isOutOfService()) {
+                    loge("Cannot place call in current ServiceState: " + mServiceState.getState());
+                    notifySelectionTerminated(DisconnectCause.OUT_OF_SERVICE);
+                } else {
+                    notifyCsSelected();
+                }
             }
         } else {
             logd("IMS is not registered or unavailable");
-            notifyCsSelected();
+            if (isOutOfService()) {
+                loge("Cannot place call in current ServiceState: " + mServiceState.getState());
+                notifySelectionTerminated(DisconnectCause.OUT_OF_SERVICE);
+            } else {
+                notifyCsSelected();
+            }
         }
     }
 }
diff --git a/tests/src/com/android/services/telephony/domainselection/NormalCallDomainSelectorTest.java b/tests/src/com/android/services/telephony/domainselection/NormalCallDomainSelectorTest.java
index f120c4e..890ca34 100644
--- a/tests/src/com/android/services/telephony/domainselection/NormalCallDomainSelectorTest.java
+++ b/tests/src/com/android/services/telephony/domainselection/NormalCallDomainSelectorTest.java
@@ -238,10 +238,10 @@
                         .setVideoCall(true)
                         .setExitedFromAirplaneMode(false)
                         .build();
-        mNormalCallDomainSelector.selectDomain(attributes, transportSelectorCallback);
         ServiceState serviceState = new ServiceState();
         serviceState.setStateOutOfService();
-        mNormalCallDomainSelector.onServiceStateUpdated(serviceState);
+        initialize(serviceState, false, false, false, false);
+        mNormalCallDomainSelector.selectDomain(attributes, transportSelectorCallback);
         assertTrue(transportSelectorCallback
                 .verifyOnSelectionTerminated(DisconnectCause.OUT_OF_SERVICE));
     }
@@ -302,6 +302,12 @@
         assertTrue(transportSelectorCallback.verifyOnWwanSelected());
         assertTrue(transportSelectorCallback
                 .verifyOnDomainSelected(NetworkRegistrationInfo.DOMAIN_CS));
+
+        //Case 5: Backup calling
+        serviceState.setStateOutOfService();
+        initialize(serviceState, true, true, true, true);
+        mNormalCallDomainSelector.selectDomain(attributes, transportSelectorCallback);
+        assertTrue(transportSelectorCallback.verifyOnWlanSelected());
     }
 
     static class MockTransportSelectorCallback implements TransportSelectorCallback,