NewOutgoingCallIntentBroadcaster.java revision cce5deb2bc998d6385152292153a9423f5206c28
133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee/* 233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Copyright (C) 2014 The Android Open Source Project 333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License"); 533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * you may not use this file except in compliance with the License. 633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * You may obtain a copy of the License at 733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * http://www.apache.org/licenses/LICENSE-2.0 933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 1033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Unless required by applicable law or agreed to in writing, software 1133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS, 1233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * See the License for the specific language governing permissions and 1433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * limitations under the License. 1533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 1633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 1733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leepackage com.android.telecomm; 1833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 1933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.app.Activity; 2033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.BroadcastReceiver; 2133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.Context; 2233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.Intent; 2333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.res.Resources; 2433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.net.Uri; 2533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.os.UserHandle; 2633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.telecomm.GatewayInfo; 2789176375c8d97db25588f720952a4fadbce2f9a3Evan Charltonimport android.telecomm.PhoneAccountHandle; 28b35fc31be63df5d78183d6c94f53eea45c3542deEvan Charltonimport android.telecomm.TelecommManager; 29c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunnimport android.telecomm.VideoCallProfile; 3033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.telephony.PhoneNumberUtils; 3133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.text.TextUtils; 3233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 3333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee/** 3433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the 3533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which 3633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * contains the phone number being dialed. Applications can use this intent to (1) see which numbers 3733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call 3833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * from being placed. 3933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 4033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it 4133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}. 4233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 4333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail 4433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * number) are exempt from being broadcast. 4533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 4633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Calls to emergency numbers are still broadcast for informative purposes. The call is placed 4733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented. 4833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 4933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeclass NewOutgoingCallIntentBroadcaster { 5033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */ 5133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS; 5233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 5333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL = 5433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL"; 5533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 5633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 5733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Legacy string constants used to retrieve gateway provider extras from intents. These still 5833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * need to be copied from the source call intent to the destination intent in order to 5933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * support third party gateway providers that are still using old string constants in 6033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Telephony. 6133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 6233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE = 6333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE"; 6433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI"; 65571f0737002251f09b9ef696dc8b4c9ce02abbaeSantos Cordon public static final String EXTRA_GATEWAY_ORIGINAL_URI = 66571f0737002251f09b9ef696dc8b4c9ce02abbaeSantos Cordon "com.android.phone.extra.GATEWAY_ORIGINAL_URI"; 6733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 68b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee private static final String SCHEME_TEL = "tel"; 69b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee private static final String SCHEME_SIP = "sip"; 70b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee 7133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private final CallsManager mCallsManager; 7233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private final Intent mIntent; 73cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee /* 74cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee * Whether or not the outgoing call intent originated from the default phone application. If 75cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent. 76cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee */ 77cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee private final boolean mIsDefaultOrSystemPhoneApp; 7833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 79cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Intent intent, 80cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee boolean isDefaultPhoneApp) { 8133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee mCallsManager = callsManager; 8233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee mIntent = intent; 83cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp; 8433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 8533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 8633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 8733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Processes the result of the outgoing call broadcast intent, and performs callbacks to 8833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * the OutgoingCallIntentBroadcasterListener as necessary. 8933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 9033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver { 9133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 9233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee @Override 9333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee public void onReceive(Context context, Intent intent) { 94b33087514fb354c9d10c29f9cb071ed2ae2a6124Sailesh Nepal Log.v(this, "onReceive: %s", intent); 9533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 9633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the 9733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // actual number to call. (If null, no call will be placed.) 9833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee String resultHandle = getResultData(); 9933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle)); 10033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 10133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (resultHandle == null) { 10233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "Call cancelled (null number), returning..."); 10333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return; 1046625545769bd9c5aa15b7e1ee9402f75ad799e81Yorke Lee } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) { 10533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle); 10633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return; 10733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 10833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 109b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee Uri resultHandleUri = Uri.fromParts( 110b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL, 111b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee resultHandle, 112b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee null); 11333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 11433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Uri originalUri = mIntent.getData(); 11533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 11633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (originalUri.getSchemeSpecificPart().equals(resultHandle)) { 11733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "Call handle unmodified after new outgoing call intent broadcast."); 11833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } else { 11933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: " 12033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee + "Original: %s, Modified: %s", 12133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.pii(originalUri), 12233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.pii(resultHandleUri)); 12333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 12433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 12533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri); 12689176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton PhoneAccountHandle accountHandle = getAccountHandleFromIntent(intent); 12789176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton mCallsManager.placeOutgoingCall(resultHandleUri, gatewayInfo, accountHandle, 128b35fc31be63df5d78183d6c94f53eea45c3542deEvan Charlton mIntent.getBooleanExtra(TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, 129c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunn false), 130b35fc31be63df5d78183d6c94f53eea45c3542deEvan Charlton mIntent.getIntExtra(TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE, 131c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunn VideoCallProfile.VIDEO_STATE_AUDIO_ONLY)); 13233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 13333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 13433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 13533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 13633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Processes the supplied intent and starts the outgoing call broadcast process relevant to the 13733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * intent. 13833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 13933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * This method will handle three kinds of actions: 14033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 14133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * - CALL (intent launched by all third party dialers) 14233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer) 14333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * - CALL_EMERGENCY (intent launched by lock screen emergency dialer) 144cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee * 145cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee * @return whether or not the caller was allowed to start the outgoing call. 14633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 147cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee boolean processIntent() { 14833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster."); 14933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 15033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee final Context context = TelecommApp.getInstance(); 15133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Intent intent = mIntent; 15233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 15333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee String handle = PhoneNumberUtils.getNumberFromIntent(intent, context); 15433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 15533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (TextUtils.isEmpty(handle)) { 15633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.w(this, "Empty handle obtained from the call intent."); 157cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee return false; 15833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 15933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 160b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle); 161b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon 162b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon if (!isUriNumber) { 16333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle); 16433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee handle = PhoneNumberUtils.stripSeparators(handle); 16533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 16633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 16733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle); 16833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber); 16933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 17033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee rewriteCallIntentAction(intent, isPotentialEmergencyNumber); 17133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // True for certain types of numbers that are not intended to be intercepted or modified 17233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // by third parties (e.g. emergency numbers). 17333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee boolean callImmediately = false; 17433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 17533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee String action = intent.getAction(); 17633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (Intent.ACTION_CALL.equals(action)) { 17733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (isPotentialEmergencyNumber) { 178cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee if (!mIsDefaultOrSystemPhoneApp) { 179cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s " 180cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee + "unless caller is system or default dialer.", handle, intent); 181cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee launchSystemDialer(context, intent.getData()); 182cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee return false; 183cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee } else { 184cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee callImmediately = true; 185cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee } 18633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 18733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) { 18833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (!isPotentialEmergencyNumber) { 18933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL " 19033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee + "Intent %s.", handle, intent); 191cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee return false; 19233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 19333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee callImmediately = true; 19433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } else { 19533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent); 196cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee return false; 19733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 19833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 19933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (callImmediately) { 20033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.i(this, "Placing call immediately instead of waiting for " 20133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee + " OutgoingCallBroadcastReceiver: %s", intent); 202b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL; 203905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal boolean speakerphoneOn = mIntent.getBooleanExtra( 204b35fc31be63df5d78183d6c94f53eea45c3542deEvan Charlton TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false); 205905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal int videoState = mIntent.getIntExtra( 206b35fc31be63df5d78183d6c94f53eea45c3542deEvan Charlton TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE, 207905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal VideoCallProfile.VIDEO_STATE_AUDIO_ONLY); 208b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon mCallsManager.placeOutgoingCall( 209905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal Uri.fromParts(scheme, handle, null), null, null, speakerphoneOn, videoState); 21033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 21133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast 21233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // so that third parties can still inspect (but not intercept) the outgoing call. When 21333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to 21433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra. 21533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 21633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 21733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee broadcastIntent(intent, handle, context, !callImmediately); 218cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee return true; 21933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 22033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 22133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 22233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Sends a new outgoing call ordered broadcast so that third party apps can cancel the 22333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * placement of the call or redirect it to a different number. 22433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 22533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param originalCallIntent The original call intent. 22633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param handle Call handle that was stored in the original call intent. 22733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param context Valid context to send the ordered broadcast using. 22833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param receiverRequired Whether or not the result from the ordered broadcast should be 22933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * processed using a {@link NewOutgoingCallIntentBroadcaster}. 23033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 23133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private void broadcastIntent( 23233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Intent originalCallIntent, 23333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee String handle, 23433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Context context, 23533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee boolean receiverRequired) { 23633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL); 23733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (handle != null) { 23833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle); 23933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 24033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 24133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // Force receivers of this broadcast intent to run at foreground priority because we 24233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // want to finish processing the broadcast intent as soon as possible. 24333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 24433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "Broadcasting intent: %s.", broadcastIntent); 24533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 24677d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen checkAndCopyProviderExtras(originalCallIntent, broadcastIntent); 24733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 24833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee context.sendOrderedBroadcastAsUser( 24933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee broadcastIntent, 25033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee UserHandle.OWNER, 25133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee PERMISSION, 25233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null, 25333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee null, // scheduler 25433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Activity.RESULT_OK, // initialCode 25533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee handle, // initialData: initial value for the result data (number to be modified) 25633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee null); // initialExtras 25733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 25833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 25933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 26033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the 26133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * source intent to the destination one. 26233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 26333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param src Intent which may contain the provider's extras. 26433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param dst Intent where a copy of the extras will be added if applicable. 26533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 26677d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen public void checkAndCopyProviderExtras(Intent src, Intent dst) { 26777d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen if (src == null) { 26877d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen return; 26977d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen } 27033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (hasGatewayProviderExtras(src)) { 27133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE, 27233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE)); 27333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee dst.putExtra(EXTRA_GATEWAY_URI, 27433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee src.getStringExtra(EXTRA_GATEWAY_URI)); 27533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.d(this, "Found and copied gateway provider extras to broadcast intent."); 27633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return; 27733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 27889176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton PhoneAccountHandle extraAccountHandle = 27989176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton src.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE); 28089176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton if (extraAccountHandle != null) { 28189176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton dst.putExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE, extraAccountHandle); 28298a556026109f816501fa887efdddae78d0c2d65Ihab Awad Log.d(this, "Found and copied account extra to broadcast intent."); 28377d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen } 28433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 28577d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen Log.d(this, "No provider extras found in call intent."); 28633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 28733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 28833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 28933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Check if valid gateway provider information is stored as extras in the intent 29033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 29133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param intent to check for 29233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @return true if the intent has all the gateway information extras needed. 29333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 29433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private boolean hasGatewayProviderExtras(Intent intent) { 29533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); 29633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI); 29733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 29833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString); 29933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 30033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 30133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private static Uri getGatewayUriFromString(String gatewayUriString) { 30233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString); 30333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 30433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 30533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 30633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Extracts gateway provider information from a provided intent.. 30733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 30833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param intent to extract gateway provider information from. 30933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param trueHandle The actual call handle that the user is trying to dial 31033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @return GatewayInfo object containing extracted gateway provider information as well as 31133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * the actual handle the user is trying to dial. 31233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 31333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) { 31433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (intent == null) { 31533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return null; 31633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 31733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 31833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // Check if gateway extras are present. 31933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); 32033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI)); 32133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) { 32233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle); 32333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 32433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 32533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee return null; 32633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 32733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 32877d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen /** 32998a556026109f816501fa887efdddae78d0c2d65Ihab Awad * Extracts account/connection provider information from a provided intent.. 33077d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen * 33198a556026109f816501fa887efdddae78d0c2d65Ihab Awad * @param intent to extract account information from. 33298a556026109f816501fa887efdddae78d0c2d65Ihab Awad * @return Account object containing extracted account information 33377d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen */ 33489176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton public static PhoneAccountHandle getAccountHandleFromIntent(Intent intent) { 33577d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen if (intent == null) { 33677d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen return null; 33777d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen } 33877d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen 33989176375c8d97db25588f720952a4fadbce2f9a3Evan Charlton return intent.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE); 34077d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen } 34177d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen 34233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private void launchSystemDialer(Context context, Uri handle) { 34333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Intent systemDialerIntent = new Intent(); 34433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee final Resources resources = context.getResources(); 34533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee systemDialerIntent.setClassName( 34633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee resources.getString(R.string.ui_default_package), 34733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee resources.getString(R.string.dialer_default_class)); 34833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee systemDialerIntent.setAction(Intent.ACTION_DIAL); 34933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee systemDialerIntent.setData(handle); 35033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 35133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent); 35233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee context.startActivity(systemDialerIntent); 35333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 35433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 35533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 35633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Check whether or not this is an emergency number, in order to enforce the restriction 35733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency 35833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * calls. 35933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 36033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * To prevent malicious 3rd party apps from making emergency calls by passing in an 36133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * "invalid" number like "9111234" (that isn't technically an emergency number but might 36233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * still result in an emergency call with some networks), we use 36333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber. 36433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 36533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param context Valid context 36633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param handle Handle to inspect in order to determine whether or not an emergency number 36733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * is potentially being dialed 36833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @return True if the handle is potentially an emergency number. 36933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 37033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private boolean isPotentialEmergencyNumber(Context context, String handle) { 37133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, "Checking restrictions for number : %s", Log.pii(handle)); 3726625545769bd9c5aa15b7e1ee9402f75ad799e81Yorke Lee return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle); 37333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 37433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 37533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /** 37633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Given a call intent and whether or not the number to dial is an emergency number, rewrite 37733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * the call intent action to an appropriate one. 37833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * 37933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param intent Intent to rewrite the action for 38033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency 38133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * number. 38233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */ 38333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) { 38433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) { 38533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // If we were launched directly from the CallActivity, not one of its more privileged 38633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee // aliases, then make sure that only the non-privileged actions are allowed. 38733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (!Intent.ACTION_CALL.equals(intent.getAction())) { 38833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL"); 38933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee intent.setAction(Intent.ACTION_CALL); 39033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 39133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 39233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 39333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee String action = intent.getAction(); 39433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee 39533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */ 39633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) { 39733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee if (isPotentialEmergencyNumber) { 39833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential" 39933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead."); 40033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee action = Intent.ACTION_CALL_EMERGENCY; 40133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } else { 40233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee action = Intent.ACTION_CALL; 40333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 40433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action); 40533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee intent.setAction(action); 40633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 40733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee } 40833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee} 409