Add gateway support to Telecomm

Add OutgoingCallIntentBroadcaster into the outgoing call flow.
This class allows third party apps to intercept and modify
outgoing phone calls, and also allows for the ability to extract
gateway information from outgoing call intents to allow for calls
to be made via gateway providers.

Bug: 13477768

Change-Id: Iee6a466b3ae7380136c3d7feba48602a14bc8507
diff --git a/res/values/config.xml b/res/values/config.xml
index 6905860..7d6fa28 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -22,4 +22,10 @@
          avoid the risk of accidental redialing from the call log UI.
          The default is false. -->
     <bool name="allow_emergency_numbers_in_call_log">false</bool>
+
+    <!-- Package name for the default in-call UI and dialer [DO NOT TRANSLATE] -->
+    <string name="ui_default_package" translatable="false">com.android.dialer</string>
+
+    <!-- Class name for the default main dialer activity [DO NOT TRANSLATE] -->
+    <string name="dialer_default_class" translatable="false">com.android.dialer.DialtactsActivity</string>
 </resources>
diff --git a/src/com/android/telecomm/Call.java b/src/com/android/telecomm/Call.java
index 57074b7..568d7c6 100644
--- a/src/com/android/telecomm/Call.java
+++ b/src/com/android/telecomm/Call.java
@@ -19,6 +19,7 @@
 import android.net.Uri;
 import android.telecomm.CallInfo;
 import android.telecomm.CallState;
+import android.telecomm.GatewayInfo;
 
 import com.google.android.collect.Sets;
 import com.google.common.base.Preconditions;
@@ -57,6 +58,12 @@
     /** The handle with which to establish this call. */
     private Uri mHandle;
 
+    /** The gateway information associated with this call. This stores the original call handle
+     * that the user is attempting to connect to via the gateway, the actual handle to dial in
+     * order to connect the call via the gateway, as well as the package name of the gateway
+     * service. */
+    private final GatewayInfo mGatewayInfo;
+
     /**
      * The call service which is attempted or already connecting this call.
      */
@@ -67,9 +74,6 @@
      */
     private CallServiceSelectorWrapper mCallServiceSelector;
 
-    /** Read-only and parcelable version of this call. */
-    private CallInfo mCallInfo;
-
     /**
      * The set of call services that were attempted in the process of placing/switching this call
      * but turned out unsuitable.  Only used in the context of call switching.
@@ -82,7 +86,7 @@
      * @param isIncoming True if this is an incoming call.
      */
     Call(boolean isIncoming) {
-        this(null, null, isIncoming);
+        this(null, null, null, isIncoming);
     }
 
     /**
@@ -90,13 +94,15 @@
      *
      * @param handle The handle to dial.
      * @param contactInfo Information about the entity being called.
+     * @param gatewayInfo Gateway information to use for the call.
      * @param isIncoming True if this is an incoming call.
      */
-    Call(Uri handle, ContactInfo contactInfo, boolean isIncoming) {
+    Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) {
         mId = UUID.randomUUID().toString();  // UUIDs should provide sufficient uniqueness.
         mState = CallState.NEW;
         mHandle = handle;
         mContactInfo = contactInfo;
+        mGatewayInfo = gatewayInfo;
         mIsIncoming = isIncoming;
         mCreationTime = new Date();
     }
@@ -126,7 +132,6 @@
         if (mState != newState) {
             Log.v(this, "setState %s -> %s", mState, newState);
             mState = newState;
-            clearCallInfo();
         }
     }
 
@@ -138,6 +143,21 @@
         mHandle = handle;
     }
 
+    /**
+     * @return The original handle this call is associated with. In-call services should use this
+     * handle when indicating in their UI the handle that is being called.
+     */
+    public Uri getOriginalHandle() {
+        if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
+            return mGatewayInfo.getOriginalHandle();
+        }
+        return getHandle();
+    }
+
+    GatewayInfo getGatewayInfo() {
+        return mGatewayInfo;
+    }
+
     ContactInfo getContactInfo() {
         return mContactInfo;
     }
@@ -309,10 +329,7 @@
      * @return An object containing read-only information about this call.
      */
     CallInfo toCallInfo() {
-        if (mCallInfo == null) {
-            mCallInfo = new CallInfo(mId, mState, mHandle);
-        }
-        return mCallInfo;
+        return new CallInfo(mId, mState, mHandle, mGatewayInfo);
     }
 
     /**
@@ -327,13 +344,6 @@
         return false;
     }
 
-    /**
-     * Resets the cached read-only version of this call.
-     */
-    private void clearCallInfo() {
-        mCallInfo = null;
-    }
-
     @SuppressWarnings("rawtypes")
     private void decrementAssociatedCallCount(ServiceBinder binder) {
         if (binder != null) {
diff --git a/src/com/android/telecomm/CallActivity.java b/src/com/android/telecomm/CallActivity.java
index 06d4267..43c4cf3 100644
--- a/src/com/android/telecomm/CallActivity.java
+++ b/src/com/android/telecomm/CallActivity.java
@@ -90,7 +90,9 @@
      */
     private void processOutgoingCallIntent(Intent intent) {
         ContactInfo contactInfo = null;
-        mCallsManager.processOutgoingCallIntent(intent.getData(), contactInfo);
+        NewOutgoingCallIntentBroadcaster broadcaster =
+                new NewOutgoingCallIntentBroadcaster(mCallsManager, contactInfo, intent);
+        broadcaster.processIntent();
     }
 
     /**
diff --git a/src/com/android/telecomm/CallLogManager.java b/src/com/android/telecomm/CallLogManager.java
index 7862afd..99eb845 100644
--- a/src/com/android/telecomm/CallLogManager.java
+++ b/src/com/android/telecomm/CallLogManager.java
@@ -101,15 +101,13 @@
      *     {@link android.provider.CallLog.Calls#MISSED_TYPE}
      */
     private void logCall(Call call, int callLogType) {
-        Uri number = call.getHandle();
         final long creationTime = call.getCreationTimeInMilliseconds();
         final long age = call.getAgeInMilliseconds();
 
         final ContactInfo contactInfo = call.getContactInfo();  // May be null.
         final String logNumber = getLogNumber(call);
 
-        Log.d(TAG, "logNumber set to:" + Log.pii(logNumber) + ", number set to: "
-                + Log.pii(number));
+        Log.d(TAG, "logNumber set to: %s", Log.pii(logNumber));
 
         final int presentation = getPresentation(call, contactInfo);
 
@@ -164,7 +162,7 @@
      * @return the phone number to be logged.
      */
     private String getLogNumber(Call call) {
-        Uri handle = call.getHandle();
+        Uri handle = call.getOriginalHandle();
 
         if (handle == null) {
             return null;
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 36fbf77..22d9ce6 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -16,14 +16,13 @@
 
 package com.android.telecomm;
 
-import android.content.Context;
 import android.net.Uri;
 import android.os.Bundle;
 import android.telecomm.CallInfo;
 import android.telecomm.CallServiceDescriptor;
 import android.telecomm.CallState;
+import android.telecomm.GatewayInfo;
 
-import com.android.internal.telecomm.ICallService;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableCollection;
@@ -161,15 +160,14 @@
     }
 
     /**
-     * Attempts to issue/connect the specified call.  From an (arbitrary) application standpoint,
-     * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
-     * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
-     * this method.
+     * Attempts to issue/connect the specified call.
      *
-     * @param handle The handle to dial.
+     * @param handle Handle to connect the call with.
      * @param contactInfo Information about the entity being called.
+     * @param gatewayInfo Optional gateway information that can be used to route the call to the
+     *     actual dialed handle via a gateway provider. May be null.
      */
-    void processOutgoingCallIntent(Uri handle, ContactInfo contactInfo) {
+    void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
         for (OutgoingCallValidator validator : mOutgoingCallValidators) {
             if (!validator.isValid(handle, contactInfo)) {
                 // TODO(gilad): Display an error message.
@@ -178,8 +176,15 @@
             }
         }
 
-        // No objection to issue the call, proceed with trying to put it through.
-        Call call = new Call(handle, contactInfo, false /* isIncoming */);
+        final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
+
+        if (gatewayInfo == null) {
+            Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
+        } else {
+            Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
+                    Log.pii(uriHandle), Log.pii(handle));
+        }
+        Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/);
         setCallState(call, CallState.DIALING);
         addCall(call);
         mSwitchboard.placeOutgoingCall(call);
diff --git a/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java b/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
new file mode 100644
index 0000000..369a1fd
--- /dev/null
+++ b/src/com/android/telecomm/NewOutgoingCallIntentBroadcaster.java
@@ -0,0 +1,370 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.telecomm;
+
+import android.app.Activity;
+import android.app.ActivityManagerNative;
+import android.app.AlertDialog;
+import android.app.AppOpsManager;
+import android.app.Dialog;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.content.res.Resources;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Binder;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.os.RemoteException;
+import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.provider.ContactsContract;
+import android.telecomm.GatewayInfo;
+import android.telecomm.TelecommConstants;
+import android.telephony.PhoneNumberUtils;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.view.View;
+import android.widget.ProgressBar;
+
+/**
+ * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
+ * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
+ * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
+ * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
+ * from being placed.
+ *
+ * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
+ * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
+ *
+ * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
+ * number) are exempt from being broadcast.
+ *
+ * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
+ * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
+ */
+class NewOutgoingCallIntentBroadcaster {
+    /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
+    private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
+
+    private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
+            "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
+
+    /**
+     * Legacy string constants used to retrieve gateway provider extras from intents. These still
+     * need to be copied from the source call intent to the destination intent in order to
+     * support third party gateway providers that are still using old string constants in
+     * Telephony.
+     */
+    public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
+            "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
+    public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
+
+    private final CallsManager mCallsManager;
+    private final ContactInfo mContactInfo;
+    private final Intent mIntent;
+
+    NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
+            Intent intent) {
+        mCallsManager = callsManager;
+        mContactInfo = contactInfo;
+        mIntent = intent;
+    }
+
+    /**
+     * Processes the result of the outgoing call broadcast intent, and performs callbacks to
+     * the OutgoingCallIntentBroadcasterListener as necessary.
+     */
+    private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            Log.v(this, "doReceive: %s");
+
+            // 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));
+
+            if (resultHandle == null) {
+                Log.v(this, "Call cancelled (null number), returning...");
+                return;
+            } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(resultHandle, context)) {
+                Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
+                return;
+            }
+
+            Uri resultHandleUri = Uri.parse(resultHandle);
+
+            Uri originalUri = mIntent.getData();
+
+            if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
+                Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
+            } else {
+                Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
+                        + "Original: %s, Modified: %s",
+                        Log.pii(originalUri),
+                        Log.pii(resultHandleUri));
+            }
+
+            GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
+            mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo);
+        }
+    }
+
+    /**
+     * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
+     * intent.
+     *
+     * This method will handle three kinds of actions:
+     *
+     * - CALL (intent launched by all third party dialers)
+     * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
+     * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
+     */
+    void processIntent() {
+        Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
+
+        final Context context = TelecommApp.getInstance();
+        Intent intent = mIntent;
+
+        String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
+
+        if (TextUtils.isEmpty(handle)) {
+            Log.w(this, "Empty handle obtained from the call intent.");
+            return;
+        }
+
+        if (!PhoneNumberUtils.isUriNumber(handle)) {
+            handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
+            handle = PhoneNumberUtils.stripSeparators(handle);
+        }
+
+        final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
+        Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
+
+        rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
+        // True for certain types of numbers that are not intended to be intercepted or modified
+        // by third parties (e.g. emergency numbers).
+        boolean callImmediately = false;
+
+        String action = intent.getAction();
+        if (Intent.ACTION_CALL.equals(action)) {
+            if (isPotentialEmergencyNumber) {
+                Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
+                        handle, intent);
+                launchSystemDialer(context, intent.getData());
+            }
+            callImmediately = false;
+        } 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);
+                return;
+            }
+            callImmediately = true;
+        } else {
+            Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
+            return;
+        }
+
+        if (callImmediately) {
+            Log.i(this, "Placing call immediately instead of waiting for "
+                    + " OutgoingCallBroadcastReceiver: %s", intent);
+            mCallsManager.placeOutgoingCall(Uri.parse(handle), mContactInfo, null);
+
+            // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
+            // so that third parties can still inspect (but not intercept) the outgoing call. When
+            // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
+            // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
+        }
+
+        broadcastIntent(intent, handle, context, !callImmediately);
+    }
+
+    /**
+     * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
+     * 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 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,
+            Context context,
+            boolean receiverRequired) {
+        Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
+        if (handle != null) {
+            broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
+        }
+
+        // Force receivers of this broadcast intent to run at foreground priority because we
+        // want to finish processing the broadcast intent as soon as possible.
+        broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+        Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
+
+        checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent);
+
+        context.sendOrderedBroadcastAsUser(
+                broadcastIntent,
+                UserHandle.OWNER,
+                PERMISSION,
+                receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
+                null,  // scheduler
+                Activity.RESULT_OK,  // initialCode
+                handle,  // initialData: initial value for the result data (number to be modified)
+                null);  // initialExtras
+    }
+
+    /**
+     * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
+     * source intent to the destination one.
+     *
+     * @param src Intent which may contain the provider's extras.
+     * @param dst Intent where a copy of the extras will be added if applicable.
+     */
+    public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) {
+        if (hasGatewayProviderExtras(src)) {
+            dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
+                    src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
+            dst.putExtra(EXTRA_GATEWAY_URI,
+                    src.getStringExtra(EXTRA_GATEWAY_URI));
+            Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
+            return;
+        }
+
+        Log.d(this, "No gateway provider extras found in call intent.");
+    }
+
+    /**
+     * Check if valid gateway provider information is stored as extras in the intent
+     *
+     * @param intent to check for
+     * @return true if the intent has all the gateway information extras needed.
+     */
+    private boolean hasGatewayProviderExtras(Intent intent) {
+        if (intent == null) {
+            return false;
+        }
+        final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
+        final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
+
+        return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
+    }
+
+    private static Uri getGatewayUriFromString(String gatewayUriString) {
+        return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
+    }
+
+    /**
+     * Extracts gateway provider information from a provided intent..
+     *
+     * @param intent to extract gateway provider information from.
+     * @param trueHandle The actual call handle that the user is trying to dial
+     * @return GatewayInfo object containing extracted gateway provider information as well as
+     *     the actual handle the user is trying to dial.
+     */
+    public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
+        if (intent == null) {
+            return null;
+        }
+
+        // Check if gateway extras are present.
+        String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
+        Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
+        if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
+            return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
+        }
+
+        return null;
+    }
+
+    private void launchSystemDialer(Context context, Uri handle) {
+        Intent systemDialerIntent = new Intent();
+        final Resources resources = context.getResources();
+        systemDialerIntent.setClassName(
+                resources.getString(R.string.ui_default_package),
+                resources.getString(R.string.dialer_default_class));
+        systemDialerIntent.setAction(Intent.ACTION_DIAL);
+        systemDialerIntent.setData(handle);
+        systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
+        context.startActivity(systemDialerIntent);
+    }
+
+    /**
+     * Check whether or not this is an emergency number, in order to enforce the restriction
+     * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
+     * calls.
+     *
+     * To prevent malicious 3rd party apps from making emergency calls by passing in an
+     * "invalid" number like "9111234" (that isn't technically an emergency number but might
+     * still result in an emergency call with some networks), we use
+     * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
+     *
+     * @param context Valid context
+     * @param handle Handle 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(handle,context);
+    }
+
+    /**
+     * Given a call intent and whether or not the number to dial is an emergency number, rewrite
+     * 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
+     * number.
+     */
+    private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
+        if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
+            // If we were launched directly from the CallActivity, not one of its more privileged
+            // aliases, then make sure that only the non-privileged actions are allowed.
+            if (!Intent.ACTION_CALL.equals(intent.getAction())) {
+                Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
+                intent.setAction(Intent.ACTION_CALL);
+            }
+        }
+
+        String action = intent.getAction();
+
+        /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
+        if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
+            if (isPotentialEmergencyNumber) {
+                Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
+                        + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
+                action = Intent.ACTION_CALL_EMERGENCY;
+            } else {
+                action = Intent.ACTION_CALL;
+            }
+            Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
+            intent.setAction(action);
+        }
+    }
+}