Bring up Select Account Dialog before launching InCallUI.

Moving code for determining phone account for call from placeOutgoingCall
to startOutgoingCall so that the select phone account dialog can show up
before the InCallUI.

Bug: 16396523

Change-Id: I191bc17d07facbce57988606736319754a381979
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 80ef854..a9c2c3e 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -250,8 +250,8 @@
     /**
      * Starts the process to attach the call to a connection service.
      *
-     * @param phoneAccountHandle The phone account which contains the component name of the connection
-     *                     service to use for this call.
+     * @param phoneAccountHandle The phone account which contains the component name of the
+     *        connection service to use for this call.
      * @param extras The optional extras Bundle passed with the intent used for the incoming call.
      */
     void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
@@ -273,7 +273,6 @@
         call.startCreateConnection();
     }
 
-
     /**
      * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
      *
@@ -281,10 +280,9 @@
      * placeOutgoingCall directly.
      *
      * @param handle Handle to connect the call with.
-     * @param phoneAccountHandle The phone account which contains the component name of the connection
-     *                     service to use for this call.
-     * @param extras The optional extras Bundle passed with the intent used for the outgoing call.
-     *
+     * @param phoneAccountHandle The phone account which contains the component name of the
+     *        connection service to use for this call.
+     * @param extras The optional extras Bundle passed with the intent used for the incoming call.
      */
     Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
         // We only allow a single outgoing call at any given time. Before placing a call, make sure
@@ -296,6 +294,27 @@
             return null;
         }
 
+        TelecommApp app = TelecommApp.getInstance();
+
+        // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
+        // as if a phoneAccount was not specified (does the default behavior instead).
+        if (phoneAccountHandle != null) {
+            List<PhoneAccountHandle> enabledAccounts =
+                    app.getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
+            if (!enabledAccounts.contains(phoneAccountHandle)) {
+                phoneAccountHandle = null;
+            }
+        }
+
+        if (phoneAccountHandle == null) {
+            // No preset account, check if default exists
+            PhoneAccountHandle defaultAccountHandle =
+                    app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
+            if (defaultAccountHandle != null) {
+                phoneAccountHandle = defaultAccountHandle;
+            }
+        }
+
         // Create a call with original handle. The handle may be changed when the call is attached
         // to a connection service, but in most cases will remain the same.
         call = new Call(
@@ -307,7 +326,14 @@
                 false /* isIncoming */,
                 false /* isConference */);
         call.setExtras(extras);
-        call.setState(CallState.CONNECTING);
+
+        final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
+        if (phoneAccountHandle == null && !emergencyCall) {
+            // This is the state where the user is expected to select an account
+            call.setState(CallState.PRE_DIAL_WAIT);
+        } else {
+            call.setState(CallState.CONNECTING);
+        }
 
         if (!isPotentialMMICode(handle)) {
             addCall(call);
@@ -327,15 +353,14 @@
      * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
      * @param videoState The desired video state for the outgoing call.
      */
-    void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo,
-            PhoneAccountHandle accountHandle, boolean speakerphoneOn, int videoState) {
+    void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
+            int videoState) {
         if (call == null) {
             // don't do anything if the call no longer exists
             Log.i(this, "Canceling unknown call.");
             return;
         }
 
-        TelecommApp app = TelecommApp.getInstance();
         final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
 
         if (gatewayInfo == null) {
@@ -345,49 +370,22 @@
                     Log.pii(uriHandle), Log.pii(handle));
         }
 
-        //TODO: phone account is already set in {@link #startOutgoingCall}, refactor so this is
-        // not redundant.
-        //
-        // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
-        // as if a phoneAccount was not specified (does the default behavior instead).
-        if (accountHandle != null) {
-            List<PhoneAccountHandle> enabledAccounts =
-                    app.getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
-            if (!enabledAccounts.contains(accountHandle)) {
-                accountHandle = null;
-            }
-        }
-
         call.setHandle(uriHandle);
         call.setGatewayInfo(gatewayInfo);
         call.setStartWithSpeakerphoneOn(speakerphoneOn);
         call.setVideoState(videoState);
 
-        // This block of code will attempt to pre-determine a phone account
+        TelecommApp app = TelecommApp.getInstance();
         final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
         if (emergencyCall) {
             // Emergency -- CreateConnectionProcessor will choose accounts automatically
             call.setTargetPhoneAccount(null);
-        } else if (accountHandle != null) {
-            // NOTE: this is currently not an option because no feature currently exists to
-            // preset a phone account
-            Log.d(this, "CALL with phone account: " + accountHandle);
-            call.setTargetPhoneAccount(accountHandle);
-        } else {
-            // No preset account, check if default exists
-            PhoneAccountHandle defaultAccountHandle =
-                    app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
-            if (defaultAccountHandle != null) {
-                call.setTargetPhoneAccount(defaultAccountHandle);
-            }
         }
 
         if (call.getTargetPhoneAccount() != null || emergencyCall) {
-            // If the account is selected, proceed to place the outgoing call
+            // If the account has been set, proceed to place the outgoing call.
+            // Otherwise the connection will be initiated when the account is set by the user.
             call.startCreateConnection();
-        } else {
-            // This is the state where the user is expected to select an account
-            setCallState(call, CallState.PRE_DIAL_WAIT);
         }
     }
 
@@ -515,6 +513,7 @@
         }
     }
 
+
     /**
      * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
      * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
@@ -755,6 +754,7 @@
         return call;
     }
 
+
     /**
      * Adds the specified call to the main list of live calls.
      *