DO NOT MERGE Double-check SIP state before issuing a swap-calls command.

Our code relied on the state of SipConnection to prevent us from holding
a HELD call and unholding an ACTIVE call.  However, while the action is
in progress, the SipConnection state can be slightly delayed so we
double-check the internal state of the lower connection prior to issuing
a switchHoldingAndActive command.

Bug: 24007856
Change-Id: I617ef215e66f6b9324dc17cb1b9832cc35390f17
diff --git a/sip/src/com/android/services/telephony/sip/SipConnection.java b/sip/src/com/android/services/telephony/sip/SipConnection.java
index e9f8e05..173bc99 100644
--- a/sip/src/com/android/services/telephony/sip/SipConnection.java
+++ b/sip/src/com/android/services/telephony/sip/SipConnection.java
@@ -134,7 +134,14 @@
         try {
             if (getPhone() != null && getState() == STATE_ACTIVE
                     && getPhone().getRingingCall().getState() != Call.State.WAITING) {
-                getPhone().switchHoldingAndActive();
+                // Double check with the internal state since a discrepancy in states could mean
+                // that the transactions is already in progress from a previous request.
+                if (mOriginalConnection != null &&
+                        mOriginalConnection.getState() == Call.State.ACTIVE) {
+                    getPhone().switchHoldingAndActive();
+                } else {
+                    log("skipping switch from onHold due to internal state:");
+                }
             }
         } catch (CallStateException e) {
             log("onHold, exception: " + e);
@@ -146,7 +153,14 @@
         if (VERBOSE) log("onUnhold");
         try {
             if (getPhone() != null && getState() == STATE_HOLDING) {
-                getPhone().switchHoldingAndActive();
+                // Double check with the internal state since a discrepancy in states could mean
+                // that the transaction is already in progress from a previous request.
+                if (mOriginalConnection != null &&
+                        mOriginalConnection.getState() == Call.State.HOLDING) {
+                    getPhone().switchHoldingAndActive();
+                } else {
+                    log("skipping switch from onUnHold due to internal state.");
+                }
             }
         } catch (CallStateException e) {
             log("onUnhold, exception: " + e);