blob: 8b5604b67cf6bf68dbcb3b15f4434f32eb0183f2 [file] [log] [blame]
Yorke Lee33501632014-03-17 19:24:12 -07001/*
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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Yorke Lee33501632014-03-17 19:24:12 -070018
Svetoslav973c4e12015-04-29 14:13:51 -070019import android.app.AppOpsManager;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070020
Yorke Lee33501632014-03-17 19:24:12 -070021import android.app.Activity;
Yorke Lee33501632014-03-17 19:24:12 -070022import android.content.BroadcastReceiver;
23import android.content.Context;
Yorke Lee33501632014-03-17 19:24:12 -070024import android.content.Intent;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.content.res.Resources;
Yorke Lee33501632014-03-17 19:24:12 -070026import android.net.Uri;
Yorke Leee4a9c412014-11-14 16:59:42 -080027import android.os.Trace;
Yorke Lee33501632014-03-17 19:24:12 -070028import android.os.UserHandle;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.GatewayInfo;
30import android.telecom.PhoneAccount;
31import android.telecom.TelecomManager;
32import android.telecom.VideoProfile;
Yorke Leed7255872014-08-25 15:03:51 -070033import android.telephony.DisconnectCause;
Yorke Lee33501632014-03-17 19:24:12 -070034import android.text.TextUtils;
Yorke Lee33501632014-03-17 19:24:12 -070035
Hall Liu220b4192015-12-11 11:33:08 -080036import com.android.internal.annotations.VisibleForTesting;
37
Tyler Gunn91d43cf2014-09-17 12:19:39 -070038// TODO: Needed for move to system service: import com.android.internal.R;
39
Yorke Lee33501632014-03-17 19:24:12 -070040/**
41 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
42 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
43 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
44 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
45 * from being placed.
46 *
47 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
48 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
49 *
50 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
51 * number) are exempt from being broadcast.
52 *
53 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
54 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
55 */
Hall Liu220b4192015-12-11 11:33:08 -080056@VisibleForTesting
57public class NewOutgoingCallIntentBroadcaster {
Yorke Lee33501632014-03-17 19:24:12 -070058 /**
59 * Legacy string constants used to retrieve gateway provider extras from intents. These still
60 * need to be copied from the source call intent to the destination intent in order to
61 * support third party gateway providers that are still using old string constants in
62 * Telephony.
63 */
64 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
65 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
66 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
67
68 private final CallsManager mCallsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070069 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070070 private final Intent mIntent;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070071 private final Context mContext;
Hall Liu220b4192015-12-11 11:33:08 -080072 private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
Hall Liuc9cf5442016-06-29 10:08:10 -070073 private final TelecomSystem.SyncRoot mLock;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070074
Yorke Leecce5deb2014-06-18 11:27:42 -070075 /*
76 * Whether or not the outgoing call intent originated from the default phone application. If
77 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
78 */
79 private final boolean mIsDefaultOrSystemPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070080
Hall Liu220b4192015-12-11 11:33:08 -080081 @VisibleForTesting
82 public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
83 Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
84 boolean isDefaultPhoneApp) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -070085 mContext = context;
Yorke Lee33501632014-03-17 19:24:12 -070086 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070087 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070088 mIntent = intent;
Hall Liu220b4192015-12-11 11:33:08 -080089 mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
Yorke Leecce5deb2014-06-18 11:27:42 -070090 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Hall Liuc9cf5442016-06-29 10:08:10 -070091 mLock = mCallsManager.getLock();
Yorke Lee33501632014-03-17 19:24:12 -070092 }
93
94 /**
95 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
96 * the OutgoingCallIntentBroadcasterListener as necessary.
97 */
Hall Liu220b4192015-12-11 11:33:08 -080098 public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
Yorke Lee33501632014-03-17 19:24:12 -070099
100 @Override
101 public void onReceive(Context context, Intent intent) {
Brad Ebinger11623a32015-11-25 13:52:02 -0800102 try {
103 Log.startSession("NOCBIR.oR");
104 Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
Hall Liuc9cf5442016-06-29 10:08:10 -0700105 synchronized (mLock) {
106 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700107
Hall Liuc9cf5442016-06-29 10:08:10 -0700108 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is
109 // used as the actual number to call. (If null, no call will be placed.)
110 String resultNumber = getResultData();
111 Log.i(this, "Received new-outgoing-call-broadcast for %s with data %s", mCall,
112 Log.pii(resultNumber));
Yorke Lee33501632014-03-17 19:24:12 -0700113
Hall Liuc9cf5442016-06-29 10:08:10 -0700114 boolean endEarly = false;
115 if (resultNumber == null) {
116 Log.v(this, "Call cancelled (null number), returning...");
117 endEarly = true;
118 } else if (mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(
119 mContext, resultNumber)) {
120 Log.w(this, "Cannot modify outgoing call to emergency number %s.",
121 resultNumber);
122 endEarly = true;
Brad Ebinger11623a32015-11-25 13:52:02 -0800123 }
Hall Liuc9cf5442016-06-29 10:08:10 -0700124
125 if (endEarly) {
126 if (mCall != null) {
127 mCall.disconnect(true /* wasViaNewOutgoingCall */);
128 }
129 return;
130 }
131
132 // If this call is already disconnected then we have nothing more to do.
133 if (mCall.isDisconnected()) {
134 Log.w(this, "Call has already been disconnected," +
135 " ignore the broadcast Call %s", mCall);
136 return;
137 }
138
139 Uri resultHandleUri = Uri.fromParts(
140 mPhoneNumberUtilsAdapter.isUriNumber(resultNumber) ?
141 PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL,
142 resultNumber, null);
143
144 Uri originalUri = mIntent.getData();
145
146 if (originalUri.getSchemeSpecificPart().equals(resultNumber)) {
147 Log.v(this, "Call number unmodified after" +
148 " new outgoing call intent broadcast.");
149 } else {
150 Log.v(this, "Retrieved modified handle after outgoing call intent" +
151 " broadcast: Original: %s, Modified: %s",
152 Log.pii(originalUri),
153 Log.pii(resultHandleUri));
154 }
155
156 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
157 mCall.setNewOutgoingCallIntentBroadcastIsDone();
158 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo,
159 mIntent.getBooleanExtra(
160 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false),
161 mIntent.getIntExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
162 VideoProfile.STATE_AUDIO_ONLY));
Brad Ebinger11623a32015-11-25 13:52:02 -0800163 }
Brad Ebinger11623a32015-11-25 13:52:02 -0800164 } finally {
Yorke Leee4a9c412014-11-14 16:59:42 -0800165 Trace.endSection();
Brad Ebinger11623a32015-11-25 13:52:02 -0800166 Log.endSession();
Yorke Lee33501632014-03-17 19:24:12 -0700167 }
Yorke Lee33501632014-03-17 19:24:12 -0700168 }
169 }
170
171 /**
172 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
173 * intent.
174 *
175 * This method will handle three kinds of actions:
176 *
177 * - CALL (intent launched by all third party dialers)
178 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
179 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Leecce5deb2014-06-18 11:27:42 -0700180 *
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700181 * @return {@link DisconnectCause#NOT_DISCONNECTED} if the call succeeded, and an appropriate
182 * {@link DisconnectCause} if the call did not, describing why it failed.
Yorke Lee33501632014-03-17 19:24:12 -0700183 */
Hall Liu220b4192015-12-11 11:33:08 -0800184 @VisibleForTesting
185 public int processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700186 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
187
Yorke Lee33501632014-03-17 19:24:12 -0700188 Intent intent = mIntent;
Nancy Chen308ab8b2014-09-02 16:18:30 -0700189 String action = intent.getAction();
190 final Uri handle = intent.getData();
Yorke Lee33501632014-03-17 19:24:12 -0700191
Nancy Chen308ab8b2014-09-02 16:18:30 -0700192 if (handle == null) {
193 Log.w(this, "Empty handle obtained from the call intent.");
194 return DisconnectCause.INVALID_NUMBER;
195 }
Yorke Lee33501632014-03-17 19:24:12 -0700196
Jay Shrauner56a76b72014-09-05 14:41:48 -0700197 boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
Nancy Chen308ab8b2014-09-02 16:18:30 -0700198 if (isVoicemailNumber) {
Yorke Leefe1ce0a2014-11-20 08:59:46 -0800199 if (Intent.ACTION_CALL.equals(action)
200 || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700201 // Voicemail calls will be handled directly by the telephony connection manager
202 Log.i(this, "Placing call immediately instead of waiting for "
203 + " OutgoingCallBroadcastReceiver: %s", intent);
204
Santos Cordon5f048fe2016-04-04 17:47:20 -0700205 // Since we are not going to go through "Outgoing call broadcast", make sure
206 // we mark it as ready.
207 mCall.setNewOutgoingCallIntentBroadcastIsDone();
208
Nancy Chen308ab8b2014-09-02 16:18:30 -0700209 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700210 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700211 mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
Tyler Gunn5b882492015-06-03 10:03:13 -0700212 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700213
214 return DisconnectCause.NOT_DISCONNECTED;
Yorke Leed7255872014-08-25 15:03:51 -0700215 } else {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700216 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
217 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leed7255872014-08-25 15:03:51 -0700218 }
Yorke Lee33501632014-03-17 19:24:12 -0700219 }
220
Hall Liu220b4192015-12-11 11:33:08 -0800221 String number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700222 if (TextUtils.isEmpty(number)) {
223 Log.w(this, "Empty number obtained from the call intent.");
224 return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
Yorke Lee33501632014-03-17 19:24:12 -0700225 }
226
Hall Liu220b4192015-12-11 11:33:08 -0800227 boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700228 if (!isUriNumber) {
Hall Liu220b4192015-12-11 11:33:08 -0800229 number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number);
230 number = mPhoneNumberUtilsAdapter.stripSeparators(number);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700231 }
232
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700233 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number);
Yorke Lee33501632014-03-17 19:24:12 -0700234 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
235
236 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
Yorke Leee05257c2014-09-08 18:08:44 -0700237 action = intent.getAction();
Yorke Lee33501632014-03-17 19:24:12 -0700238 // True for certain types of numbers that are not intended to be intercepted or modified
239 // by third parties (e.g. emergency numbers).
240 boolean callImmediately = false;
241
Yorke Lee33501632014-03-17 19:24:12 -0700242 if (Intent.ACTION_CALL.equals(action)) {
243 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700244 if (!mIsDefaultOrSystemPhoneApp) {
245 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
Nancy Chen308ab8b2014-09-02 16:18:30 -0700246 + "unless caller is system or default dialer.", number, intent);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700247 launchSystemDialer(intent.getData());
Yorke Leed7255872014-08-25 15:03:51 -0700248 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leecce5deb2014-06-18 11:27:42 -0700249 } else {
250 callImmediately = true;
251 }
Yorke Lee33501632014-03-17 19:24:12 -0700252 }
Yorke Lee33501632014-03-17 19:24:12 -0700253 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
254 if (!isPotentialEmergencyNumber) {
255 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
Nancy Chen308ab8b2014-09-02 16:18:30 -0700256 + "Intent %s.", number, intent);
Yorke Leed7255872014-08-25 15:03:51 -0700257 return DisconnectCause.OUTGOING_CANCELED;
Yorke Lee33501632014-03-17 19:24:12 -0700258 }
259 callImmediately = true;
260 } else {
261 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed7255872014-08-25 15:03:51 -0700262 return DisconnectCause.INVALID_NUMBER;
Yorke Lee33501632014-03-17 19:24:12 -0700263 }
264
265 if (callImmediately) {
266 Log.i(this, "Placing call immediately instead of waiting for "
267 + " OutgoingCallBroadcastReceiver: %s", intent);
Jay Shrauner56a76b72014-09-05 14:41:48 -0700268 String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700269 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700270 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700271 int videoState = mIntent.getIntExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700272 TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Tyler Gunn5b882492015-06-03 10:03:13 -0700273 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700274 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700275 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700276
277 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
278 // so that third parties can still inspect (but not intercept) the outgoing call. When
279 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
280 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
281 }
282
Tony Makd3bacb72016-02-03 15:35:27 +0000283 UserHandle targetUser = mCall.getInitiatingUser();
284 Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser);
285 broadcastIntent(intent, number, !callImmediately, targetUser);
Yorke Leed7255872014-08-25 15:03:51 -0700286 return DisconnectCause.NOT_DISCONNECTED;
Yorke Lee33501632014-03-17 19:24:12 -0700287 }
288
289 /**
290 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
291 * placement of the call or redirect it to a different number.
292 *
293 * @param originalCallIntent The original call intent.
Nancy Chen308ab8b2014-09-02 16:18:30 -0700294 * @param number Call number that was stored in the original call intent.
Yorke Lee33501632014-03-17 19:24:12 -0700295 * @param receiverRequired Whether or not the result from the ordered broadcast should be
Tony Makd3bacb72016-02-03 15:35:27 +0000296 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
297 * @param targetUser User that the broadcast sent to.
Yorke Lee33501632014-03-17 19:24:12 -0700298 */
299 private void broadcastIntent(
300 Intent originalCallIntent,
Nancy Chen308ab8b2014-09-02 16:18:30 -0700301 String number,
Tony Makd3bacb72016-02-03 15:35:27 +0000302 boolean receiverRequired,
303 UserHandle targetUser) {
Yorke Lee33501632014-03-17 19:24:12 -0700304 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700305 if (number != null) {
306 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
Yorke Lee33501632014-03-17 19:24:12 -0700307 }
308
309 // Force receivers of this broadcast intent to run at foreground priority because we
310 // want to finish processing the broadcast intent as soon as possible.
311 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
312 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
313
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700314 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700315
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700316 mContext.sendOrderedBroadcastAsUser(
Yorke Lee33501632014-03-17 19:24:12 -0700317 broadcastIntent,
Tony Makd3bacb72016-02-03 15:35:27 +0000318 targetUser,
Svetoslav973c4e12015-04-29 14:13:51 -0700319 android.Manifest.permission.PROCESS_OUTGOING_CALLS,
320 AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
Yorke Lee33501632014-03-17 19:24:12 -0700321 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
322 null, // scheduler
323 Activity.RESULT_OK, // initialCode
Nancy Chen308ab8b2014-09-02 16:18:30 -0700324 number, // initialData: initial value for the result data (number to be modified)
Yorke Lee33501632014-03-17 19:24:12 -0700325 null); // initialExtras
326 }
327
328 /**
329 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
330 * source intent to the destination one.
331 *
332 * @param src Intent which may contain the provider's extras.
333 * @param dst Intent where a copy of the extras will be added if applicable.
334 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700335 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
336 if (src == null) {
337 return;
338 }
Yorke Lee33501632014-03-17 19:24:12 -0700339 if (hasGatewayProviderExtras(src)) {
340 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
341 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
342 dst.putExtra(EXTRA_GATEWAY_URI,
343 src.getStringExtra(EXTRA_GATEWAY_URI));
344 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
345 return;
346 }
347
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700348 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700349 }
350
351 /**
352 * Check if valid gateway provider information is stored as extras in the intent
353 *
354 * @param intent to check for
355 * @return true if the intent has all the gateway information extras needed.
356 */
357 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700358 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
359 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
360
361 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
362 }
363
364 private static Uri getGatewayUriFromString(String gatewayUriString) {
365 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
366 }
367
368 /**
369 * Extracts gateway provider information from a provided intent..
370 *
371 * @param intent to extract gateway provider information from.
372 * @param trueHandle The actual call handle that the user is trying to dial
373 * @return GatewayInfo object containing extracted gateway provider information as well as
374 * the actual handle the user is trying to dial.
375 */
376 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
377 if (intent == null) {
378 return null;
379 }
380
381 // Check if gateway extras are present.
382 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
383 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
384 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
385 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
386 }
387
388 return null;
389 }
390
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700391 private void launchSystemDialer(Uri handle) {
Yorke Lee33501632014-03-17 19:24:12 -0700392 Intent systemDialerIntent = new Intent();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700393 final Resources resources = mContext.getResources();
Yorke Lee33501632014-03-17 19:24:12 -0700394 systemDialerIntent.setClassName(
395 resources.getString(R.string.ui_default_package),
396 resources.getString(R.string.dialer_default_class));
397 systemDialerIntent.setAction(Intent.ACTION_DIAL);
398 systemDialerIntent.setData(handle);
399 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
400 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
Yorke Lee39d94c12014-10-08 17:21:28 -0700401 mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
Yorke Lee33501632014-03-17 19:24:12 -0700402 }
403
404 /**
405 * Check whether or not this is an emergency number, in order to enforce the restriction
406 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
407 * calls.
408 *
409 * To prevent malicious 3rd party apps from making emergency calls by passing in an
410 * "invalid" number like "9111234" (that isn't technically an emergency number but might
411 * still result in an emergency call with some networks), we use
412 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
413 *
Nancy Chen308ab8b2014-09-02 16:18:30 -0700414 * @param number number to inspect in order to determine whether or not an emergency number
Yorke Lee33501632014-03-17 19:24:12 -0700415 * is potentially being dialed
416 * @return True if the handle is potentially an emergency number.
417 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700418 private boolean isPotentialEmergencyNumber(String number) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700419 Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
Hall Liu220b4192015-12-11 11:33:08 -0800420 return (number != null)
421 && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number);
Yorke Lee33501632014-03-17 19:24:12 -0700422 }
423
424 /**
425 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
426 * the call intent action to an appropriate one.
427 *
428 * @param intent Intent to rewrite the action for
Nancy Chen308ab8b2014-09-02 16:18:30 -0700429 * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
Yorke Lee33501632014-03-17 19:24:12 -0700430 * number.
431 */
432 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700433 String action = intent.getAction();
434
435 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
436 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
437 if (isPotentialEmergencyNumber) {
438 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
439 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
440 action = Intent.ACTION_CALL_EMERGENCY;
441 } else {
442 action = Intent.ACTION_CALL;
443 }
444 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
445 intent.setAction(action);
446 }
447 }
448}