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