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