Merge "Allow emergency calling in airplane mode without a SIM." into lmp-dev
diff --git a/src/com/android/services/telephony/EmergencyCallHelper.java b/src/com/android/services/telephony/EmergencyCallHelper.java
index 5b3ebfc..c64a649 100644
--- a/src/com/android/services/telephony/EmergencyCallHelper.java
+++ b/src/com/android/services/telephony/EmergencyCallHelper.java
@@ -46,7 +46,7 @@
     }
 
     // Number of times to retry the call, and time between retry attempts.
-    public static final int MAX_NUM_RETRIES = 6;
+    public static final int MAX_NUM_RETRIES = 5;
     public static final long TIME_BETWEEN_RETRIES_MILLIS = 5000;  // msec
 
     // Handler message codes; see handleMessage()
@@ -180,9 +180,13 @@
     private boolean isOkToCall(int serviceState, PhoneConstants.State phoneState) {
         // Once we reach either STATE_IN_SERVICE or STATE_EMERGENCY_ONLY, it's finally OK to place
         // the emergency call.
-        return (phoneState == PhoneConstants.State.OFFHOOK)
+        return ((phoneState == PhoneConstants.State.OFFHOOK)
                 || (serviceState == ServiceState.STATE_IN_SERVICE)
-                || (serviceState == ServiceState.STATE_EMERGENCY_ONLY);
+                || (serviceState == ServiceState.STATE_EMERGENCY_ONLY)) ||
+
+                // Allow STATE_OUT_OF_SERVICE if we are at the max number of retries.
+                (mNumRetriesSoFar == MAX_NUM_RETRIES &&
+                 serviceState == ServiceState.STATE_OUT_OF_SERVICE);
     }
 
     /**
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 0ffb7c7..adb430c 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -361,14 +361,14 @@
         if (mOriginalConnection != null) {
             try {
                 mOriginalConnection.hangup();
-
-                // Set state deliberately since we are going to close() and will no longer be
-                // listening to state updates from mOriginalConnection
-                setDisconnected(disconnectCause, null);
             } catch (CallStateException e) {
                 Log.e(this, e, "Call to Connection.hangup failed with exception");
             }
         }
+
+        // Set state deliberately since we are going to close() and will no longer be
+        // listening to state updates from mOriginalConnection
+        setDisconnected(disconnectCause, null);
         close();
     }
 
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index dbabaa2..7c4d1ae 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -137,13 +137,16 @@
                     new EmergencyCallHelper.Callback() {
                         @Override
                         public void onComplete(boolean isRadioReady) {
-                            if (isRadioReady) {
+                            if (connection.getState() == Connection.STATE_DISCONNECTED) {
+                                // If the connection has already been disconnected, do nothing.
+                            } else if (isRadioReady) {
                                 connection.setInitialized();
                                 placeOutgoingConnection(connection, phone, request);
                             } else {
                                 Log.d(this, "onCreateOutgoingConnection, failed to turn on radio");
                                 connection.setDisconnected(DisconnectCause.POWER_OFF,
                                         "Failed to turn on radio.");
+                                connection.destroy();
                             }
                         }
                     });