Switch to using TelephonyConnection#hangup instead of onDisconnect.

Where there is an ongoing call on one sim and an incoming call is answered
on the other sim, we used TelephonyConnection#onDisconnect to terminate
the ongoing call.  The problem is that onDisconnect posts the operation
to a handler, so in reality the order of operation would be to answer the
incoming call and THEN disconnect the ongoing call.

Using the hangup method does NOT post to a handler and ensures that we
disconnect the ongoing call BEFORE answering the incoming one.

Test: Manual test to verify through log inspection the ordering of
the operations.
Test: Live network test.
Test: Modify unit tests to verify hangup is called.
Bug: 178649879

Change-Id: I499a19dffa2504fc33a6d2528be17746a56580bc
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 64c008a..2383981 100755
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -2097,7 +2097,8 @@
         phone.unregisterForInCallVoicePrivacyOff(mHandler);
     }
 
-    protected void hangup(int telephonyDisconnectCode) {
+    @VisibleForTesting
+    public void hangup(int telephonyDisconnectCode) {
         if (mOriginalConnection != null) {
             mHangupDisconnectCause = telephonyDisconnectCode;
             try {
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 2e82743..2855692 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -2628,7 +2628,10 @@
                         if (!tc.shouldTreatAsEmergencyCall()) {
                             Log.i(LOG_TAG, "maybeDisconnectCallsOnOtherSubs: disconnect %s due to "
                                     + "incoming call on other sub.", tc.getTelecomCallId());
-                            tc.onDisconnect();
+                            // Note: intentionally calling hangup instead of onDisconnect.
+                            // onDisconnect posts the disconnection to a handle which means that the
+                            // disconnection will take place AFTER we answer the incoming call.
+                            tc.hangup(android.telephony.DisconnectCause.LOCAL);
                         }
                     }
                 });
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
index 898b5ad..e109586 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
@@ -96,9 +96,8 @@
             return null;
         }
 
-
         @Override
-        public void onDisconnect() {
+        public void hangup(int telephonyDisconnectCode) {
             wasDisconnected = true;
         }
     }