Home | History | Annotate | Download | only in telecom
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.telecom;
     18 
     19 import android.app.Activity;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.res.Resources;
     24 import android.net.Uri;
     25 import android.os.UserHandle;
     26 import android.telecom.GatewayInfo;
     27 import android.telecom.PhoneAccount;
     28 import android.telecom.TelecomManager;
     29 import android.telecom.VideoProfile;
     30 import android.telephony.DisconnectCause;
     31 import android.telephony.PhoneNumberUtils;
     32 import android.text.TextUtils;
     33 
     34 // TODO: Needed for move to system service: import com.android.internal.R;
     35 
     36 /**
     37  * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
     38  * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
     39  * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
     40  * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
     41  * from being placed.
     42  *
     43  * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
     44  * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
     45  *
     46  * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
     47  * number) are exempt from being broadcast.
     48  *
     49  * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
     50  * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
     51  */
     52 class NewOutgoingCallIntentBroadcaster {
     53     /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
     54     private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
     55 
     56     private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
     57             "android.telecom.extra.ACTUAL_NUMBER_TO_DIAL";
     58 
     59     /**
     60      * Legacy string constants used to retrieve gateway provider extras from intents. These still
     61      * need to be copied from the source call intent to the destination intent in order to
     62      * support third party gateway providers that are still using old string constants in
     63      * Telephony.
     64      */
     65     public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
     66             "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
     67     public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
     68     public static final String EXTRA_GATEWAY_ORIGINAL_URI =
     69             "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
     70 
     71     private final CallsManager mCallsManager;
     72     private final Call mCall;
     73     private final Intent mIntent;
     74     private final Context mContext;
     75 
     76     /*
     77      * Whether or not the outgoing call intent originated from the default phone application. If
     78      * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
     79      */
     80     private final boolean mIsDefaultOrSystemPhoneApp;
     81 
     82     NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
     83             Intent intent, boolean isDefaultPhoneApp) {
     84         mContext = context;
     85         mCallsManager = callsManager;
     86         mCall = call;
     87         mIntent = intent;
     88         mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
     89     }
     90 
     91     /**
     92      * Processes the result of the outgoing call broadcast intent, and performs callbacks to
     93      * the OutgoingCallIntentBroadcasterListener as necessary.
     94      */
     95     private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
     96 
     97         @Override
     98         public void onReceive(Context context, Intent intent) {
     99             Log.v(this, "onReceive: %s", intent);
    100 
    101             // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
    102             // actual number to call. (If null, no call will be placed.)
    103             String resultNumber = getResultData();
    104             Log.v(this, "- got number from resultData: %s", Log.pii(resultNumber));
    105 
    106             boolean endEarly = false;
    107             if (resultNumber == null) {
    108                 Log.v(this, "Call cancelled (null number), returning...");
    109                 endEarly = true;
    110             } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(mContext, resultNumber)) {
    111                 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultNumber);
    112                 endEarly = true;
    113             }
    114 
    115             if (endEarly) {
    116                 if (mCall != null) {
    117                     mCall.disconnect();
    118                 }
    119                 return;
    120             }
    121 
    122             Uri resultHandleUri = Uri.fromParts(PhoneNumberUtils.isUriNumber(resultNumber) ?
    123                     PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL, resultNumber, null);
    124 
    125             Uri originalUri = mIntent.getData();
    126 
    127             if (originalUri.getSchemeSpecificPart().equals(resultNumber)) {
    128                 Log.v(this, "Call number unmodified after new outgoing call intent broadcast.");
    129             } else {
    130                 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
    131                         + "Original: %s, Modified: %s",
    132                         Log.pii(originalUri),
    133                         Log.pii(resultHandleUri));
    134             }
    135 
    136             GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
    137             mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo,
    138                     mIntent.getBooleanExtra(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE,
    139                             false),
    140                     mIntent.getIntExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
    141                             VideoProfile.VideoState.AUDIO_ONLY));
    142         }
    143     }
    144 
    145     /**
    146      * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
    147      * intent.
    148      *
    149      * This method will handle three kinds of actions:
    150      *
    151      * - CALL (intent launched by all third party dialers)
    152      * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
    153      * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
    154      *
    155      * @return {@link CallActivity#OUTGOING_CALL_SUCCEEDED} if the call succeeded, and an
    156      *         appropriate {@link DisconnectCause} if the call did not, describing why it failed.
    157      */
    158     int processIntent() {
    159         Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
    160 
    161         Intent intent = mIntent;
    162         String action = intent.getAction();
    163         final Uri handle = intent.getData();
    164 
    165         if (handle == null) {
    166             Log.w(this, "Empty handle obtained from the call intent.");
    167             return DisconnectCause.INVALID_NUMBER;
    168         }
    169 
    170         boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
    171         if (isVoicemailNumber) {
    172             if (Intent.ACTION_CALL.equals(action)
    173                     || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
    174                 // Voicemail calls will be handled directly by the telephony connection manager
    175                 Log.i(this, "Placing call immediately instead of waiting for "
    176                         + " OutgoingCallBroadcastReceiver: %s", intent);
    177 
    178                 boolean speakerphoneOn = mIntent.getBooleanExtra(
    179                         TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
    180                 mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
    181                         VideoProfile.VideoState.AUDIO_ONLY);
    182 
    183                 return DisconnectCause.NOT_DISCONNECTED;
    184             } else {
    185                 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
    186                 return DisconnectCause.OUTGOING_CANCELED;
    187             }
    188         }
    189 
    190         String number = PhoneNumberUtils.getNumberFromIntent(intent, mContext);
    191         if (TextUtils.isEmpty(number)) {
    192             Log.w(this, "Empty number obtained from the call intent.");
    193             return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
    194         }
    195 
    196         boolean isUriNumber = PhoneNumberUtils.isUriNumber(number);
    197         if (!isUriNumber) {
    198             number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
    199             number = PhoneNumberUtils.stripSeparators(number);
    200         }
    201 
    202         final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number);
    203         Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
    204 
    205         rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
    206         action = intent.getAction();
    207         // True for certain types of numbers that are not intended to be intercepted or modified
    208         // by third parties (e.g. emergency numbers).
    209         boolean callImmediately = false;
    210 
    211         if (Intent.ACTION_CALL.equals(action)) {
    212             if (isPotentialEmergencyNumber) {
    213                 if (!mIsDefaultOrSystemPhoneApp) {
    214                     Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
    215                             + "unless caller is system or default dialer.", number, intent);
    216                     launchSystemDialer(intent.getData());
    217                     return DisconnectCause.OUTGOING_CANCELED;
    218                 } else {
    219                     callImmediately = true;
    220                 }
    221             }
    222         } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
    223             if (!isPotentialEmergencyNumber) {
    224                 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
    225                         + "Intent %s.", number, intent);
    226                 return DisconnectCause.OUTGOING_CANCELED;
    227             }
    228             callImmediately = true;
    229         } else {
    230             Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
    231             return DisconnectCause.INVALID_NUMBER;
    232         }
    233 
    234         if (callImmediately) {
    235             Log.i(this, "Placing call immediately instead of waiting for "
    236                     + " OutgoingCallBroadcastReceiver: %s", intent);
    237             String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
    238             boolean speakerphoneOn = mIntent.getBooleanExtra(
    239                     TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
    240             int videoState = mIntent.getIntExtra(
    241                     TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
    242                     VideoProfile.VideoState.AUDIO_ONLY);
    243             mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
    244                     speakerphoneOn, videoState);
    245 
    246             // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
    247             // so that third parties can still inspect (but not intercept) the outgoing call. When
    248             // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
    249             // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
    250         }
    251 
    252         broadcastIntent(intent, number, !callImmediately);
    253         return DisconnectCause.NOT_DISCONNECTED;
    254     }
    255 
    256     /**
    257      * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
    258      * placement of the call or redirect it to a different number.
    259      *
    260      * @param originalCallIntent The original call intent.
    261      * @param number Call number that was stored in the original call intent.
    262      * @param receiverRequired Whether or not the result from the ordered broadcast should be
    263      *     processed using a {@link NewOutgoingCallIntentBroadcaster}.
    264      */
    265     private void broadcastIntent(
    266             Intent originalCallIntent,
    267             String number,
    268             boolean receiverRequired) {
    269         Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
    270         if (number != null) {
    271             broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
    272         }
    273 
    274         // Force receivers of this broadcast intent to run at foreground priority because we
    275         // want to finish processing the broadcast intent as soon as possible.
    276         broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    277         Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
    278 
    279         checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
    280 
    281         mContext.sendOrderedBroadcastAsUser(
    282                 broadcastIntent,
    283                 UserHandle.CURRENT,
    284                 PERMISSION,
    285                 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
    286                 null,  // scheduler
    287                 Activity.RESULT_OK,  // initialCode
    288                 number,  // initialData: initial value for the result data (number to be modified)
    289                 null);  // initialExtras
    290     }
    291 
    292     /**
    293      * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
    294      * source intent to the destination one.
    295      *
    296      * @param src Intent which may contain the provider's extras.
    297      * @param dst Intent where a copy of the extras will be added if applicable.
    298      */
    299     public void checkAndCopyProviderExtras(Intent src, Intent dst) {
    300         if (src == null) {
    301             return;
    302         }
    303         if (hasGatewayProviderExtras(src)) {
    304             dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
    305                     src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
    306             dst.putExtra(EXTRA_GATEWAY_URI,
    307                     src.getStringExtra(EXTRA_GATEWAY_URI));
    308             Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
    309             return;
    310         }
    311 
    312         Log.d(this, "No provider extras found in call intent.");
    313     }
    314 
    315     /**
    316      * Check if valid gateway provider information is stored as extras in the intent
    317      *
    318      * @param intent to check for
    319      * @return true if the intent has all the gateway information extras needed.
    320      */
    321     private boolean hasGatewayProviderExtras(Intent intent) {
    322         final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
    323         final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
    324 
    325         return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
    326     }
    327 
    328     private static Uri getGatewayUriFromString(String gatewayUriString) {
    329         return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
    330     }
    331 
    332     /**
    333      * Extracts gateway provider information from a provided intent..
    334      *
    335      * @param intent to extract gateway provider information from.
    336      * @param trueHandle The actual call handle that the user is trying to dial
    337      * @return GatewayInfo object containing extracted gateway provider information as well as
    338      *     the actual handle the user is trying to dial.
    339      */
    340     public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
    341         if (intent == null) {
    342             return null;
    343         }
    344 
    345         // Check if gateway extras are present.
    346         String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
    347         Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
    348         if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
    349             return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
    350         }
    351 
    352         return null;
    353     }
    354 
    355     private void launchSystemDialer(Uri handle) {
    356         Intent systemDialerIntent = new Intent();
    357         final Resources resources = mContext.getResources();
    358         systemDialerIntent.setClassName(
    359                 resources.getString(R.string.ui_default_package),
    360                 resources.getString(R.string.dialer_default_class));
    361         systemDialerIntent.setAction(Intent.ACTION_DIAL);
    362         systemDialerIntent.setData(handle);
    363         systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    364         Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
    365         mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
    366     }
    367 
    368     /**
    369      * Check whether or not this is an emergency number, in order to enforce the restriction
    370      * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
    371      * calls.
    372      *
    373      * To prevent malicious 3rd party apps from making emergency calls by passing in an
    374      * "invalid" number like "9111234" (that isn't technically an emergency number but might
    375      * still result in an emergency call with some networks), we use
    376      * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
    377      *
    378      * @param number number to inspect in order to determine whether or not an emergency number
    379      * is potentially being dialed
    380      * @return True if the handle is potentially an emergency number.
    381      */
    382     private boolean isPotentialEmergencyNumber(String number) {
    383         Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
    384         return (number != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(mContext,
    385                 number);
    386     }
    387 
    388     /**
    389      * Given a call intent and whether or not the number to dial is an emergency number, rewrite
    390      * the call intent action to an appropriate one.
    391      *
    392      * @param intent Intent to rewrite the action for
    393      * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
    394      * number.
    395      */
    396     private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
    397         String action = intent.getAction();
    398 
    399         /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
    400         if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
    401             if (isPotentialEmergencyNumber) {
    402                 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
    403                         + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
    404                 action = Intent.ACTION_CALL_EMERGENCY;
    405             } else {
    406                 action = Intent.ACTION_CALL;
    407             }
    408             Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
    409             intent.setAction(action);
    410         }
    411     }
    412 }
    413