Don't disable radio from thermal if in emergency call only.

Test: place non-emergency voice call, emulate radio_off thermal mitigation with
https: //docs.google.com/document/d/1mRsIPGdb-zajRhCJm5N1jEKtwZ0yMcQzVST4p0Afj7c/edit?resourcekey=0-AtIFNnN8wNLBkrBYkkWbgg#, observe call drop, undo thermal mitigation, register mock emergency number with https://sites.google.com/a/google.com/android-telecom/testing/test-emergency-call, call mock emergency number, emulate radio_off thermal mitigation with https://docs.google.com/document/d/1mRsIPGdb-zajRhCJm5N1jEKtwZ0yMcQzVST4p0Afj7c/edit?resourcekey=0-AtIFNnN8wNLBkrBYkkWbgg#, observe call is not dropped
Bug: 187848574
Change-Id: I3a92c0d117de4df7f6a853937fb11f6f4e1022af
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index f3a0a26..d2f9c75 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -9798,6 +9798,24 @@
         return sThermalMitigationAllowlistedPackages;
     }
 
+    private boolean isAnyPhoneInEmergencyState() {
+        TelecomManager tm = mApp.getSystemService(TelecomManager.class);
+        if (tm.isInEmergencyCall()) {
+            Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
+            return true;
+        }
+        for (Phone phone : PhoneFactory.getPhones()) {
+            if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
+                Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
+                    + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
+                    + phone.isInEcm());
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Used by shell commands to add an authorized package name for thermal mitigation.
      * @param packageName name of package to be allowlisted
@@ -9880,8 +9898,6 @@
 
                     TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
                     if (registry != null) {
-                        TelephonyConnectionService service =
-                                registry.getTelephonyConnectionService();
                         Phone phone = getPhone(subId);
                         if (phone == null) {
                             thermalMitigationResult =
@@ -9889,19 +9905,20 @@
                             break;
                         }
 
-                        if (PhoneConstantConversions.convertCallState(phone.getState())
-                                    != TelephonyManager.CALL_STATE_IDLE
-                                    || phone.isInEmergencySmsMode() || phone.isInEcm()
-                                    || (service != null && service.isEmergencyCallPending())) {
-                            String errorMessage = "Phone state is not valid. call state = "
-                                    + PhoneConstantConversions.convertCallState(phone.getState())
-                                    + " isInEmergencySmsMode = " + phone.isInEmergencySmsMode()
-                                    + " isInEmergencyCallbackMode = " + phone.isInEcm();
-                            errorMessage += service == null
-                                    ? " TelephonyConnectionService is null"
-                                    : " isEmergencyCallPending = "
-                                            + service.isEmergencyCallPending();
-                            Log.e(LOG_TAG, errorMessage);
+                        TelephonyConnectionService service =
+                                registry.getTelephonyConnectionService();
+                        if (service == null) {
+                            Log.e(LOG_TAG, "TelephonyConnectionService is null");
+                            thermalMitigationResult =
+                                    TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
+                            break;
+
+                        } else if (service.isEmergencyCallPending()) {
+                            Log.e(LOG_TAG, "An emergency call is pending");
+                            thermalMitigationResult =
+                                    TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
+                            break;
+                        } else if (isAnyPhoneInEmergencyState()) {
                             thermalMitigationResult =
                                 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
                             break;