Merge "Attempt to unhold remaining holding call on disconnect (1/2)" into lmp-dev
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 4775ff5..08dcbea 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -34,6 +34,7 @@
 import com.google.common.collect.ImmutableList;
 
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
@@ -114,6 +115,7 @@
     private final Context mContext;
     private final PhoneAccountRegistrar mPhoneAccountRegistrar;
     private final MissedCallNotifier mMissedCallNotifier;
+    private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
 
     /**
      * The call the user is currently interacting with. This is the call that should have audio
@@ -571,6 +573,7 @@
         if (!mCalls.contains(call)) {
             Log.w(this, "Unknown call (%s) asked to disconnect", call);
         } else {
+            mLocallyDisconnectingCalls.add(call);
             call.disconnect();
         }
     }
@@ -709,7 +712,6 @@
     void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
         call.setDisconnectCause(disconnectCause);
         setCallState(call, CallState.DISCONNECTED);
-        removeCall(call);
     }
 
     /**
@@ -717,6 +719,12 @@
      */
     void markCallAsRemoved(Call call) {
         removeCall(call);
+        if (mLocallyDisconnectingCalls.contains(call)) {
+            mLocallyDisconnectingCalls.remove(call);
+            if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
+                mForegroundCall.unhold();
+            }
+        }
     }
 
     /**
@@ -1009,9 +1017,7 @@
             Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
             Call oldForegroundCall = mForegroundCall;
             mForegroundCall = newForegroundCall;
-            if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
-                mForegroundCall.unhold();
-            }
+
             for (CallsManagerListener listener : mListeners) {
                 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
             }