Merge "Move WFC connection event to "onCreateConnectionComplete"." into oc-dev
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index b9be502..51139e9 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -653,6 +653,21 @@
         }
     }
 
+    /**
+     * Called by the {@link ConnectionService} when a newly created {@link Connection} has been
+     * added to the {@link ConnectionService} and sent to Telecom.  Here it is safe to send
+     * connection events.
+     *
+     * @param connection the {@link Connection}.
+     */
+    @Override
+    public void onCreateConnectionComplete(Connection connection) {
+        if (connection instanceof TelephonyConnection) {
+            TelephonyConnection telephonyConnection = (TelephonyConnection) connection;
+            maybeSendInternationalCallEvent(telephonyConnection);
+        }
+    }
+
     @Override
     public void triggerConferenceRecalculate() {
         if (mTelephonyConferenceController.shouldRecalculate()) {
@@ -893,18 +908,6 @@
         try {
             if (phone != null) {
                 originalConnection = phone.dial(number, null, videoState, extras);
-
-                if (phone instanceof GsmCdmaPhone) {
-                    GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
-                    if (gsmCdmaPhone.isNotificationOfWfcCallRequired(number)) {
-                        // Send connection event to InCall UI to inform the user of the fact they
-                        // are potentially placing an international call on WFC.
-                        Log.i(this, "placeOutgoingConnection - sending international call on WFC " +
-                                "confirmation event");
-                        connection.sendConnectionEvent(
-                                TelephonyManager.EVENT_NOTIFY_INTERNATIONAL_CALL_ON_WFC, null);
-                    }
-                }
             }
         } catch (CallStateException e) {
             Log.e(this, e, "placeOutgoingConnection, phone.dial exception: " + e);
@@ -1240,4 +1243,20 @@
                 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
                 TelecomManager.TTY_MODE_OFF) != TelecomManager.TTY_MODE_OFF);
     }
+
+    private void maybeSendInternationalCallEvent(TelephonyConnection telephonyConnection) {
+        Phone phone = telephonyConnection.getPhone().getDefaultPhone();
+        if (phone instanceof GsmCdmaPhone) {
+            GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
+            if (gsmCdmaPhone.isNotificationOfWfcCallRequired(
+                    telephonyConnection.getOriginalConnection().getOrigDialString())) {
+                // Send connection event to InCall UI to inform the user of the fact they
+                // are potentially placing an international call on WFC.
+                Log.i(this, "placeOutgoingConnection - sending international call on WFC " +
+                        "confirmation event");
+                telephonyConnection.sendConnectionEvent(
+                        TelephonyManager.EVENT_NOTIFY_INTERNATIONAL_CALL_ON_WFC, null);
+            }
+        }
+    }
 }