Fix MMI code handling

If the handle to be dialed is potentially an MMI code, postpone
the addition of the call so that the InCallUI doesn't show up
immediately. Instead, add it in onSuccessfulOutgoingCall instead once
it is determined that the handle is not actually an MMI code.

Bug: 16305480

Change-Id: I0c5c67543650b74398d358f5723b621fb16f9e1e
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 74b6e44..fa24144 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -124,15 +124,17 @@
     @Override
     public void onSuccessfulOutgoingCall(Call call, int callState) {
         Log.v(this, "onSuccessfulOutgoingCall, %s", call);
+
         setCallState(call, callState);
-        if (mCalls.contains(call)) {
-            // The call's ConnectionService has been updated.
-            for (CallsManagerListener listener : mListeners) {
-                listener.onConnectionServiceChanged(call, null, call.getConnectionService());
-            }
-        } else {
-            Log.wtf(this, "unexpected successful call notification: %s", call);
-            return;
+        if (!mCalls.contains(call)) {
+            // Call was not added previously in startOutgoingCall due to it being a potential MMI
+            // code, so add it now.
+            addCall(call);
+        }
+
+        // The call's ConnectionService has been updated.
+        for (CallsManagerListener listener : mListeners) {
+            listener.onConnectionServiceChanged(call, null, call.getConnectionService());
         }
 
         markCallAsDialing(call);
@@ -141,6 +143,7 @@
     @Override
     public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
         Log.v(this, "onFailedOutgoingCall, call: %s", call);
+
         // TODO: Replace disconnect cause with more specific disconnect causes.
         markCallAsDisconnected(call, errorCode, errorMsg);
     }
@@ -312,9 +315,11 @@
                 false /* isConference */);
         call.setState(CallState.CONNECTING);
 
-        // TODO: Move this to be a part of addCall()
-        call.addListener(this);
-        addCall(call);
+        if (!isPotentialMMICode(handle)) {
+            addCall(call);
+        } else {
+            call.addListener(this);
+        }
 
         return call;
     }
@@ -732,6 +737,9 @@
      * @param call The call to add.
      */
     private void addCall(Call call) {
+        Log.v(this, "addCall(%s)", call);
+
+        call.addListener(this);
         mCalls.add(call);
 
         // TODO: Update mForegroundCall prior to invoking
@@ -828,4 +836,9 @@
             }
         }
     }
+
+    private boolean isPotentialMMICode(Uri handle) {
+        return (handle != null && handle.getSchemeSpecificPart() != null
+                && handle.getSchemeSpecificPart().contains("#"));
+    }
 }