Fixing issue where Google Voice calls are logged twice to call log.

1. Changed disconnect cause for aborted calls to "CANCELED" which ensures
they are not logged.
2. Also, added a check in Telecom to avoid bringing up the InCall UI
immediately when a new call is made, if Google Voice is installed.  This
is to ensure that the user does not see two reveal animations.  This was
due to the fact that when the call was initially added and subsequently
aborted, the InCall ui would be shown.   With this change, if GVoice is
installed, adding the call is delayed.

Bug: 17961478
Change-Id: Ic0055a53ac85980fa22136a5c8b6cb59be347da9
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index de53c05..932084b 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -17,6 +17,9 @@
 package com.android.server.telecom;
 
 import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.CallLog.Calls;
@@ -443,7 +446,13 @@
 
         call.setExtras(extras);
 
-        if (isPotentialMMICode(handle) || isPotentialInCallMMICode) {
+        // Do not add the call if it is a potential MMI code.
+        // We also want to skip adding the call if there is a broadcast receiver which could
+        // intercept the outgoing call and cancel it.  We do this to ensure that we do not show the
+        // InCall UI for the cancelled call.  If the call is not intercepted, it will be added in
+        // {@link CallsManager#onSuccessfulOutgoingCall}.
+        if (isPotentialMMICode(handle) || isPotentialInCallMMICode ||
+                (!isEmergencyCall && canOutgoingCallBroadcastsBeIntercepted())) {
             call.addListener(this);
         } else {
             addCall(call);
@@ -1194,6 +1203,28 @@
     }
 
     /**
+     * Determines if the {@link Intent#ACTION_NEW_OUTGOING_CALL} intent can be received by another
+     * package with priority 0, potentially providing the ability to cancel the intent before it
+     * is received.
+     *
+     * @return {@code true} if the intent can be intercepted by another
+     */
+    private boolean canOutgoingCallBroadcastsBeIntercepted() {
+        PackageManager packageManager = mContext.getPackageManager();
+        Intent intent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
+        List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
+
+        for (ResolveInfo info : receivers) {
+            // Check for an interceptor with priority 0; this would potentially receive the
+            // broadcast before Telecom and cancel it.
+            if (info.priority == 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Dumps the state of the {@link CallsManager}.
      *
      * @param pw The {@code IndentingPrintWriter} to write the state to.