Merge "Exempt Dialer from power restrictions temporarily after missing a call"
diff --git a/res/xml/layout_blocked_number.xml b/res/xml/layout_blocked_number.xml
index 3cdd771..9dbfc2f 100644
--- a/res/xml/layout_blocked_number.xml
+++ b/res/xml/layout_blocked_number.xml
@@ -28,9 +28,8 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentStart="true"
+        android:layout_toStartOf="@+id/delete_blocked_number"
         android:paddingTop="@dimen/blocked_numbers_delete_icon_padding"
-        android:layout_alignParentLeft="true"
-        android:layout_toLeftOf="@+id/delete_blocked_number"
         android:textDirection="ltr" />
 
     <ImageView
diff --git a/src/com/android/server/telecom/CreateConnectionProcessor.java b/src/com/android/server/telecom/CreateConnectionProcessor.java
index 2221cb9..2e67b08 100644
--- a/src/com/android/server/telecom/CreateConnectionProcessor.java
+++ b/src/com/android/server/telecom/CreateConnectionProcessor.java
@@ -403,6 +403,7 @@
             // When testing emergency calls, we want the calls to go through to the test connection
             // service, not the telephony ConnectionService.
             if (mCall.isTestEmergencyCall()) {
+                Log.i(this, "Processing test emergency call -- special rules");
                 allAccounts = mPhoneAccountRegistrar.filterRestrictedPhoneAccounts(allAccounts);
             }
 
@@ -411,7 +412,7 @@
                     preferredPAH);
             // Next, add all SIM phone accounts which can place emergency calls.
             sortSimPhoneAccountsForEmergency(allAccounts, preferredPA);
-            // and pick the fist one that can place emergency calls.
+            // and pick the first one that can place emergency calls.
             for (PhoneAccount phoneAccount : allAccounts) {
                 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS)
                         && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index e9ddfa0..9a4f7df 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -1266,11 +1266,11 @@
             // Comparator which places PhoneAccounts with a specified sort order first, followed by
             // those with no sort order.
             Comparator<PhoneAccount> bySortOrder = (p1, p2) -> {
-                String sort1 = p1.getExtras() == null ? null :
-                        p1.getExtras().getString(PhoneAccount.EXTRA_SORT_ORDER, null);
-                String sort2 = p2.getExtras() == null ? null :
-                        p2.getExtras().getString(PhoneAccount.EXTRA_SORT_ORDER, null);
-                return nullSafeStringComparator.compare(sort1, sort2);
+                int sort1 = p1.getExtras() == null ? Integer.MAX_VALUE:
+                        p1.getExtras().getInt(PhoneAccount.EXTRA_SORT_ORDER, Integer.MAX_VALUE);
+                int sort2 = p2.getExtras() == null ? Integer.MAX_VALUE:
+                        p2.getExtras().getInt(PhoneAccount.EXTRA_SORT_ORDER, Integer.MAX_VALUE);
+                return Integer.compare(sort1, sort2);
             };
 
             // Comparator which sorts PhoneAccounts by label.
diff --git a/src/com/android/server/telecom/PhoneStateBroadcaster.java b/src/com/android/server/telecom/PhoneStateBroadcaster.java
index f2531ed..f02f7e8 100644
--- a/src/com/android/server/telecom/PhoneStateBroadcaster.java
+++ b/src/com/android/server/telecom/PhoneStateBroadcaster.java
@@ -17,8 +17,16 @@
 package com.android.server.telecom;
 
 import android.telecom.Log;
+import android.telephony.PhoneNumberUtils;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.telephony.TelephonyRegistryManager;
+import android.telephony.emergency.EmergencyNumber;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
 
 /**
  * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
@@ -52,6 +60,10 @@
             return;
         }
         updateStates(call);
+
+        if (call.isEmergencyCall() && !call.isIncoming()) {
+            sendOutgoingEmergencyCallEvent(call);
+        }
     }
 
     @Override
@@ -113,4 +125,31 @@
             Log.i(this, "Broadcasted state change: %s", mCurrentState);
         }
     }
+
+    private void sendOutgoingEmergencyCallEvent(Call call) {
+        TelephonyManager tm = mCallsManager.getContext().getSystemService(TelephonyManager.class);
+        String strippedNumber =
+                PhoneNumberUtils.stripSeparators(call.getHandle().getSchemeSpecificPart());
+        Optional<EmergencyNumber> emergencyNumber = tm.getEmergencyNumberList().values().stream()
+                .flatMap(List::stream)
+                .filter(numberObj -> Objects.equals(numberObj.getNumber(), strippedNumber))
+                .findFirst();
+
+        int subscriptionId = tm.getSubscriptionId(call.getTargetPhoneAccount());
+        SubscriptionManager subscriptionManager =
+                mCallsManager.getContext().getSystemService(SubscriptionManager.class);
+        int simSlotIndex = SubscriptionManager.DEFAULT_PHONE_INDEX;
+        if (subscriptionManager != null) {
+            SubscriptionInfo subInfo =
+                    subscriptionManager.getActiveSubscriptionInfo(subscriptionId);
+            if (subInfo != null) {
+                simSlotIndex = subInfo.getSimSlotIndex();
+            }
+        }
+
+        if (emergencyNumber.isPresent()) {
+            mRegistry.notifyOutgoingEmergencyCall(
+                    simSlotIndex, subscriptionId, emergencyNumber.get());
+        }
+    }
 }
diff --git a/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java b/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java
index 00ef87c..a56036a 100644
--- a/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java
+++ b/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java
@@ -725,16 +725,20 @@
         mComponentContextFixture.addConnectionService(componentC,
                 Mockito.mock(IConnectionService.class));
 
+        Bundle account1Extras = new Bundle();
+        account1Extras.putInt(PhoneAccount.EXTRA_SORT_ORDER, 1);
         PhoneAccount account1 = new PhoneAccount.Builder(
                 makeQuickAccountHandle(componentA, "c"), "c")
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
-                .setExtras(Bundle.forPair(PhoneAccount.EXTRA_SORT_ORDER, "A"))
+                .setExtras(account1Extras)
                 .build();
 
+        Bundle account2Extras = new Bundle();
+        account2Extras.putInt(PhoneAccount.EXTRA_SORT_ORDER, 2);
         PhoneAccount account2 = new PhoneAccount.Builder(
                 makeQuickAccountHandle(componentB, "b"), "b")
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
-                .setExtras(Bundle.forPair(PhoneAccount.EXTRA_SORT_ORDER, "B"))
+                .setExtras(account2Extras)
                 .build();
 
         PhoneAccount account3 = new PhoneAccount.Builder(
@@ -814,18 +818,23 @@
                 Mockito.mock(IConnectionService.class));
         mComponentContextFixture.addConnectionService(componentZ,
                 Mockito.mock(IConnectionService.class));
+
+        Bundle account1Extras = new Bundle();
+        account1Extras.putInt(PhoneAccount.EXTRA_SORT_ORDER, 2);
         PhoneAccount account1 = new PhoneAccount.Builder(makeQuickAccountHandle(
                 makeQuickConnectionServiceComponentName(), "y"), "y")
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
                         PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
-                .setExtras(Bundle.forPair(PhoneAccount.EXTRA_SORT_ORDER, "2"))
+                .setExtras(account1Extras)
                 .build();
 
+        Bundle account2Extras = new Bundle();
+        account2Extras.putInt(PhoneAccount.EXTRA_SORT_ORDER, 1);
         PhoneAccount account2 = new PhoneAccount.Builder(makeQuickAccountHandle(
                 makeQuickConnectionServiceComponentName(), "z"), "z")
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
                         PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
-                .setExtras(Bundle.forPair(PhoneAccount.EXTRA_SORT_ORDER, "1"))
+                .setExtras(account2Extras)
                 .build();
 
         PhoneAccount account3 = new PhoneAccount.Builder(makeQuickAccountHandle(