Move onRttInitiated callback onto TelephonyConnection handler.

In some cases it is possible for onRttInitiated to come in while a call
is being added to Telecom.  The call is added to Telecom on the main thread
handler, but the rtt initiated update comes in via a binder thread from
the IMS service.   As a consequence the RTT status is sent to Telecom and
THEN the new connection and its state is reported to Telecom.  This
overwrites the new rtt state previously reported.

Test: Manual regression testing for RTT initiation on live network.
Fixes: 234650328
Change-Id: I508a6960f7ace160ea787b2ea2c21a8d94d262e2
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 962053c..31c589e 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -142,6 +142,7 @@
     private static final int MSG_REJECT = 21;
     private static final int MSG_DTMF_DONE = 22;
     private static final int MSG_MEDIA_ATTRIBUTES_CHANGED = 23;
+    private static final int MSG_ON_RTT_INITIATED = 24;
 
     private static final String JAPAN_COUNTRY_CODE_WITH_PLUS_SIGN = "+81";
     private static final String JAPAN_ISO_COUNTRY_CODE = "JP";
@@ -329,6 +330,15 @@
                         args.recycle();
                     }
                     break;
+                case MSG_ON_RTT_INITIATED:
+                    if (mOriginalConnection != null) {
+                        // if mOriginalConnection is null, the properties will get set when
+                        // mOriginalConnection gets set.
+                        updateConnectionProperties();
+                        refreshConferenceSupported();
+                    }
+                    sendRttInitiationSuccess();
+                    break;
             }
         }
     };
@@ -750,13 +760,11 @@
 
         @Override
         public void onRttInitiated() {
-            if (mOriginalConnection != null) {
-                // if mOriginalConnection is null, the properties will get set when
-                // mOriginalConnection gets set.
-                updateConnectionProperties();
-                refreshConferenceSupported();
-            }
-            sendRttInitiationSuccess();
+            Log.i(TelephonyConnection.this, "onRttInitiated: callId=%s", getTelecomCallId());
+            // Post RTT initiation to the Handler associated with this TelephonyConnection.
+            // This avoids a race condition where a call starts as RTT but ConnectionService call to
+            // handleCreateConnectionComplete happens AFTER the RTT status is reported to Telecom.
+            mHandler.obtainMessage(MSG_ON_RTT_INITIATED).sendToTarget();
         }
 
         @Override