DO NOT MERGE Inform registrants of which connection started or stopped the hold tone.

When receiving a hold tone signal and informing registrants via
startOnHoldTone/stopOnHoldTone, also include the connection which initiated
the start/stop. This is necessray to ensure only the TelephonyConnection
which initiated a hold tone issues a Connection Event.

Bug: 25357778
Change-Id: I1590b61affb28c40173f08a8f6233d8efe6a5e6e
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java
index e8370b8..8cea32a 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java
@@ -30,6 +30,7 @@
 import android.telephony.ServiceState;
 import android.telephony.SignalStrength;
 import android.telephony.Rlog;
+import android.util.Pair;
 
 import com.android.internal.telephony.Call;
 import com.android.internal.telephony.CallStateException;
@@ -102,14 +103,24 @@
         mOnHoldRegistrants.remove(h);
     }
 
-    protected void startOnHoldTone() {
-        AsyncResult result = new AsyncResult(null, Boolean.TRUE, null);
-        mOnHoldRegistrants.notifyRegistrants(result);
+    /**
+     * Signals all registrants that the remote hold tone should be started for a connection.
+     *
+     * @param cn The connection.
+     */
+    protected void startOnHoldTone(Connection cn) {
+        Pair<Connection, Boolean> result = new Pair<Connection, Boolean>(cn, Boolean.TRUE);
+        mOnHoldRegistrants.notifyRegistrants(new AsyncResult(null, result, null));
     }
 
-    protected void stopOnHoldTone() {
-        AsyncResult result = new AsyncResult(null, Boolean.FALSE, null);
-        mOnHoldRegistrants.notifyRegistrants(result);
+    /**
+     * Signals all registrants that the remote hold tone should be stopped for a connection.
+     *
+     * @param cn The connection.
+     */
+    protected void stopOnHoldTone(Connection cn) {
+        Pair<Connection, Boolean> result = new Pair<Connection, Boolean>(cn, Boolean.FALSE);
+        mOnHoldRegistrants.notifyRegistrants(new AsyncResult(null, result, null));
     }
 
     @Override
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java
index 29f1926..70d228f 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java
@@ -184,6 +184,7 @@
 
     private boolean mDesiredMute = false;    // false = mute off
     private boolean mOnHoldToneStarted = false;
+    private int mOnHoldToneId = -1;
 
     PhoneConstants.State mState = PhoneConstants.State.IDLE;
 
@@ -1163,6 +1164,13 @@
             ImsPhoneConnection conn = findConnection(imsCall);
             if (DBG) log("cause = " + cause + " conn = " + conn);
 
+            if (mOnHoldToneId == System.identityHashCode(conn)) {
+                if (conn != null && mOnHoldToneStarted) {
+                    mPhone.stopOnHoldTone(conn);
+                }
+                mOnHoldToneStarted = false;
+                mOnHoldToneId = -1;
+            }
             if (conn != null && conn.isIncoming() && conn.getConnectTime() == 0) {
                 // Missed
                 if (cause == DisconnectCause.NORMAL) {
@@ -1297,9 +1305,9 @@
         @Override
         public void onCallResumeReceived(ImsCall imsCall) {
             if (DBG) log("onCallResumeReceived");
-
-            if (mOnHoldToneStarted) {
-                mPhone.stopOnHoldTone();
+            ImsPhoneConnection conn = findConnection(imsCall);
+            if (conn != null && mOnHoldToneStarted) {
+                mPhone.stopOnHoldTone(conn);
                 mOnHoldToneStarted = false;
             }
 
@@ -1318,8 +1326,9 @@
             ImsPhoneConnection conn = findConnection(imsCall);
             if (conn != null && conn.getState() == ImsPhoneCall.State.ACTIVE) {
                 if (!mOnHoldToneStarted && ImsPhoneCall.isLocalTone(imsCall)) {
-                    mPhone.startOnHoldTone();
+                    mPhone.startOnHoldTone(conn);
                     mOnHoldToneStarted = true;
+                    mOnHoldToneId = System.identityHashCode(conn);
                 }
             }