Pass voicemail: URIs directly to Telephony. Rename handle -> number

voicemail calls should be transformed to tel: numbers through Telephony.
TelephonyConnectionManager is able to pick the right number because it
has knowledge of the subscription being used for the call.

Also rename "handle" to "number" in all cases where "handle" was
referring to the phone number within a URI. The code referred to
URI "handles" as well as phone number "handles", which made it confusing
to differentiate.

Additionally removed an incorrect comment and parsed CallStates in log
messages for easier readability.

Bug: 17317190
Change-Id: I8269adef76d7770f8ef180f97c75d72b31b0bdd8
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 8935766..40e97f4 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -276,9 +276,6 @@
     /**
      * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
      *
-     * NOTE: emergency calls will never pass through this because they call
-     * 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.
@@ -812,7 +809,8 @@
             return;
         }
         int oldState = call.getState();
-        Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
+        Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
+                CallState.toString(newState), call);
         if (newState != oldState) {
             // Unfortunately, in the telephony world the radio is king. So if the call notifies
             // us that the call is in a particular state, we allow it even if it doesn't make
diff --git a/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java b/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
index 1c44fb2..57a776f 100644
--- a/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
+++ b/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
@@ -98,15 +98,15 @@
 
             // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
             // actual number to call. (If null, no call will be placed.)
-            String resultHandle = getResultData();
-            Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
+            String resultNumber = getResultData();
+            Log.v(this, "- got number from resultData: %s", Log.pii(resultNumber));
 
             boolean endEarly = false;
-            if (resultHandle == null) {
+            if (resultNumber == null) {
                 Log.v(this, "Call cancelled (null number), returning...");
                 endEarly = true;
-            } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
-                Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
+            } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultNumber)) {
+                Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultNumber);
                 endEarly = true;
             }
 
@@ -118,14 +118,14 @@
             }
 
             Uri resultHandleUri = Uri.fromParts(
-                    PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
-                    resultHandle,
+                    PhoneNumberUtils.isUriNumber(resultNumber) ? SCHEME_SIP : SCHEME_TEL,
+                    resultNumber,
                     null);
 
             Uri originalUri = mIntent.getData();
 
-            if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
-                Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
+            if (originalUri.getSchemeSpecificPart().equals(resultNumber)) {
+                Log.v(this, "Call number unmodified after new outgoing call intent broadcast.");
             } else {
                 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
                         + "Original: %s, Modified: %s",
@@ -159,29 +159,48 @@
         Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
 
         final Context context = TelecommApp.getInstance();
+
         Intent intent = mIntent;
+        String action = intent.getAction();
+        final Uri handle = intent.getData();
 
-        String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
+        if (handle == null) {
+            Log.w(this, "Empty handle obtained from the call intent.");
+            return DisconnectCause.INVALID_NUMBER;
+        }
 
-        if (TextUtils.isEmpty(handle)) {
-            final Uri data = intent.getData();
-            if (data != null && SCHEME_VOICEMAIL.equals(data.getScheme())) {
-                Log.w(this, "Voicemail scheme provided but no voicemail number set.");
-                return DisconnectCause.VOICEMAIL_NUMBER_MISSING;
+        boolean isVoicemailNumber = SCHEME_VOICEMAIL.equals(handle.getScheme());
+        if (isVoicemailNumber) {
+            if (Intent.ACTION_CALL.equals(action)) {
+                // Voicemail calls will be handled directly by the telephony connection manager
+                Log.i(this, "Placing call immediately instead of waiting for "
+                        + " OutgoingCallBroadcastReceiver: %s", intent);
+
+                boolean speakerphoneOn = mIntent.getBooleanExtra(
+                        TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
+                mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
+                        VideoProfile.VideoState.AUDIO_ONLY);
+
+                return DisconnectCause.NOT_DISCONNECTED;
             } else {
-                Log.w(this, "Empty handle obtained from the call intent.");
-                return DisconnectCause.INVALID_NUMBER;
+                Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
+                return DisconnectCause.OUTGOING_CANCELED;
             }
         }
 
-        boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
-
-        if (!isUriNumber) {
-            handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
-            handle = PhoneNumberUtils.stripSeparators(handle);
+        String number = PhoneNumberUtils.getNumberFromIntent(intent, context);
+        if (TextUtils.isEmpty(number)) {
+            Log.w(this, "Empty number obtained from the call intent.");
+            return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
         }
 
-        final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
+        boolean isUriNumber = PhoneNumberUtils.isUriNumber(number);
+        if (!isUriNumber) {
+            number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
+            number = PhoneNumberUtils.stripSeparators(number);
+        }
+
+        final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, number);
         Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
 
         rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
@@ -189,12 +208,11 @@
         // by third parties (e.g. emergency numbers).
         boolean callImmediately = false;
 
-        String action = intent.getAction();
         if (Intent.ACTION_CALL.equals(action)) {
             if (isPotentialEmergencyNumber) {
                 if (!mIsDefaultOrSystemPhoneApp) {
                     Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
-                            + "unless caller is system or default dialer.", handle, intent);
+                            + "unless caller is system or default dialer.", number, intent);
                     launchSystemDialer(context, intent.getData());
                     return DisconnectCause.OUTGOING_CANCELED;
                 } else {
@@ -204,7 +222,7 @@
         } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
             if (!isPotentialEmergencyNumber) {
                 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
-                        + "Intent %s.", handle, intent);
+                        + "Intent %s.", number, intent);
                 return DisconnectCause.OUTGOING_CANCELED;
             }
             callImmediately = true;
@@ -222,7 +240,7 @@
             int videoState = mIntent.getIntExtra(
                     TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
                     VideoProfile.VideoState.AUDIO_ONLY);
-            mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, handle, null), null,
+            mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
                     speakerphoneOn, videoState);
 
             // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
@@ -231,7 +249,7 @@
             // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
         }
 
-        broadcastIntent(intent, handle, context, !callImmediately);
+        broadcastIntent(intent, number, context, !callImmediately);
         return DisconnectCause.NOT_DISCONNECTED;
     }
 
@@ -240,19 +258,19 @@
      * placement of the call or redirect it to a different number.
      *
      * @param originalCallIntent The original call intent.
-     * @param handle Call handle that was stored in the original call intent.
+     * @param number Call number that was stored in the original call intent.
      * @param context Valid context to send the ordered broadcast using.
      * @param receiverRequired Whether or not the result from the ordered broadcast should be
      *     processed using a {@link NewOutgoingCallIntentBroadcaster}.
      */
     private void broadcastIntent(
             Intent originalCallIntent,
-            String handle,
+            String number,
             Context context,
             boolean receiverRequired) {
         Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
-        if (handle != null) {
-            broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
+        if (number != null) {
+            broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
         }
 
         // Force receivers of this broadcast intent to run at foreground priority because we
@@ -269,7 +287,7 @@
                 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
                 null,  // scheduler
                 Activity.RESULT_OK,  // initialCode
-                handle,  // initialData: initial value for the result data (number to be modified)
+                number,  // initialData: initial value for the result data (number to be modified)
                 null);  // initialExtras
     }
 
@@ -360,13 +378,13 @@
      * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
      *
      * @param context Valid context
-     * @param handle Handle to inspect in order to determine whether or not an emergency number
+     * @param number number to inspect in order to determine whether or not an emergency number
      * is potentially being dialed
      * @return True if the handle is potentially an emergency number.
      */
-    private boolean isPotentialEmergencyNumber(Context context, String handle) {
-        Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
-        return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
+    private boolean isPotentialEmergencyNumber(Context context, String number) {
+        Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
+        return (number != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,number);
     }
 
     /**
@@ -374,7 +392,7 @@
      * the call intent action to an appropriate one.
      *
      * @param intent Intent to rewrite the action for
-     * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
+     * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
      * number.
      */
     private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {