Make call back order right for DISCONNECTED call

If call is initially a DISCONNECTED call, the callback will be fire in wrong sequence where onCallRemoved is fired before onCallAdded.
Bug: 203563157
Test: manual test


Change-Id: I4b13dc13b34a1547f0c68ced01b3c1177dfa9eb3
(cherry picked from commit 5495c83fb5dbf833082ce4c9c21c415580420a7f)
(cherry picked from commit fb9aec0c1175618759f5d7328fc42ec3c152cbe2)
diff --git a/telecomm/java/android/telecom/Call.java b/telecomm/java/android/telecom/Call.java
index 78b86d3..5bdcdf4 100644
--- a/telecomm/java/android/telecom/Call.java
+++ b/telecomm/java/android/telecom/Call.java
@@ -2661,7 +2661,9 @@
         // remove ourselves from the Phone. Note that we do this after completing all state updates
         // so a client can cleanly transition all their UI to the state appropriate for a
         // DISCONNECTED Call while still relying on the existence of that Call in the Phone's list.
-        if (mState == STATE_DISCONNECTED) {
+        // Check if the original state is already disconnected, otherwise onCallRemoved will be
+        // triggered before onCallAdded.
+        if (mState == STATE_DISCONNECTED && stateChanged) {
             fireCallDestroyed();
         }
     }
diff --git a/telecomm/java/android/telecom/Phone.java b/telecomm/java/android/telecom/Phone.java
index 95a8e16..61e829e7 100644
--- a/telecomm/java/android/telecom/Phone.java
+++ b/telecomm/java/android/telecom/Phone.java
@@ -174,6 +174,9 @@
             checkCallTree(parcelableCall);
             call.internalUpdate(parcelableCall, mCallByTelecomCallId);
             fireCallAdded(call);
+            if (call.getState() == Call.STATE_DISCONNECTED) {
+                internalRemoveCall(call);
+            }
         } else {
             Log.w(this, "Call %s added, but it was already present", call.internalGetCallId());
             checkCallTree(parcelableCall);