• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AppOpsManager;
20 
21 import android.app.Activity;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.res.Resources;
26 import android.net.Uri;
27 import android.os.Trace;
28 import android.os.UserHandle;
29 import android.telecom.GatewayInfo;
30 import android.telecom.PhoneAccount;
31 import android.telecom.TelecomManager;
32 import android.telecom.VideoProfile;
33 import android.telephony.DisconnectCause;
34 import android.text.TextUtils;
35 
36 import com.android.internal.annotations.VisibleForTesting;
37 
38 // TODO: Needed for move to system service: import com.android.internal.R;
39 
40 /**
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  */
56 @VisibleForTesting
57 public class NewOutgoingCallIntentBroadcaster {
58     /**
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;
69     private final Call mCall;
70     private final Intent mIntent;
71     private final Context mContext;
72     private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
73     private final TelecomSystem.SyncRoot mLock;
74 
75     /*
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;
80 
81     @VisibleForTesting
NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call, Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter, boolean isDefaultPhoneApp)82     public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
83             Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
84             boolean isDefaultPhoneApp) {
85         mContext = context;
86         mCallsManager = callsManager;
87         mCall = call;
88         mIntent = intent;
89         mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
90         mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
91         mLock = mCallsManager.getLock();
92     }
93 
94     /**
95      * Processes the result of the outgoing call broadcast intent, and performs callbacks to
96      * the OutgoingCallIntentBroadcasterListener as necessary.
97      */
98     public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
99 
100         @Override
onReceive(Context context, Intent intent)101         public void onReceive(Context context, Intent intent) {
102             try {
103                 Log.startSession("NOCBIR.oR");
104                 Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
105                 synchronized (mLock) {
106                     Log.v(this, "onReceive: %s", intent);
107 
108                     // 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));
113 
114                     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;
123                     }
124 
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));
163                 }
164             } finally {
165                 Trace.endSection();
166                 Log.endSession();
167             }
168         }
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)
180      *
181      * @return {@link DisconnectCause#NOT_DISCONNECTED} if the call succeeded, and an appropriate
182      *         {@link DisconnectCause} if the call did not, describing why it failed.
183      */
184     @VisibleForTesting
processIntent()185     public int processIntent() {
186         Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
187 
188         Intent intent = mIntent;
189         String action = intent.getAction();
190         final Uri handle = intent.getData();
191 
192         if (handle == null) {
193             Log.w(this, "Empty handle obtained from the call intent.");
194             return DisconnectCause.INVALID_NUMBER;
195         }
196 
197         boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
198         if (isVoicemailNumber) {
199             if (Intent.ACTION_CALL.equals(action)
200                     || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
201                 // 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 
205                 // Since we are not going to go through "Outgoing call broadcast", make sure
206                 // we mark it as ready.
207                 mCall.setNewOutgoingCallIntentBroadcastIsDone();
208 
209                 boolean speakerphoneOn = mIntent.getBooleanExtra(
210                         TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
211                 mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
212                         VideoProfile.STATE_AUDIO_ONLY);
213 
214                 return DisconnectCause.NOT_DISCONNECTED;
215             } else {
216                 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
217                 return DisconnectCause.OUTGOING_CANCELED;
218             }
219         }
220 
221         String number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext);
222         if (TextUtils.isEmpty(number)) {
223             Log.w(this, "Empty number obtained from the call intent.");
224             return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
225         }
226 
227         boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number);
228         if (!isUriNumber) {
229             number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number);
230             number = mPhoneNumberUtilsAdapter.stripSeparators(number);
231         }
232 
233         final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number);
234         Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
235 
236         rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
237         action = intent.getAction();
238         // 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 
242         if (Intent.ACTION_CALL.equals(action)) {
243             if (isPotentialEmergencyNumber) {
244                 if (!mIsDefaultOrSystemPhoneApp) {
245                     Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
246                             + "unless caller is system or default dialer.", number, intent);
247                     launchSystemDialer(intent.getData());
248                     return DisconnectCause.OUTGOING_CANCELED;
249                 } else {
250                     callImmediately = true;
251                 }
252             }
253         } 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 "
256                         + "Intent %s.", number, intent);
257                 return DisconnectCause.OUTGOING_CANCELED;
258             }
259             callImmediately = true;
260         } else {
261             Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
262             return DisconnectCause.INVALID_NUMBER;
263         }
264 
265         if (callImmediately) {
266             Log.i(this, "Placing call immediately instead of waiting for "
267                     + " OutgoingCallBroadcastReceiver: %s", intent);
268             String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
269             boolean speakerphoneOn = mIntent.getBooleanExtra(
270                     TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
271             int videoState = mIntent.getIntExtra(
272                     TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
273                     VideoProfile.STATE_AUDIO_ONLY);
274             mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
275                     speakerphoneOn, videoState);
276 
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 
283         UserHandle targetUser = mCall.getInitiatingUser();
284         Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser);
285         broadcastIntent(intent, number, !callImmediately, targetUser);
286         return DisconnectCause.NOT_DISCONNECTED;
287     }
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.
294      * @param number Call number that was stored in the original call intent.
295      * @param receiverRequired Whether or not the result from the ordered broadcast should be
296      *                         processed using a {@link NewOutgoingCallIntentBroadcaster}.
297      * @param targetUser User that the broadcast sent to.
298      */
broadcastIntent( Intent originalCallIntent, String number, boolean receiverRequired, UserHandle targetUser)299     private void broadcastIntent(
300             Intent originalCallIntent,
301             String number,
302             boolean receiverRequired,
303             UserHandle targetUser) {
304         Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
305         if (number != null) {
306             broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
307         }
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 
314         checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
315 
316         mContext.sendOrderedBroadcastAsUser(
317                 broadcastIntent,
318                 targetUser,
319                 android.Manifest.permission.PROCESS_OUTGOING_CALLS,
320                 AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
321                 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
322                 null,  // scheduler
323                 Activity.RESULT_OK,  // initialCode
324                 number,  // initialData: initial value for the result data (number to be modified)
325                 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      */
checkAndCopyProviderExtras(Intent src, Intent dst)335     public void checkAndCopyProviderExtras(Intent src, Intent dst) {
336         if (src == null) {
337             return;
338         }
339         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 
348         Log.d(this, "No provider extras found in call intent.");
349     }
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      */
hasGatewayProviderExtras(Intent intent)357     private boolean hasGatewayProviderExtras(Intent intent) {
358         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 
getGatewayUriFromString(String gatewayUriString)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      */
getGateWayInfoFromIntent(Intent intent, Uri trueHandle)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 
launchSystemDialer(Uri handle)391     private void launchSystemDialer(Uri handle) {
392         Intent systemDialerIntent = new Intent();
393         final Resources resources = mContext.getResources();
394         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);
401         mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
402     }
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      *
414      * @param number number to inspect in order to determine whether or not an emergency number
415      * is potentially being dialed
416      * @return True if the handle is potentially an emergency number.
417      */
isPotentialEmergencyNumber(String number)418     private boolean isPotentialEmergencyNumber(String number) {
419         Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
420         return (number != null)
421                 && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number);
422     }
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
429      * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
430      * number.
431      */
rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber)432     private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
433         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 }
449