blob: 2b7d2ff7d252fce88bc6ac467e72ede4b487bdbd [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 com.android.internal.telephony.Call;
20import com.android.internal.telephony.CallManager;
21import com.android.internal.telephony.CallerInfo;
22import com.android.internal.telephony.CallerInfoAsyncQuery;
23import com.android.internal.telephony.Connection;
24import com.android.internal.telephony.Phone;
25import com.android.internal.telephony.PhoneConstants;
26import com.android.internal.telephony.PhoneBase;
27import com.android.internal.telephony.TelephonyCapabilities;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaDisplayInfoRec;
29import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaSignalInfoRec;
30import com.android.internal.telephony.cdma.SignalToneUtil;
31
32import android.app.ActivityManagerNative;
33import android.bluetooth.BluetoothAdapter;
34import android.bluetooth.BluetoothHeadset;
35import android.bluetooth.BluetoothProfile;
36import android.content.Context;
John Spurlock6ee06d02014-07-18 20:06:20 -040037import android.media.AudioAttributes;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.media.AudioManager;
39import android.media.ToneGenerator;
40import android.net.Uri;
41import android.os.AsyncResult;
42import android.os.Handler;
43import android.os.Message;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.SystemProperties;
45import android.os.SystemVibrator;
46import android.os.Vibrator;
47import android.provider.CallLog.Calls;
48import android.provider.Settings;
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -080049import android.telecom.TelecomManager;
Anders Kristensen0b35f042014-02-27 14:31:07 -080050import android.telephony.DisconnectCause;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.telephony.PhoneNumberUtils;
52import android.telephony.PhoneStateListener;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080053import android.telephony.SubscriptionInfo;
54import android.telephony.SubscriptionManager;
55import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.telephony.TelephonyManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080057import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.util.EventLog;
59import android.util.Log;
60
Andrew Lee2fcb6c32014-12-04 14:52:35 -080061import java.util.Iterator;
62import java.util.List;
63import java.util.Map;
64
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065/**
66 * Phone app module that listens for phone state changes and various other
67 * events from the telephony layer, and triggers any resulting UI behavior
Santos Cordon5422a8d2014-09-12 04:20:56 -070068 * (like starting the Incoming Call UI, playing in-call tones,
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069 * updating notifications, writing call log entries, etc.)
70 */
Santos Cordon5422a8d2014-09-12 04:20:56 -070071public class CallNotifier extends Handler {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072 private static final String LOG_TAG = "CallNotifier";
73 private static final boolean DBG =
74 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
75 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
76
Anthony Leee9468532014-11-15 15:21:00 -080077 // Time to display the message from the underlying phone layers.
78 private static final int SHOW_MESSAGE_NOTIFICATION_TIME = 3000; // msec
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079
John Spurlock6ee06d02014-07-18 20:06:20 -040080 private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
81 .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
82 .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
83 .build();
84
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 /** The singleton instance. */
86 private static CallNotifier sInstance;
87
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 // values used to track the query state
89 private static final int CALLERINFO_QUERY_READY = 0;
90 private static final int CALLERINFO_QUERYING = -1;
91
92 // the state of the CallerInfo Query.
93 private int mCallerInfoQueryState;
94
95 // object used to synchronize access to mCallerInfoQueryState
96 private Object mCallerInfoQueryStateGuard = new Object();
Andrew Lee2fcb6c32014-12-04 14:52:35 -080097 private Map<Integer, CallNotifierPhoneStateListener> mPhoneStateListeners =
98 new ArrayMap<Integer, CallNotifierPhoneStateListener>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100 private PhoneGlobals mApplication;
101 private CallManager mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private BluetoothHeadset mBluetoothHeadset;
103 private CallLogger mCallLogger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104
105 // ToneGenerator instance for playing SignalInfo tones
106 private ToneGenerator mSignalInfoToneGenerator;
107
108 // The tone volume relative to other sounds in the stream SignalInfo
109 private static final int TONE_RELATIVE_VOLUME_SIGNALINFO = 80;
110
111 private Call.State mPreviousCdmaCallState;
112 private boolean mVoicePrivacyState = false;
113 private boolean mIsCdmaRedialCall = false;
114
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 // Cached AudioManager
116 private AudioManager mAudioManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700117 private final BluetoothManager mBluetoothManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800118 private SubscriptionManager mSubscriptionManager;
119 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700120
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 /**
122 * Initialize the singleton CallNotifier instance.
123 * This is only done once, at startup, from PhoneApp.onCreate().
124 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800125 /* package */ static CallNotifier init(
126 PhoneGlobals app,
127 CallLogger callLogger,
128 CallStateMonitor callStateMonitor,
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700129 BluetoothManager bluetoothManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130 synchronized (CallNotifier.class) {
131 if (sInstance == null) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800132 sInstance = new CallNotifier(app, callLogger, callStateMonitor, bluetoothManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133 } else {
134 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
135 }
136 return sInstance;
137 }
138 }
139
140 /** Private constructor; @see init() */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800141 private CallNotifier(
142 PhoneGlobals app,
143 CallLogger callLogger,
144 CallStateMonitor callStateMonitor,
145 BluetoothManager bluetoothManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 mApplication = app;
147 mCM = app.mCM;
148 mCallLogger = callLogger;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700149 mBluetoothManager = bluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150
151 mAudioManager = (AudioManager) mApplication.getSystemService(Context.AUDIO_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800152 mTelephonyManager =
153 (TelephonyManager) mApplication.getSystemService(Context.TELEPHONY_SERVICE);
154 mSubscriptionManager = (SubscriptionManager) mApplication.getSystemService(
155 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156
157 callStateMonitor.addListener(this);
158
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
160 if (adapter != null) {
161 adapter.getProfileProxy(mApplication.getApplicationContext(),
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800162 mBluetoothProfileServiceListener,
163 BluetoothProfile.HEADSET);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 }
165
Wink Savillef67832f2015-01-12 16:51:50 -0800166 mSubscriptionManager.addOnSubscriptionsChangedListener(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800167 new OnSubscriptionsChangedListener() {
168 @Override
169 public void onSubscriptionsChanged() {
170 updatePhoneStateListeners();
171 }
172 });
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 }
174
175 private void createSignalInfoToneGenerator() {
176 // Instantiate the ToneGenerator for SignalInfo and CallWaiting
177 // TODO: We probably don't need the mSignalInfoToneGenerator instance
178 // around forever. Need to change it so as to create a ToneGenerator instance only
179 // when a tone is being played and releases it after its done playing.
180 if (mSignalInfoToneGenerator == null) {
181 try {
182 mSignalInfoToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL,
183 TONE_RELATIVE_VOLUME_SIGNALINFO);
184 Log.d(LOG_TAG, "CallNotifier: mSignalInfoToneGenerator created when toneplay");
185 } catch (RuntimeException e) {
186 Log.w(LOG_TAG, "CallNotifier: Exception caught while creating " +
187 "mSignalInfoToneGenerator: " + e);
188 mSignalInfoToneGenerator = null;
189 }
190 } else {
191 Log.d(LOG_TAG, "mSignalInfoToneGenerator created already, hence skipping");
192 }
193 }
194
195 @Override
196 public void handleMessage(Message msg) {
197 switch (msg.what) {
198 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
199 log("RINGING... (new)");
200 onNewRingingConnection((AsyncResult) msg.obj);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 break;
202
203 case CallStateMonitor.PHONE_STATE_CHANGED:
204 onPhoneStateChanged((AsyncResult) msg.obj);
205 break;
206
207 case CallStateMonitor.PHONE_DISCONNECT:
208 if (DBG) log("DISCONNECT");
Roshan Pius19f39cf2015-08-12 10:44:38 -0700209 // Stop any signalInfo tone being played when a call gets ended, the rest of the
210 // disconnect functionality in onDisconnect() is handled in ConnectionService.
211 stopSignalInfoTone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 break;
213
214 case CallStateMonitor.PHONE_UNKNOWN_CONNECTION_APPEARED:
215 onUnknownConnectionAppeared((AsyncResult) msg.obj);
216 break;
217
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 case CallStateMonitor.PHONE_STATE_DISPLAYINFO:
219 if (DBG) log("Received PHONE_STATE_DISPLAYINFO event");
220 onDisplayInfo((AsyncResult) msg.obj);
221 break;
222
223 case CallStateMonitor.PHONE_STATE_SIGNALINFO:
224 if (DBG) log("Received PHONE_STATE_SIGNALINFO event");
225 onSignalInfo((AsyncResult) msg.obj);
226 break;
227
Anthony Leee9468532014-11-15 15:21:00 -0800228 case CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 if (DBG) log("Received Display Info notification done event ...");
Anthony Leee9468532014-11-15 15:21:00 -0800230 PhoneDisplayMessage.dismissMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231 break;
232
233 case CallStateMonitor.EVENT_OTA_PROVISION_CHANGE:
234 if (DBG) log("EVENT_OTA_PROVISION_CHANGE...");
235 mApplication.handleOtaspEvent(msg);
236 break;
237
238 case CallStateMonitor.PHONE_ENHANCED_VP_ON:
239 if (DBG) log("PHONE_ENHANCED_VP_ON...");
240 if (!mVoicePrivacyState) {
241 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
242 new InCallTonePlayer(toneToPlay).start();
243 mVoicePrivacyState = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245 break;
246
247 case CallStateMonitor.PHONE_ENHANCED_VP_OFF:
248 if (DBG) log("PHONE_ENHANCED_VP_OFF...");
249 if (mVoicePrivacyState) {
250 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
251 new InCallTonePlayer(toneToPlay).start();
252 mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 }
254 break;
255
Anthony Leee9468532014-11-15 15:21:00 -0800256 case CallStateMonitor.PHONE_SUPP_SERVICE_FAILED:
257 if (DBG) log("PHONE_SUPP_SERVICE_FAILED...");
258 onSuppServiceFailed((AsyncResult) msg.obj);
259 break;
260
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800261 case CallStateMonitor.PHONE_TTY_MODE_RECEIVED:
262 if (DBG) log("Received PHONE_TTY_MODE_RECEIVED event");
263 onTtyModeReceived((AsyncResult) msg.obj);
264 break;
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 default:
267 // super.handleMessage(msg);
268 }
269 }
270
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271 /**
272 * Handles a "new ringing connection" event from the telephony layer.
273 */
274 private void onNewRingingConnection(AsyncResult r) {
275 Connection c = (Connection) r.result;
276 log("onNewRingingConnection(): state = " + mCM.getState() + ", conn = { " + c + " }");
277 Call ringing = c.getCall();
278 Phone phone = ringing.getPhone();
279
280 // Check for a few cases where we totally ignore incoming calls.
281 if (ignoreAllIncomingCalls(phone)) {
282 // Immediately reject the call, without even indicating to the user
283 // that an incoming call occurred. (This will generally send the
284 // caller straight to voicemail, just as if we *had* shown the
285 // incoming-call UI and the user had declined the call.)
286 PhoneUtils.hangupRingingCall(ringing);
287 return;
288 }
289
290 if (!c.isRinging()) {
291 Log.i(LOG_TAG, "CallNotifier.onNewRingingConnection(): connection not ringing!");
292 // This is a very strange case: an incoming call that stopped
293 // ringing almost instantly after the onNewRingingConnection()
294 // event. There's nothing we can do here, so just bail out
295 // without doing anything. (But presumably we'll log it in
296 // the call log when the disconnect event comes in...)
297 return;
298 }
299
300 // Stop any signalInfo tone being played on receiving a Call
301 stopSignalInfoTone();
302
303 Call.State state = c.getState();
304 // State will be either INCOMING or WAITING.
305 if (VDBG) log("- connection is ringing! state = " + state);
306 // if (DBG) PhoneUtils.dumpCallState(mPhone);
307
308 // No need to do any service state checks here (like for
309 // "emergency mode"), since in those states the SIM won't let
310 // us get incoming connections in the first place.
311
312 // TODO: Consider sending out a serialized broadcast Intent here
313 // (maybe "ACTION_NEW_INCOMING_CALL"), *before* starting the
314 // ringer and going to the in-call UI. The intent should contain
315 // the caller-id info for the current connection, and say whether
316 // it would be a "call waiting" call or a regular ringing call.
317 // If anybody consumed the broadcast, we'd bail out without
318 // ringing or bringing up the in-call UI.
319 //
320 // This would give 3rd party apps a chance to listen for (and
321 // intercept) new ringing connections. An app could reject the
322 // incoming call by consuming the broadcast and doing nothing, or
323 // it could "pick up" the call (without any action by the user!)
324 // via some future TelephonyManager API.
325 //
326 // See bug 1312336 for more details.
327 // We'd need to protect this with a new "intercept incoming calls"
328 // system permission.
329
330 // Obtain a partial wake lock to make sure the CPU doesn't go to
331 // sleep before we finish bringing up the InCallScreen.
332 // (This will be upgraded soon to a full wake lock; see
333 // showIncomingCall().)
334 if (VDBG) log("Holding wake lock on new incoming connection.");
335 mApplication.requestWakeState(PhoneGlobals.WakeState.PARTIAL);
336
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 // Note we *don't* post a status bar notification here, since
338 // we're not necessarily ready to actually show the incoming call
339 // to the user. (For calls in the INCOMING state, at least, we
340 // still need to run a caller-id query, and we may not even ring
341 // at all if the "send directly to voicemail" flag is set.)
342 //
343 // Instead, we update the notification (and potentially launch the
344 // InCallScreen) from the showIncomingCall() method, which runs
345 // when the caller-id query completes or times out.
346
347 if (VDBG) log("- onNewRingingConnection() done.");
348 }
349
350 /**
351 * Determines whether or not we're allowed to present incoming calls to the
352 * user, based on the capabilities and/or current state of the device.
353 *
354 * If this method returns true, that means we should immediately reject the
355 * current incoming call, without even indicating to the user that an
356 * incoming call occurred.
357 *
358 * (We only reject incoming calls in a few cases, like during an OTASP call
359 * when we can't interrupt the user, or if the device hasn't completed the
360 * SetupWizard yet. We also don't allow incoming calls on non-voice-capable
361 * devices. But note that we *always* allow incoming calls while in ECM.)
362 *
363 * @return true if we're *not* allowed to present an incoming call to
364 * the user.
365 */
366 private boolean ignoreAllIncomingCalls(Phone phone) {
367 // Incoming calls are totally ignored on non-voice-capable devices.
368 if (!PhoneGlobals.sVoiceCapable) {
369 // ...but still log a warning, since we shouldn't have gotten this
370 // event in the first place! (Incoming calls *should* be blocked at
371 // the telephony layer on non-voice-capable capable devices.)
372 Log.w(LOG_TAG, "Got onNewRingingConnection() on non-voice-capable device! Ignoring...");
373 return true;
374 }
375
376 // In ECM (emergency callback mode), we ALWAYS allow incoming calls
377 // to get through to the user. (Note that ECM is applicable only to
378 // voice-capable CDMA devices).
379 if (PhoneUtils.isPhoneInEcm(phone)) {
380 if (DBG) log("Incoming call while in ECM: always allow...");
381 return false;
382 }
383
384 // Incoming calls are totally ignored if the device isn't provisioned yet.
385 boolean provisioned = Settings.Global.getInt(mApplication.getContentResolver(),
386 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
387 if (!provisioned) {
388 Log.i(LOG_TAG, "Ignoring incoming call: not provisioned");
389 return true;
390 }
391
392 // Incoming calls are totally ignored if an OTASP call is active.
393 if (TelephonyCapabilities.supportsOtasp(phone)) {
394 boolean activateState = (mApplication.cdmaOtaScreenState.otaScreenState
395 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_ACTIVATION);
396 boolean dialogState = (mApplication.cdmaOtaScreenState.otaScreenState
397 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_SUCCESS_FAILURE_DLG);
398 boolean spcState = mApplication.cdmaOtaProvisionData.inOtaSpcState;
399
400 if (spcState) {
401 Log.i(LOG_TAG, "Ignoring incoming call: OTA call is active");
402 return true;
403 } else if (activateState || dialogState) {
404 // We *are* allowed to receive incoming calls at this point.
405 // But clear out any residual OTASP UI first.
406 // TODO: It's an MVC violation to twiddle the OTA UI state here;
407 // we should instead provide a higher-level API via OtaUtils.
408 if (dialogState) mApplication.dismissOtaDialogs();
409 mApplication.clearOtaState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 return false;
411 }
412 }
413
414 // Normal case: allow this call to be presented to the user.
415 return false;
416 }
417
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 private void onUnknownConnectionAppeared(AsyncResult r) {
419 PhoneConstants.State state = mCM.getState();
420
421 if (state == PhoneConstants.State.OFFHOOK) {
Santos Cordon54fdb592013-09-19 05:16:18 -0700422 if (DBG) log("unknown connection appeared...");
Chiao Cheng312b9c92013-09-16 15:40:53 -0700423
Santos Cordon54fdb592013-09-19 05:16:18 -0700424 onPhoneStateChanged(r);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425 }
426 }
427
Christine Chenb5e4b652013-09-19 11:20:18 -0700428 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 * Updates the phone UI in response to phone state changes.
430 *
431 * Watch out: certain state changes are actually handled by their own
432 * specific methods:
433 * - see onNewRingingConnection() for new incoming calls
434 * - see onDisconnect() for calls being hung up or disconnected
435 */
436 private void onPhoneStateChanged(AsyncResult r) {
437 PhoneConstants.State state = mCM.getState();
438 if (VDBG) log("onPhoneStateChanged: state = " + state);
439
440 // Turn status bar notifications on or off depending upon the state
441 // of the phone. Notification Alerts (audible or vibrating) should
442 // be on if and only if the phone is IDLE.
443 mApplication.notificationMgr.statusBarHelper
444 .enableNotificationAlerts(state == PhoneConstants.State.IDLE);
445
446 Phone fgPhone = mCM.getFgPhone();
447 if (fgPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
448 if ((fgPhone.getForegroundCall().getState() == Call.State.ACTIVE)
449 && ((mPreviousCdmaCallState == Call.State.DIALING)
450 || (mPreviousCdmaCallState == Call.State.ALERTING))) {
451 if (mIsCdmaRedialCall) {
452 int toneToPlay = InCallTonePlayer.TONE_REDIAL;
453 new InCallTonePlayer(toneToPlay).start();
454 }
455 // Stop any signal info tone when call moves to ACTIVE state
456 stopSignalInfoTone();
457 }
458 mPreviousCdmaCallState = fgPhone.getForegroundCall().getState();
459 }
460
461 // Have the PhoneApp recompute its mShowBluetoothIndication
462 // flag based on the (new) telephony state.
463 // There's no need to force a UI update since we update the
464 // in-call notification ourselves (below), and the InCallScreen
465 // listens for phone state changes itself.
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700466 mBluetoothManager.updateBluetoothIndication();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 // Update the phone state and other sensor/lock.
469 mApplication.updatePhoneState(state);
470
471 if (state == PhoneConstants.State.OFFHOOK) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700472
473 if (VDBG) log("onPhoneStateChanged: OFF HOOK");
474 // make sure audio is in in-call mode now
475 PhoneUtils.setAudioMode(mCM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700477 }
478
479 void updateCallNotifierRegistrationsAfterRadioTechnologyChange() {
480 if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
481
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700482 // Instantiate mSignalInfoToneGenerator
483 createSignalInfoToneGenerator();
484 }
485
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 private void onDisconnect(AsyncResult r) {
487 if (VDBG) log("onDisconnect()... CallManager state: " + mCM.getState());
488
489 mVoicePrivacyState = false;
490 Connection c = (Connection) r.result;
491 if (c != null) {
Anders Kristensen0b35f042014-02-27 14:31:07 -0800492 log("onDisconnect: cause = " + DisconnectCause.toString(c.getDisconnectCause())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493 + ", incoming = " + c.isIncoming()
494 + ", date = " + c.getCreateTime());
495 } else {
496 Log.w(LOG_TAG, "onDisconnect: null connection");
497 }
498
499 int autoretrySetting = 0;
Anthony Leee9468532014-11-15 15:21:00 -0800500 if ((c != null) &&
501 (c.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 autoretrySetting = android.provider.Settings.Global.getInt(mApplication.
503 getContentResolver(),android.provider.Settings.Global.CALL_AUTO_RETRY, 0);
504 }
505
506 // Stop any signalInfo tone being played when a call gets ended
507 stopSignalInfoTone();
508
Anthony Leee9468532014-11-15 15:21:00 -0800509 if ((c != null) &&
510 (c.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700511 // Resetting the CdmaPhoneCallState members
512 mApplication.cdmaPhoneCallState.resetCdmaPhoneCallState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700513 }
514
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 // If this is the end of an OTASP call, pass it on to the PhoneApp.
516 if (c != null && TelephonyCapabilities.supportsOtasp(c.getCall().getPhone())) {
517 final String number = c.getAddress();
518 if (c.getCall().getPhone().isOtaSpNumber(number)) {
519 if (DBG) log("onDisconnect: this was an OTASP call!");
520 mApplication.handleOtaspDisconnect();
521 }
522 }
523
524 // Check for the various tones we might need to play (thru the
525 // earpiece) after a call disconnects.
526 int toneToPlay = InCallTonePlayer.TONE_NONE;
527
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528 // If we don't need to play BUSY or CONGESTION, then play the
529 // "call ended" tone if this was a "regular disconnect" (i.e. a
530 // normal call where one end or the other hung up) *and* this
531 // disconnect event caused the phone to become idle. (In other
532 // words, we *don't* play the sound if one call hangs up but
533 // there's still an active call on the other line.)
534 // TODO: We may eventually want to disable this via a preference.
535 if ((toneToPlay == InCallTonePlayer.TONE_NONE)
536 && (mCM.getState() == PhoneConstants.State.IDLE)
537 && (c != null)) {
Anders Kristensen0b35f042014-02-27 14:31:07 -0800538 int cause = c.getDisconnectCause();
539 if ((cause == DisconnectCause.NORMAL) // remote hangup
540 || (cause == DisconnectCause.LOCAL)) { // local hangup
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 if (VDBG) log("- need to play CALL_ENDED tone!");
542 toneToPlay = InCallTonePlayer.TONE_CALL_ENDED;
543 mIsCdmaRedialCall = false;
544 }
545 }
546
547 // All phone calls are disconnected.
548 if (mCM.getState() == PhoneConstants.State.IDLE) {
549 // Don't reset the audio mode or bluetooth/speakerphone state
550 // if we still need to let the user hear a tone through the earpiece.
551 if (toneToPlay == InCallTonePlayer.TONE_NONE) {
552 resetAudioStateAfterDisconnect();
553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554 }
555
556 if (c != null) {
557 mCallLogger.logCall(c);
558
559 final String number = c.getAddress();
560 final Phone phone = c.getCall().getPhone();
561 final boolean isEmergencyNumber =
Yorke Lee36bb2542014-06-05 08:09:52 -0700562 PhoneNumberUtils.isLocalEmergencyNumber(mApplication, number);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 // Possibly play a "post-disconnect tone" thru the earpiece.
565 // We do this here, rather than from the InCallScreen
566 // activity, since we need to do this even if you're not in
567 // the Phone UI at the moment the connection ends.
568 if (toneToPlay != InCallTonePlayer.TONE_NONE) {
569 if (VDBG) log("- starting post-disconnect tone (" + toneToPlay + ")...");
570 new InCallTonePlayer(toneToPlay).start();
571
572 // TODO: alternatively, we could start an InCallTonePlayer
573 // here with an "unlimited" tone length,
574 // and manually stop it later when this connection truly goes
575 // away. (The real connection over the network was closed as soon
576 // as we got the BUSY message. But our telephony layer keeps the
577 // connection open for a few extra seconds so we can show the
578 // "busy" indication to the user. We could stop the busy tone
579 // when *that* connection's "disconnect" event comes in.)
580 }
581
Santos Cordonf68db2e2014-07-02 14:40:44 -0700582 final int cause = c.getDisconnectCause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 if (((mPreviousCdmaCallState == Call.State.DIALING)
584 || (mPreviousCdmaCallState == Call.State.ALERTING))
585 && (!isEmergencyNumber)
Anders Kristensen0b35f042014-02-27 14:31:07 -0800586 && (cause != DisconnectCause.INCOMING_MISSED )
587 && (cause != DisconnectCause.NORMAL)
588 && (cause != DisconnectCause.LOCAL)
589 && (cause != DisconnectCause.INCOMING_REJECTED)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 if (!mIsCdmaRedialCall) {
591 if (autoretrySetting == InCallScreen.AUTO_RETRY_ON) {
592 // TODO: (Moto): The contact reference data may need to be stored and use
593 // here when redialing a call. For now, pass in NULL as the URI parameter.
Santos Cordonce02f3a2013-09-19 01:58:42 -0700594 final int status =
595 PhoneUtils.placeCall(mApplication, phone, number, null, false);
596 if (status != PhoneUtils.CALL_STATUS_FAILED) {
597 mIsCdmaRedialCall = true;
598 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 } else {
600 mIsCdmaRedialCall = false;
601 }
602 } else {
603 mIsCdmaRedialCall = false;
604 }
605 }
606 }
607 }
608
609 /**
610 * Resets the audio mode and speaker state when a call ends.
611 */
612 private void resetAudioStateAfterDisconnect() {
613 if (VDBG) log("resetAudioStateAfterDisconnect()...");
614
615 if (mBluetoothHeadset != null) {
616 mBluetoothHeadset.disconnectAudio();
617 }
618
619 // call turnOnSpeaker() with state=false and store=true even if speaker
620 // is already off to reset user requested speaker state.
621 PhoneUtils.turnOnSpeaker(mApplication, false, true);
622
623 PhoneUtils.setAudioMode(mCM);
624 }
625
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 * Helper class to play tones through the earpiece (or speaker / BT)
628 * during a call, using the ToneGenerator.
629 *
630 * To use, just instantiate a new InCallTonePlayer
631 * (passing in the TONE_* constant for the tone you want)
632 * and start() it.
633 *
634 * When we're done playing the tone, if the phone is idle at that
635 * point, we'll reset the audio routing and speaker state.
636 * (That means that for tones that get played *after* a call
637 * disconnects, like "busy" or "congestion" or "call ended", you
638 * should NOT call resetAudioStateAfterDisconnect() yourself.
639 * Instead, just start the InCallTonePlayer, which will automatically
640 * defer the resetAudioStateAfterDisconnect() call until the tone
641 * finishes playing.)
642 */
643 private class InCallTonePlayer extends Thread {
644 private int mToneId;
645 private int mState;
646 // The possible tones we can play.
647 public static final int TONE_NONE = 0;
648 public static final int TONE_CALL_WAITING = 1;
649 public static final int TONE_BUSY = 2;
650 public static final int TONE_CONGESTION = 3;
651 public static final int TONE_CALL_ENDED = 4;
652 public static final int TONE_VOICE_PRIVACY = 5;
653 public static final int TONE_REORDER = 6;
654 public static final int TONE_INTERCEPT = 7;
655 public static final int TONE_CDMA_DROP = 8;
656 public static final int TONE_OUT_OF_SERVICE = 9;
657 public static final int TONE_REDIAL = 10;
658 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
660
661 // The tone volume relative to other sounds in the stream
662 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
663 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
664 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
665
666 // Buffer time (in msec) to add on to tone timeout value.
667 // Needed mainly when the timeout value for a tone is the
668 // exact duration of the tone itself.
669 static final int TONE_TIMEOUT_BUFFER = 20;
670
671 // The tone state
672 static final int TONE_OFF = 0;
673 static final int TONE_ON = 1;
674 static final int TONE_STOPPED = 2;
675
676 InCallTonePlayer(int toneId) {
677 super();
678 mToneId = toneId;
679 mState = TONE_OFF;
680 }
681
682 @Override
683 public void run() {
684 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
685
686 int toneType = 0; // passed to ToneGenerator.startTone()
687 int toneVolume; // passed to the ToneGenerator constructor
688 int toneLengthMillis;
689 int phoneType = mCM.getFgPhone().getPhoneType();
690
691 switch (mToneId) {
692 case TONE_CALL_WAITING:
693 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
694 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
695 // Call waiting tone is stopped by stopTone() method
696 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
697 break;
698 case TONE_BUSY:
699 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
700 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
701 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
702 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800703 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
704 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700705 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800706 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 toneType = ToneGenerator.TONE_SUP_BUSY;
708 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
709 toneLengthMillis = 4000;
710 } else {
711 throw new IllegalStateException("Unexpected phone type: " + phoneType);
712 }
713 break;
714 case TONE_CONGESTION:
715 toneType = ToneGenerator.TONE_SUP_CONGESTION;
716 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
717 toneLengthMillis = 4000;
718 break;
719
720 case TONE_CALL_ENDED:
721 toneType = ToneGenerator.TONE_PROP_PROMPT;
722 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
723 toneLengthMillis = 200;
724 break;
725 case TONE_OTA_CALL_END:
726 if (mApplication.cdmaOtaConfigData.otaPlaySuccessFailureTone ==
727 OtaUtils.OTA_PLAY_SUCCESS_FAILURE_TONE_ON) {
728 toneType = ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD;
729 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
730 toneLengthMillis = 750;
731 } else {
732 toneType = ToneGenerator.TONE_PROP_PROMPT;
733 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
734 toneLengthMillis = 200;
735 }
736 break;
737 case TONE_VOICE_PRIVACY:
738 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
739 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
740 toneLengthMillis = 5000;
741 break;
742 case TONE_REORDER:
743 toneType = ToneGenerator.TONE_CDMA_REORDER;
744 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
745 toneLengthMillis = 4000;
746 break;
747 case TONE_INTERCEPT:
748 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
749 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
750 toneLengthMillis = 500;
751 break;
752 case TONE_CDMA_DROP:
753 case TONE_OUT_OF_SERVICE:
754 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
755 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
756 toneLengthMillis = 375;
757 break;
758 case TONE_REDIAL:
759 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
760 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
761 toneLengthMillis = 5000;
762 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700763 case TONE_UNOBTAINABLE_NUMBER:
764 toneType = ToneGenerator.TONE_SUP_ERROR;
765 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
766 toneLengthMillis = 4000;
767 break;
768 default:
769 throw new IllegalArgumentException("Bad toneId: " + mToneId);
770 }
771
772 // If the mToneGenerator creation fails, just continue without it. It is
773 // a local audio signal, and is not as important.
774 ToneGenerator toneGenerator;
775 try {
776 int stream;
777 if (mBluetoothHeadset != null) {
778 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
779 AudioManager.STREAM_VOICE_CALL;
780 } else {
781 stream = AudioManager.STREAM_VOICE_CALL;
782 }
783 toneGenerator = new ToneGenerator(stream, toneVolume);
784 // if (DBG) log("- created toneGenerator: " + toneGenerator);
785 } catch (RuntimeException e) {
786 Log.w(LOG_TAG,
787 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
788 toneGenerator = null;
789 }
790
791 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
792 // CONGESTION tones at least), the ToneGenerator itself knows
793 // the right pattern of tones to play; we do NOT need to
794 // manually start/stop each individual tone, or manually
795 // insert the correct delay between tones. (We just start it
796 // and let it run for however long we want the tone pattern to
797 // continue.)
798 //
799 // TODO: When we stop the ToneGenerator in the middle of a
800 // "tone pattern", it sounds bad if we cut if off while the
801 // tone is actually playing. Consider adding API to the
802 // ToneGenerator to say "stop at the next silent part of the
803 // pattern", or simply "play the pattern N times and then
804 // stop."
805 boolean needToStopTone = true;
806 boolean okToPlayTone = false;
807
808 if (toneGenerator != null) {
809 int ringerMode = mAudioManager.getRingerMode();
810 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
811 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
812 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
813 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
814 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
815 okToPlayTone = true;
816 needToStopTone = false;
817 }
818 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
819 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
820 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
821 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
822 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
823 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
824 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
825 okToPlayTone = true;
826 needToStopTone = false;
827 }
828 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
829 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
830 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
831 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
832 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
833 okToPlayTone = true;
834 needToStopTone = false;
835 }
836 } else { // For the rest of the tones, always OK to play.
837 okToPlayTone = true;
838 }
839 } else { // Not "CDMA"
840 okToPlayTone = true;
841 }
842
843 synchronized (this) {
844 if (okToPlayTone && mState != TONE_STOPPED) {
845 mState = TONE_ON;
846 toneGenerator.startTone(toneType);
847 try {
848 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
849 } catch (InterruptedException e) {
850 Log.w(LOG_TAG,
851 "InCallTonePlayer stopped: " + e);
852 }
853 if (needToStopTone) {
854 toneGenerator.stopTone();
855 }
856 }
857 // if (DBG) log("- InCallTonePlayer: done playing.");
858 toneGenerator.release();
859 mState = TONE_OFF;
860 }
861 }
862
863 // Finally, do the same cleanup we otherwise would have done
864 // in onDisconnect().
865 //
866 // (But watch out: do NOT do this if the phone is in use,
867 // since some of our tones get played *during* a call (like
868 // CALL_WAITING) and we definitely *don't*
869 // want to reset the audio mode / speaker / bluetooth after
870 // playing those!
871 // This call is really here for use with tones that get played
872 // *after* a call disconnects, like "busy" or "congestion" or
873 // "call ended", where the phone has already become idle but
874 // we need to defer the resetAudioStateAfterDisconnect() call
875 // till the tone finishes playing.)
876 if (mCM.getState() == PhoneConstants.State.IDLE) {
877 resetAudioStateAfterDisconnect();
878 }
879 }
880
881 public void stopTone() {
882 synchronized (this) {
883 if (mState == TONE_ON) {
884 notify();
885 }
886 mState = TONE_STOPPED;
887 }
888 }
889 }
890
891 /**
892 * Displays a notification when the phone receives a DisplayInfo record.
893 */
894 private void onDisplayInfo(AsyncResult r) {
895 // Extract the DisplayInfo String from the message
896 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
897
898 if (displayInfoRec != null) {
899 String displayInfo = displayInfoRec.alpha;
900 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800901 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902
Anthony Leee9468532014-11-15 15:21:00 -0800903 // start a timer that kills the dialog
904 sendEmptyMessageDelayed(CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
905 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 }
907 }
908
909 /**
Anthony Leee9468532014-11-15 15:21:00 -0800910 * Displays a notification when the phone receives a notice that a supplemental
911 * service has failed.
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800912 * TODO: This is a NOOP if it isn't for conferences or resuming call failures right now.
Anthony Leee9468532014-11-15 15:21:00 -0800913 */
914 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800915 if (r.result != Phone.SuppService.CONFERENCE && r.result != Phone.SuppService.RESUME) {
916 if (DBG) log("onSuppServiceFailed: not a merge or resume failure event");
Anthony Leee9468532014-11-15 15:21:00 -0800917 return;
918 }
919
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800920 String mergeFailedString = "";
921 if (r.result == Phone.SuppService.CONFERENCE) {
922 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
923 mergeFailedString = mApplication.getResources().getString(
924 R.string.incall_error_supp_service_conference);
925 } else if (r.result == Phone.SuppService.RESUME) {
926 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
927 mergeFailedString = mApplication.getResources().getString(
928 R.string.incall_error_supp_service_switch);
929 }
Anthony Leee9468532014-11-15 15:21:00 -0800930 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
931
932 // start a timer that kills the dialog
933 sendEmptyMessageDelayed(CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
934 SHOW_MESSAGE_NOTIFICATION_TIME);
935 }
936
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800937 public void updatePhoneStateListeners() {
938 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
939
940 // Unregister phone listeners for inactive subscriptions.
941 Iterator<Integer> itr = mPhoneStateListeners.keySet().iterator();
942 while (itr.hasNext()) {
943 int subId = itr.next();
944 if (subInfos == null || !containsSubId(subInfos, subId)) {
945 // Hide the outstanding notifications.
946 mApplication.notificationMgr.updateMwi(subId, false);
947 mApplication.notificationMgr.updateCfi(subId, false);
948
949 // Listening to LISTEN_NONE removes the listener.
950 mTelephonyManager.listen(
951 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
952 itr.remove();
953 }
954 }
955
956 if (subInfos == null) {
957 return;
958 }
959
960 // Register new phone listeners for active subscriptions.
961 for (int i = 0; i < subInfos.size(); i++) {
962 int subId = subInfos.get(i).getSubscriptionId();
963 if (!mPhoneStateListeners.containsKey(subId)) {
964 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
965 mTelephonyManager.listen(listener,
966 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
967 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
968 mPhoneStateListeners.put(subId, listener);
969 }
970 }
971 }
972
973 /**
974 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
975 */
976 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
977 if (subInfos == null) {
978 return false;
979 }
980
981 for (int i = 0; i < subInfos.size(); i++) {
982 if (subInfos.get(i).getSubscriptionId() == subId) {
983 return true;
984 }
985 }
986 return false;
987 }
988
Anthony Leee9468532014-11-15 15:21:00 -0800989 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800990 * Displays a notification when the phone receives a notice that TTY mode
991 * has changed on remote end.
992 */
993 private void onTtyModeReceived(AsyncResult r) {
994 if (DBG) log("TtyModeReceived: displaying notification message");
995
996 int resId = 0;
997 switch (((Integer)r.result).intValue()) {
998 case TelecomManager.TTY_MODE_FULL:
999 resId = com.android.internal.R.string.peerTtyModeFull;
1000 break;
1001 case TelecomManager.TTY_MODE_HCO:
1002 resId = com.android.internal.R.string.peerTtyModeHco;
1003 break;
1004 case TelecomManager.TTY_MODE_VCO:
1005 resId = com.android.internal.R.string.peerTtyModeVco;
1006 break;
1007 case TelecomManager.TTY_MODE_OFF:
1008 resId = com.android.internal.R.string.peerTtyModeOff;
1009 break;
1010 default:
1011 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
1012 break;
1013 }
1014 if (resId != 0) {
1015 PhoneDisplayMessage.displayNetworkMessage(mApplication,
1016 mApplication.getResources().getString(resId));
1017
1018 // start a timer that kills the dialog
1019 sendEmptyMessageDelayed(
1020 CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
1021 SHOW_MESSAGE_NOTIFICATION_TIME);
1022 }
1023 }
1024
1025 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 * Helper class to play SignalInfo tones using the ToneGenerator.
1027 *
1028 * To use, just instantiate a new SignalInfoTonePlayer
1029 * (passing in the ToneID constant for the tone you want)
1030 * and start() it.
1031 */
1032 private class SignalInfoTonePlayer extends Thread {
1033 private int mToneId;
1034
1035 SignalInfoTonePlayer(int toneId) {
1036 super();
1037 mToneId = toneId;
1038 }
1039
1040 @Override
1041 public void run() {
1042 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -07001043 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044 if (mSignalInfoToneGenerator != null) {
1045 //First stop any ongoing SignalInfo tone
1046 mSignalInfoToneGenerator.stopTone();
1047
1048 //Start playing the new tone if its a valid tone
1049 mSignalInfoToneGenerator.startTone(mToneId);
1050 }
1051 }
1052 }
1053
1054 /**
1055 * Plays a tone when the phone receives a SignalInfo record.
1056 */
1057 private void onSignalInfo(AsyncResult r) {
1058 // Signal Info are totally ignored on non-voice-capable devices.
1059 if (!PhoneGlobals.sVoiceCapable) {
1060 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
1061 return;
1062 }
1063
1064 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
1065 // Do not start any new SignalInfo tone when Call state is INCOMING
1066 // and stop any previous SignalInfo tone which is being played
1067 stopSignalInfoTone();
1068 } else {
1069 // Extract the SignalInfo String from the message
1070 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
1071 // Only proceed if a Signal info is present.
1072 if (signalInfoRec != null) {
1073 boolean isPresent = signalInfoRec.isPresent;
1074 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
1075 if (isPresent) {// if tone is valid
1076 int uSignalType = signalInfoRec.signalType;
1077 int uAlertPitch = signalInfoRec.alertPitch;
1078 int uSignal = signalInfoRec.signal;
1079
1080 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
1081 uAlertPitch + ", uSignal=" + uSignal);
1082 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
1083 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
1084 (uSignalType, uAlertPitch, uSignal);
1085
1086 //Create the SignalInfo tone player and pass the ToneID
1087 new SignalInfoTonePlayer(toneID).start();
1088 }
1089 }
1090 }
1091 }
1092
1093 /**
1094 * Stops a SignalInfo tone in the following condition
1095 * 1 - On receiving a New Ringing Call
1096 * 2 - On disconnecting a call
1097 * 3 - On answering a Call Waiting Call
1098 */
1099 /* package */ void stopSignalInfoTone() {
1100 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
1101 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
1102 }
1103
1104 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 * Return the private variable mPreviousCdmaCallState.
1106 */
1107 /* package */ Call.State getPreviousCdmaCallState() {
1108 return mPreviousCdmaCallState;
1109 }
1110
1111 /**
1112 * Return the private variable mVoicePrivacyState.
1113 */
1114 /* package */ boolean getVoicePrivacyState() {
1115 return mVoicePrivacyState;
1116 }
1117
1118 /**
1119 * Return the private variable mIsCdmaRedialCall.
1120 */
1121 /* package */ boolean getIsCdmaRedialCall() {
1122 return mIsCdmaRedialCall;
1123 }
1124
Santos Cordon5c046722014-09-18 15:41:13 -07001125 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -08001126 new BluetoothProfile.ServiceListener() {
1127 public void onServiceConnected(int profile, BluetoothProfile proxy) {
1128 mBluetoothHeadset = (BluetoothHeadset) proxy;
1129 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
1130 }
1131
1132 public void onServiceDisconnected(int profile) {
1133 mBluetoothHeadset = null;
1134 }
1135 };
1136
1137 private class CallNotifierPhoneStateListener extends PhoneStateListener {
1138 public CallNotifierPhoneStateListener(int subId) {
1139 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140 }
1141
Andrew Lee2fcb6c32014-12-04 14:52:35 -08001142 @Override
1143 public void onMessageWaitingIndicatorChanged(boolean visible) {
1144 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
1145 mApplication.notificationMgr.updateMwi(this.mSubId, visible);
1146 }
1147
1148 @Override
1149 public void onCallForwardingIndicatorChanged(boolean visible) {
1150 if (VDBG) log("onCallForwardingIndicatorChanged(): " + this.mSubId + " " + visible);
1151 mApplication.notificationMgr.updateCfi(this.mSubId, visible);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001152 }
1153 };
1154
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001155 private void log(String msg) {
1156 Log.d(LOG_TAG, msg);
1157 }
1158}