blob: 2e5b0e1353f836d3ac7fd1b9b6cdf956fb609985 [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 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
Tyler Gunn7cc70b42014-09-12 22:17:27 -070059 "android.telecom.extra.ACTUAL_NUMBER_TO_DIAL";
Yorke Lee33501632014-03-17 19:24:12 -070060
61 /**
62 * Legacy string constants used to retrieve gateway provider extras from intents. These still
63 * need to be copied from the source call intent to the destination intent in order to
64 * support third party gateway providers that are still using old string constants in
65 * Telephony.
66 */
67 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
68 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
69 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
Santos Cordon571f0732014-06-25 18:13:15 -070070 public static final String EXTRA_GATEWAY_ORIGINAL_URI =
71 "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
Yorke Lee33501632014-03-17 19:24:12 -070072
73 private final CallsManager mCallsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070074 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070075 private final Intent mIntent;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070076 private final Context mContext;
Hall Liu220b4192015-12-11 11:33:08 -080077 private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070078
Yorke Leecce5deb2014-06-18 11:27:42 -070079 /*
80 * Whether or not the outgoing call intent originated from the default phone application. If
81 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
82 */
83 private final boolean mIsDefaultOrSystemPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070084
Hall Liu220b4192015-12-11 11:33:08 -080085 @VisibleForTesting
86 public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
87 Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
88 boolean isDefaultPhoneApp) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -070089 mContext = context;
Yorke Lee33501632014-03-17 19:24:12 -070090 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070091 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070092 mIntent = intent;
Hall Liu220b4192015-12-11 11:33:08 -080093 mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
Yorke Leecce5deb2014-06-18 11:27:42 -070094 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070095 }
96
97 /**
98 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
99 * the OutgoingCallIntentBroadcasterListener as necessary.
100 */
Hall Liu220b4192015-12-11 11:33:08 -0800101 public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
Yorke Lee33501632014-03-17 19:24:12 -0700102
103 @Override
104 public void onReceive(Context context, Intent intent) {
Brad Ebinger11623a32015-11-25 13:52:02 -0800105 try {
106 Log.startSession("NOCBIR.oR");
107 Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
108 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700109
Brad Ebinger11623a32015-11-25 13:52:02 -0800110 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
111 // actual number to call. (If null, no call will be placed.)
112 String resultNumber = getResultData();
113 Log.i(this, "Received new-outgoing-call-broadcast for %s with data %s", mCall,
114 Log.pii(resultNumber));
Yorke Lee33501632014-03-17 19:24:12 -0700115
Brad Ebinger11623a32015-11-25 13:52:02 -0800116 boolean endEarly = false;
117 if (resultNumber == null) {
118 Log.v(this, "Call cancelled (null number), returning...");
119 endEarly = true;
Hall Liu220b4192015-12-11 11:33:08 -0800120 } else if (mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(
Brad Ebinger11623a32015-11-25 13:52:02 -0800121 mContext, resultNumber)) {
122 Log.w(this, "Cannot modify outgoing call to emergency number %s.",
123 resultNumber);
124 endEarly = true;
Santos Cordon2d0b3312014-08-15 15:04:17 -0700125 }
Brad Ebinger11623a32015-11-25 13:52:02 -0800126
127 if (endEarly) {
128 if (mCall != null) {
129 mCall.disconnect(true /* wasViaNewOutgoingCall */);
130 }
131 return;
132 }
133
Hall Liu220b4192015-12-11 11:33:08 -0800134 Uri resultHandleUri = Uri.fromParts(
135 mPhoneNumberUtilsAdapter.isUriNumber(resultNumber) ?
136 PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL,
137 resultNumber, null);
Brad Ebinger11623a32015-11-25 13:52:02 -0800138
139 Uri originalUri = mIntent.getData();
140
141 if (originalUri.getSchemeSpecificPart().equals(resultNumber)) {
142 Log.v(this, "Call number unmodified after new outgoing call intent broadcast.");
143 } else {
144 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
145 + "Original: %s, Modified: %s",
146 Log.pii(originalUri),
147 Log.pii(resultHandleUri));
148 }
149
150 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Hall Liu4b1759d2016-02-02 18:17:40 -0800151 mCall.setNewOutgoingCallIntentBroadcastIsDone();
Brad Ebinger11623a32015-11-25 13:52:02 -0800152 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo,
153 mIntent.getBooleanExtra(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE,
154 false),
155 mIntent.getIntExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
156 VideoProfile.STATE_AUDIO_ONLY));
157
158 } finally {
Yorke Leee4a9c412014-11-14 16:59:42 -0800159 Trace.endSection();
Brad Ebinger11623a32015-11-25 13:52:02 -0800160 Log.endSession();
Yorke Lee33501632014-03-17 19:24:12 -0700161 }
Yorke Lee33501632014-03-17 19:24:12 -0700162 }
163 }
164
165 /**
166 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
167 * intent.
168 *
169 * This method will handle three kinds of actions:
170 *
171 * - CALL (intent launched by all third party dialers)
172 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
173 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Leecce5deb2014-06-18 11:27:42 -0700174 *
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700175 * @return {@link DisconnectCause#NOT_DISCONNECTED} if the call succeeded, and an appropriate
176 * {@link DisconnectCause} if the call did not, describing why it failed.
Yorke Lee33501632014-03-17 19:24:12 -0700177 */
Hall Liu220b4192015-12-11 11:33:08 -0800178 @VisibleForTesting
179 public int processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700180 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
181
Yorke Lee33501632014-03-17 19:24:12 -0700182 Intent intent = mIntent;
Nancy Chen308ab8b2014-09-02 16:18:30 -0700183 String action = intent.getAction();
184 final Uri handle = intent.getData();
Yorke Lee33501632014-03-17 19:24:12 -0700185
Nancy Chen308ab8b2014-09-02 16:18:30 -0700186 if (handle == null) {
187 Log.w(this, "Empty handle obtained from the call intent.");
188 return DisconnectCause.INVALID_NUMBER;
189 }
Yorke Lee33501632014-03-17 19:24:12 -0700190
Jay Shrauner56a76b72014-09-05 14:41:48 -0700191 boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
Nancy Chen308ab8b2014-09-02 16:18:30 -0700192 if (isVoicemailNumber) {
Yorke Leefe1ce0a2014-11-20 08:59:46 -0800193 if (Intent.ACTION_CALL.equals(action)
194 || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700195 // Voicemail calls will be handled directly by the telephony connection manager
196 Log.i(this, "Placing call immediately instead of waiting for "
197 + " OutgoingCallBroadcastReceiver: %s", intent);
198
Santos Cordon5f048fe2016-04-04 17:47:20 -0700199 // Since we are not going to go through "Outgoing call broadcast", make sure
200 // we mark it as ready.
201 mCall.setNewOutgoingCallIntentBroadcastIsDone();
202
Nancy Chen308ab8b2014-09-02 16:18:30 -0700203 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700204 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700205 mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
Tyler Gunn5b882492015-06-03 10:03:13 -0700206 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700207
208 return DisconnectCause.NOT_DISCONNECTED;
Yorke Leed7255872014-08-25 15:03:51 -0700209 } else {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700210 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
211 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leed7255872014-08-25 15:03:51 -0700212 }
Yorke Lee33501632014-03-17 19:24:12 -0700213 }
214
Hall Liu220b4192015-12-11 11:33:08 -0800215 String number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700216 if (TextUtils.isEmpty(number)) {
217 Log.w(this, "Empty number obtained from the call intent.");
218 return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
Yorke Lee33501632014-03-17 19:24:12 -0700219 }
220
Hall Liu220b4192015-12-11 11:33:08 -0800221 boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700222 if (!isUriNumber) {
Hall Liu220b4192015-12-11 11:33:08 -0800223 number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number);
224 number = mPhoneNumberUtilsAdapter.stripSeparators(number);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700225 }
226
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700227 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number);
Yorke Lee33501632014-03-17 19:24:12 -0700228 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
229
230 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
Yorke Leee05257c2014-09-08 18:08:44 -0700231 action = intent.getAction();
Yorke Lee33501632014-03-17 19:24:12 -0700232 // True for certain types of numbers that are not intended to be intercepted or modified
233 // by third parties (e.g. emergency numbers).
234 boolean callImmediately = false;
235
Yorke Lee33501632014-03-17 19:24:12 -0700236 if (Intent.ACTION_CALL.equals(action)) {
237 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700238 if (!mIsDefaultOrSystemPhoneApp) {
239 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
Nancy Chen308ab8b2014-09-02 16:18:30 -0700240 + "unless caller is system or default dialer.", number, intent);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700241 launchSystemDialer(intent.getData());
Yorke Leed7255872014-08-25 15:03:51 -0700242 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leecce5deb2014-06-18 11:27:42 -0700243 } else {
244 callImmediately = true;
245 }
Yorke Lee33501632014-03-17 19:24:12 -0700246 }
Yorke Lee33501632014-03-17 19:24:12 -0700247 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
248 if (!isPotentialEmergencyNumber) {
249 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
Nancy Chen308ab8b2014-09-02 16:18:30 -0700250 + "Intent %s.", number, intent);
Yorke Leed7255872014-08-25 15:03:51 -0700251 return DisconnectCause.OUTGOING_CANCELED;
Yorke Lee33501632014-03-17 19:24:12 -0700252 }
253 callImmediately = true;
254 } else {
255 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed7255872014-08-25 15:03:51 -0700256 return DisconnectCause.INVALID_NUMBER;
Yorke Lee33501632014-03-17 19:24:12 -0700257 }
258
259 if (callImmediately) {
260 Log.i(this, "Placing call immediately instead of waiting for "
261 + " OutgoingCallBroadcastReceiver: %s", intent);
Jay Shrauner56a76b72014-09-05 14:41:48 -0700262 String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700263 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700264 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700265 int videoState = mIntent.getIntExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700266 TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Tyler Gunn5b882492015-06-03 10:03:13 -0700267 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700268 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700269 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700270
271 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
272 // so that third parties can still inspect (but not intercept) the outgoing call. When
273 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
274 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
275 }
276
Tony Makd3bacb72016-02-03 15:35:27 +0000277 UserHandle targetUser = mCall.getInitiatingUser();
278 Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser);
279 broadcastIntent(intent, number, !callImmediately, targetUser);
Yorke Leed7255872014-08-25 15:03:51 -0700280 return DisconnectCause.NOT_DISCONNECTED;
Yorke Lee33501632014-03-17 19:24:12 -0700281 }
282
283 /**
284 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
285 * placement of the call or redirect it to a different number.
286 *
287 * @param originalCallIntent The original call intent.
Nancy Chen308ab8b2014-09-02 16:18:30 -0700288 * @param number Call number that was stored in the original call intent.
Yorke Lee33501632014-03-17 19:24:12 -0700289 * @param receiverRequired Whether or not the result from the ordered broadcast should be
Tony Makd3bacb72016-02-03 15:35:27 +0000290 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
291 * @param targetUser User that the broadcast sent to.
Yorke Lee33501632014-03-17 19:24:12 -0700292 */
293 private void broadcastIntent(
294 Intent originalCallIntent,
Nancy Chen308ab8b2014-09-02 16:18:30 -0700295 String number,
Tony Makd3bacb72016-02-03 15:35:27 +0000296 boolean receiverRequired,
297 UserHandle targetUser) {
Yorke Lee33501632014-03-17 19:24:12 -0700298 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700299 if (number != null) {
300 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
Yorke Lee33501632014-03-17 19:24:12 -0700301 }
302
303 // Force receivers of this broadcast intent to run at foreground priority because we
304 // want to finish processing the broadcast intent as soon as possible.
305 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
306 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
307
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700308 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700309
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700310 mContext.sendOrderedBroadcastAsUser(
Yorke Lee33501632014-03-17 19:24:12 -0700311 broadcastIntent,
Tony Makd3bacb72016-02-03 15:35:27 +0000312 targetUser,
Svetoslav973c4e12015-04-29 14:13:51 -0700313 android.Manifest.permission.PROCESS_OUTGOING_CALLS,
314 AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
Yorke Lee33501632014-03-17 19:24:12 -0700315 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
316 null, // scheduler
317 Activity.RESULT_OK, // initialCode
Nancy Chen308ab8b2014-09-02 16:18:30 -0700318 number, // initialData: initial value for the result data (number to be modified)
Yorke Lee33501632014-03-17 19:24:12 -0700319 null); // initialExtras
320 }
321
322 /**
323 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
324 * source intent to the destination one.
325 *
326 * @param src Intent which may contain the provider's extras.
327 * @param dst Intent where a copy of the extras will be added if applicable.
328 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700329 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
330 if (src == null) {
331 return;
332 }
Yorke Lee33501632014-03-17 19:24:12 -0700333 if (hasGatewayProviderExtras(src)) {
334 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
335 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
336 dst.putExtra(EXTRA_GATEWAY_URI,
337 src.getStringExtra(EXTRA_GATEWAY_URI));
338 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
339 return;
340 }
341
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700342 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700343 }
344
345 /**
346 * Check if valid gateway provider information is stored as extras in the intent
347 *
348 * @param intent to check for
349 * @return true if the intent has all the gateway information extras needed.
350 */
351 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700352 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
353 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
354
355 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
356 }
357
358 private static Uri getGatewayUriFromString(String gatewayUriString) {
359 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
360 }
361
362 /**
363 * Extracts gateway provider information from a provided intent..
364 *
365 * @param intent to extract gateway provider information from.
366 * @param trueHandle The actual call handle that the user is trying to dial
367 * @return GatewayInfo object containing extracted gateway provider information as well as
368 * the actual handle the user is trying to dial.
369 */
370 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
371 if (intent == null) {
372 return null;
373 }
374
375 // Check if gateway extras are present.
376 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
377 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
378 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
379 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
380 }
381
382 return null;
383 }
384
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700385 private void launchSystemDialer(Uri handle) {
Yorke Lee33501632014-03-17 19:24:12 -0700386 Intent systemDialerIntent = new Intent();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700387 final Resources resources = mContext.getResources();
Yorke Lee33501632014-03-17 19:24:12 -0700388 systemDialerIntent.setClassName(
389 resources.getString(R.string.ui_default_package),
390 resources.getString(R.string.dialer_default_class));
391 systemDialerIntent.setAction(Intent.ACTION_DIAL);
392 systemDialerIntent.setData(handle);
393 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
394 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
Yorke Lee39d94c12014-10-08 17:21:28 -0700395 mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
Yorke Lee33501632014-03-17 19:24:12 -0700396 }
397
398 /**
399 * Check whether or not this is an emergency number, in order to enforce the restriction
400 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
401 * calls.
402 *
403 * To prevent malicious 3rd party apps from making emergency calls by passing in an
404 * "invalid" number like "9111234" (that isn't technically an emergency number but might
405 * still result in an emergency call with some networks), we use
406 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
407 *
Nancy Chen308ab8b2014-09-02 16:18:30 -0700408 * @param number number to inspect in order to determine whether or not an emergency number
Yorke Lee33501632014-03-17 19:24:12 -0700409 * is potentially being dialed
410 * @return True if the handle is potentially an emergency number.
411 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700412 private boolean isPotentialEmergencyNumber(String number) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700413 Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
Hall Liu220b4192015-12-11 11:33:08 -0800414 return (number != null)
415 && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number);
Yorke Lee33501632014-03-17 19:24:12 -0700416 }
417
418 /**
419 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
420 * the call intent action to an appropriate one.
421 *
422 * @param intent Intent to rewrite the action for
Nancy Chen308ab8b2014-09-02 16:18:30 -0700423 * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
Yorke Lee33501632014-03-17 19:24:12 -0700424 * number.
425 */
426 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700427 String action = intent.getAction();
428
429 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
430 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
431 if (isPotentialEmergencyNumber) {
432 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
433 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
434 action = Intent.ACTION_CALL_EMERGENCY;
435 } else {
436 action = Intent.ACTION_CALL;
437 }
438 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
439 intent.setAction(action);
440 }
441 }
442}