Prevent DISCONNECTED unknown connections. am: fff1c1dc2f
am: 3f0d84ba93

* commit '3f0d84ba9389f7fefaed506b53092ee03d2fcb83':
  Prevent DISCONNECTED unknown connections.
diff --git a/src/com/android/services/telephony/PstnIncomingCallNotifier.java b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
index 21a39c8..5f64f6d 100644
--- a/src/com/android/services/telephony/PstnIncomingCallNotifier.java
+++ b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
@@ -202,6 +202,15 @@
         }
         Connection connection = (Connection) asyncResult.result;
         if (connection != null) {
+            // Because there is a handler between telephony and here, it causes this action to be
+            // asynchronous which means that the call can switch to DISCONNECTED by the time it gets
+            // to this code. Check here to ensure we are not adding a disconnected or IDLE call.
+            Call.State state = connection.getState();
+            if (state == Call.State.DISCONNECTED || state == Call.State.IDLE) {
+                Log.i(this, "Skipping new unknown connection because it is idle. " + connection);
+                return;
+            }
+
             Call call = connection.getCall();
             if (call != null && call.getState().isAlive()) {
                 addNewUnknownCall(connection);
@@ -211,6 +220,7 @@
 
     private void addNewUnknownCall(Connection connection) {
         Log.i(this, "addNewUnknownCall, connection is: %s", connection);
+
         if (!maybeSwapAnyWithUnknownConnection(connection)) {
             Log.i(this, "determined new connection is: %s", connection);
             Bundle extras = null;
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index b700706..fe6e4f4 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -618,7 +618,6 @@
 
         // Set video state and capabilities
         setVideoState(mOriginalConnection.getVideoState());
-        updateState();
         setLocalVideoCapable(mOriginalConnection.isLocalVideoCapable());
         setRemoteVideoCapable(mOriginalConnection.isRemoteVideoCapable());
         setWifi(mOriginalConnection.isWifi());
@@ -630,8 +629,15 @@
         }
         mIsMultiParty = mOriginalConnection.isMultiparty();
 
+        // updateState can set mOriginalConnection to null if its state is DISCONNECTED, so this
+        // should be executed *after* the above setters have run.
+        updateState();
+        if (mOriginalConnection == null) {
+            Log.w(this, "original Connection was nulled out as part of setOriginalConnection. " +
+                    originalConnection);
+        }
+
         fireOnOriginalConnectionConfigured();
-        updateAddress();
     }
 
     /**