Merge cherrypicks of [8655472, 8655254, 8655915, 8655916, 8655554, 8655935, 8655917, 8655800, 8655789, 8655473, 8655918, 8655474, 8655408, 8655351] into qt-release

Change-Id: I45448ce3163f897e815d61fb43ce62367f955129
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 80ba5b1..6637e19 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -56,6 +56,7 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.PhoneSwitcher;
 import com.android.internal.telephony.RIL;
+import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.imsphone.ImsExternalCallTracker;
 import com.android.internal.telephony.imsphone.ImsPhone;
 import com.android.internal.telephony.imsphone.ImsPhoneConnection;
@@ -637,8 +638,11 @@
             // Notify Telecom of the new Connection type.
             // TODO: Switch out the underlying connection instead of creating a new
             // one and causing UI Jank.
+            boolean noActiveSimCard = SubscriptionController.getInstance()
+                    .getActiveSubInfoCount(phone.getContext().getOpPackageName()) == 0;
+            // If there's no active sim card and the device is in emergency mode, use E account.
             addExistingConnection(PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
-                    phone, "", isEmergencyNumber), repConnection);
+                    phone, "", isEmergencyNumber && noActiveSimCard), repConnection);
             // Remove the old connection from Telecom after.
             connectionToEvaluate.setDisconnected(
                     DisconnectCauseUtil.toTelecomDisconnectCause(