blob: aa62575c5faccb9e630d2418c7281347fdde01d0 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 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
17package com.android.phone;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.ProgressDialog;
22import android.bluetooth.IBluetoothHeadsetPhone;
23import android.content.ActivityNotFoundException;
Sailesh Nepalbfb68322013-11-07 14:07:41 -080024import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.res.Configuration;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.media.AudioManager;
30import android.net.Uri;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.os.Handler;
32import android.os.Message;
Jonathan Basseri3b9c21f2015-06-19 15:51:10 -070033import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.os.RemoteException;
35import android.os.SystemProperties;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070036import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080037import android.telecom.PhoneAccountHandle;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070038import android.telecom.VideoProfile;
Jonathan Basseri3b9c21f2015-06-19 15:51:10 -070039import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.PhoneNumberUtils;
Santos Cordonf69df962015-02-03 11:15:19 -080041import android.telephony.SubscriptionManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.text.TextUtils;
43import android.util.Log;
Andrew Leeb3ee4dc2015-01-23 17:35:00 -080044import android.view.ContextThemeWrapper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.view.KeyEvent;
46import android.view.LayoutInflater;
47import android.view.View;
48import android.view.WindowManager;
49import android.widget.EditText;
50import android.widget.Toast;
51
52import com.android.internal.telephony.Call;
53import com.android.internal.telephony.CallManager;
54import com.android.internal.telephony.CallStateException;
55import com.android.internal.telephony.CallerInfo;
56import com.android.internal.telephony.CallerInfoAsyncQuery;
57import com.android.internal.telephony.Connection;
Stuart Scottdcf40a92014-12-09 10:45:01 -080058import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.MmiCode;
60import com.android.internal.telephony.Phone;
61import com.android.internal.telephony.PhoneConstants;
Stuart Scottdcf40a92014-12-09 10:45:01 -080062import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.TelephonyCapabilities;
64import com.android.internal.telephony.TelephonyProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.android.internal.telephony.sip.SipPhone;
Santos Cordon69a69192013-08-22 14:25:42 -070066import com.android.phone.CallGatewayManager.RawGatewayInfo;
Andrew Leed5165b02014-12-05 15:53:58 -080067import com.android.services.telephony.TelephonyConnectionService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import java.util.Arrays;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import java.util.List;
71
72/**
73 * Misc utilities for the Phone app.
74 */
75public class PhoneUtils {
Brad Ebingerd3edc222016-03-23 19:44:38 -070076 public static final String EMERGENCY_ACCOUNT_HANDLE_ID = "E";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077 private static final String LOG_TAG = "PhoneUtils";
78 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
79
80 // Do not check in with VDBG = true, since that may write PII to the system log.
81 private static final boolean VDBG = false;
82
83 /** Control stack trace for Audio Mode settings */
84 private static final boolean DBG_SETAUDIOMODE_STACK = false;
85
86 /** Identifier for the "Add Call" intent extra. */
87 static final String ADD_CALL_MODE_KEY = "add_call_mode";
88
89 // Return codes from placeCall()
90 static final int CALL_STATUS_DIALED = 0; // The number was successfully dialed
91 static final int CALL_STATUS_DIALED_MMI = 1; // The specified number was an MMI code
92 static final int CALL_STATUS_FAILED = 2; // The call failed
93
94 // State of the Phone's audio modes
95 // Each state can move to the other states, but within the state only certain
96 // transitions for AudioManager.setMode() are allowed.
97 static final int AUDIO_IDLE = 0; /** audio behaviour at phone idle */
98 static final int AUDIO_RINGING = 1; /** audio behaviour while ringing */
99 static final int AUDIO_OFFHOOK = 2; /** audio behaviour while in call. */
100
101 // USSD string length for MMI operations
102 static final int MIN_USSD_LEN = 1;
103 static final int MAX_USSD_LEN = 160;
104
105 /** Speaker state, persisting between wired headset connection events */
106 private static boolean sIsSpeakerEnabled = false;
107
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 /** Static handler for the connection/mute tracking */
109 private static ConnectionHandler mConnectionHandler;
110
111 /** Phone state changed event*/
112 private static final int PHONE_STATE_CHANGED = -1;
113
Zhihai Xua4915c72013-10-18 17:01:16 -0700114 /** check status then decide whether answerCall */
115 private static final int MSG_CHECK_STATUS_ANSWERCALL = 100;
116
117 /** poll phone DISCONNECTING status interval */
118 private static final int DISCONNECTING_POLLING_INTERVAL_MS = 200;
119
120 /** poll phone DISCONNECTING status times limit */
121 private static final int DISCONNECTING_POLLING_TIMES_LIMIT = 8;
122
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123 /** Define for not a special CNAP string */
124 private static final int CNAP_SPECIAL_CASE_NO = -1;
125
126 /** Noise suppression status as selected by user */
127 private static boolean sIsNoiseSuppressionEnabled = true;
128
Yorke Leee0f2b792014-10-15 12:30:44 -0700129 /**
130 * Theme to use for dialogs displayed by utility methods in this class. This is needed
131 * because these dialogs are displayed using the application context, which does not resolve
132 * the dialog theme correctly.
133 */
134 private static final int THEME = AlertDialog.THEME_DEVICE_DEFAULT_LIGHT;
135
Zhihai Xua4915c72013-10-18 17:01:16 -0700136 private static class FgRingCalls {
137 private Call fgCall;
138 private Call ringing;
139 public FgRingCalls(Call fg, Call ring) {
140 fgCall = fg;
141 ringing = ring;
142 }
143 }
144
Etan Cohen37abbab2014-03-07 16:57:03 -0800145 /** USSD information used to aggregate all USSD messages */
146 private static AlertDialog sUssdDialog = null;
147 private static StringBuilder sUssdMsg = new StringBuilder();
148
Tony Mak4c8f3c62015-12-07 02:30:37 +0000149 private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT =
150 new ComponentName("com.android.phone",
151 "com.android.services.telephony.TelephonyConnectionService");
152
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 /**
154 * Handler that tracks the connections and updates the value of the
155 * Mute settings for each connection as needed.
156 */
157 private static class ConnectionHandler extends Handler {
158 @Override
159 public void handleMessage(Message msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 switch (msg.what) {
Zhihai Xua4915c72013-10-18 17:01:16 -0700161 case MSG_CHECK_STATUS_ANSWERCALL:
162 FgRingCalls frC = (FgRingCalls) msg.obj;
163 // wait for finishing disconnecting
164 // before check the ringing call state
165 if ((frC.fgCall != null) &&
166 (frC.fgCall.getState() == Call.State.DISCONNECTING) &&
167 (msg.arg1 < DISCONNECTING_POLLING_TIMES_LIMIT)) {
168 Message retryMsg =
169 mConnectionHandler.obtainMessage(MSG_CHECK_STATUS_ANSWERCALL);
170 retryMsg.arg1 = 1 + msg.arg1;
171 retryMsg.obj = msg.obj;
172 mConnectionHandler.sendMessageDelayed(retryMsg,
173 DISCONNECTING_POLLING_INTERVAL_MS);
174 // since hangupActiveCall() also accepts the ringing call
175 // check if the ringing call was already answered or not
176 // only answer it when the call still is ringing
177 } else if (frC.ringing.isRinging()) {
178 if (msg.arg1 == DISCONNECTING_POLLING_TIMES_LIMIT) {
179 Log.e(LOG_TAG, "DISCONNECTING time out");
180 }
181 answerCall(frC.ringing);
182 }
183 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 }
185 }
186 }
187
188 /**
189 * Register the ConnectionHandler with the phone, to receive connection events
190 */
191 public static void initializeConnectionHandler(CallManager cm) {
192 if (mConnectionHandler == null) {
193 mConnectionHandler = new ConnectionHandler();
194 }
195
196 // pass over cm as user.obj
197 cm.registerForPreciseCallStateChanged(mConnectionHandler, PHONE_STATE_CHANGED, cm);
198
199 }
200
201 /** This class is never instantiated. */
202 private PhoneUtils() {
203 }
204
205 /**
206 * Answer the currently-ringing call.
207 *
208 * @return true if we answered the call, or false if there wasn't
209 * actually a ringing incoming call, or some other error occurred.
210 *
211 * @see #answerAndEndHolding(CallManager, Call)
212 * @see #answerAndEndActive(CallManager, Call)
213 */
214 /* package */ static boolean answerCall(Call ringingCall) {
215 log("answerCall(" + ringingCall + ")...");
216 final PhoneGlobals app = PhoneGlobals.getInstance();
217 final CallNotifier notifier = app.notifier;
218
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219 final Phone phone = ringingCall.getPhone();
220 final boolean phoneIsCdma = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
221 boolean answered = false;
222 IBluetoothHeadsetPhone btPhone = null;
223
224 if (phoneIsCdma) {
225 // Stop any signalInfo tone being played when a Call waiting gets answered
226 if (ringingCall.getState() == Call.State.WAITING) {
227 notifier.stopSignalInfoTone();
228 }
229 }
230
231 if (ringingCall != null && ringingCall.isRinging()) {
232 if (DBG) log("answerCall: call state = " + ringingCall.getState());
233 try {
234 if (phoneIsCdma) {
235 if (app.cdmaPhoneCallState.getCurrentCallState()
236 == CdmaPhoneCallState.PhoneCallState.IDLE) {
237 // This is the FIRST incoming call being answered.
238 // Set the Phone Call State to SINGLE_ACTIVE
239 app.cdmaPhoneCallState.setCurrentCallState(
240 CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE);
241 } else {
242 // This is the CALL WAITING call being answered.
243 // Set the Phone Call State to CONF_CALL
244 app.cdmaPhoneCallState.setCurrentCallState(
245 CdmaPhoneCallState.PhoneCallState.CONF_CALL);
246 // Enable "Add Call" option after answering a Call Waiting as the user
247 // should be allowed to add another call in case one of the parties
248 // drops off
249 app.cdmaPhoneCallState.setAddCallMenuStateAfterCallWaiting(true);
Santos Cordon13fed9f2014-09-24 13:10:34 -0700250 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 }
252
253 final boolean isRealIncomingCall = isRealIncomingCall(ringingCall.getState());
254
255 //if (DBG) log("sPhone.acceptCall");
256 app.mCM.acceptCall(ringingCall);
257 answered = true;
258
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 setAudioMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 } catch (CallStateException ex) {
261 Log.w(LOG_TAG, "answerCall: caught " + ex, ex);
262
263 if (phoneIsCdma) {
264 // restore the cdmaPhoneCallState and btPhone.cdmaSetSecondCallState:
265 app.cdmaPhoneCallState.setCurrentCallState(
266 app.cdmaPhoneCallState.getPreviousCallState());
267 if (btPhone != null) {
268 try {
269 btPhone.cdmaSetSecondCallState(false);
270 } catch (RemoteException e) {
271 Log.e(LOG_TAG, Log.getStackTraceString(new Throwable()));
272 }
273 }
274 }
275 }
276 }
277 return answered;
278 }
279
280 /**
Santos Cordonde10b752013-09-19 04:11:33 -0700281 * Hangs up all active calls.
282 */
283 static void hangupAllCalls(CallManager cm) {
284 final Call ringing = cm.getFirstActiveRingingCall();
285 final Call fg = cm.getActiveFgCall();
286 final Call bg = cm.getFirstActiveBgCall();
287
288 // We go in reverse order, BG->FG->RINGING because hanging up a ringing call or an active
289 // call can move a bg call to a fg call which would force us to loop over each call
290 // several times. This ordering works best to ensure we dont have any more calls.
291 if (bg != null && !bg.isIdle()) {
292 hangup(bg);
293 }
294 if (fg != null && !fg.isIdle()) {
295 hangup(fg);
296 }
297 if (ringing != null && !ringing.isIdle()) {
298 hangupRingingCall(fg);
299 }
300 }
301
302 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 * Smart "hang up" helper method which hangs up exactly one connection,
304 * based on the current Phone state, as follows:
305 * <ul>
306 * <li>If there's a ringing call, hang that up.
307 * <li>Else if there's a foreground call, hang that up.
308 * <li>Else if there's a background call, hang that up.
309 * <li>Otherwise do nothing.
310 * </ul>
311 * @return true if we successfully hung up, or false
312 * if there were no active calls at all.
313 */
314 static boolean hangup(CallManager cm) {
315 boolean hungup = false;
316 Call ringing = cm.getFirstActiveRingingCall();
317 Call fg = cm.getActiveFgCall();
318 Call bg = cm.getFirstActiveBgCall();
319
320 if (!ringing.isIdle()) {
321 log("hangup(): hanging up ringing call");
322 hungup = hangupRingingCall(ringing);
323 } else if (!fg.isIdle()) {
324 log("hangup(): hanging up foreground call");
325 hungup = hangup(fg);
326 } else if (!bg.isIdle()) {
327 log("hangup(): hanging up background call");
328 hungup = hangup(bg);
329 } else {
330 // No call to hang up! This is unlikely in normal usage,
331 // since the UI shouldn't be providing an "End call" button in
332 // the first place. (But it *can* happen, rarely, if an
333 // active call happens to disconnect on its own right when the
334 // user is trying to hang up..)
335 log("hangup(): no active call to hang up");
336 }
337 if (DBG) log("==> hungup = " + hungup);
338
339 return hungup;
340 }
341
342 static boolean hangupRingingCall(Call ringing) {
343 if (DBG) log("hangup ringing call");
344 int phoneType = ringing.getPhone().getPhoneType();
345 Call.State state = ringing.getState();
346
347 if (state == Call.State.INCOMING) {
348 // Regular incoming call (with no other active calls)
349 log("hangupRingingCall(): regular incoming call: hangup()");
350 return hangup(ringing);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 } else {
352 // Unexpected state: the ringing call isn't INCOMING or
353 // WAITING, so there's no reason to have called
354 // hangupRingingCall() in the first place.
355 // (Presumably the incoming call went away at the exact moment
356 // we got here, so just do nothing.)
357 Log.w(LOG_TAG, "hangupRingingCall: no INCOMING or WAITING call");
358 return false;
359 }
360 }
361
362 static boolean hangupActiveCall(Call foreground) {
363 if (DBG) log("hangup active call");
364 return hangup(foreground);
365 }
366
367 static boolean hangupHoldingCall(Call background) {
368 if (DBG) log("hangup holding call");
369 return hangup(background);
370 }
371
372 /**
373 * Used in CDMA phones to end the complete Call session
374 * @param phone the Phone object.
375 * @return true if *any* call was successfully hung up
376 */
377 static boolean hangupRingingAndActive(Phone phone) {
378 boolean hungUpRingingCall = false;
379 boolean hungUpFgCall = false;
380 Call ringingCall = phone.getRingingCall();
381 Call fgCall = phone.getForegroundCall();
382
383 // Hang up any Ringing Call
384 if (!ringingCall.isIdle()) {
385 log("hangupRingingAndActive: Hang up Ringing Call");
386 hungUpRingingCall = hangupRingingCall(ringingCall);
387 }
388
389 // Hang up any Active Call
390 if (!fgCall.isIdle()) {
391 log("hangupRingingAndActive: Hang up Foreground Call");
392 hungUpFgCall = hangupActiveCall(fgCall);
393 }
394
395 return hungUpRingingCall || hungUpFgCall;
396 }
397
398 /**
399 * Trivial wrapper around Call.hangup(), except that we return a
400 * boolean success code rather than throwing CallStateException on
401 * failure.
402 *
403 * @return true if the call was successfully hung up, or false
404 * if the call wasn't actually active.
405 */
406 static boolean hangup(Call call) {
407 try {
408 CallManager cm = PhoneGlobals.getInstance().mCM;
409
410 if (call.getState() == Call.State.ACTIVE && cm.hasActiveBgCall()) {
411 // handle foreground call hangup while there is background call
412 log("- hangup(Call): hangupForegroundResumeBackground...");
413 cm.hangupForegroundResumeBackground(cm.getFirstActiveBgCall());
414 } else {
415 log("- hangup(Call): regular hangup()...");
416 call.hangup();
417 }
418 return true;
419 } catch (CallStateException ex) {
420 Log.e(LOG_TAG, "Call hangup: caught " + ex, ex);
421 }
422
423 return false;
424 }
425
426 /**
427 * Trivial wrapper around Connection.hangup(), except that we silently
428 * do nothing (rather than throwing CallStateException) if the
429 * connection wasn't actually active.
430 */
431 static void hangup(Connection c) {
432 try {
433 if (c != null) {
434 c.hangup();
435 }
436 } catch (CallStateException ex) {
437 Log.w(LOG_TAG, "Connection hangup: caught " + ex, ex);
438 }
439 }
440
441 static boolean answerAndEndHolding(CallManager cm, Call ringing) {
442 if (DBG) log("end holding & answer waiting: 1");
443 if (!hangupHoldingCall(cm.getFirstActiveBgCall())) {
444 Log.e(LOG_TAG, "end holding failed!");
445 return false;
446 }
447
448 if (DBG) log("end holding & answer waiting: 2");
449 return answerCall(ringing);
450
451 }
452
453 /**
454 * Answers the incoming call specified by "ringing", and ends the currently active phone call.
455 *
456 * This method is useful when's there's an incoming call which we cannot manage with the
457 * current call. e.g. when you are having a phone call with CDMA network and has received
458 * a SIP call, then we won't expect our telephony can manage those phone calls simultaneously.
459 * Note that some types of network may allow multiple phone calls at once; GSM allows to hold
460 * an ongoing phone call, so we don't need to end the active call. The caller of this method
461 * needs to check if the network allows multiple phone calls or not.
462 *
463 * @see #answerCall(Call)
464 * @see InCallScreen#internalAnswerCall()
465 */
466 /* package */ static boolean answerAndEndActive(CallManager cm, Call ringing) {
467 if (DBG) log("answerAndEndActive()...");
468
469 // Unlike the answerCall() method, we *don't* need to stop the
470 // ringer or change audio modes here since the user is already
471 // in-call, which means that the audio mode is already set
472 // correctly, and that we wouldn't have started the ringer in the
473 // first place.
474
475 // hanging up the active call also accepts the waiting call
476 // while active call and waiting call are from the same phone
477 // i.e. both from GSM phone
Zhihai Xua4915c72013-10-18 17:01:16 -0700478 Call fgCall = cm.getActiveFgCall();
479 if (!hangupActiveCall(fgCall)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 Log.w(LOG_TAG, "end active call failed!");
481 return false;
482 }
483
Zhihai Xua4915c72013-10-18 17:01:16 -0700484 mConnectionHandler.removeMessages(MSG_CHECK_STATUS_ANSWERCALL);
485 Message msg = mConnectionHandler.obtainMessage(MSG_CHECK_STATUS_ANSWERCALL);
486 msg.arg1 = 1;
487 msg.obj = new FgRingCalls(fgCall, ringing);
488 mConnectionHandler.sendMessage(msg);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489
490 return true;
491 }
492
493 /**
494 * For a CDMA phone, advance the call state upon making a new
495 * outgoing call.
496 *
497 * <pre>
498 * IDLE -> SINGLE_ACTIVE
499 * or
500 * SINGLE_ACTIVE -> THRWAY_ACTIVE
501 * </pre>
502 * @param app The phone instance.
503 */
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700504 private static void updateCdmaCallStateOnNewOutgoingCall(PhoneGlobals app,
505 Connection connection) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 if (app.cdmaPhoneCallState.getCurrentCallState() ==
507 CdmaPhoneCallState.PhoneCallState.IDLE) {
508 // This is the first outgoing call. Set the Phone Call State to ACTIVE
509 app.cdmaPhoneCallState.setCurrentCallState(
510 CdmaPhoneCallState.PhoneCallState.SINGLE_ACTIVE);
511 } else {
512 // This is the second outgoing call. Set the Phone Call State to 3WAY
513 app.cdmaPhoneCallState.setCurrentCallState(
514 CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE);
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700515
Santos Cordonda120f42014-08-06 04:44:34 -0700516 // TODO: Remove this code.
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700517 //app.getCallModeler().setCdmaOutgoing3WayCall(connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 }
519 }
520
521 /**
Santos Cordon69a69192013-08-22 14:25:42 -0700522 * @see placeCall below
523 */
524 public static int placeCall(Context context, Phone phone, String number, Uri contactRef,
525 boolean isEmergencyCall) {
526 return placeCall(context, phone, number, contactRef, isEmergencyCall,
527 CallGatewayManager.EMPTY_INFO, null);
528 }
529
530 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 * Dial the number using the phone passed in.
532 *
533 * If the connection is establised, this method issues a sync call
534 * that may block to query the caller info.
535 * TODO: Change the logic to use the async query.
536 *
537 * @param context To perform the CallerInfo query.
538 * @param phone the Phone object.
539 * @param number to be dialed as requested by the user. This is
540 * NOT the phone number to connect to. It is used only to build the
541 * call card and to update the call log. See above for restrictions.
542 * @param contactRef that triggered the call. Typically a 'tel:'
543 * uri but can also be a 'content://contacts' one.
544 * @param isEmergencyCall indicates that whether or not this is an
545 * emergency call
546 * @param gatewayUri Is the address used to setup the connection, null
547 * if not using a gateway
Santos Cordon69a69192013-08-22 14:25:42 -0700548 * @param callGateway Class for setting gateway data on a successful call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 *
550 * @return either CALL_STATUS_DIALED or CALL_STATUS_FAILED
551 */
Santos Cordon69a69192013-08-22 14:25:42 -0700552 public static int placeCall(Context context, Phone phone, String number, Uri contactRef,
553 boolean isEmergencyCall, RawGatewayInfo gatewayInfo, CallGatewayManager callGateway) {
554 final Uri gatewayUri = gatewayInfo.gatewayUri;
555
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 if (VDBG) {
557 log("placeCall()... number: '" + number + "'"
558 + ", GW:'" + gatewayUri + "'"
559 + ", contactRef:" + contactRef
560 + ", isEmergencyCall: " + isEmergencyCall);
561 } else {
562 log("placeCall()... number: " + toLogSafePhoneNumber(number)
563 + ", GW: " + (gatewayUri != null ? "non-null" : "null")
564 + ", emergency? " + isEmergencyCall);
565 }
566 final PhoneGlobals app = PhoneGlobals.getInstance();
567
568 boolean useGateway = false;
569 if (null != gatewayUri &&
570 !isEmergencyCall &&
571 PhoneUtils.isRoutableViaGateway(number)) { // Filter out MMI, OTA and other codes.
572 useGateway = true;
573 }
574
575 int status = CALL_STATUS_DIALED;
576 Connection connection;
577 String numberToDial;
578 if (useGateway) {
579 // TODO: 'tel' should be a constant defined in framework base
580 // somewhere (it is in webkit.)
Jay Shrauner137458b2014-09-05 14:27:25 -0700581 if (null == gatewayUri || !PhoneAccount.SCHEME_TEL.equals(gatewayUri.getScheme())) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 Log.e(LOG_TAG, "Unsupported URL:" + gatewayUri);
583 return CALL_STATUS_FAILED;
584 }
585
586 // We can use getSchemeSpecificPart because we don't allow #
587 // in the gateway numbers (treated a fragment delim.) However
588 // if we allow more complex gateway numbers sequence (with
589 // passwords or whatnot) that use #, this may break.
590 // TODO: Need to support MMI codes.
591 numberToDial = gatewayUri.getSchemeSpecificPart();
592 } else {
593 numberToDial = number;
594 }
595
596 // Remember if the phone state was in IDLE state before this call.
597 // After calling CallManager#dial(), getState() will return different state.
598 final boolean initiallyIdle = app.mCM.getState() == PhoneConstants.State.IDLE;
599
600 try {
Tyler Gunnc6514352015-06-02 21:46:34 -0700601 connection = app.mCM.dial(phone, numberToDial, VideoProfile.STATE_AUDIO_ONLY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 } catch (CallStateException ex) {
603 // CallStateException means a new outgoing call is not currently
604 // possible: either no more call slots exist, or there's another
605 // call already in the process of dialing or ringing.
606 Log.w(LOG_TAG, "Exception from app.mCM.dial()", ex);
607 return CALL_STATUS_FAILED;
608
609 // Note that it's possible for CallManager.dial() to return
610 // null *without* throwing an exception; that indicates that
611 // we dialed an MMI (see below).
612 }
613
614 int phoneType = phone.getPhoneType();
615
616 // On GSM phones, null is returned for MMI codes
617 if (null == connection) {
Santos Cordonf77a3392014-07-21 19:48:10 -0700618 status = CALL_STATUS_FAILED;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 } else {
Tyler Gunn10f1c6a2014-01-17 12:01:11 -0800620 // Now that the call is successful, we can save the gateway info for the call
621 if (callGateway != null) {
622 callGateway.setGatewayInfoForConnection(connection, gatewayInfo);
623 }
624
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700625 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700626 updateCdmaCallStateOnNewOutgoingCall(app, connection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 }
628
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 if (gatewayUri == null) {
630 // phone.dial() succeeded: we're now in a normal phone call.
631 // attach the URI to the CallerInfo Object if it is there,
632 // otherwise just attach the Uri Reference.
633 // if the uri does not have a "content" scheme, then we treat
634 // it as if it does NOT have a unique reference.
635 String content = context.getContentResolver().SCHEME_CONTENT;
636 if ((contactRef != null) && (contactRef.getScheme().equals(content))) {
637 Object userDataObject = connection.getUserData();
638 if (userDataObject == null) {
639 connection.setUserData(contactRef);
640 } else {
641 // TODO: This branch is dead code, we have
642 // just created the connection which has
643 // no user data (null) by default.
644 if (userDataObject instanceof CallerInfo) {
645 ((CallerInfo) userDataObject).contactRefUri = contactRef;
646 } else {
647 ((CallerInfoToken) userDataObject).currentInfo.contactRefUri =
648 contactRef;
649 }
650 }
651 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700652 }
Santos Cordonc264cba2013-09-19 04:55:40 -0700653
Chiao Chenge365ba72013-09-26 14:18:12 -0700654 startGetCallerInfo(context, connection, null, null, gatewayInfo);
655
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 setAudioMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 }
658
659 return status;
660 }
661
662 /* package */ static String toLogSafePhoneNumber(String number) {
663 // For unknown number, log empty string.
664 if (number == null) {
665 return "";
666 }
667
668 if (VDBG) {
669 // When VDBG is true we emit PII.
670 return number;
671 }
672
673 // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
674 // sanitized phone numbers.
675 StringBuilder builder = new StringBuilder();
676 for (int i = 0; i < number.length(); i++) {
677 char c = number.charAt(i);
678 if (c == '-' || c == '@' || c == '.') {
679 builder.append(c);
680 } else {
681 builder.append('x');
682 }
683 }
684 return builder.toString();
685 }
686
687 /**
688 * Wrapper function to control when to send an empty Flash command to the network.
689 * Mainly needed for CDMA networks, such as scenarios when we need to send a blank flash
690 * to the network prior to placing a 3-way call for it to be successful.
691 */
692 static void sendEmptyFlash(Phone phone) {
693 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
694 Call fgCall = phone.getForegroundCall();
695 if (fgCall.getState() == Call.State.ACTIVE) {
696 // Send the empty flash
697 if (DBG) Log.d(LOG_TAG, "onReceive: (CDMA) sending empty flash to network");
698 switchHoldingAndActive(phone.getBackgroundCall());
699 }
700 }
701 }
702
Gabriel Peal36ebb0d2014-03-20 09:20:43 -0700703 static void swap() {
704 final PhoneGlobals mApp = PhoneGlobals.getInstance();
705 if (!okToSwapCalls(mApp.mCM)) {
706 // TODO: throw an error instead?
707 return;
708 }
709
710 // Swap the fg and bg calls.
711 // In the future we may provide some way for user to choose among
712 // multiple background calls, for now, always act on the first background call.
713 PhoneUtils.switchHoldingAndActive(mApp.mCM.getFirstActiveBgCall());
Gabriel Peal36ebb0d2014-03-20 09:20:43 -0700714 }
715
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700716 /**
717 * @param heldCall is the background call want to be swapped
718 */
719 static void switchHoldingAndActive(Call heldCall) {
720 log("switchHoldingAndActive()...");
721 try {
722 CallManager cm = PhoneGlobals.getInstance().mCM;
723 if (heldCall.isIdle()) {
724 // no heldCall, so it is to hold active call
725 cm.switchHoldingAndActive(cm.getFgPhone().getBackgroundCall());
726 } else {
727 // has particular heldCall, so to switch
728 cm.switchHoldingAndActive(heldCall);
729 }
730 setAudioMode(cm);
731 } catch (CallStateException ex) {
732 Log.w(LOG_TAG, "switchHoldingAndActive: caught " + ex, ex);
733 }
734 }
735
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 static void mergeCalls() {
737 mergeCalls(PhoneGlobals.getInstance().mCM);
738 }
739
740 static void mergeCalls(CallManager cm) {
741 int phoneType = cm.getFgPhone().getPhoneType();
742 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
743 log("mergeCalls(): CDMA...");
744 PhoneGlobals app = PhoneGlobals.getInstance();
745 if (app.cdmaPhoneCallState.getCurrentCallState()
746 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
747 // Set the Phone Call State to conference
748 app.cdmaPhoneCallState.setCurrentCallState(
749 CdmaPhoneCallState.PhoneCallState.CONF_CALL);
750
751 // Send flash cmd
752 // TODO: Need to change the call from switchHoldingAndActive to
753 // something meaningful as we are not actually trying to swap calls but
754 // instead are merging two calls by sending a Flash command.
755 log("- sending flash...");
756 switchHoldingAndActive(cm.getFirstActiveBgCall());
757 }
758 } else {
759 try {
760 log("mergeCalls(): calling cm.conference()...");
761 cm.conference(cm.getFirstActiveBgCall());
762 } catch (CallStateException ex) {
763 Log.w(LOG_TAG, "mergeCalls: caught " + ex, ex);
764 }
765 }
766 }
767
768 static void separateCall(Connection c) {
769 try {
770 if (DBG) log("separateCall: " + toLogSafePhoneNumber(c.getAddress()));
771 c.separate();
772 } catch (CallStateException ex) {
773 Log.w(LOG_TAG, "separateCall: caught " + ex, ex);
774 }
775 }
776
777 /**
778 * Handle the MMIInitiate message and put up an alert that lets
779 * the user cancel the operation, if applicable.
780 *
781 * @param context context to get strings.
782 * @param mmiCode the MmiCode object being started.
783 * @param buttonCallbackMessage message to post when button is clicked.
784 * @param previousAlert a previous alert used in this activity.
785 * @return the dialog handle
786 */
787 static Dialog displayMMIInitiate(Context context,
788 MmiCode mmiCode,
789 Message buttonCallbackMessage,
790 Dialog previousAlert) {
791 if (DBG) log("displayMMIInitiate: " + mmiCode);
792 if (previousAlert != null) {
793 previousAlert.dismiss();
794 }
795
796 // The UI paradigm we are using now requests that all dialogs have
797 // user interaction, and that any other messages to the user should
798 // be by way of Toasts.
799 //
800 // In adhering to this request, all MMI initiating "OK" dialogs
801 // (non-cancelable MMIs) that end up being closed when the MMI
802 // completes (thereby showing a completion dialog) are being
803 // replaced with Toasts.
804 //
805 // As a side effect, moving to Toasts for the non-cancelable MMIs
806 // also means that buttonCallbackMessage (which was tied into "OK")
807 // is no longer invokable for these dialogs. This is not a problem
808 // since the only callback messages we supported were for cancelable
809 // MMIs anyway.
810 //
811 // A cancelable MMI is really just a USSD request. The term
812 // "cancelable" here means that we can cancel the request when the
813 // system prompts us for a response, NOT while the network is
814 // processing the MMI request. Any request to cancel a USSD while
815 // the network is NOT ready for a response may be ignored.
816 //
817 // With this in mind, we replace the cancelable alert dialog with
818 // a progress dialog, displayed until we receive a request from
819 // the the network. For more information, please see the comments
820 // in the displayMMIComplete() method below.
821 //
822 // Anything that is NOT a USSD request is a normal MMI request,
823 // which will bring up a toast (desribed above).
824
825 boolean isCancelable = (mmiCode != null) && mmiCode.isCancelable();
826
827 if (!isCancelable) {
828 if (DBG) log("not a USSD code, displaying status toast.");
829 CharSequence text = context.getText(R.string.mmiStarted);
830 Toast.makeText(context, text, Toast.LENGTH_SHORT)
831 .show();
832 return null;
833 } else {
834 if (DBG) log("running USSD code, displaying indeterminate progress.");
835
836 // create the indeterminate progress dialog and display it.
Alex Hills779841a2015-10-22 13:23:28 -0400837 ProgressDialog pd = new ProgressDialog(context, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 pd.setMessage(context.getText(R.string.ussdRunning));
839 pd.setCancelable(false);
840 pd.setIndeterminate(true);
841 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
842
843 pd.show();
844
845 return pd;
846 }
847
848 }
849
850 /**
851 * Handle the MMIComplete message and fire off an intent to display
852 * the message.
853 *
854 * @param context context to get strings.
855 * @param mmiCode MMI result.
856 * @param previousAlert a previous alert used in this activity.
857 */
858 static void displayMMIComplete(final Phone phone, Context context, final MmiCode mmiCode,
859 Message dismissCallbackMessage,
860 AlertDialog previousAlert) {
861 final PhoneGlobals app = PhoneGlobals.getInstance();
862 CharSequence text;
863 int title = 0; // title for the progress dialog, if needed.
864 MmiCode.State state = mmiCode.getState();
865
866 if (DBG) log("displayMMIComplete: state=" + state);
867
868 switch (state) {
869 case PENDING:
870 // USSD code asking for feedback from user.
871 text = mmiCode.getMessage();
872 if (DBG) log("- using text from PENDING MMI message: '" + text + "'");
873 break;
874 case CANCELLED:
875 text = null;
876 break;
877 case COMPLETE:
878 if (app.getPUKEntryActivity() != null) {
879 // if an attempt to unPUK the device was made, we specify
880 // the title and the message here.
881 title = com.android.internal.R.string.PinMmi;
882 text = context.getText(R.string.puk_unlocked);
883 break;
884 }
885 // All other conditions for the COMPLETE mmi state will cause
886 // the case to fall through to message logic in common with
887 // the FAILED case.
888
889 case FAILED:
890 text = mmiCode.getMessage();
891 if (DBG) log("- using text from MMI message: '" + text + "'");
892 break;
893 default:
894 throw new IllegalStateException("Unexpected MmiCode state: " + state);
895 }
896
897 if (previousAlert != null) {
898 previousAlert.dismiss();
899 }
900
901 // Check to see if a UI exists for the PUK activation. If it does
902 // exist, then it indicates that we're trying to unblock the PUK.
903 if ((app.getPUKEntryActivity() != null) && (state == MmiCode.State.COMPLETE)) {
904 if (DBG) log("displaying PUK unblocking progress dialog.");
905
906 // create the progress dialog, make sure the flags and type are
907 // set correctly.
Alex Hills779841a2015-10-22 13:23:28 -0400908 ProgressDialog pd = new ProgressDialog(app, THEME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 pd.setTitle(title);
910 pd.setMessage(text);
911 pd.setCancelable(false);
912 pd.setIndeterminate(true);
913 pd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
914 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
915
916 // display the dialog
917 pd.show();
918
919 // indicate to the Phone app that the progress dialog has
920 // been assigned for the PUK unlock / SIM READY process.
921 app.setPukEntryProgressDialog(pd);
922
923 } else {
924 // In case of failure to unlock, we'll need to reset the
925 // PUK unlock activity, so that the user may try again.
926 if (app.getPUKEntryActivity() != null) {
927 app.setPukEntryActivity(null);
928 }
929
930 // A USSD in a pending state means that it is still
931 // interacting with the user.
932 if (state != MmiCode.State.PENDING) {
933 if (DBG) log("MMI code has finished running.");
934
935 if (DBG) log("Extended NW displayMMIInitiate (" + text + ")");
936 if (text == null || text.length() == 0)
937 return;
938
939 // displaying system alert dialog on the screen instead of
940 // using another activity to display the message. This
941 // places the message at the forefront of the UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942
Etan Cohen37abbab2014-03-07 16:57:03 -0800943 if (sUssdDialog == null) {
Yorke Leee0f2b792014-10-15 12:30:44 -0700944 sUssdDialog = new AlertDialog.Builder(context, THEME)
Etan Cohen37abbab2014-03-07 16:57:03 -0800945 .setPositiveButton(R.string.ok, null)
946 .setCancelable(true)
947 .setOnDismissListener(new DialogInterface.OnDismissListener() {
948 @Override
949 public void onDismiss(DialogInterface dialog) {
950 sUssdMsg.setLength(0);
951 }
952 })
953 .create();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954
Etan Cohen37abbab2014-03-07 16:57:03 -0800955 sUssdDialog.getWindow().setType(
Wenlu Wue8e05712015-03-06 14:20:18 +0100956 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
Etan Cohen37abbab2014-03-07 16:57:03 -0800957 sUssdDialog.getWindow().addFlags(
958 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
959 }
960 if (sUssdMsg.length() != 0) {
961 sUssdMsg
962 .insert(0, "\n")
963 .insert(0, app.getResources().getString(R.string.ussd_dialog_sep))
964 .insert(0, "\n");
965 }
966 sUssdMsg.insert(0, text);
967 sUssdDialog.setMessage(sUssdMsg.toString());
968 sUssdDialog.show();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 } else {
970 if (DBG) log("USSD code has requested user input. Constructing input dialog.");
971
972 // USSD MMI code that is interacting with the user. The
973 // basic set of steps is this:
974 // 1. User enters a USSD request
975 // 2. We recognize the request and displayMMIInitiate
976 // (above) creates a progress dialog.
977 // 3. Request returns and we get a PENDING or COMPLETE
978 // message.
979 // 4. These MMI messages are caught in the PhoneApp
980 // (onMMIComplete) and the InCallScreen
981 // (mHandler.handleMessage) which bring up this dialog
982 // and closes the original progress dialog,
983 // respectively.
984 // 5. If the message is anything other than PENDING,
985 // we are done, and the alert dialog (directly above)
986 // displays the outcome.
987 // 6. If the network is requesting more information from
988 // the user, the MMI will be in a PENDING state, and
989 // we display this dialog with the message.
990 // 7. User input, or cancel requests result in a return
991 // to step 1. Keep in mind that this is the only
992 // time that a USSD should be canceled.
993
994 // inflate the layout with the scrolling text area for the dialog.
Andrew Leeb3ee4dc2015-01-23 17:35:00 -0800995 ContextThemeWrapper contextThemeWrapper =
996 new ContextThemeWrapper(context, R.style.DialerAlertDialogTheme);
997 LayoutInflater inflater = (LayoutInflater) contextThemeWrapper.getSystemService(
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 Context.LAYOUT_INFLATER_SERVICE);
999 View dialogView = inflater.inflate(R.layout.dialog_ussd_response, null);
1000
1001 // get the input field.
1002 final EditText inputText = (EditText) dialogView.findViewById(R.id.input_field);
1003
1004 // specify the dialog's click listener, with SEND and CANCEL logic.
1005 final DialogInterface.OnClickListener mUSSDDialogListener =
1006 new DialogInterface.OnClickListener() {
1007 public void onClick(DialogInterface dialog, int whichButton) {
1008 switch (whichButton) {
1009 case DialogInterface.BUTTON_POSITIVE:
1010 // As per spec 24.080, valid length of ussd string
1011 // is 1 - 160. If length is out of the range then
1012 // display toast message & Cancel MMI operation.
1013 if (inputText.length() < MIN_USSD_LEN
1014 || inputText.length() > MAX_USSD_LEN) {
1015 Toast.makeText(app,
1016 app.getResources().getString(R.string.enter_input,
1017 MIN_USSD_LEN, MAX_USSD_LEN),
1018 Toast.LENGTH_LONG).show();
1019 if (mmiCode.isCancelable()) {
1020 mmiCode.cancel();
1021 }
1022 } else {
1023 phone.sendUssdResponse(inputText.getText().toString());
1024 }
1025 break;
1026 case DialogInterface.BUTTON_NEGATIVE:
1027 if (mmiCode.isCancelable()) {
1028 mmiCode.cancel();
1029 }
1030 break;
1031 }
1032 }
1033 };
1034
1035 // build the dialog
Andrew Leeb3ee4dc2015-01-23 17:35:00 -08001036 final AlertDialog newDialog = new AlertDialog.Builder(contextThemeWrapper)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 .setMessage(text)
1038 .setView(dialogView)
1039 .setPositiveButton(R.string.send_button, mUSSDDialogListener)
1040 .setNegativeButton(R.string.cancel, mUSSDDialogListener)
1041 .setCancelable(false)
1042 .create();
1043
1044 // attach the key listener to the dialog's input field and make
1045 // sure focus is set.
1046 final View.OnKeyListener mUSSDDialogInputListener =
1047 new View.OnKeyListener() {
1048 public boolean onKey(View v, int keyCode, KeyEvent event) {
1049 switch (keyCode) {
1050 case KeyEvent.KEYCODE_CALL:
1051 case KeyEvent.KEYCODE_ENTER:
1052 if(event.getAction() == KeyEvent.ACTION_DOWN) {
1053 phone.sendUssdResponse(inputText.getText().toString());
1054 newDialog.dismiss();
1055 }
1056 return true;
1057 }
1058 return false;
1059 }
1060 };
1061 inputText.setOnKeyListener(mUSSDDialogInputListener);
1062 inputText.requestFocus();
1063
1064 // set the window properties of the dialog
1065 newDialog.getWindow().setType(
1066 WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
1067 newDialog.getWindow().addFlags(
1068 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
1069
1070 // now show the dialog!
1071 newDialog.show();
Andrew Leeb3ee4dc2015-01-23 17:35:00 -08001072
1073 newDialog.getButton(DialogInterface.BUTTON_POSITIVE)
1074 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
1075 newDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
1076 .setTextColor(context.getResources().getColor(R.color.dialer_theme_color));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 }
1078 }
1079 }
1080
1081 /**
1082 * Cancels the current pending MMI operation, if applicable.
1083 * @return true if we canceled an MMI operation, or false
1084 * if the current pending MMI wasn't cancelable
1085 * or if there was no current pending MMI at all.
1086 *
1087 * @see displayMMIInitiate
1088 */
1089 static boolean cancelMmiCode(Phone phone) {
1090 List<? extends MmiCode> pendingMmis = phone.getPendingMmiCodes();
1091 int count = pendingMmis.size();
1092 if (DBG) log("cancelMmiCode: num pending MMIs = " + count);
1093
1094 boolean canceled = false;
1095 if (count > 0) {
1096 // assume that we only have one pending MMI operation active at a time.
1097 // I don't think it's possible to enter multiple MMI codes concurrently
1098 // in the phone UI, because during the MMI operation, an Alert panel
1099 // is displayed, which prevents more MMI code from being entered.
1100 MmiCode mmiCode = pendingMmis.get(0);
1101 if (mmiCode.isCancelable()) {
1102 mmiCode.cancel();
1103 canceled = true;
1104 }
1105 }
1106 return canceled;
1107 }
1108
1109 public static class VoiceMailNumberMissingException extends Exception {
1110 VoiceMailNumberMissingException() {
1111 super();
1112 }
1113
1114 VoiceMailNumberMissingException(String msg) {
1115 super(msg);
1116 }
1117 }
1118
1119 /**
1120 * Given an Intent (which is presumably the ACTION_CALL intent that
1121 * initiated this outgoing call), figure out the actual phone number we
1122 * should dial.
1123 *
1124 * Note that the returned "number" may actually be a SIP address,
1125 * if the specified intent contains a sip: URI.
1126 *
1127 * This method is basically a wrapper around PhoneUtils.getNumberFromIntent(),
1128 * except it's also aware of the EXTRA_ACTUAL_NUMBER_TO_DIAL extra.
1129 * (That extra, if present, tells us the exact string to pass down to the
1130 * telephony layer. It's guaranteed to be safe to dial: it's either a PSTN
1131 * phone number with separators and keypad letters stripped out, or a raw
1132 * unencoded SIP address.)
1133 *
1134 * @return the phone number corresponding to the specified Intent, or null
1135 * if the Intent has no action or if the intent's data is malformed or
1136 * missing.
1137 *
1138 * @throws VoiceMailNumberMissingException if the intent
1139 * contains a "voicemail" URI, but there's no voicemail
1140 * number configured on the device.
1141 */
1142 public static String getInitialNumber(Intent intent)
1143 throws PhoneUtils.VoiceMailNumberMissingException {
1144 if (DBG) log("getInitialNumber(): " + intent);
1145
1146 String action = intent.getAction();
1147 if (TextUtils.isEmpty(action)) {
1148 return null;
1149 }
1150
1151 // If the EXTRA_ACTUAL_NUMBER_TO_DIAL extra is present, get the phone
1152 // number from there. (That extra takes precedence over the actual data
1153 // included in the intent.)
1154 if (intent.hasExtra(OutgoingCallBroadcaster.EXTRA_ACTUAL_NUMBER_TO_DIAL)) {
1155 String actualNumberToDial =
1156 intent.getStringExtra(OutgoingCallBroadcaster.EXTRA_ACTUAL_NUMBER_TO_DIAL);
1157 if (DBG) {
1158 log("==> got EXTRA_ACTUAL_NUMBER_TO_DIAL; returning '"
1159 + toLogSafePhoneNumber(actualNumberToDial) + "'");
1160 }
1161 return actualNumberToDial;
1162 }
1163
1164 return getNumberFromIntent(PhoneGlobals.getInstance(), intent);
1165 }
1166
1167 /**
1168 * Gets the phone number to be called from an intent. Requires a Context
1169 * to access the contacts database, and a Phone to access the voicemail
1170 * number.
1171 *
1172 * <p>If <code>phone</code> is <code>null</code>, the function will return
1173 * <code>null</code> for <code>voicemail:</code> URIs;
1174 * if <code>context</code> is <code>null</code>, the function will return
1175 * <code>null</code> for person/phone URIs.</p>
1176 *
1177 * <p>If the intent contains a <code>sip:</code> URI, the returned
1178 * "number" is actually the SIP address.
1179 *
1180 * @param context a context to use (or
1181 * @param intent the intent
1182 *
1183 * @throws VoiceMailNumberMissingException if <code>intent</code> contains
1184 * a <code>voicemail:</code> URI, but <code>phone</code> does not
1185 * have a voicemail number set.
1186 *
1187 * @return the phone number (or SIP address) that would be called by the intent,
1188 * or <code>null</code> if the number cannot be found.
1189 */
1190 private static String getNumberFromIntent(Context context, Intent intent)
1191 throws VoiceMailNumberMissingException {
1192 Uri uri = intent.getData();
1193 String scheme = uri.getScheme();
1194
1195 // The sip: scheme is simple: just treat the rest of the URI as a
1196 // SIP address.
Jay Shrauner137458b2014-09-05 14:27:25 -07001197 if (PhoneAccount.SCHEME_SIP.equals(scheme)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 return uri.getSchemeSpecificPart();
1199 }
1200
1201 // Otherwise, let PhoneNumberUtils.getNumberFromIntent() handle
1202 // the other cases (i.e. tel: and voicemail: and contact: URIs.)
1203
1204 final String number = PhoneNumberUtils.getNumberFromIntent(intent, context);
1205
1206 // Check for a voicemail-dialing request. If the voicemail number is
1207 // empty, throw a VoiceMailNumberMissingException.
Jay Shrauner137458b2014-09-05 14:27:25 -07001208 if (PhoneAccount.SCHEME_VOICEMAIL.equals(scheme) &&
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 (number == null || TextUtils.isEmpty(number)))
1210 throw new VoiceMailNumberMissingException();
1211
1212 return number;
1213 }
1214
1215 /**
1216 * Returns the caller-id info corresponding to the specified Connection.
1217 * (This is just a simple wrapper around CallerInfo.getCallerInfo(): we
1218 * extract a phone number from the specified Connection, and feed that
1219 * number into CallerInfo.getCallerInfo().)
1220 *
1221 * The returned CallerInfo may be null in certain error cases, like if the
1222 * specified Connection was null, or if we weren't able to get a valid
1223 * phone number from the Connection.
1224 *
1225 * Finally, if the getCallerInfo() call did succeed, we save the resulting
1226 * CallerInfo object in the "userData" field of the Connection.
1227 *
1228 * NOTE: This API should be avoided, with preference given to the
1229 * asynchronous startGetCallerInfo API.
1230 */
1231 static CallerInfo getCallerInfo(Context context, Connection c) {
1232 CallerInfo info = null;
1233
1234 if (c != null) {
1235 //See if there is a URI attached. If there is, this means
1236 //that there is no CallerInfo queried yet, so we'll need to
1237 //replace the URI with a full CallerInfo object.
1238 Object userDataObject = c.getUserData();
1239 if (userDataObject instanceof Uri) {
1240 info = CallerInfo.getCallerInfo(context, (Uri) userDataObject);
1241 if (info != null) {
1242 c.setUserData(info);
1243 }
1244 } else {
1245 if (userDataObject instanceof CallerInfoToken) {
1246 //temporary result, while query is running
1247 info = ((CallerInfoToken) userDataObject).currentInfo;
1248 } else {
1249 //final query result
1250 info = (CallerInfo) userDataObject;
1251 }
1252 if (info == null) {
1253 // No URI, or Existing CallerInfo, so we'll have to make do with
1254 // querying a new CallerInfo using the connection's phone number.
1255 String number = c.getAddress();
1256
1257 if (DBG) log("getCallerInfo: number = " + toLogSafePhoneNumber(number));
1258
1259 if (!TextUtils.isEmpty(number)) {
1260 info = CallerInfo.getCallerInfo(context, number);
1261 if (info != null) {
1262 c.setUserData(info);
1263 }
1264 }
1265 }
1266 }
1267 }
1268 return info;
1269 }
1270
1271 /**
1272 * Class returned by the startGetCallerInfo call to package a temporary
1273 * CallerInfo Object, to be superceded by the CallerInfo Object passed
1274 * into the listener when the query with token mAsyncQueryToken is complete.
1275 */
1276 public static class CallerInfoToken {
1277 /**indicates that there will no longer be updates to this request.*/
1278 public boolean isFinal;
1279
1280 public CallerInfo currentInfo;
1281 public CallerInfoAsyncQuery asyncQuery;
1282 }
1283
1284 /**
1285 * Start a CallerInfo Query based on the earliest connection in the call.
1286 */
1287 static CallerInfoToken startGetCallerInfo(Context context, Call call,
1288 CallerInfoAsyncQuery.OnQueryCompleteListener listener, Object cookie) {
1289 Connection conn = null;
1290 int phoneType = call.getPhone().getPhoneType();
1291 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1292 conn = call.getLatestConnection();
1293 } else if ((phoneType == PhoneConstants.PHONE_TYPE_GSM)
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001294 || (phoneType == PhoneConstants.PHONE_TYPE_SIP)
Etan Cohen0ca1c802014-07-07 15:35:48 -07001295 || (phoneType == PhoneConstants.PHONE_TYPE_IMS)
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001296 || (phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 conn = call.getEarliestConnection();
1298 } else {
1299 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1300 }
1301
1302 return startGetCallerInfo(context, conn, listener, cookie);
1303 }
1304
Chiao Chenge365ba72013-09-26 14:18:12 -07001305 static CallerInfoToken startGetCallerInfo(Context context, Connection c,
1306 CallerInfoAsyncQuery.OnQueryCompleteListener listener, Object cookie) {
1307 return startGetCallerInfo(context, c, listener, cookie, null);
1308 }
1309
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001310 /**
1311 * place a temporary callerinfo object in the hands of the caller and notify
1312 * caller when the actual query is done.
1313 */
1314 static CallerInfoToken startGetCallerInfo(Context context, Connection c,
Chiao Chenge365ba72013-09-26 14:18:12 -07001315 CallerInfoAsyncQuery.OnQueryCompleteListener listener, Object cookie,
1316 RawGatewayInfo info) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 CallerInfoToken cit;
1318
1319 if (c == null) {
1320 //TODO: perhaps throw an exception here.
1321 cit = new CallerInfoToken();
1322 cit.asyncQuery = null;
1323 return cit;
1324 }
1325
1326 Object userDataObject = c.getUserData();
1327
1328 // There are now 3 states for the Connection's userData object:
1329 //
1330 // (1) Uri - query has not been executed yet
1331 //
1332 // (2) CallerInfoToken - query is executing, but has not completed.
1333 //
1334 // (3) CallerInfo - query has executed.
1335 //
1336 // In each case we have slightly different behaviour:
1337 // 1. If the query has not been executed yet (Uri or null), we start
1338 // query execution asynchronously, and note it by attaching a
1339 // CallerInfoToken as the userData.
1340 // 2. If the query is executing (CallerInfoToken), we've essentially
1341 // reached a state where we've received multiple requests for the
1342 // same callerInfo. That means that once the query is complete,
1343 // we'll need to execute the additional listener requested.
1344 // 3. If the query has already been executed (CallerInfo), we just
1345 // return the CallerInfo object as expected.
1346 // 4. Regarding isFinal - there are cases where the CallerInfo object
1347 // will not be attached, like when the number is empty (caller id
1348 // blocking). This flag is used to indicate that the
1349 // CallerInfoToken object is going to be permanent since no
1350 // query results will be returned. In the case where a query
1351 // has been completed, this flag is used to indicate to the caller
1352 // that the data will not be updated since it is valid.
1353 //
1354 // Note: For the case where a number is NOT retrievable, we leave
1355 // the CallerInfo as null in the CallerInfoToken. This is
1356 // something of a departure from the original code, since the old
1357 // code manufactured a CallerInfo object regardless of the query
1358 // outcome. From now on, we will append an empty CallerInfo
1359 // object, to mirror previous behaviour, and to avoid Null Pointer
1360 // Exceptions.
1361
1362 if (userDataObject instanceof Uri) {
1363 // State (1): query has not been executed yet
1364
1365 //create a dummy callerinfo, populate with what we know from URI.
1366 cit = new CallerInfoToken();
1367 cit.currentInfo = new CallerInfo();
1368 cit.asyncQuery = CallerInfoAsyncQuery.startQuery(QUERY_TOKEN, context,
1369 (Uri) userDataObject, sCallerInfoQueryListener, c);
1370 cit.asyncQuery.addQueryListener(QUERY_TOKEN, listener, cookie);
1371 cit.isFinal = false;
1372
1373 c.setUserData(cit);
1374
1375 if (DBG) log("startGetCallerInfo: query based on Uri: " + userDataObject);
1376
1377 } else if (userDataObject == null) {
1378 // No URI, or Existing CallerInfo, so we'll have to make do with
1379 // querying a new CallerInfo using the connection's phone number.
1380 String number = c.getAddress();
1381
Chiao Cheng685dcb62013-10-02 11:51:06 -07001382 if (info != null && info != CallGatewayManager.EMPTY_INFO) {
Chiao Chenge365ba72013-09-26 14:18:12 -07001383 // Gateway number, the connection number is actually the gateway number.
1384 // need to lookup via dialed number.
1385 number = info.trueNumber;
1386 }
1387
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 if (DBG) {
1389 log("PhoneUtils.startGetCallerInfo: new query for phone number...");
1390 log("- number (address): " + toLogSafePhoneNumber(number));
1391 log("- c: " + c);
1392 log("- phone: " + c.getCall().getPhone());
1393 int phoneType = c.getCall().getPhone().getPhoneType();
1394 log("- phoneType: " + phoneType);
1395 switch (phoneType) {
1396 case PhoneConstants.PHONE_TYPE_NONE: log(" ==> PHONE_TYPE_NONE"); break;
1397 case PhoneConstants.PHONE_TYPE_GSM: log(" ==> PHONE_TYPE_GSM"); break;
Etan Cohen0ca1c802014-07-07 15:35:48 -07001398 case PhoneConstants.PHONE_TYPE_IMS: log(" ==> PHONE_TYPE_IMS"); break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 case PhoneConstants.PHONE_TYPE_CDMA: log(" ==> PHONE_TYPE_CDMA"); break;
1400 case PhoneConstants.PHONE_TYPE_SIP: log(" ==> PHONE_TYPE_SIP"); break;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001401 case PhoneConstants.PHONE_TYPE_THIRD_PARTY:
1402 log(" ==> PHONE_TYPE_THIRD_PARTY");
1403 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 default: log(" ==> Unknown phone type"); break;
1405 }
1406 }
1407
1408 cit = new CallerInfoToken();
1409 cit.currentInfo = new CallerInfo();
1410
1411 // Store CNAP information retrieved from the Connection (we want to do this
1412 // here regardless of whether the number is empty or not).
1413 cit.currentInfo.cnapName = c.getCnapName();
1414 cit.currentInfo.name = cit.currentInfo.cnapName; // This can still get overwritten
1415 // by ContactInfo later
1416 cit.currentInfo.numberPresentation = c.getNumberPresentation();
1417 cit.currentInfo.namePresentation = c.getCnapNamePresentation();
1418
1419 if (VDBG) {
1420 log("startGetCallerInfo: number = " + number);
1421 log("startGetCallerInfo: CNAP Info from FW(1): name="
1422 + cit.currentInfo.cnapName
1423 + ", Name/Number Pres=" + cit.currentInfo.numberPresentation);
1424 }
1425
1426 // handling case where number is null (caller id hidden) as well.
1427 if (!TextUtils.isEmpty(number)) {
1428 // Check for special CNAP cases and modify the CallerInfo accordingly
1429 // to be sure we keep the right information to display/log later
1430 number = modifyForSpecialCnapCases(context, cit.currentInfo, number,
1431 cit.currentInfo.numberPresentation);
1432
1433 cit.currentInfo.phoneNumber = number;
1434 // For scenarios where we may receive a valid number from the network but a
1435 // restricted/unavailable presentation, we do not want to perform a contact query
1436 // (see note on isFinal above). So we set isFinal to true here as well.
1437 if (cit.currentInfo.numberPresentation != PhoneConstants.PRESENTATION_ALLOWED) {
1438 cit.isFinal = true;
1439 } else {
1440 if (DBG) log("==> Actually starting CallerInfoAsyncQuery.startQuery()...");
1441 cit.asyncQuery = CallerInfoAsyncQuery.startQuery(QUERY_TOKEN, context,
1442 number, sCallerInfoQueryListener, c);
1443 cit.asyncQuery.addQueryListener(QUERY_TOKEN, listener, cookie);
1444 cit.isFinal = false;
1445 }
1446 } else {
1447 // This is the case where we are querying on a number that
1448 // is null or empty, like a caller whose caller id is
1449 // blocked or empty (CLIR). The previous behaviour was to
1450 // throw a null CallerInfo object back to the user, but
1451 // this departure is somewhat cleaner.
1452 if (DBG) log("startGetCallerInfo: No query to start, send trivial reply.");
1453 cit.isFinal = true; // please see note on isFinal, above.
1454 }
1455
1456 c.setUserData(cit);
1457
1458 if (DBG) {
1459 log("startGetCallerInfo: query based on number: " + toLogSafePhoneNumber(number));
1460 }
1461
1462 } else if (userDataObject instanceof CallerInfoToken) {
1463 // State (2): query is executing, but has not completed.
1464
1465 // just tack on this listener to the queue.
1466 cit = (CallerInfoToken) userDataObject;
1467
1468 // handling case where number is null (caller id hidden) as well.
1469 if (cit.asyncQuery != null) {
1470 cit.asyncQuery.addQueryListener(QUERY_TOKEN, listener, cookie);
1471
1472 if (DBG) log("startGetCallerInfo: query already running, adding listener: " +
1473 listener.getClass().toString());
1474 } else {
1475 // handling case where number/name gets updated later on by the network
1476 String updatedNumber = c.getAddress();
Chiao Chenge365ba72013-09-26 14:18:12 -07001477
1478 if (info != null) {
1479 // Gateway number, the connection number is actually the gateway number.
1480 // need to lookup via dialed number.
1481 updatedNumber = info.trueNumber;
1482 }
1483
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 if (DBG) {
1485 log("startGetCallerInfo: updatedNumber initially = "
1486 + toLogSafePhoneNumber(updatedNumber));
1487 }
1488 if (!TextUtils.isEmpty(updatedNumber)) {
1489 // Store CNAP information retrieved from the Connection
1490 cit.currentInfo.cnapName = c.getCnapName();
1491 // This can still get overwritten by ContactInfo
1492 cit.currentInfo.name = cit.currentInfo.cnapName;
1493 cit.currentInfo.numberPresentation = c.getNumberPresentation();
1494 cit.currentInfo.namePresentation = c.getCnapNamePresentation();
1495
1496 updatedNumber = modifyForSpecialCnapCases(context, cit.currentInfo,
1497 updatedNumber, cit.currentInfo.numberPresentation);
1498
1499 cit.currentInfo.phoneNumber = updatedNumber;
1500 if (DBG) {
1501 log("startGetCallerInfo: updatedNumber="
1502 + toLogSafePhoneNumber(updatedNumber));
1503 }
1504 if (VDBG) {
1505 log("startGetCallerInfo: CNAP Info from FW(2): name="
1506 + cit.currentInfo.cnapName
1507 + ", Name/Number Pres=" + cit.currentInfo.numberPresentation);
1508 } else if (DBG) {
1509 log("startGetCallerInfo: CNAP Info from FW(2)");
1510 }
1511 // For scenarios where we may receive a valid number from the network but a
1512 // restricted/unavailable presentation, we do not want to perform a contact query
1513 // (see note on isFinal above). So we set isFinal to true here as well.
1514 if (cit.currentInfo.numberPresentation != PhoneConstants.PRESENTATION_ALLOWED) {
1515 cit.isFinal = true;
1516 } else {
1517 cit.asyncQuery = CallerInfoAsyncQuery.startQuery(QUERY_TOKEN, context,
1518 updatedNumber, sCallerInfoQueryListener, c);
1519 cit.asyncQuery.addQueryListener(QUERY_TOKEN, listener, cookie);
1520 cit.isFinal = false;
1521 }
1522 } else {
1523 if (DBG) log("startGetCallerInfo: No query to attach to, send trivial reply.");
1524 if (cit.currentInfo == null) {
1525 cit.currentInfo = new CallerInfo();
1526 }
1527 // Store CNAP information retrieved from the Connection
1528 cit.currentInfo.cnapName = c.getCnapName(); // This can still get
1529 // overwritten by ContactInfo
1530 cit.currentInfo.name = cit.currentInfo.cnapName;
1531 cit.currentInfo.numberPresentation = c.getNumberPresentation();
1532 cit.currentInfo.namePresentation = c.getCnapNamePresentation();
1533
1534 if (VDBG) {
1535 log("startGetCallerInfo: CNAP Info from FW(3): name="
1536 + cit.currentInfo.cnapName
1537 + ", Name/Number Pres=" + cit.currentInfo.numberPresentation);
1538 } else if (DBG) {
1539 log("startGetCallerInfo: CNAP Info from FW(3)");
1540 }
1541 cit.isFinal = true; // please see note on isFinal, above.
1542 }
1543 }
1544 } else {
1545 // State (3): query is complete.
1546
1547 // The connection's userDataObject is a full-fledged
1548 // CallerInfo instance. Wrap it in a CallerInfoToken and
1549 // return it to the user.
1550
1551 cit = new CallerInfoToken();
1552 cit.currentInfo = (CallerInfo) userDataObject;
1553 cit.asyncQuery = null;
1554 cit.isFinal = true;
1555 // since the query is already done, call the listener.
1556 if (DBG) log("startGetCallerInfo: query already done, returning CallerInfo");
1557 if (DBG) log("==> cit.currentInfo = " + cit.currentInfo);
1558 }
1559 return cit;
1560 }
1561
1562 /**
1563 * Static CallerInfoAsyncQuery.OnQueryCompleteListener instance that
1564 * we use with all our CallerInfoAsyncQuery.startQuery() requests.
1565 */
1566 private static final int QUERY_TOKEN = -1;
1567 static CallerInfoAsyncQuery.OnQueryCompleteListener sCallerInfoQueryListener =
1568 new CallerInfoAsyncQuery.OnQueryCompleteListener () {
1569 /**
1570 * When the query completes, we stash the resulting CallerInfo
1571 * object away in the Connection's "userData" (where it will
1572 * later be retrieved by the in-call UI.)
1573 */
1574 public void onQueryComplete(int token, Object cookie, CallerInfo ci) {
1575 if (DBG) log("query complete, updating connection.userdata");
1576 Connection conn = (Connection) cookie;
1577
1578 // Added a check if CallerInfo is coming from ContactInfo or from Connection.
1579 // If no ContactInfo, then we want to use CNAP information coming from network
1580 if (DBG) log("- onQueryComplete: CallerInfo:" + ci);
1581 if (ci.contactExists || ci.isEmergencyNumber() || ci.isVoiceMailNumber()) {
1582 // If the number presentation has not been set by
1583 // the ContactInfo, use the one from the
1584 // connection.
1585
1586 // TODO: Need a new util method to merge the info
1587 // from the Connection in a CallerInfo object.
1588 // Here 'ci' is a new CallerInfo instance read
1589 // from the DB. It has lost all the connection
1590 // info preset before the query (see PhoneUtils
1591 // line 1334). We should have a method to merge
1592 // back into this new instance the info from the
1593 // connection object not set by the DB. If the
1594 // Connection already has a CallerInfo instance in
1595 // userData, then we could use this instance to
1596 // fill 'ci' in. The same routine could be used in
1597 // PhoneUtils.
1598 if (0 == ci.numberPresentation) {
1599 ci.numberPresentation = conn.getNumberPresentation();
1600 }
1601 } else {
1602 // No matching contact was found for this number.
1603 // Return a new CallerInfo based solely on the CNAP
1604 // information from the network.
1605
1606 CallerInfo newCi = getCallerInfo(null, conn);
1607
1608 // ...but copy over the (few) things we care about
1609 // from the original CallerInfo object:
1610 if (newCi != null) {
1611 newCi.phoneNumber = ci.phoneNumber; // To get formatted phone number
1612 newCi.geoDescription = ci.geoDescription; // To get geo description string
1613 ci = newCi;
1614 }
1615 }
1616
1617 if (DBG) log("==> Stashing CallerInfo " + ci + " into the connection...");
1618 conn.setUserData(ci);
1619 }
1620 };
1621
1622
1623 /**
1624 * Returns a single "name" for the specified given a CallerInfo object.
1625 * If the name is null, return defaultString as the default value, usually
1626 * context.getString(R.string.unknown).
1627 */
1628 static String getCompactNameFromCallerInfo(CallerInfo ci, Context context) {
1629 if (DBG) log("getCompactNameFromCallerInfo: info = " + ci);
1630
1631 String compactName = null;
1632 if (ci != null) {
1633 if (TextUtils.isEmpty(ci.name)) {
1634 // Perform any modifications for special CNAP cases to
1635 // the phone number being displayed, if applicable.
1636 compactName = modifyForSpecialCnapCases(context, ci, ci.phoneNumber,
1637 ci.numberPresentation);
1638 } else {
1639 // Don't call modifyForSpecialCnapCases on regular name. See b/2160795.
1640 compactName = ci.name;
1641 }
1642 }
1643
1644 if ((compactName == null) || (TextUtils.isEmpty(compactName))) {
1645 // If we're still null/empty here, then check if we have a presentation
1646 // string that takes precedence that we could return, otherwise display
1647 // "unknown" string.
1648 if (ci != null && ci.numberPresentation == PhoneConstants.PRESENTATION_RESTRICTED) {
1649 compactName = context.getString(R.string.private_num);
1650 } else if (ci != null && ci.numberPresentation == PhoneConstants.PRESENTATION_PAYPHONE) {
1651 compactName = context.getString(R.string.payphone);
1652 } else {
1653 compactName = context.getString(R.string.unknown);
1654 }
1655 }
1656 if (VDBG) log("getCompactNameFromCallerInfo: compactName=" + compactName);
1657 return compactName;
1658 }
1659
1660 /**
1661 * Returns true if the specified Call is a "conference call", meaning
1662 * that it owns more than one Connection object. This information is
1663 * used to trigger certain UI changes that appear when a conference
1664 * call is active (like displaying the label "Conference call", and
1665 * enabling the "Manage conference" UI.)
1666 *
1667 * Watch out: This method simply checks the number of Connections,
1668 * *not* their states. So if a Call has (for example) one ACTIVE
1669 * connection and one DISCONNECTED connection, this method will return
1670 * true (which is unintuitive, since the Call isn't *really* a
1671 * conference call any more.)
1672 *
1673 * @return true if the specified call has more than one connection (in any state.)
1674 */
1675 static boolean isConferenceCall(Call call) {
1676 // CDMA phones don't have the same concept of "conference call" as
1677 // GSM phones do; there's no special "conference call" state of
1678 // the UI or a "manage conference" function. (Instead, when
1679 // you're in a 3-way call, all we can do is display the "generic"
1680 // state of the UI.) So as far as the in-call UI is concerned,
1681 // Conference corresponds to generic display.
1682 final PhoneGlobals app = PhoneGlobals.getInstance();
1683 int phoneType = call.getPhone().getPhoneType();
1684 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1685 CdmaPhoneCallState.PhoneCallState state = app.cdmaPhoneCallState.getCurrentCallState();
1686 if ((state == CdmaPhoneCallState.PhoneCallState.CONF_CALL)
1687 || ((state == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE)
1688 && !app.cdmaPhoneCallState.IsThreeWayCallOrigStateDialing())) {
1689 return true;
1690 }
1691 } else {
1692 List<Connection> connections = call.getConnections();
1693 if (connections != null && connections.size() > 1) {
1694 return true;
1695 }
1696 }
1697 return false;
1698
1699 // TODO: We may still want to change the semantics of this method
1700 // to say that a given call is only really a conference call if
1701 // the number of ACTIVE connections, not the total number of
1702 // connections, is greater than one. (See warning comment in the
1703 // javadoc above.)
1704 // Here's an implementation of that:
1705 // if (connections == null) {
1706 // return false;
1707 // }
1708 // int numActiveConnections = 0;
1709 // for (Connection conn : connections) {
1710 // if (DBG) log(" - CONN: " + conn + ", state = " + conn.getState());
1711 // if (conn.getState() == Call.State.ACTIVE) numActiveConnections++;
1712 // if (numActiveConnections > 1) {
1713 // return true;
1714 // }
1715 // }
1716 // return false;
1717 }
1718
1719 /**
1720 * Launch the Dialer to start a new call.
1721 * This is just a wrapper around the ACTION_DIAL intent.
1722 */
1723 /* package */ static boolean startNewCall(final CallManager cm) {
1724 final PhoneGlobals app = PhoneGlobals.getInstance();
1725
1726 // Sanity-check that this is OK given the current state of the phone.
1727 if (!okToAddCall(cm)) {
1728 Log.w(LOG_TAG, "startNewCall: can't add a new call in the current state");
1729 dumpCallManager();
1730 return false;
1731 }
1732
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001733 Intent intent = new Intent(Intent.ACTION_DIAL);
1734 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1735
1736 // when we request the dialer come up, we also want to inform
1737 // it that we're going through the "add call" option from the
1738 // InCallScreen / PhoneUtils.
1739 intent.putExtra(ADD_CALL_MODE_KEY, true);
1740 try {
1741 app.startActivity(intent);
1742 } catch (ActivityNotFoundException e) {
1743 // This is rather rare but possible.
1744 // Note: this method is used even when the phone is encrypted. At that moment
1745 // the system may not find any Activity which can accept this Intent.
1746 Log.e(LOG_TAG, "Activity for adding calls isn't found.");
1747 return false;
1748 }
1749
1750 return true;
1751 }
1752
1753 /**
1754 * Turns on/off speaker.
1755 *
1756 * @param context Context
1757 * @param flag True when speaker should be on. False otherwise.
1758 * @param store True when the settings should be stored in the device.
1759 */
1760 /* package */ static void turnOnSpeaker(Context context, boolean flag, boolean store) {
1761 if (DBG) log("turnOnSpeaker(flag=" + flag + ", store=" + store + ")...");
1762 final PhoneGlobals app = PhoneGlobals.getInstance();
1763
1764 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
1765 audioManager.setSpeakerphoneOn(flag);
1766
1767 // record the speaker-enable value
1768 if (store) {
1769 sIsSpeakerEnabled = flag;
1770 }
1771
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001772 // We also need to make a fresh call to PhoneApp.updateWakeState()
1773 // any time the speaker state changes, since the screen timeout is
1774 // sometimes different depending on whether or not the speaker is
1775 // in use.
1776 app.updateWakeState();
1777
Hariprasad Jayakumareee6a9e2014-04-25 15:33:13 -07001778 app.mCM.setEchoSuppressionEnabled();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001779 }
1780
1781 /**
1782 * Restore the speaker mode, called after a wired headset disconnect
1783 * event.
1784 */
1785 static void restoreSpeakerMode(Context context) {
1786 if (DBG) log("restoreSpeakerMode, restoring to: " + sIsSpeakerEnabled);
1787
1788 // change the mode if needed.
1789 if (isSpeakerOn(context) != sIsSpeakerEnabled) {
1790 turnOnSpeaker(context, sIsSpeakerEnabled, false);
1791 }
1792 }
1793
1794 static boolean isSpeakerOn(Context context) {
1795 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
1796 return audioManager.isSpeakerphoneOn();
1797 }
1798
1799
1800 static void turnOnNoiseSuppression(Context context, boolean flag, boolean store) {
1801 if (DBG) log("turnOnNoiseSuppression: " + flag);
1802 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
1803
Jonathan Basseri3b9c21f2015-06-19 15:51:10 -07001804 PersistableBundle b = PhoneGlobals.getInstance().getCarrierConfig();
1805 if (!b.getBoolean(CarrierConfigManager.KEY_HAS_IN_CALL_NOISE_SUPPRESSION_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 return;
1807 }
1808
1809 if (flag) {
1810 audioManager.setParameters("noise_suppression=auto");
1811 } else {
1812 audioManager.setParameters("noise_suppression=off");
1813 }
1814
1815 // record the speaker-enable value
1816 if (store) {
1817 sIsNoiseSuppressionEnabled = flag;
1818 }
1819
1820 // TODO: implement and manage ICON
1821
1822 }
1823
1824 static void restoreNoiseSuppression(Context context) {
1825 if (DBG) log("restoreNoiseSuppression, restoring to: " + sIsNoiseSuppressionEnabled);
1826
Jonathan Basseri3b9c21f2015-06-19 15:51:10 -07001827 PersistableBundle b = PhoneGlobals.getInstance().getCarrierConfig();
1828 if (!b.getBoolean(CarrierConfigManager.KEY_HAS_IN_CALL_NOISE_SUPPRESSION_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001829 return;
1830 }
1831
1832 // change the mode if needed.
1833 if (isNoiseSuppressionOn(context) != sIsNoiseSuppressionEnabled) {
1834 turnOnNoiseSuppression(context, sIsNoiseSuppressionEnabled, false);
1835 }
1836 }
1837
1838 static boolean isNoiseSuppressionOn(Context context) {
1839
Jonathan Basseri3b9c21f2015-06-19 15:51:10 -07001840 PersistableBundle b = PhoneGlobals.getInstance().getCarrierConfig();
1841 if (!b.getBoolean(CarrierConfigManager.KEY_HAS_IN_CALL_NOISE_SUPPRESSION_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001842 return false;
1843 }
1844
1845 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
1846 String noiseSuppression = audioManager.getParameters("noise_suppression");
1847 if (DBG) log("isNoiseSuppressionOn: " + noiseSuppression);
1848 if (noiseSuppression.contains("off")) {
1849 return false;
1850 } else {
1851 return true;
1852 }
1853 }
1854
Santos Cordonc264cba2013-09-19 04:55:40 -07001855 static boolean isInEmergencyCall(CallManager cm) {
1856 for (Connection cn : cm.getActiveFgCall().getConnections()) {
Yorke Lee36bb2542014-06-05 08:09:52 -07001857 if (PhoneNumberUtils.isLocalEmergencyNumber(PhoneGlobals.getInstance(),
1858 cn.getAddress())) {
Santos Cordonc264cba2013-09-19 04:55:40 -07001859 return true;
1860 }
1861 }
1862 return false;
1863 }
1864
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001865 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001866 * Get the mute state of foreground phone, which has the current
1867 * foreground call
1868 */
1869 static boolean getMute() {
Santos Cordonbb2bcef2014-08-05 19:08:10 -07001870 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001871 }
1872
1873 /* package */ static void setAudioMode() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 }
1875
1876 /**
1877 * Sets the audio mode per current phone state.
1878 */
1879 /* package */ static void setAudioMode(CallManager cm) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 }
1881
1882 /**
1883 * Look for ANY connections on the phone that qualify as being
1884 * disconnected.
1885 *
1886 * @return true if we find a connection that is disconnected over
1887 * all the phone's call objects.
1888 */
1889 /* package */ static boolean hasDisconnectedConnections(Phone phone) {
1890 return hasDisconnectedConnections(phone.getForegroundCall()) ||
1891 hasDisconnectedConnections(phone.getBackgroundCall()) ||
1892 hasDisconnectedConnections(phone.getRingingCall());
1893 }
1894
1895 /**
1896 * Iterate over all connections in a call to see if there are any
1897 * that are not alive (disconnected or idle).
1898 *
1899 * @return true if we find a connection that is disconnected, and
1900 * pending removal via
Amit Mahajan6d83e5e2015-12-14 13:24:48 -08001901 * {@link com.android.internal.telephony.Call#clearDisconnected()}.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001902 */
1903 private static final boolean hasDisconnectedConnections(Call call) {
1904 // look through all connections for non-active ones.
1905 for (Connection c : call.getConnections()) {
1906 if (!c.isAlive()) {
1907 return true;
1908 }
1909 }
1910 return false;
1911 }
1912
1913 //
1914 // Misc UI policy helper functions
1915 //
1916
1917 /**
Christine Chenaf2fd0a2013-09-13 16:27:40 -07001918 * @return true if we're allowed to hold calls, given the current
1919 * state of the Phone.
1920 */
1921 /* package */ static boolean okToHoldCall(CallManager cm) {
1922 final Call fgCall = cm.getActiveFgCall();
1923 final boolean hasHoldingCall = cm.hasActiveBgCall();
1924 final Call.State fgCallState = fgCall.getState();
1925
1926 // The "Hold" control is disabled entirely if there's
1927 // no way to either hold or unhold in the current state.
1928 final boolean okToHold = (fgCallState == Call.State.ACTIVE) && !hasHoldingCall;
1929 final boolean okToUnhold = cm.hasActiveBgCall() && (fgCallState == Call.State.IDLE);
1930 final boolean canHold = okToHold || okToUnhold;
1931
1932 return canHold;
1933 }
1934
1935 /**
1936 * @return true if we support holding calls, given the current
1937 * state of the Phone.
1938 */
1939 /* package */ static boolean okToSupportHold(CallManager cm) {
1940 boolean supportsHold = false;
1941
1942 final Call fgCall = cm.getActiveFgCall();
1943 final boolean hasHoldingCall = cm.hasActiveBgCall();
1944 final Call.State fgCallState = fgCall.getState();
1945
1946 if (TelephonyCapabilities.supportsHoldAndUnhold(fgCall.getPhone())) {
1947 // This phone has the concept of explicit "Hold" and "Unhold" actions.
1948 supportsHold = true;
1949 } else if (hasHoldingCall && (fgCallState == Call.State.IDLE)) {
1950 // Even when foreground phone device doesn't support hold/unhold, phone devices
1951 // for background holding calls may do.
1952 final Call bgCall = cm.getFirstActiveBgCall();
1953 if (bgCall != null &&
1954 TelephonyCapabilities.supportsHoldAndUnhold(bgCall.getPhone())) {
1955 supportsHold = true;
1956 }
1957 }
1958 return supportsHold;
1959 }
1960
1961 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001962 * @return true if we're allowed to swap calls, given the current
1963 * state of the Phone.
1964 */
1965 /* package */ static boolean okToSwapCalls(CallManager cm) {
1966 int phoneType = cm.getDefaultPhone().getPhoneType();
1967 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1968 // CDMA: "Swap" is enabled only when the phone reaches a *generic*.
1969 // state by either accepting a Call Waiting or by merging two calls
1970 PhoneGlobals app = PhoneGlobals.getInstance();
1971 return (app.cdmaPhoneCallState.getCurrentCallState()
1972 == CdmaPhoneCallState.PhoneCallState.CONF_CALL);
1973 } else if ((phoneType == PhoneConstants.PHONE_TYPE_GSM)
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001974 || (phoneType == PhoneConstants.PHONE_TYPE_SIP)
Etan Cohen0ca1c802014-07-07 15:35:48 -07001975 || (phoneType == PhoneConstants.PHONE_TYPE_IMS)
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001976 || (phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001977 // GSM: "Swap" is available if both lines are in use and there's no
1978 // incoming call. (Actually we need to verify that the active
1979 // call really is in the ACTIVE state and the holding call really
1980 // is in the HOLDING state, since you *can't* actually swap calls
1981 // when the foreground call is DIALING or ALERTING.)
1982 return !cm.hasActiveRingingCall()
1983 && (cm.getActiveFgCall().getState() == Call.State.ACTIVE)
1984 && (cm.getFirstActiveBgCall().getState() == Call.State.HOLDING);
1985 } else {
1986 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1987 }
1988 }
1989
1990 /**
1991 * @return true if we're allowed to merge calls, given the current
1992 * state of the Phone.
1993 */
1994 /* package */ static boolean okToMergeCalls(CallManager cm) {
1995 int phoneType = cm.getFgPhone().getPhoneType();
1996 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1997 // CDMA: "Merge" is enabled only when the user is in a 3Way call.
1998 PhoneGlobals app = PhoneGlobals.getInstance();
1999 return ((app.cdmaPhoneCallState.getCurrentCallState()
2000 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE)
2001 && !app.cdmaPhoneCallState.IsThreeWayCallOrigStateDialing());
2002 } else {
2003 // GSM: "Merge" is available if both lines are in use and there's no
2004 // incoming call, *and* the current conference isn't already
2005 // "full".
2006 // TODO: shall move all okToMerge logic to CallManager
2007 return !cm.hasActiveRingingCall() && cm.hasActiveFgCall()
2008 && cm.hasActiveBgCall()
2009 && cm.canConference(cm.getFirstActiveBgCall());
2010 }
2011 }
2012
2013 /**
2014 * @return true if the UI should let you add a new call, given the current
2015 * state of the Phone.
2016 */
2017 /* package */ static boolean okToAddCall(CallManager cm) {
2018 Phone phone = cm.getActiveFgCall().getPhone();
2019
2020 // "Add call" is never allowed in emergency callback mode (ECM).
2021 if (isPhoneInEcm(phone)) {
2022 return false;
2023 }
2024
2025 int phoneType = phone.getPhoneType();
2026 final Call.State fgCallState = cm.getActiveFgCall().getState();
2027 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
2028 // CDMA: "Add call" button is only enabled when:
2029 // - ForegroundCall is in ACTIVE state
2030 // - After 30 seconds of user Ignoring/Missing a Call Waiting call.
2031 PhoneGlobals app = PhoneGlobals.getInstance();
2032 return ((fgCallState == Call.State.ACTIVE)
2033 && (app.cdmaPhoneCallState.getAddCallMenuStateAfterCallWaiting()));
2034 } else if ((phoneType == PhoneConstants.PHONE_TYPE_GSM)
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002035 || (phoneType == PhoneConstants.PHONE_TYPE_SIP)
Etan Cohen0ca1c802014-07-07 15:35:48 -07002036 || (phoneType == PhoneConstants.PHONE_TYPE_IMS)
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002037 || (phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002038 // GSM: "Add call" is available only if ALL of the following are true:
2039 // - There's no incoming ringing call
2040 // - There's < 2 lines in use
2041 // - The foreground call is ACTIVE or IDLE or DISCONNECTED.
2042 // (We mainly need to make sure it *isn't* DIALING or ALERTING.)
2043 final boolean hasRingingCall = cm.hasActiveRingingCall();
2044 final boolean hasActiveCall = cm.hasActiveFgCall();
2045 final boolean hasHoldingCall = cm.hasActiveBgCall();
2046 final boolean allLinesTaken = hasActiveCall && hasHoldingCall;
2047
2048 return !hasRingingCall
2049 && !allLinesTaken
2050 && ((fgCallState == Call.State.ACTIVE)
2051 || (fgCallState == Call.State.IDLE)
2052 || (fgCallState == Call.State.DISCONNECTED));
2053 } else {
2054 throw new IllegalStateException("Unexpected phone type: " + phoneType);
2055 }
2056 }
2057
2058 /**
2059 * Based on the input CNAP number string,
2060 * @return _RESTRICTED or _UNKNOWN for all the special CNAP strings.
2061 * Otherwise, return CNAP_SPECIAL_CASE_NO.
2062 */
2063 private static int checkCnapSpecialCases(String n) {
2064 if (n.equals("PRIVATE") ||
2065 n.equals("P") ||
2066 n.equals("RES")) {
2067 if (DBG) log("checkCnapSpecialCases, PRIVATE string: " + n);
2068 return PhoneConstants.PRESENTATION_RESTRICTED;
2069 } else if (n.equals("UNAVAILABLE") ||
2070 n.equals("UNKNOWN") ||
2071 n.equals("UNA") ||
2072 n.equals("U")) {
2073 if (DBG) log("checkCnapSpecialCases, UNKNOWN string: " + n);
2074 return PhoneConstants.PRESENTATION_UNKNOWN;
2075 } else {
2076 if (DBG) log("checkCnapSpecialCases, normal str. number: " + n);
2077 return CNAP_SPECIAL_CASE_NO;
2078 }
2079 }
2080
2081 /**
2082 * Handles certain "corner cases" for CNAP. When we receive weird phone numbers
2083 * from the network to indicate different number presentations, convert them to
2084 * expected number and presentation values within the CallerInfo object.
2085 * @param number number we use to verify if we are in a corner case
2086 * @param presentation presentation value used to verify if we are in a corner case
2087 * @return the new String that should be used for the phone number
2088 */
2089 /* package */ static String modifyForSpecialCnapCases(Context context, CallerInfo ci,
2090 String number, int presentation) {
2091 // Obviously we return number if ci == null, but still return number if
2092 // number == null, because in these cases the correct string will still be
2093 // displayed/logged after this function returns based on the presentation value.
2094 if (ci == null || number == null) return number;
2095
2096 if (DBG) {
2097 log("modifyForSpecialCnapCases: initially, number="
2098 + toLogSafePhoneNumber(number)
2099 + ", presentation=" + presentation + " ci " + ci);
2100 }
2101
2102 // "ABSENT NUMBER" is a possible value we could get from the network as the
2103 // phone number, so if this happens, change it to "Unknown" in the CallerInfo
2104 // and fix the presentation to be the same.
2105 final String[] absentNumberValues =
2106 context.getResources().getStringArray(R.array.absent_num);
2107 if (Arrays.asList(absentNumberValues).contains(number)
2108 && presentation == PhoneConstants.PRESENTATION_ALLOWED) {
2109 number = context.getString(R.string.unknown);
2110 ci.numberPresentation = PhoneConstants.PRESENTATION_UNKNOWN;
2111 }
2112
2113 // Check for other special "corner cases" for CNAP and fix them similarly. Corner
2114 // cases only apply if we received an allowed presentation from the network, so check
2115 // if we think we have an allowed presentation, or if the CallerInfo presentation doesn't
2116 // match the presentation passed in for verification (meaning we changed it previously
2117 // because it's a corner case and we're being called from a different entry point).
2118 if (ci.numberPresentation == PhoneConstants.PRESENTATION_ALLOWED
2119 || (ci.numberPresentation != presentation
2120 && presentation == PhoneConstants.PRESENTATION_ALLOWED)) {
2121 int cnapSpecialCase = checkCnapSpecialCases(number);
2122 if (cnapSpecialCase != CNAP_SPECIAL_CASE_NO) {
2123 // For all special strings, change number & numberPresentation.
2124 if (cnapSpecialCase == PhoneConstants.PRESENTATION_RESTRICTED) {
2125 number = context.getString(R.string.private_num);
2126 } else if (cnapSpecialCase == PhoneConstants.PRESENTATION_UNKNOWN) {
2127 number = context.getString(R.string.unknown);
2128 }
2129 if (DBG) {
2130 log("SpecialCnap: number=" + toLogSafePhoneNumber(number)
2131 + "; presentation now=" + cnapSpecialCase);
2132 }
2133 ci.numberPresentation = cnapSpecialCase;
2134 }
2135 }
2136 if (DBG) {
2137 log("modifyForSpecialCnapCases: returning number string="
2138 + toLogSafePhoneNumber(number));
2139 }
2140 return number;
2141 }
2142
2143 //
2144 // Support for 3rd party phone service providers.
2145 //
2146
2147 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002148 * Check if a phone number can be route through a 3rd party
2149 * gateway. The number must be a global phone number in numerical
2150 * form (1-800-666-SEXY won't work).
2151 *
2152 * MMI codes and the like cannot be used as a dial number for the
2153 * gateway either.
2154 *
2155 * @param number To be dialed via a 3rd party gateway.
2156 * @return true If the number can be routed through the 3rd party network.
2157 */
Santos Cordon69a69192013-08-22 14:25:42 -07002158 private static boolean isRoutableViaGateway(String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002159 if (TextUtils.isEmpty(number)) {
2160 return false;
2161 }
2162 number = PhoneNumberUtils.stripSeparators(number);
2163 if (!number.equals(PhoneNumberUtils.convertKeypadLettersToDigits(number))) {
2164 return false;
2165 }
2166 number = PhoneNumberUtils.extractNetworkPortion(number);
2167 return PhoneNumberUtils.isGlobalPhoneNumber(number);
2168 }
2169
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002170 /**
2171 * Returns whether the phone is in ECM ("Emergency Callback Mode") or not.
2172 */
2173 /* package */ static boolean isPhoneInEcm(Phone phone) {
2174 if ((phone != null) && TelephonyCapabilities.supportsEcm(phone)) {
2175 // For phones that support ECM, return true iff PROPERTY_INECM_MODE == "true".
2176 // TODO: There ought to be a better API for this than just
2177 // exposing a system property all the way up to the app layer,
2178 // probably a method like "inEcm()" provided by the telephony
2179 // layer.
2180 String ecmMode =
2181 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE);
2182 if (ecmMode != null) {
2183 return ecmMode.equals("true");
2184 }
2185 }
2186 return false;
2187 }
2188
2189 /**
2190 * Returns the most appropriate Phone object to handle a call
2191 * to the specified number.
2192 *
2193 * @param cm the CallManager.
2194 * @param scheme the scheme from the data URI that the number originally came from.
2195 * @param number the phone number, or SIP address.
2196 */
Sailesh Nepalbfb68322013-11-07 14:07:41 -08002197 public static Phone pickPhoneBasedOnNumber(CallManager cm, String scheme, String number,
2198 String primarySipUri, ComponentName thirdPartyCallComponent) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002199 if (DBG) {
2200 log("pickPhoneBasedOnNumber: scheme " + scheme
2201 + ", number " + toLogSafePhoneNumber(number)
2202 + ", sipUri "
Sailesh Nepalbfb68322013-11-07 14:07:41 -08002203 + (primarySipUri != null ? Uri.parse(primarySipUri).toSafeString() : "null")
2204 + ", thirdPartyCallComponent: " + thirdPartyCallComponent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002205 }
2206
2207 if (primarySipUri != null) {
2208 Phone phone = getSipPhoneFromUri(cm, primarySipUri);
2209 if (phone != null) return phone;
2210 }
Sailesh Nepalbfb68322013-11-07 14:07:41 -08002211
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002212 return cm.getDefaultPhone();
2213 }
2214
2215 public static Phone getSipPhoneFromUri(CallManager cm, String target) {
2216 for (Phone phone : cm.getAllPhones()) {
2217 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_SIP) {
2218 String sipUri = ((SipPhone) phone).getSipUri();
2219 if (target.equals(sipUri)) {
2220 if (DBG) log("- pickPhoneBasedOnNumber:" +
2221 "found SipPhone! obj = " + phone + ", "
2222 + phone.getClass());
2223 return phone;
2224 }
2225 }
2226 }
2227 return null;
2228 }
2229
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002230 /**
2231 * Returns true when the given call is in INCOMING state and there's no foreground phone call,
2232 * meaning the call is the first real incoming call the phone is having.
2233 */
2234 public static boolean isRealIncomingCall(Call.State state) {
2235 return (state == Call.State.INCOMING && !PhoneGlobals.getInstance().mCM.hasActiveFgCall());
2236 }
2237
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002238 public static String getPresentationString(Context context, int presentation) {
2239 String name = context.getString(R.string.unknown);
2240 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) {
2241 name = context.getString(R.string.private_num);
2242 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) {
2243 name = context.getString(R.string.payphone);
2244 }
2245 return name;
2246 }
2247
2248 public static void sendViewNotificationAsync(Context context, Uri contactUri) {
2249 if (DBG) Log.d(LOG_TAG, "Send view notification to Contacts (uri: " + contactUri + ")");
2250 Intent intent = new Intent("com.android.contacts.VIEW_NOTIFICATION", contactUri);
2251 intent.setClassName("com.android.contacts",
2252 "com.android.contacts.ViewNotificationService");
2253 context.startService(intent);
2254 }
2255
2256 //
2257 // General phone and call state debugging/testing code
2258 //
2259
2260 /* package */ static void dumpCallState(Phone phone) {
2261 PhoneGlobals app = PhoneGlobals.getInstance();
2262 Log.d(LOG_TAG, "dumpCallState():");
2263 Log.d(LOG_TAG, "- Phone: " + phone + ", name = " + phone.getPhoneName()
2264 + ", state = " + phone.getState());
2265
2266 StringBuilder b = new StringBuilder(128);
2267
2268 Call call = phone.getForegroundCall();
2269 b.setLength(0);
2270 b.append(" - FG call: ").append(call.getState());
2271 b.append(" isAlive ").append(call.getState().isAlive());
2272 b.append(" isRinging ").append(call.getState().isRinging());
2273 b.append(" isDialing ").append(call.getState().isDialing());
2274 b.append(" isIdle ").append(call.isIdle());
2275 b.append(" hasConnections ").append(call.hasConnections());
2276 Log.d(LOG_TAG, b.toString());
2277
2278 call = phone.getBackgroundCall();
2279 b.setLength(0);
2280 b.append(" - BG call: ").append(call.getState());
2281 b.append(" isAlive ").append(call.getState().isAlive());
2282 b.append(" isRinging ").append(call.getState().isRinging());
2283 b.append(" isDialing ").append(call.getState().isDialing());
2284 b.append(" isIdle ").append(call.isIdle());
2285 b.append(" hasConnections ").append(call.hasConnections());
2286 Log.d(LOG_TAG, b.toString());
2287
2288 call = phone.getRingingCall();
2289 b.setLength(0);
2290 b.append(" - RINGING call: ").append(call.getState());
2291 b.append(" isAlive ").append(call.getState().isAlive());
2292 b.append(" isRinging ").append(call.getState().isRinging());
2293 b.append(" isDialing ").append(call.getState().isDialing());
2294 b.append(" isIdle ").append(call.isIdle());
2295 b.append(" hasConnections ").append(call.hasConnections());
2296 Log.d(LOG_TAG, b.toString());
2297
2298
2299 final boolean hasRingingCall = !phone.getRingingCall().isIdle();
2300 final boolean hasActiveCall = !phone.getForegroundCall().isIdle();
2301 final boolean hasHoldingCall = !phone.getBackgroundCall().isIdle();
2302 final boolean allLinesTaken = hasActiveCall && hasHoldingCall;
2303 b.setLength(0);
2304 b.append(" - hasRingingCall ").append(hasRingingCall);
2305 b.append(" hasActiveCall ").append(hasActiveCall);
2306 b.append(" hasHoldingCall ").append(hasHoldingCall);
2307 b.append(" allLinesTaken ").append(allLinesTaken);
2308 Log.d(LOG_TAG, b.toString());
2309
2310 // On CDMA phones, dump out the CdmaPhoneCallState too:
2311 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2312 if (app.cdmaPhoneCallState != null) {
2313 Log.d(LOG_TAG, " - CDMA call state: "
2314 + app.cdmaPhoneCallState.getCurrentCallState());
2315 } else {
2316 Log.d(LOG_TAG, " - CDMA device, but null cdmaPhoneCallState!");
2317 }
2318 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002319 }
2320
2321 private static void log(String msg) {
2322 Log.d(LOG_TAG, msg);
2323 }
2324
2325 static void dumpCallManager() {
2326 Call call;
2327 CallManager cm = PhoneGlobals.getInstance().mCM;
2328 StringBuilder b = new StringBuilder(128);
2329
2330
2331
2332 Log.d(LOG_TAG, "############### dumpCallManager() ##############");
2333 // TODO: Don't log "cm" itself, since CallManager.toString()
2334 // already spews out almost all this same information.
2335 // We should fix CallManager.toString() to be more minimal, and
2336 // use an explicit dumpState() method for the verbose dump.
2337 // Log.d(LOG_TAG, "CallManager: " + cm
2338 // + ", state = " + cm.getState());
2339 Log.d(LOG_TAG, "CallManager: state = " + cm.getState());
2340 b.setLength(0);
2341 call = cm.getActiveFgCall();
2342 b.append(" - FG call: ").append(cm.hasActiveFgCall()? "YES ": "NO ");
2343 b.append(call);
2344 b.append( " State: ").append(cm.getActiveFgCallState());
2345 b.append( " Conn: ").append(cm.getFgCallConnections());
2346 Log.d(LOG_TAG, b.toString());
2347 b.setLength(0);
2348 call = cm.getFirstActiveBgCall();
2349 b.append(" - BG call: ").append(cm.hasActiveBgCall()? "YES ": "NO ");
2350 b.append(call);
2351 b.append( " State: ").append(cm.getFirstActiveBgCall().getState());
2352 b.append( " Conn: ").append(cm.getBgCallConnections());
2353 Log.d(LOG_TAG, b.toString());
2354 b.setLength(0);
2355 call = cm.getFirstActiveRingingCall();
2356 b.append(" - RINGING call: ").append(cm.hasActiveRingingCall()? "YES ": "NO ");
2357 b.append(call);
2358 b.append( " State: ").append(cm.getFirstActiveRingingCall().getState());
2359 Log.d(LOG_TAG, b.toString());
2360
2361
2362
2363 for (Phone phone : CallManager.getInstance().getAllPhones()) {
2364 if (phone != null) {
2365 Log.d(LOG_TAG, "Phone: " + phone + ", name = " + phone.getPhoneName()
2366 + ", state = " + phone.getState());
2367 b.setLength(0);
2368 call = phone.getForegroundCall();
2369 b.append(" - FG call: ").append(call);
2370 b.append( " State: ").append(call.getState());
2371 b.append( " Conn: ").append(call.hasConnections());
2372 Log.d(LOG_TAG, b.toString());
2373 b.setLength(0);
2374 call = phone.getBackgroundCall();
2375 b.append(" - BG call: ").append(call);
2376 b.append( " State: ").append(call.getState());
2377 b.append( " Conn: ").append(call.hasConnections());
2378 Log.d(LOG_TAG, b.toString());b.setLength(0);
2379 call = phone.getRingingCall();
2380 b.append(" - RINGING call: ").append(call);
2381 b.append( " State: ").append(call.getState());
2382 b.append( " Conn: ").append(call.hasConnections());
2383 Log.d(LOG_TAG, b.toString());
2384 }
2385 }
2386
2387 Log.d(LOG_TAG, "############## END dumpCallManager() ###############");
2388 }
2389
2390 /**
2391 * @return if the context is in landscape orientation.
2392 */
2393 public static boolean isLandscape(Context context) {
2394 return context.getResources().getConfiguration().orientation
2395 == Configuration.ORIENTATION_LANDSCAPE;
2396 }
Andrew Leed5165b02014-12-05 15:53:58 -08002397
Nancy Chene53a0642015-03-12 11:20:12 -07002398 public static PhoneAccountHandle makePstnPhoneAccountHandle(String id) {
2399 return makePstnPhoneAccountHandleWithPrefix(id, "", false);
2400 }
2401
Nancy Chen32031f42015-03-04 21:00:07 -08002402 public static PhoneAccountHandle makePstnPhoneAccountHandle(int phoneId) {
2403 return makePstnPhoneAccountHandle(PhoneFactory.getPhone(phoneId));
2404 }
2405
Andrew Leed5165b02014-12-05 15:53:58 -08002406 public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
2407 return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
2408 }
2409
2410 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
2411 Phone phone, String prefix, boolean isEmergency) {
Andrew Leed5165b02014-12-05 15:53:58 -08002412 // TODO: Should use some sort of special hidden flag to decorate this account as
2413 // an emergency-only account
Brad Ebingerd3edc222016-03-23 19:44:38 -07002414 String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix +
2415 String.valueOf(phone.getIccSerialNumber());
Nancy Chene53a0642015-03-12 11:20:12 -07002416 return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency);
2417 }
2418
2419 public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
2420 String id, String prefix, boolean isEmergency) {
2421 ComponentName pstnConnectionServiceName = getPstnConnectionServiceName();
Andrew Leed5165b02014-12-05 15:53:58 -08002422 return new PhoneAccountHandle(pstnConnectionServiceName, id);
2423 }
Stuart Scottdcf40a92014-12-09 10:45:01 -08002424
Santos Cordonf69df962015-02-03 11:15:19 -08002425 public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Santos Cordon21e78ac2015-02-06 05:19:29 -08002426 if (phoneAccount != null
2427 && phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
2428 return getSubIdForPhoneAccountHandle(phoneAccount.getAccountHandle());
2429 }
2430 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
2431 }
2432
2433 public static int getSubIdForPhoneAccountHandle(PhoneAccountHandle handle) {
Nancy Chen31f9ba12016-01-06 11:42:12 -08002434 Phone phone = getPhoneForPhoneAccountHandle(handle);
2435 if (phone != null) {
2436 return phone.getSubId();
Santos Cordonf69df962015-02-03 11:15:19 -08002437 }
2438 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
2439 }
2440
Nancy Chen31f9ba12016-01-06 11:42:12 -08002441 static Phone getPhoneForPhoneAccountHandle(PhoneAccountHandle handle) {
2442 if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) {
2443 return getPhoneFromIccId(handle.getId());
2444 }
2445 return null;
2446 }
2447
2448
Nancy Chene53a0642015-03-12 11:20:12 -07002449 /**
2450 * Determine if a given phone account corresponds to an active SIM
2451 *
2452 * @param sm An instance of the subscription manager so it is not recreated for each calling of
2453 * this method.
2454 * @param handle The handle for the phone account to check
2455 * @return {@code true} If there is an active SIM for this phone account,
2456 * {@code false} otherwise.
2457 */
2458 public static boolean isPhoneAccountActive(SubscriptionManager sm, PhoneAccountHandle handle) {
2459 return sm.getActiveSubscriptionInfoForIccIndex(handle.getId()) != null;
2460 }
2461
Santos Cordonf69df962015-02-03 11:15:19 -08002462 private static ComponentName getPstnConnectionServiceName() {
Tony Mak4c8f3c62015-12-07 02:30:37 +00002463 return PSTN_CONNECTION_SERVICE_COMPONENT;
Santos Cordonf69df962015-02-03 11:15:19 -08002464 }
2465
Santos Cordon80855d52015-02-10 04:33:36 -08002466 private static Phone getPhoneFromIccId(String iccId) {
2467 if (!TextUtils.isEmpty(iccId)) {
2468 for (Phone phone : PhoneFactory.getPhones()) {
2469 String phoneIccId = phone.getIccSerialNumber();
2470 if (iccId.equals(phoneIccId)) {
2471 return phone;
2472 }
2473 }
2474 }
Santos Cordon80855d52015-02-10 04:33:36 -08002475 return null;
2476 }
2477
Stuart Scottdcf40a92014-12-09 10:45:01 -08002478 /**
2479 * Register ICC status for all phones.
2480 */
2481 static final void registerIccStatus(Handler handler, int event) {
2482 for (Phone phone : PhoneFactory.getPhones()) {
2483 IccCard sim = phone.getIccCard();
2484 if (sim != null) {
2485 if (VDBG) Log.v(LOG_TAG, "register for ICC status, phone " + phone.getPhoneId());
2486 sim.registerForNetworkLocked(handler, event, phone);
2487 }
2488 }
2489 }
2490
2491 /**
2492 * Set the radio power on/off state for all phones.
2493 *
2494 * @param enabled true means on, false means off.
2495 */
2496 static final void setRadioPower(boolean enabled) {
2497 for (Phone phone : PhoneFactory.getPhones()) {
2498 phone.setRadioPower(enabled);
2499 }
2500 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002501}