Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.phone; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.ActivityManagerNative; |
| 21 | import android.app.AlertDialog; |
| 22 | import android.app.AppOpsManager; |
| 23 | import android.app.Dialog; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.Intent; |
| 28 | import android.content.res.Configuration; |
Yorke Lee | d3105fe | 2013-09-25 12:44:45 -0700 | [diff] [blame] | 29 | import android.content.res.Resources; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
| 31 | import android.os.Binder; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.Handler; |
| 34 | import android.os.Message; |
| 35 | import android.os.RemoteException; |
| 36 | import android.os.SystemProperties; |
| 37 | import android.os.UserHandle; |
| 38 | import android.telephony.PhoneNumberUtils; |
| 39 | import android.text.TextUtils; |
| 40 | import android.util.Log; |
| 41 | import android.view.View; |
| 42 | import android.widget.ProgressBar; |
| 43 | |
| 44 | import com.android.internal.telephony.Phone; |
| 45 | import com.android.internal.telephony.PhoneConstants; |
| 46 | import com.android.internal.telephony.TelephonyCapabilities; |
| 47 | |
| 48 | /** |
| 49 | * OutgoingCallBroadcaster receives CALL and CALL_PRIVILEGED Intents, and |
| 50 | * broadcasts the ACTION_NEW_OUTGOING_CALL intent which allows other |
| 51 | * applications to monitor, redirect, or prevent the outgoing call. |
| 52 | |
| 53 | * After the other applications have had a chance to see the |
| 54 | * ACTION_NEW_OUTGOING_CALL intent, it finally reaches the |
| 55 | * {@link OutgoingCallReceiver}, which passes the (possibly modified) |
| 56 | * intent on to the {@link SipCallOptionHandler}, which will |
| 57 | * ultimately start the call using the CallController.placeCall() API. |
| 58 | * |
| 59 | * Emergency calls and calls where no number is present (like for a CDMA |
| 60 | * "empty flash" or a nonexistent voicemail number) are exempt from being |
| 61 | * broadcast. |
| 62 | */ |
| 63 | public class OutgoingCallBroadcaster extends Activity |
| 64 | implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener { |
| 65 | |
| 66 | private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS; |
| 67 | private static final String TAG = "OutgoingCallBroadcaster"; |
| 68 | private static final boolean DBG = |
| 69 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
| 70 | // Do not check in with VDBG = true, since that may write PII to the system log. |
| 71 | private static final boolean VDBG = false; |
| 72 | |
| 73 | public static final String ACTION_SIP_SELECT_PHONE = "com.android.phone.SIP_SELECT_PHONE"; |
| 74 | public static final String EXTRA_ALREADY_CALLED = "android.phone.extra.ALREADY_CALLED"; |
| 75 | public static final String EXTRA_ORIGINAL_URI = "android.phone.extra.ORIGINAL_URI"; |
| 76 | public static final String EXTRA_NEW_CALL_INTENT = "android.phone.extra.NEW_CALL_INTENT"; |
| 77 | public static final String EXTRA_SIP_PHONE_URI = "android.phone.extra.SIP_PHONE_URI"; |
| 78 | public static final String EXTRA_ACTUAL_NUMBER_TO_DIAL = |
| 79 | "android.phone.extra.ACTUAL_NUMBER_TO_DIAL"; |
| 80 | |
| 81 | /** |
| 82 | * Identifier for intent extra for sending an empty Flash message for |
| 83 | * CDMA networks. This message is used by the network to simulate a |
| 84 | * press/depress of the "hookswitch" of a landline phone. Aka "empty flash". |
| 85 | * |
| 86 | * TODO: Receiving an intent extra to tell the phone to send this flash is a |
| 87 | * temporary measure. To be replaced with an external ITelephony call in the future. |
| 88 | * TODO: Keep in sync with the string defined in TwelveKeyDialer.java in Contacts app |
| 89 | * until this is replaced with the ITelephony API. |
| 90 | */ |
| 91 | public static final String EXTRA_SEND_EMPTY_FLASH = |
| 92 | "com.android.phone.extra.SEND_EMPTY_FLASH"; |
| 93 | |
| 94 | // Dialog IDs |
| 95 | private static final int DIALOG_NOT_VOICE_CAPABLE = 1; |
| 96 | |
| 97 | /** Note message codes < 100 are reserved for the PhoneApp. */ |
| 98 | private static final int EVENT_OUTGOING_CALL_TIMEOUT = 101; |
Santos Cordon | 7d86bec | 2013-08-08 02:01:35 -0700 | [diff] [blame] | 99 | private static final int EVENT_DELAYED_FINISH = 102; |
| 100 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 101 | private static final int OUTGOING_CALL_TIMEOUT_THRESHOLD = 2000; // msec |
Santos Cordon | 7d86bec | 2013-08-08 02:01:35 -0700 | [diff] [blame] | 102 | private static final int DELAYED_FINISH_TIME = 2000; // msec |
| 103 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 104 | /** |
| 105 | * ProgressBar object with "spinner" style, which will be shown if we take more than |
| 106 | * {@link #EVENT_OUTGOING_CALL_TIMEOUT} msec to handle the incoming Intent. |
| 107 | */ |
| 108 | private ProgressBar mWaitingSpinner; |
| 109 | private final Handler mHandler = new Handler() { |
| 110 | @Override |
| 111 | public void handleMessage(Message msg) { |
| 112 | if (msg.what == EVENT_OUTGOING_CALL_TIMEOUT) { |
| 113 | Log.i(TAG, "Outgoing call takes too long. Showing the spinner."); |
| 114 | mWaitingSpinner.setVisibility(View.VISIBLE); |
Santos Cordon | 7d86bec | 2013-08-08 02:01:35 -0700 | [diff] [blame] | 115 | } else if (msg.what == EVENT_DELAYED_FINISH) { |
| 116 | finish(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 117 | } else { |
| 118 | Log.wtf(TAG, "Unknown message id: " + msg.what); |
| 119 | } |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | /** |
Santos Cordon | 7d86bec | 2013-08-08 02:01:35 -0700 | [diff] [blame] | 124 | * Starts the delayed finish() of OutgoingCallBroadcaster in order to give the UI |
| 125 | * some time to start up. |
| 126 | */ |
| 127 | private void startDelayedFinish() { |
| 128 | mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_FINISH, DELAYED_FINISH_TIME); |
| 129 | } |
| 130 | |
| 131 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 132 | * OutgoingCallReceiver finishes NEW_OUTGOING_CALL broadcasts, starting |
| 133 | * the InCallScreen if the broadcast has not been canceled, possibly with |
| 134 | * a modified phone number and optional provider info (uri + package name + remote views.) |
| 135 | */ |
| 136 | public class OutgoingCallReceiver extends BroadcastReceiver { |
| 137 | private static final String TAG = "OutgoingCallReceiver"; |
| 138 | |
| 139 | @Override |
| 140 | public void onReceive(Context context, Intent intent) { |
| 141 | mHandler.removeMessages(EVENT_OUTGOING_CALL_TIMEOUT); |
| 142 | doReceive(context, intent); |
| 143 | if (DBG) Log.v(TAG, "OutgoingCallReceiver is going to finish the Activity itself."); |
Santos Cordon | 7d86bec | 2013-08-08 02:01:35 -0700 | [diff] [blame] | 144 | |
| 145 | // We cannot finish the activity immediately here because it would cause the temporary |
| 146 | // black screen of OutgoingBroadcaster to go away and we need it to stay up until the |
| 147 | // UI (in a different process) has time to come up. |
| 148 | startDelayedFinish(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | public void doReceive(Context context, Intent intent) { |
| 152 | if (DBG) Log.v(TAG, "doReceive: " + intent); |
| 153 | |
| 154 | boolean alreadyCalled; |
| 155 | String number; |
| 156 | String originalUri; |
| 157 | |
| 158 | alreadyCalled = intent.getBooleanExtra( |
| 159 | OutgoingCallBroadcaster.EXTRA_ALREADY_CALLED, false); |
| 160 | if (alreadyCalled) { |
| 161 | if (DBG) Log.v(TAG, "CALL already placed -- returning."); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData |
| 166 | // is used as the actual number to call. (If null, no call will be |
| 167 | // placed.) |
| 168 | |
| 169 | number = getResultData(); |
| 170 | if (VDBG) Log.v(TAG, "- got number from resultData: '" + number + "'"); |
| 171 | |
| 172 | final PhoneGlobals app = PhoneGlobals.getInstance(); |
| 173 | |
| 174 | // OTASP-specific checks. |
| 175 | // TODO: This should probably all happen in |
| 176 | // OutgoingCallBroadcaster.onCreate(), since there's no reason to |
| 177 | // even bother with the NEW_OUTGOING_CALL broadcast if we're going |
| 178 | // to disallow the outgoing call anyway... |
| 179 | if (TelephonyCapabilities.supportsOtasp(app.phone)) { |
| 180 | boolean activateState = (app.cdmaOtaScreenState.otaScreenState |
| 181 | == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_ACTIVATION); |
| 182 | boolean dialogState = (app.cdmaOtaScreenState.otaScreenState |
| 183 | == OtaUtils.CdmaOtaScreenState.OtaScreenState |
| 184 | .OTA_STATUS_SUCCESS_FAILURE_DLG); |
| 185 | boolean isOtaCallActive = false; |
| 186 | |
| 187 | // TODO: Need cleaner way to check if OTA is active. |
| 188 | // Also, this check seems to be broken in one obscure case: if |
| 189 | // you interrupt an OTASP call by pressing Back then Skip, |
| 190 | // otaScreenState somehow gets left in either PROGRESS or |
| 191 | // LISTENING. |
| 192 | if ((app.cdmaOtaScreenState.otaScreenState |
| 193 | == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_PROGRESS) |
| 194 | || (app.cdmaOtaScreenState.otaScreenState |
| 195 | == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_LISTENING)) { |
| 196 | isOtaCallActive = true; |
| 197 | } |
| 198 | |
| 199 | if (activateState || dialogState) { |
| 200 | // The OTASP sequence is active, but either (1) the call |
| 201 | // hasn't started yet, or (2) the call has ended and we're |
| 202 | // showing the success/failure screen. In either of these |
| 203 | // cases it's OK to make a new outgoing call, but we need |
| 204 | // to take down any OTASP-related UI first. |
| 205 | if (dialogState) app.dismissOtaDialogs(); |
| 206 | app.clearOtaState(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 207 | } else if (isOtaCallActive) { |
| 208 | // The actual OTASP call is active. Don't allow new |
| 209 | // outgoing calls at all from this state. |
| 210 | Log.w(TAG, "OTASP call is active: disallowing a new outgoing call."); |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (number == null) { |
| 216 | if (DBG) Log.v(TAG, "CALL cancelled (null number), returning..."); |
| 217 | return; |
| 218 | } else if (TelephonyCapabilities.supportsOtasp(app.phone) |
| 219 | && (app.phone.getState() != PhoneConstants.State.IDLE) |
| 220 | && (app.phone.isOtaSpNumber(number))) { |
| 221 | if (DBG) Log.v(TAG, "Call is active, a 2nd OTA call cancelled -- returning."); |
| 222 | return; |
| 223 | } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, context)) { |
| 224 | // Just like 3rd-party apps aren't allowed to place emergency |
| 225 | // calls via the ACTION_CALL intent, we also don't allow 3rd |
| 226 | // party apps to use the NEW_OUTGOING_CALL broadcast to rewrite |
| 227 | // an outgoing call into an emergency number. |
| 228 | Log.w(TAG, "Cannot modify outgoing call to emergency number " + number + "."); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | originalUri = intent.getStringExtra( |
| 233 | OutgoingCallBroadcaster.EXTRA_ORIGINAL_URI); |
| 234 | if (originalUri == null) { |
| 235 | Log.e(TAG, "Intent is missing EXTRA_ORIGINAL_URI -- returning."); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | Uri uri = Uri.parse(originalUri); |
| 240 | |
| 241 | // We already called convertKeypadLettersToDigits() and |
| 242 | // stripSeparators() way back in onCreate(), before we sent out the |
| 243 | // NEW_OUTGOING_CALL broadcast. But we need to do it again here |
| 244 | // too, since the number might have been modified/rewritten during |
| 245 | // the broadcast (and may now contain letters or separators again.) |
| 246 | number = PhoneNumberUtils.convertKeypadLettersToDigits(number); |
| 247 | number = PhoneNumberUtils.stripSeparators(number); |
| 248 | |
| 249 | if (DBG) Log.v(TAG, "doReceive: proceeding with call..."); |
| 250 | if (VDBG) Log.v(TAG, "- uri: " + uri); |
| 251 | if (VDBG) Log.v(TAG, "- actual number to dial: '" + number + "'"); |
| 252 | |
| 253 | startSipCallOptionHandler(context, intent, uri, number); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Launch the SipCallOptionHandler, which is the next step(*) in the |
| 259 | * outgoing-call sequence after the outgoing call broadcast is |
| 260 | * complete. |
| 261 | * |
| 262 | * (*) We now know exactly what phone number we need to dial, so the next |
| 263 | * step is for the SipCallOptionHandler to decide which Phone type (SIP |
| 264 | * or PSTN) should be used. (Depending on the user's preferences, this |
| 265 | * decision may also involve popping up a dialog to ask the user to |
| 266 | * choose what type of call this should be.) |
| 267 | * |
| 268 | * @param context used for the startActivity() call |
| 269 | * |
| 270 | * @param intent the intent from the previous step of the outgoing-call |
| 271 | * sequence. Normally this will be the NEW_OUTGOING_CALL broadcast intent |
| 272 | * that came in to the OutgoingCallReceiver, although it can also be the |
| 273 | * original ACTION_CALL intent that started the whole sequence (in cases |
| 274 | * where we don't do the NEW_OUTGOING_CALL broadcast at all, like for |
| 275 | * emergency numbers or SIP addresses). |
| 276 | * |
| 277 | * @param uri the data URI from the original CALL intent, presumably either |
| 278 | * a tel: or sip: URI. For tel: URIs, note that the scheme-specific part |
| 279 | * does *not* necessarily have separators and keypad letters stripped (so |
| 280 | * we might see URIs like "tel:(650)%20555-1234" or "tel:1-800-GOOG-411" |
| 281 | * here.) |
| 282 | * |
| 283 | * @param number the actual number (or SIP address) to dial. This is |
| 284 | * guaranteed to be either a PSTN phone number with separators stripped |
| 285 | * out and keypad letters converted to digits (like "16505551234"), or a |
| 286 | * raw SIP address (like "user@example.com"). |
| 287 | */ |
| 288 | private void startSipCallOptionHandler(Context context, Intent intent, |
| 289 | Uri uri, String number) { |
| 290 | if (VDBG) { |
| 291 | Log.i(TAG, "startSipCallOptionHandler..."); |
| 292 | Log.i(TAG, "- intent: " + intent); |
| 293 | Log.i(TAG, "- uri: " + uri); |
| 294 | Log.i(TAG, "- number: " + number); |
| 295 | } |
| 296 | |
| 297 | // Create a copy of the original CALL intent that started the whole |
| 298 | // outgoing-call sequence. This intent will ultimately be passed to |
| 299 | // CallController.placeCall() after the SipCallOptionHandler step. |
| 300 | |
| 301 | Intent newIntent = new Intent(Intent.ACTION_CALL, uri); |
| 302 | newIntent.putExtra(EXTRA_ACTUAL_NUMBER_TO_DIAL, number); |
Santos Cordon | 69a6919 | 2013-08-22 14:25:42 -0700 | [diff] [blame] | 303 | CallGatewayManager.checkAndCopyPhoneProviderExtras(intent, newIntent); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 304 | |
| 305 | // Finally, launch the SipCallOptionHandler, with the copy of the |
| 306 | // original CALL intent stashed away in the EXTRA_NEW_CALL_INTENT |
| 307 | // extra. |
| 308 | |
| 309 | Intent selectPhoneIntent = new Intent(ACTION_SIP_SELECT_PHONE, uri); |
| 310 | selectPhoneIntent.setClass(context, SipCallOptionHandler.class); |
| 311 | selectPhoneIntent.putExtra(EXTRA_NEW_CALL_INTENT, newIntent); |
| 312 | selectPhoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 313 | if (DBG) { |
| 314 | Log.v(TAG, "startSipCallOptionHandler(): " + |
| 315 | "calling startActivity: " + selectPhoneIntent); |
| 316 | } |
| 317 | context.startActivity(selectPhoneIntent); |
| 318 | // ...and see SipCallOptionHandler.onCreate() for the next step of the sequence. |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * This method is the single point of entry for the CALL intent, which is used (by built-in |
| 323 | * apps like Contacts / Dialer, as well as 3rd-party apps) to initiate an outgoing voice call. |
| 324 | * |
| 325 | * |
| 326 | */ |
| 327 | @Override |
| 328 | protected void onCreate(Bundle icicle) { |
| 329 | super.onCreate(icicle); |
| 330 | setContentView(R.layout.outgoing_call_broadcaster); |
| 331 | mWaitingSpinner = (ProgressBar) findViewById(R.id.spinner); |
| 332 | |
| 333 | Intent intent = getIntent(); |
| 334 | if (DBG) { |
| 335 | final Configuration configuration = getResources().getConfiguration(); |
| 336 | Log.v(TAG, "onCreate: this = " + this + ", icicle = " + icicle); |
| 337 | Log.v(TAG, " - getIntent() = " + intent); |
| 338 | Log.v(TAG, " - configuration = " + configuration); |
| 339 | } |
| 340 | |
| 341 | if (icicle != null) { |
| 342 | // A non-null icicle means that this activity is being |
| 343 | // re-initialized after previously being shut down. |
| 344 | // |
| 345 | // In practice this happens very rarely (because the lifetime |
| 346 | // of this activity is so short!), but it *can* happen if the |
| 347 | // framework detects a configuration change at exactly the |
| 348 | // right moment; see bug 2202413. |
| 349 | // |
| 350 | // In this case, do nothing. Our onCreate() method has already |
| 351 | // run once (with icicle==null the first time), which means |
| 352 | // that the NEW_OUTGOING_CALL broadcast for this new call has |
| 353 | // already been sent. |
| 354 | Log.i(TAG, "onCreate: non-null icicle! " |
| 355 | + "Bailing out, not sending NEW_OUTGOING_CALL broadcast..."); |
| 356 | |
| 357 | // No need to finish() here, since the OutgoingCallReceiver from |
| 358 | // our original instance will do that. (It'll actually call |
| 359 | // finish() on our original instance, which apparently works fine |
| 360 | // even though the ActivityManager has already shut that instance |
| 361 | // down. And note that if we *do* call finish() here, that just |
| 362 | // results in an "ActivityManager: Duplicate finish request" |
| 363 | // warning when the OutgoingCallReceiver runs.) |
| 364 | |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | processIntent(intent); |
| 369 | |
| 370 | // isFinishing() return false when 1. broadcast is still ongoing, or 2. dialog is being |
| 371 | // shown. Otherwise finish() is called inside processIntent(), is isFinishing() here will |
| 372 | // return true. |
| 373 | if (DBG) Log.v(TAG, "At the end of onCreate(). isFinishing(): " + isFinishing()); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Interprets a given Intent and starts something relevant to the Intent. |
| 378 | * |
| 379 | * This method will handle three kinds of actions: |
| 380 | * |
| 381 | * - CALL (action for usual outgoing voice calls) |
| 382 | * - CALL_PRIVILEGED (can come from built-in apps like contacts / voice dialer / bluetooth) |
| 383 | * - CALL_EMERGENCY (from the EmergencyDialer that's reachable from the lockscreen.) |
| 384 | * |
| 385 | * The exact behavior depends on the intent's data: |
| 386 | * |
| 387 | * - The most typical is a tel: URI, which we handle by starting the |
| 388 | * NEW_OUTGOING_CALL broadcast. That broadcast eventually triggers |
| 389 | * the sequence OutgoingCallReceiver -> SipCallOptionHandler -> |
| 390 | * InCallScreen. |
| 391 | * |
| 392 | * - Or, with a sip: URI we skip the NEW_OUTGOING_CALL broadcast and |
| 393 | * go directly to SipCallOptionHandler, which then leads to the |
| 394 | * InCallScreen. |
| 395 | * |
| 396 | * - voicemail: URIs take the same path as regular tel: URIs. |
| 397 | * |
| 398 | * Other special cases: |
| 399 | * |
| 400 | * - Outgoing calls are totally disallowed on non-voice-capable |
| 401 | * devices (see handleNonVoiceCapable()). |
| 402 | * |
| 403 | * - A CALL intent with the EXTRA_SEND_EMPTY_FLASH extra (and |
| 404 | * presumably no data at all) means "send an empty flash" (which |
| 405 | * is only meaningful on CDMA devices while a call is already |
| 406 | * active.) |
| 407 | * |
| 408 | */ |
| 409 | private void processIntent(Intent intent) { |
| 410 | if (DBG) { |
| 411 | Log.v(TAG, "processIntent() = " + intent + ", thread: " + Thread.currentThread()); |
| 412 | } |
| 413 | final Configuration configuration = getResources().getConfiguration(); |
| 414 | |
| 415 | // Outgoing phone calls are only allowed on "voice-capable" devices. |
| 416 | if (!PhoneGlobals.sVoiceCapable) { |
| 417 | Log.i(TAG, "This device is detected as non-voice-capable device."); |
| 418 | handleNonVoiceCapable(intent); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | String action = intent.getAction(); |
| 423 | String number = PhoneNumberUtils.getNumberFromIntent(intent, this); |
| 424 | // Check the number, don't convert for sip uri |
| 425 | // TODO put uriNumber under PhoneNumberUtils |
| 426 | if (number != null) { |
| 427 | if (!PhoneNumberUtils.isUriNumber(number)) { |
| 428 | number = PhoneNumberUtils.convertKeypadLettersToDigits(number); |
| 429 | number = PhoneNumberUtils.stripSeparators(number); |
| 430 | } |
| 431 | } else { |
| 432 | Log.w(TAG, "The number obtained from Intent is null."); |
| 433 | } |
| 434 | |
| 435 | AppOpsManager appOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE); |
| 436 | int launchedFromUid; |
| 437 | String launchedFromPackage; |
| 438 | try { |
| 439 | launchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid( |
| 440 | getActivityToken()); |
| 441 | launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage( |
| 442 | getActivityToken()); |
| 443 | } catch (RemoteException e) { |
| 444 | launchedFromUid = -1; |
| 445 | launchedFromPackage = null; |
| 446 | } |
| 447 | if (appOps.noteOp(AppOpsManager.OP_CALL_PHONE, launchedFromUid, launchedFromPackage) |
| 448 | != AppOpsManager.MODE_ALLOWED) { |
| 449 | Log.w(TAG, "Rejecting call from uid " + launchedFromUid + " package " |
| 450 | + launchedFromPackage); |
| 451 | finish(); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | // If true, this flag will indicate that the current call is a special kind |
| 456 | // of call (most likely an emergency number) that 3rd parties aren't allowed |
| 457 | // to intercept or affect in any way. (In that case, we start the call |
| 458 | // immediately rather than going through the NEW_OUTGOING_CALL sequence.) |
| 459 | boolean callNow; |
| 460 | |
| 461 | if (getClass().getName().equals(intent.getComponent().getClassName())) { |
| 462 | // If we were launched directly from the OutgoingCallBroadcaster, |
| 463 | // not one of its more privileged aliases, then make sure that |
| 464 | // only the non-privileged actions are allowed. |
| 465 | if (!Intent.ACTION_CALL.equals(intent.getAction())) { |
| 466 | Log.w(TAG, "Attempt to deliver non-CALL action; forcing to CALL"); |
| 467 | intent.setAction(Intent.ACTION_CALL); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // Check whether or not this is an emergency number, in order to |
| 472 | // enforce the restriction that only the CALL_PRIVILEGED and |
| 473 | // CALL_EMERGENCY intents are allowed to make emergency calls. |
| 474 | // |
| 475 | // (Note that the ACTION_CALL check below depends on the result of |
| 476 | // isPotentialLocalEmergencyNumber() rather than just plain |
| 477 | // isLocalEmergencyNumber(), to be 100% certain that we *don't* |
| 478 | // allow 3rd party apps to make emergency calls by passing in an |
| 479 | // "invalid" number like "9111234" that isn't technically an |
| 480 | // emergency number but might still result in an emergency call |
| 481 | // with some networks.) |
| 482 | final boolean isExactEmergencyNumber = |
| 483 | (number != null) && PhoneNumberUtils.isLocalEmergencyNumber(number, this); |
| 484 | final boolean isPotentialEmergencyNumber = |
| 485 | (number != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, this); |
| 486 | if (VDBG) { |
| 487 | Log.v(TAG, " - Checking restrictions for number '" + number + "':"); |
| 488 | Log.v(TAG, " isExactEmergencyNumber = " + isExactEmergencyNumber); |
| 489 | Log.v(TAG, " isPotentialEmergencyNumber = " + isPotentialEmergencyNumber); |
| 490 | } |
| 491 | |
| 492 | /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */ |
| 493 | // TODO: This code is redundant with some code in InCallScreen: refactor. |
| 494 | if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) { |
| 495 | // We're handling a CALL_PRIVILEGED intent, so we know this request came |
| 496 | // from a trusted source (like the built-in dialer.) So even a number |
| 497 | // that's *potentially* an emergency number can safely be promoted to |
| 498 | // CALL_EMERGENCY (since we *should* allow you to dial "91112345" from |
| 499 | // the dialer if you really want to.) |
| 500 | if (isPotentialEmergencyNumber) { |
| 501 | Log.i(TAG, "ACTION_CALL_PRIVILEGED is used while the number is a potential" |
| 502 | + " emergency number. Use ACTION_CALL_EMERGENCY as an action instead."); |
| 503 | action = Intent.ACTION_CALL_EMERGENCY; |
| 504 | } else { |
| 505 | action = Intent.ACTION_CALL; |
| 506 | } |
| 507 | if (DBG) Log.v(TAG, " - updating action from CALL_PRIVILEGED to " + action); |
| 508 | intent.setAction(action); |
| 509 | } |
| 510 | |
| 511 | if (Intent.ACTION_CALL.equals(action)) { |
| 512 | if (isPotentialEmergencyNumber) { |
| 513 | Log.w(TAG, "Cannot call potential emergency number '" + number |
| 514 | + "' with CALL Intent " + intent + "."); |
| 515 | Log.i(TAG, "Launching default dialer instead..."); |
| 516 | |
| 517 | Intent invokeFrameworkDialer = new Intent(); |
| 518 | |
| 519 | // TwelveKeyDialer is in a tab so we really want |
| 520 | // DialtactsActivity. Build the intent 'manually' to |
| 521 | // use the java resolver to find the dialer class (as |
| 522 | // opposed to a Context which look up known android |
| 523 | // packages only) |
Yorke Lee | d3105fe | 2013-09-25 12:44:45 -0700 | [diff] [blame] | 524 | final Resources resources = getResources(); |
| 525 | invokeFrameworkDialer.setClassName( |
| 526 | resources.getString(R.string.ui_default_package), |
| 527 | resources.getString(R.string.dialer_default_class)); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 528 | invokeFrameworkDialer.setAction(Intent.ACTION_DIAL); |
| 529 | invokeFrameworkDialer.setData(intent.getData()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 530 | if (DBG) Log.v(TAG, "onCreate(): calling startActivity for Dialer: " |
| 531 | + invokeFrameworkDialer); |
| 532 | startActivity(invokeFrameworkDialer); |
| 533 | finish(); |
| 534 | return; |
| 535 | } |
| 536 | callNow = false; |
| 537 | } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) { |
| 538 | // ACTION_CALL_EMERGENCY case: this is either a CALL_PRIVILEGED |
| 539 | // intent that we just turned into a CALL_EMERGENCY intent (see |
| 540 | // above), or else it really is an CALL_EMERGENCY intent that |
| 541 | // came directly from some other app (e.g. the EmergencyDialer |
| 542 | // activity built in to the Phone app.) |
| 543 | // Make sure it's at least *possible* that this is really an |
| 544 | // emergency number. |
| 545 | if (!isPotentialEmergencyNumber) { |
| 546 | Log.w(TAG, "Cannot call non-potential-emergency number " + number |
| 547 | + " with EMERGENCY_CALL Intent " + intent + "." |
| 548 | + " Finish the Activity immediately."); |
| 549 | finish(); |
| 550 | return; |
| 551 | } |
| 552 | callNow = true; |
| 553 | } else { |
| 554 | Log.e(TAG, "Unhandled Intent " + intent + ". Finish the Activity immediately."); |
| 555 | finish(); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | // Make sure the screen is turned on. This is probably the right |
| 560 | // thing to do, and more importantly it works around an issue in the |
| 561 | // activity manager where we will not launch activities consistently |
| 562 | // when the screen is off (since it is trying to keep them paused |
| 563 | // and has... issues). |
| 564 | // |
| 565 | // Also, this ensures the device stays awake while doing the following |
| 566 | // broadcast; technically we should be holding a wake lock here |
| 567 | // as well. |
| 568 | PhoneGlobals.getInstance().wakeUpScreen(); |
| 569 | |
| 570 | // If number is null, we're probably trying to call a non-existent voicemail number, |
| 571 | // send an empty flash or something else is fishy. Whatever the problem, there's no |
| 572 | // number, so there's no point in allowing apps to modify the number. |
| 573 | if (TextUtils.isEmpty(number)) { |
| 574 | if (intent.getBooleanExtra(EXTRA_SEND_EMPTY_FLASH, false)) { |
| 575 | Log.i(TAG, "onCreate: SEND_EMPTY_FLASH..."); |
| 576 | PhoneUtils.sendEmptyFlash(PhoneGlobals.getPhone()); |
| 577 | finish(); |
| 578 | return; |
| 579 | } else { |
| 580 | Log.i(TAG, "onCreate: null or empty number, setting callNow=true..."); |
| 581 | callNow = true; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | if (callNow) { |
| 586 | // This is a special kind of call (most likely an emergency number) |
| 587 | // that 3rd parties aren't allowed to intercept or affect in any way. |
| 588 | // So initiate the outgoing call immediately. |
| 589 | |
| 590 | Log.i(TAG, "onCreate(): callNow case! Calling placeCall(): " + intent); |
| 591 | |
| 592 | // Initiate the outgoing call, and simultaneously launch the |
| 593 | // InCallScreen to display the in-call UI: |
| 594 | PhoneGlobals.getInstance().callController.placeCall(intent); |
| 595 | |
| 596 | // Note we do *not* "return" here, but instead continue and |
| 597 | // send the ACTION_NEW_OUTGOING_CALL broadcast like for any |
| 598 | // other outgoing call. (But when the broadcast finally |
| 599 | // reaches the OutgoingCallReceiver, we'll know not to |
| 600 | // initiate the call again because of the presence of the |
| 601 | // EXTRA_ALREADY_CALLED extra.) |
| 602 | } |
| 603 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 604 | // For now, SIP calls will be processed directly without a |
| 605 | // NEW_OUTGOING_CALL broadcast. |
| 606 | // |
| 607 | // TODO: In the future, though, 3rd party apps *should* be allowed to |
| 608 | // intercept outgoing calls to SIP addresses as well. To do this, we should |
| 609 | // (1) update the NEW_OUTGOING_CALL intent documentation to explain this |
| 610 | // case, and (2) pass the outgoing SIP address by *not* overloading the |
| 611 | // EXTRA_PHONE_NUMBER extra, but instead using a new separate extra to hold |
| 612 | // the outgoing SIP address. (Be sure to document whether it's a URI or just |
| 613 | // a plain address, whether it could be a tel: URI, etc.) |
| 614 | Uri uri = intent.getData(); |
| 615 | String scheme = uri.getScheme(); |
| 616 | if (Constants.SCHEME_SIP.equals(scheme) || PhoneNumberUtils.isUriNumber(number)) { |
| 617 | Log.i(TAG, "The requested number was detected as SIP call."); |
| 618 | startSipCallOptionHandler(this, intent, uri, number); |
| 619 | finish(); |
| 620 | return; |
| 621 | |
| 622 | // TODO: if there's ever a way for SIP calls to trigger a |
| 623 | // "callNow=true" case (see above), we'll need to handle that |
| 624 | // case here too (most likely by just doing nothing at all.) |
| 625 | } |
| 626 | |
| 627 | Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL); |
| 628 | if (number != null) { |
| 629 | broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number); |
| 630 | } |
Santos Cordon | 69a6919 | 2013-08-22 14:25:42 -0700 | [diff] [blame] | 631 | CallGatewayManager.checkAndCopyPhoneProviderExtras(intent, broadcastIntent); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 632 | broadcastIntent.putExtra(EXTRA_ALREADY_CALLED, callNow); |
| 633 | broadcastIntent.putExtra(EXTRA_ORIGINAL_URI, uri.toString()); |
| 634 | // Need to raise foreground in-call UI as soon as possible while allowing 3rd party app |
| 635 | // to intercept the outgoing call. |
| 636 | broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 637 | if (DBG) Log.v(TAG, " - Broadcasting intent: " + broadcastIntent + "."); |
| 638 | |
| 639 | // Set a timer so that we can prepare for unexpected delay introduced by the broadcast. |
| 640 | // If it takes too much time, the timer will show "waiting" spinner. |
| 641 | // This message will be removed when OutgoingCallReceiver#onReceive() is called before the |
| 642 | // timeout. |
| 643 | mHandler.sendEmptyMessageDelayed(EVENT_OUTGOING_CALL_TIMEOUT, |
| 644 | OUTGOING_CALL_TIMEOUT_THRESHOLD); |
| 645 | sendOrderedBroadcastAsUser(broadcastIntent, UserHandle.OWNER, |
| 646 | PERMISSION, new OutgoingCallReceiver(), |
| 647 | null, // scheduler |
| 648 | Activity.RESULT_OK, // initialCode |
| 649 | number, // initialData: initial value for the result data |
| 650 | null); // initialExtras |
| 651 | } |
| 652 | |
| 653 | @Override |
| 654 | protected void onStop() { |
| 655 | // Clean up (and dismiss if necessary) any managed dialogs. |
| 656 | // |
| 657 | // We don't do this in onPause() since we can be paused/resumed |
| 658 | // due to orientation changes (in which case we don't want to |
| 659 | // disturb the dialog), but we *do* need it here in onStop() to be |
| 660 | // sure we clean up if the user hits HOME while the dialog is up. |
| 661 | // |
| 662 | // Note it's safe to call removeDialog() even if there's no dialog |
| 663 | // associated with that ID. |
| 664 | removeDialog(DIALOG_NOT_VOICE_CAPABLE); |
| 665 | |
| 666 | super.onStop(); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Handle the specified CALL or CALL_* intent on a non-voice-capable |
| 671 | * device. |
| 672 | * |
| 673 | * This method may launch a different intent (if there's some useful |
| 674 | * alternative action to take), or otherwise display an error dialog, |
| 675 | * and in either case will finish() the current activity when done. |
| 676 | */ |
| 677 | private void handleNonVoiceCapable(Intent intent) { |
| 678 | if (DBG) Log.v(TAG, "handleNonVoiceCapable: handling " + intent |
| 679 | + " on non-voice-capable device..."); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 680 | |
Chiao Cheng | 88653e7 | 2013-10-04 14:30:14 -0700 | [diff] [blame^] | 681 | // Just show a generic "voice calling not supported" dialog. |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 682 | showDialog(DIALOG_NOT_VOICE_CAPABLE); |
| 683 | // ...and we'll eventually finish() when the user dismisses |
| 684 | // or cancels the dialog. |
| 685 | } |
| 686 | |
| 687 | @Override |
| 688 | protected Dialog onCreateDialog(int id) { |
| 689 | Dialog dialog; |
| 690 | switch(id) { |
| 691 | case DIALOG_NOT_VOICE_CAPABLE: |
| 692 | dialog = new AlertDialog.Builder(this) |
| 693 | .setTitle(R.string.not_voice_capable) |
| 694 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 695 | .setPositiveButton(android.R.string.ok, this) |
| 696 | .setOnCancelListener(this) |
| 697 | .create(); |
| 698 | break; |
| 699 | default: |
| 700 | Log.w(TAG, "onCreateDialog: unexpected ID " + id); |
| 701 | dialog = null; |
| 702 | break; |
| 703 | } |
| 704 | return dialog; |
| 705 | } |
| 706 | |
| 707 | /** DialogInterface.OnClickListener implementation */ |
| 708 | @Override |
| 709 | public void onClick(DialogInterface dialog, int id) { |
| 710 | // DIALOG_NOT_VOICE_CAPABLE is the only dialog we ever use (so far |
| 711 | // at least), and its only button is "OK". |
| 712 | finish(); |
| 713 | } |
| 714 | |
| 715 | /** DialogInterface.OnCancelListener implementation */ |
| 716 | @Override |
| 717 | public void onCancel(DialogInterface dialog) { |
| 718 | // DIALOG_NOT_VOICE_CAPABLE is the only dialog we ever use (so far |
| 719 | // at least), and canceling it is just like hitting "OK". |
| 720 | finish(); |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Implement onConfigurationChanged() purely for debugging purposes, |
| 725 | * to make sure that the android:configChanges element in our manifest |
| 726 | * is working properly. |
| 727 | */ |
| 728 | @Override |
| 729 | public void onConfigurationChanged(Configuration newConfig) { |
| 730 | super.onConfigurationChanged(newConfig); |
| 731 | if (DBG) Log.v(TAG, "onConfigurationChanged: newConfig = " + newConfig); |
| 732 | } |
| 733 | } |