Prevent DISCONNECTED unknown connections.

1) Add an early return to disallow disconnected unknown connections.
2) Move code in run updateState() after uses of mOriginalConnection
   since it is possible for updateState() to set it to null.
3) Add logging to the bug so that we can recognize when and how this
   situation happens in the wild.

Bug: 24469962
Change-Id: Ic7c986bff98ee65677a4a2cb06beff54d79e5ab6
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();
     }
 
     /**