Refuse outgoing call if max outgoing exceeded.

This bug was happening because multiple calls in the CONNECTING state
were being added. Now, if there's an attempt to place multiple calls,
ignore when the maximum number of outgoing calls is exceeded.

Exception: if the outgoing call is an emergency call, cancel the
current outgoing call if the current outgoing call is not an
emergency call itself.

Bug: 17610917
Change-Id: I0ae041fa3a09a2fb9508f2342894e77b87001b43
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 706ab7b..ed555b7 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -19,7 +19,6 @@
 import android.content.Context;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.Parcelable;
 import android.provider.CallLog.Calls;
 import android.telecom.AudioState;
 import android.telecom.CallState;
@@ -30,12 +29,10 @@
 import android.telecom.PhoneCapabilities;
 import android.telephony.TelephonyManager;
 
-import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.IndentingPrintWriter;
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableList;
 
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
@@ -1028,6 +1025,20 @@
             // have to change.
             Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
 
+            if (hasMaximumOutgoingCalls()) {
+                // Disconnect the current outgoing call if it's not an emergency call. If the user
+                // tries to make two outgoing calls to different emergency call numbers, we will try
+                // to connect the first outgoing call.
+                if (isEmergency) {
+                    Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
+                    if (!outgoingCall.isEmergencyCall()) {
+                        outgoingCall.disconnect();
+                        return true;
+                    }
+                }
+                return false;
+            }
+
             if (hasMaximumHoldingCalls()) {
                 // There is no more room for any more calls, unless it's an emergency.
                 if (isEmergency) {