Merge "Reject e911 call when connected to oem enabled satellite." into 24D1-dev
diff --git a/res/values/config.xml b/res/values/config.xml
index 1441cd1..575e766 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -370,4 +370,7 @@
          service. The device should fallback to the modem based domain selection architecture
          if this is not configured. -->
     <string name="config_domain_selection_service_component_name" translatable="false"></string>
+
+    <!-- Whether to turn off OEM-enabled satellite during emergency call -->
+    <bool name="config_turn_off_oem_enabled_satellite_during_emergency_call">false</bool>
 </resources>
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 3a05c6d..cf3b354 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -33,6 +33,7 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.res.Resources;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.ParcelUuid;
@@ -152,6 +153,10 @@
     private static final String DISCONNECT_REASON_CARRIER_ROAMING_SATELLITE_MODE =
             "CARRIER_ROAMING_SATELLITE_MODE";
 
+    private static final String KEY_TURN_OFF_OEM_ENABLED_SATELLITE_DURING_EMERGENCY_CALL =
+            "config_turn_off_oem_enabled_satellite_during_emergency_call";
+
+
     private final TelephonyConnectionServiceProxy mTelephonyConnectionServiceProxy =
             new TelephonyConnectionServiceProxy() {
         @Override
@@ -1163,6 +1168,23 @@
         boolean needToTurnOnRadio = (isEmergencyNumber && (!isRadioOn() || isAirplaneModeOn))
                 || (isRadioPowerDownOnBluetooth() && !isPhoneWifiCallingEnabled);
 
+        if (mSatelliteController.isSatelliteEnabled()) {
+            Log.d(this, "onCreateOutgoingConnection, "
+                    + " needToTurnOnRadio=" + needToTurnOnRadio
+                    + " needToTurnOffSatellite=" + needToTurnOffSatellite
+                    + " isEmergencyNumber=" + isEmergencyNumber);
+
+            if (!needToTurnOffSatellite) {
+                // Block outgoing call and do not turn off satellite
+                Log.d(this, "onCreateOutgoingConnection, "
+                        + "cannot make call in satellite mode.");
+                return Connection.createFailedConnection(
+                        mDisconnectCauseFactory.toTelecomDisconnectCause(
+                                android.telephony.DisconnectCause.SATELLITE_ENABLED,
+                                DISCONNECT_REASON_SATELLITE_ENABLED));
+            }
+        }
+
         if (mDomainSelectionResolver.isDomainSelectionSupported()) {
             // Normal routing emergency number shall be handled by normal call domain selctor.
             if (isEmergencyNumber && !isNormalRouting(phone, number)) {
@@ -1270,14 +1292,7 @@
             }
 
             if (!isEmergencyNumber) {
-                if (mSatelliteController.isSatelliteEnabled()) {
-                    Log.d(this, "onCreateOutgoingConnection, cannot make call in "
-                            + "satellite mode.");
-                    return Connection.createFailedConnection(
-                            mDisconnectCauseFactory.toTelecomDisconnectCause(
-                                    android.telephony.DisconnectCause.SATELLITE_ENABLED,
-                                    DISCONNECT_REASON_SATELLITE_ENABLED));
-                } else if (isCallDisallowedDueToSatellite(phone)
+                if (isCallDisallowedDueToSatellite(phone)
                         && (imsPhone == null || !imsPhone.canMakeWifiCall())) {
                     Log.d(this, "onCreateOutgoingConnection, cannot make call "
                             + "when device is connected to carrier roaming satellite network");
@@ -2102,11 +2117,20 @@
     }
 
     private boolean isSatelliteBlockingCall(boolean isEmergencyNumber) {
-        if (isEmergencyNumber) {
-            return mSatelliteController.isSatelliteEnabled();
-        } else {
-            return mSatelliteController.isDemoModeEnabled();
+        if (!mSatelliteController.isSatelliteEnabled()) {
+            return false;
         }
+
+        if (isEmergencyNumber) {
+            if (mSatelliteController.isDemoModeEnabled()) {
+                // If user makes emergency call in demo mode, end the satellite session
+                return true;
+            } else {
+                return getTurnOffOemEnabledSatelliteDuringEmergencyCall();
+            }
+        }
+
+        return false;
     }
 
     private Pair<WeakReference<TelephonyConnection>, Queue<Phone>> makeCachedConnectionPhonePair(
@@ -4678,4 +4702,15 @@
         // Call is disallowed while using satellite
         return true;
     }
+
+    private boolean getTurnOffOemEnabledSatelliteDuringEmergencyCall() {
+        boolean turnOffSatellite = false;
+        try {
+            turnOffSatellite = getApplicationContext().getResources().getBoolean(
+                    R.bool.config_turn_off_oem_enabled_satellite_during_emergency_call);
+        } catch (Resources.NotFoundException ex) {
+            Log.e(this, ex, "getTurnOffOemEnabledSatelliteDuringEmergencyCall: ex=" + ex);
+        }
+        return turnOffSatellite;
+    }
 }
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
index 3470d4a..0b252c3 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
@@ -56,6 +56,7 @@
 
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.res.Resources;
 import android.net.Uri;
 import android.os.AsyncResult;
 import android.os.Bundle;
@@ -252,6 +253,7 @@
     @Mock ImsPhone mImsPhone;
     @Mock private SatelliteSOSMessageRecommender mSatelliteSOSMessageRecommender;
     @Mock private EmergencyStateTracker mEmergencyStateTracker;
+    @Mock private Resources mMockResources;
     private Phone mPhone0;
     private Phone mPhone1;
 
@@ -320,6 +322,8 @@
                 any(), anyInt(), anyBoolean());
         replaceInstance(TelephonyConnectionService.class,
                 "mSatelliteController", mTestConnectionService, mSatelliteController);
+        doReturn(mMockResources).when(mContext).getResources();
+
         mBinderStub = (IConnectionService.Stub) mTestConnectionService.onBind(null);
         mSetFlagsRule.disableFlags(Flags.FLAG_CARRIER_ENABLED_SATELLITE_FLAG);
         mSetFlagsRule.enableFlags(Flags.FLAG_DO_NOT_OVERRIDE_PRECISE_LABEL);
@@ -1431,6 +1435,9 @@
     @SmallTest
     public void testCreateOutgoingEmergencyConnection_exitingSatellite_placeCall() {
         when(mSatelliteController.isSatelliteEnabled()).thenReturn(true);
+        doReturn(true).when(mMockResources).getBoolean(anyInt());
+        doReturn(true).when(mTelephonyManagerProxy).isCurrentEmergencyNumber(
+                anyString());
         Phone testPhone = setupConnectionServiceInApm();
 
         ArgumentCaptor<RadioOnStateListener.Callback> callback =
@@ -3602,6 +3609,24 @@
     }
 
     @Test
+    public void testEmergencyCallSatelliteEnabled_blockEmergencyCall() {
+        setupForCallTest();
+        doReturn(true).when(mSatelliteController).isSatelliteEnabled();
+        doReturn(false).when(mMockResources).getBoolean(anyInt());
+        doReturn(true).when(mTelephonyManagerProxy).isCurrentEmergencyNumber(
+                anyString());
+
+        // Simulates an outgoing emergency call.
+        mConnection = mTestConnectionService.onCreateOutgoingConnection(PHONE_ACCOUNT_HANDLE_1,
+                createConnectionRequest(PHONE_ACCOUNT_HANDLE_1,
+                        TEST_EMERGENCY_NUMBER, TELECOM_CALL_ID1));
+        DisconnectCause disconnectCause = mConnection.getDisconnectCause();
+        assertEquals(android.telephony.DisconnectCause.SATELLITE_ENABLED,
+                disconnectCause.getTelephonyDisconnectCause());
+        assertEquals(DISCONNECT_REASON_SATELLITE_ENABLED, disconnectCause.getReason());
+    }
+
+    @Test
     public void testNormalCallUsingNonTerrestrialNetwork_enableFlag() throws Exception {
         mSetFlagsRule.enableFlags(Flags.FLAG_CARRIER_ENABLED_SATELLITE_FLAG);