blob: dd1eaab0e7bad8001125c773221e773cf5613cbf [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;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700108 private final BluetoothManager mBluetoothManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800109 private SubscriptionManager mSubscriptionManager;
110 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700111
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 /**
113 * Initialize the singleton CallNotifier instance.
114 * This is only done once, at startup, from PhoneApp.onCreate().
115 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800116 /* package */ static CallNotifier init(
117 PhoneGlobals app,
118 CallLogger callLogger,
119 CallStateMonitor callStateMonitor,
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700120 BluetoothManager bluetoothManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 synchronized (CallNotifier.class) {
122 if (sInstance == null) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800123 sInstance = new CallNotifier(app, callLogger, callStateMonitor, bluetoothManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 } else {
125 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
126 }
127 return sInstance;
128 }
129 }
130
131 /** Private constructor; @see init() */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800132 private CallNotifier(
133 PhoneGlobals app,
134 CallLogger callLogger,
135 CallStateMonitor callStateMonitor,
136 BluetoothManager bluetoothManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 mApplication = app;
138 mCM = app.mCM;
139 mCallLogger = callLogger;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700140 mBluetoothManager = bluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141
142 mAudioManager = (AudioManager) mApplication.getSystemService(Context.AUDIO_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800143 mTelephonyManager =
144 (TelephonyManager) mApplication.getSystemService(Context.TELEPHONY_SERVICE);
145 mSubscriptionManager = (SubscriptionManager) mApplication.getSystemService(
146 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147
148 callStateMonitor.addListener(this);
149
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
151 if (adapter != null) {
152 adapter.getProfileProxy(mApplication.getApplicationContext(),
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800153 mBluetoothProfileServiceListener,
154 BluetoothProfile.HEADSET);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 }
156
Wink Savillef67832f2015-01-12 16:51:50 -0800157 mSubscriptionManager.addOnSubscriptionsChangedListener(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800158 new OnSubscriptionsChangedListener() {
159 @Override
160 public void onSubscriptionsChanged() {
161 updatePhoneStateListeners();
162 }
163 });
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 }
165
166 private void createSignalInfoToneGenerator() {
167 // Instantiate the ToneGenerator for SignalInfo and CallWaiting
168 // TODO: We probably don't need the mSignalInfoToneGenerator instance
169 // around forever. Need to change it so as to create a ToneGenerator instance only
170 // when a tone is being played and releases it after its done playing.
171 if (mSignalInfoToneGenerator == null) {
172 try {
173 mSignalInfoToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL,
174 TONE_RELATIVE_VOLUME_SIGNALINFO);
175 Log.d(LOG_TAG, "CallNotifier: mSignalInfoToneGenerator created when toneplay");
176 } catch (RuntimeException e) {
177 Log.w(LOG_TAG, "CallNotifier: Exception caught while creating " +
178 "mSignalInfoToneGenerator: " + e);
179 mSignalInfoToneGenerator = null;
180 }
181 } else {
182 Log.d(LOG_TAG, "mSignalInfoToneGenerator created already, hence skipping");
183 }
184 }
185
186 @Override
187 public void handleMessage(Message msg) {
188 switch (msg.what) {
189 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
190 log("RINGING... (new)");
191 onNewRingingConnection((AsyncResult) msg.obj);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 break;
193
194 case CallStateMonitor.PHONE_STATE_CHANGED:
195 onPhoneStateChanged((AsyncResult) msg.obj);
196 break;
197
198 case CallStateMonitor.PHONE_DISCONNECT:
199 if (DBG) log("DISCONNECT");
Roshan Pius19f39cf2015-08-12 10:44:38 -0700200 // Stop any signalInfo tone being played when a call gets ended, the rest of the
201 // disconnect functionality in onDisconnect() is handled in ConnectionService.
202 stopSignalInfoTone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 break;
204
205 case CallStateMonitor.PHONE_UNKNOWN_CONNECTION_APPEARED:
206 onUnknownConnectionAppeared((AsyncResult) msg.obj);
207 break;
208
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 case CallStateMonitor.PHONE_STATE_DISPLAYINFO:
210 if (DBG) log("Received PHONE_STATE_DISPLAYINFO event");
211 onDisplayInfo((AsyncResult) msg.obj);
212 break;
213
214 case CallStateMonitor.PHONE_STATE_SIGNALINFO:
215 if (DBG) log("Received PHONE_STATE_SIGNALINFO event");
216 onSignalInfo((AsyncResult) msg.obj);
217 break;
218
Anthony Leee9468532014-11-15 15:21:00 -0800219 case CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 if (DBG) log("Received Display Info notification done event ...");
Anthony Leee9468532014-11-15 15:21:00 -0800221 PhoneDisplayMessage.dismissMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 break;
223
224 case CallStateMonitor.EVENT_OTA_PROVISION_CHANGE:
225 if (DBG) log("EVENT_OTA_PROVISION_CHANGE...");
226 mApplication.handleOtaspEvent(msg);
227 break;
228
229 case CallStateMonitor.PHONE_ENHANCED_VP_ON:
230 if (DBG) log("PHONE_ENHANCED_VP_ON...");
231 if (!mVoicePrivacyState) {
232 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
233 new InCallTonePlayer(toneToPlay).start();
234 mVoicePrivacyState = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235 }
236 break;
237
238 case CallStateMonitor.PHONE_ENHANCED_VP_OFF:
239 if (DBG) log("PHONE_ENHANCED_VP_OFF...");
240 if (mVoicePrivacyState) {
241 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
242 new InCallTonePlayer(toneToPlay).start();
243 mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245 break;
246
Anthony Leee9468532014-11-15 15:21:00 -0800247 case CallStateMonitor.PHONE_SUPP_SERVICE_FAILED:
248 if (DBG) log("PHONE_SUPP_SERVICE_FAILED...");
249 onSuppServiceFailed((AsyncResult) msg.obj);
250 break;
251
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800252 case CallStateMonitor.PHONE_TTY_MODE_RECEIVED:
253 if (DBG) log("Received PHONE_TTY_MODE_RECEIVED event");
254 onTtyModeReceived((AsyncResult) msg.obj);
255 break;
256
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 default:
258 // super.handleMessage(msg);
259 }
260 }
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 /**
263 * Handles a "new ringing connection" event from the telephony layer.
264 */
265 private void onNewRingingConnection(AsyncResult r) {
266 Connection c = (Connection) r.result;
267 log("onNewRingingConnection(): state = " + mCM.getState() + ", conn = { " + c + " }");
268 Call ringing = c.getCall();
269 Phone phone = ringing.getPhone();
270
271 // Check for a few cases where we totally ignore incoming calls.
272 if (ignoreAllIncomingCalls(phone)) {
273 // Immediately reject the call, without even indicating to the user
274 // that an incoming call occurred. (This will generally send the
275 // caller straight to voicemail, just as if we *had* shown the
276 // incoming-call UI and the user had declined the call.)
277 PhoneUtils.hangupRingingCall(ringing);
278 return;
279 }
280
281 if (!c.isRinging()) {
282 Log.i(LOG_TAG, "CallNotifier.onNewRingingConnection(): connection not ringing!");
283 // This is a very strange case: an incoming call that stopped
284 // ringing almost instantly after the onNewRingingConnection()
285 // event. There's nothing we can do here, so just bail out
286 // without doing anything. (But presumably we'll log it in
287 // the call log when the disconnect event comes in...)
288 return;
289 }
290
291 // Stop any signalInfo tone being played on receiving a Call
292 stopSignalInfoTone();
293
294 Call.State state = c.getState();
295 // State will be either INCOMING or WAITING.
296 if (VDBG) log("- connection is ringing! state = " + state);
297 // if (DBG) PhoneUtils.dumpCallState(mPhone);
298
299 // No need to do any service state checks here (like for
300 // "emergency mode"), since in those states the SIM won't let
301 // us get incoming connections in the first place.
302
303 // TODO: Consider sending out a serialized broadcast Intent here
304 // (maybe "ACTION_NEW_INCOMING_CALL"), *before* starting the
305 // ringer and going to the in-call UI. The intent should contain
306 // the caller-id info for the current connection, and say whether
307 // it would be a "call waiting" call or a regular ringing call.
308 // If anybody consumed the broadcast, we'd bail out without
309 // ringing or bringing up the in-call UI.
310 //
311 // This would give 3rd party apps a chance to listen for (and
312 // intercept) new ringing connections. An app could reject the
313 // incoming call by consuming the broadcast and doing nothing, or
314 // it could "pick up" the call (without any action by the user!)
315 // via some future TelephonyManager API.
316 //
317 // See bug 1312336 for more details.
318 // We'd need to protect this with a new "intercept incoming calls"
319 // system permission.
320
321 // Obtain a partial wake lock to make sure the CPU doesn't go to
322 // sleep before we finish bringing up the InCallScreen.
323 // (This will be upgraded soon to a full wake lock; see
324 // showIncomingCall().)
325 if (VDBG) log("Holding wake lock on new incoming connection.");
326 mApplication.requestWakeState(PhoneGlobals.WakeState.PARTIAL);
327
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 // Note we *don't* post a status bar notification here, since
329 // we're not necessarily ready to actually show the incoming call
330 // to the user. (For calls in the INCOMING state, at least, we
331 // still need to run a caller-id query, and we may not even ring
332 // at all if the "send directly to voicemail" flag is set.)
333 //
334 // Instead, we update the notification (and potentially launch the
335 // InCallScreen) from the showIncomingCall() method, which runs
336 // when the caller-id query completes or times out.
337
338 if (VDBG) log("- onNewRingingConnection() done.");
339 }
340
341 /**
342 * Determines whether or not we're allowed to present incoming calls to the
343 * user, based on the capabilities and/or current state of the device.
344 *
345 * If this method returns true, that means we should immediately reject the
346 * current incoming call, without even indicating to the user that an
347 * incoming call occurred.
348 *
349 * (We only reject incoming calls in a few cases, like during an OTASP call
350 * when we can't interrupt the user, or if the device hasn't completed the
351 * SetupWizard yet. We also don't allow incoming calls on non-voice-capable
352 * devices. But note that we *always* allow incoming calls while in ECM.)
353 *
354 * @return true if we're *not* allowed to present an incoming call to
355 * the user.
356 */
357 private boolean ignoreAllIncomingCalls(Phone phone) {
358 // Incoming calls are totally ignored on non-voice-capable devices.
359 if (!PhoneGlobals.sVoiceCapable) {
360 // ...but still log a warning, since we shouldn't have gotten this
361 // event in the first place! (Incoming calls *should* be blocked at
362 // the telephony layer on non-voice-capable capable devices.)
363 Log.w(LOG_TAG, "Got onNewRingingConnection() on non-voice-capable device! Ignoring...");
364 return true;
365 }
366
367 // In ECM (emergency callback mode), we ALWAYS allow incoming calls
368 // to get through to the user. (Note that ECM is applicable only to
369 // voice-capable CDMA devices).
370 if (PhoneUtils.isPhoneInEcm(phone)) {
371 if (DBG) log("Incoming call while in ECM: always allow...");
372 return false;
373 }
374
375 // Incoming calls are totally ignored if the device isn't provisioned yet.
376 boolean provisioned = Settings.Global.getInt(mApplication.getContentResolver(),
377 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
378 if (!provisioned) {
379 Log.i(LOG_TAG, "Ignoring incoming call: not provisioned");
380 return true;
381 }
382
383 // Incoming calls are totally ignored if an OTASP call is active.
384 if (TelephonyCapabilities.supportsOtasp(phone)) {
385 boolean activateState = (mApplication.cdmaOtaScreenState.otaScreenState
386 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_ACTIVATION);
387 boolean dialogState = (mApplication.cdmaOtaScreenState.otaScreenState
388 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_SUCCESS_FAILURE_DLG);
389 boolean spcState = mApplication.cdmaOtaProvisionData.inOtaSpcState;
390
391 if (spcState) {
392 Log.i(LOG_TAG, "Ignoring incoming call: OTA call is active");
393 return true;
394 } else if (activateState || dialogState) {
395 // We *are* allowed to receive incoming calls at this point.
396 // But clear out any residual OTASP UI first.
397 // TODO: It's an MVC violation to twiddle the OTA UI state here;
398 // we should instead provide a higher-level API via OtaUtils.
399 if (dialogState) mApplication.dismissOtaDialogs();
400 mApplication.clearOtaState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 return false;
402 }
403 }
404
405 // Normal case: allow this call to be presented to the user.
406 return false;
407 }
408
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 private void onUnknownConnectionAppeared(AsyncResult r) {
410 PhoneConstants.State state = mCM.getState();
411
412 if (state == PhoneConstants.State.OFFHOOK) {
Santos Cordon54fdb592013-09-19 05:16:18 -0700413 if (DBG) log("unknown connection appeared...");
Chiao Cheng312b9c92013-09-16 15:40:53 -0700414
Santos Cordon54fdb592013-09-19 05:16:18 -0700415 onPhoneStateChanged(r);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 }
417 }
418
Christine Chenb5e4b652013-09-19 11:20:18 -0700419 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 * Updates the phone UI in response to phone state changes.
421 *
422 * Watch out: certain state changes are actually handled by their own
423 * specific methods:
424 * - see onNewRingingConnection() for new incoming calls
425 * - see onDisconnect() for calls being hung up or disconnected
426 */
427 private void onPhoneStateChanged(AsyncResult r) {
428 PhoneConstants.State state = mCM.getState();
429 if (VDBG) log("onPhoneStateChanged: state = " + state);
430
431 // Turn status bar notifications on or off depending upon the state
432 // of the phone. Notification Alerts (audible or vibrating) should
433 // be on if and only if the phone is IDLE.
434 mApplication.notificationMgr.statusBarHelper
435 .enableNotificationAlerts(state == PhoneConstants.State.IDLE);
436
437 Phone fgPhone = mCM.getFgPhone();
438 if (fgPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
439 if ((fgPhone.getForegroundCall().getState() == Call.State.ACTIVE)
440 && ((mPreviousCdmaCallState == Call.State.DIALING)
441 || (mPreviousCdmaCallState == Call.State.ALERTING))) {
442 if (mIsCdmaRedialCall) {
443 int toneToPlay = InCallTonePlayer.TONE_REDIAL;
444 new InCallTonePlayer(toneToPlay).start();
445 }
446 // Stop any signal info tone when call moves to ACTIVE state
447 stopSignalInfoTone();
448 }
449 mPreviousCdmaCallState = fgPhone.getForegroundCall().getState();
450 }
451
452 // Have the PhoneApp recompute its mShowBluetoothIndication
453 // flag based on the (new) telephony state.
454 // There's no need to force a UI update since we update the
455 // in-call notification ourselves (below), and the InCallScreen
456 // listens for phone state changes itself.
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700457 mBluetoothManager.updateBluetoothIndication();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700459 // Update the phone state and other sensor/lock.
460 mApplication.updatePhoneState(state);
461
462 if (state == PhoneConstants.State.OFFHOOK) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700463
464 if (VDBG) log("onPhoneStateChanged: OFF HOOK");
465 // make sure audio is in in-call mode now
466 PhoneUtils.setAudioMode(mCM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 }
469
470 void updateCallNotifierRegistrationsAfterRadioTechnologyChange() {
471 if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
472
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700473 // Instantiate mSignalInfoToneGenerator
474 createSignalInfoToneGenerator();
475 }
476
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700477 private void onDisconnect(AsyncResult r) {
478 if (VDBG) log("onDisconnect()... CallManager state: " + mCM.getState());
479
480 mVoicePrivacyState = false;
481 Connection c = (Connection) r.result;
482 if (c != null) {
Anders Kristensen0b35f042014-02-27 14:31:07 -0800483 log("onDisconnect: cause = " + DisconnectCause.toString(c.getDisconnectCause())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484 + ", incoming = " + c.isIncoming()
485 + ", date = " + c.getCreateTime());
486 } else {
487 Log.w(LOG_TAG, "onDisconnect: null connection");
488 }
489
490 int autoretrySetting = 0;
Anthony Leee9468532014-11-15 15:21:00 -0800491 if ((c != null) &&
492 (c.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493 autoretrySetting = android.provider.Settings.Global.getInt(mApplication.
494 getContentResolver(),android.provider.Settings.Global.CALL_AUTO_RETRY, 0);
495 }
496
497 // Stop any signalInfo tone being played when a call gets ended
498 stopSignalInfoTone();
499
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 // Resetting the CdmaPhoneCallState members
503 mApplication.cdmaPhoneCallState.resetCdmaPhoneCallState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700504 }
505
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 // If this is the end of an OTASP call, pass it on to the PhoneApp.
507 if (c != null && TelephonyCapabilities.supportsOtasp(c.getCall().getPhone())) {
508 final String number = c.getAddress();
509 if (c.getCall().getPhone().isOtaSpNumber(number)) {
510 if (DBG) log("onDisconnect: this was an OTASP call!");
511 mApplication.handleOtaspDisconnect();
512 }
513 }
514
515 // Check for the various tones we might need to play (thru the
516 // earpiece) after a call disconnects.
517 int toneToPlay = InCallTonePlayer.TONE_NONE;
518
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 // If we don't need to play BUSY or CONGESTION, then play the
520 // "call ended" tone if this was a "regular disconnect" (i.e. a
521 // normal call where one end or the other hung up) *and* this
522 // disconnect event caused the phone to become idle. (In other
523 // words, we *don't* play the sound if one call hangs up but
524 // there's still an active call on the other line.)
525 // TODO: We may eventually want to disable this via a preference.
526 if ((toneToPlay == InCallTonePlayer.TONE_NONE)
527 && (mCM.getState() == PhoneConstants.State.IDLE)
528 && (c != null)) {
Anders Kristensen0b35f042014-02-27 14:31:07 -0800529 int cause = c.getDisconnectCause();
530 if ((cause == DisconnectCause.NORMAL) // remote hangup
531 || (cause == DisconnectCause.LOCAL)) { // local hangup
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 if (VDBG) log("- need to play CALL_ENDED tone!");
533 toneToPlay = InCallTonePlayer.TONE_CALL_ENDED;
534 mIsCdmaRedialCall = false;
535 }
536 }
537
538 // All phone calls are disconnected.
539 if (mCM.getState() == PhoneConstants.State.IDLE) {
540 // Don't reset the audio mode or bluetooth/speakerphone state
541 // if we still need to let the user hear a tone through the earpiece.
542 if (toneToPlay == InCallTonePlayer.TONE_NONE) {
543 resetAudioStateAfterDisconnect();
544 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 }
546
547 if (c != null) {
548 mCallLogger.logCall(c);
549
550 final String number = c.getAddress();
551 final Phone phone = c.getCall().getPhone();
552 final boolean isEmergencyNumber =
Yorke Lee36bb2542014-06-05 08:09:52 -0700553 PhoneNumberUtils.isLocalEmergencyNumber(mApplication, number);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 // Possibly play a "post-disconnect tone" thru the earpiece.
556 // We do this here, rather than from the InCallScreen
557 // activity, since we need to do this even if you're not in
558 // the Phone UI at the moment the connection ends.
559 if (toneToPlay != InCallTonePlayer.TONE_NONE) {
560 if (VDBG) log("- starting post-disconnect tone (" + toneToPlay + ")...");
561 new InCallTonePlayer(toneToPlay).start();
562
563 // TODO: alternatively, we could start an InCallTonePlayer
564 // here with an "unlimited" tone length,
565 // and manually stop it later when this connection truly goes
566 // away. (The real connection over the network was closed as soon
567 // as we got the BUSY message. But our telephony layer keeps the
568 // connection open for a few extra seconds so we can show the
569 // "busy" indication to the user. We could stop the busy tone
570 // when *that* connection's "disconnect" event comes in.)
571 }
572
Santos Cordonf68db2e2014-07-02 14:40:44 -0700573 final int cause = c.getDisconnectCause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700574 if (((mPreviousCdmaCallState == Call.State.DIALING)
575 || (mPreviousCdmaCallState == Call.State.ALERTING))
576 && (!isEmergencyNumber)
Anders Kristensen0b35f042014-02-27 14:31:07 -0800577 && (cause != DisconnectCause.INCOMING_MISSED )
578 && (cause != DisconnectCause.NORMAL)
579 && (cause != DisconnectCause.LOCAL)
580 && (cause != DisconnectCause.INCOMING_REJECTED)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 if (!mIsCdmaRedialCall) {
582 if (autoretrySetting == InCallScreen.AUTO_RETRY_ON) {
583 // TODO: (Moto): The contact reference data may need to be stored and use
584 // here when redialing a call. For now, pass in NULL as the URI parameter.
Santos Cordonce02f3a2013-09-19 01:58:42 -0700585 final int status =
586 PhoneUtils.placeCall(mApplication, phone, number, null, false);
587 if (status != PhoneUtils.CALL_STATUS_FAILED) {
588 mIsCdmaRedialCall = true;
589 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 } else {
591 mIsCdmaRedialCall = false;
592 }
593 } else {
594 mIsCdmaRedialCall = false;
595 }
596 }
597 }
598 }
599
600 /**
601 * Resets the audio mode and speaker state when a call ends.
602 */
603 private void resetAudioStateAfterDisconnect() {
604 if (VDBG) log("resetAudioStateAfterDisconnect()...");
605
606 if (mBluetoothHeadset != null) {
607 mBluetoothHeadset.disconnectAudio();
608 }
609
610 // call turnOnSpeaker() with state=false and store=true even if speaker
611 // is already off to reset user requested speaker state.
612 PhoneUtils.turnOnSpeaker(mApplication, false, true);
613
614 PhoneUtils.setAudioMode(mCM);
615 }
616
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 * Helper class to play tones through the earpiece (or speaker / BT)
619 * during a call, using the ToneGenerator.
620 *
621 * To use, just instantiate a new InCallTonePlayer
622 * (passing in the TONE_* constant for the tone you want)
623 * and start() it.
624 *
625 * When we're done playing the tone, if the phone is idle at that
626 * point, we'll reset the audio routing and speaker state.
627 * (That means that for tones that get played *after* a call
628 * disconnects, like "busy" or "congestion" or "call ended", you
629 * should NOT call resetAudioStateAfterDisconnect() yourself.
630 * Instead, just start the InCallTonePlayer, which will automatically
631 * defer the resetAudioStateAfterDisconnect() call until the tone
632 * finishes playing.)
633 */
634 private class InCallTonePlayer extends Thread {
635 private int mToneId;
636 private int mState;
637 // The possible tones we can play.
638 public static final int TONE_NONE = 0;
639 public static final int TONE_CALL_WAITING = 1;
640 public static final int TONE_BUSY = 2;
641 public static final int TONE_CONGESTION = 3;
642 public static final int TONE_CALL_ENDED = 4;
643 public static final int TONE_VOICE_PRIVACY = 5;
644 public static final int TONE_REORDER = 6;
645 public static final int TONE_INTERCEPT = 7;
646 public static final int TONE_CDMA_DROP = 8;
647 public static final int TONE_OUT_OF_SERVICE = 9;
648 public static final int TONE_REDIAL = 10;
649 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
651
652 // The tone volume relative to other sounds in the stream
653 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
654 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
655 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
656
657 // Buffer time (in msec) to add on to tone timeout value.
658 // Needed mainly when the timeout value for a tone is the
659 // exact duration of the tone itself.
660 static final int TONE_TIMEOUT_BUFFER = 20;
661
662 // The tone state
663 static final int TONE_OFF = 0;
664 static final int TONE_ON = 1;
665 static final int TONE_STOPPED = 2;
666
667 InCallTonePlayer(int toneId) {
668 super();
669 mToneId = toneId;
670 mState = TONE_OFF;
671 }
672
673 @Override
674 public void run() {
675 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
676
677 int toneType = 0; // passed to ToneGenerator.startTone()
678 int toneVolume; // passed to the ToneGenerator constructor
679 int toneLengthMillis;
680 int phoneType = mCM.getFgPhone().getPhoneType();
681
682 switch (mToneId) {
683 case TONE_CALL_WAITING:
684 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
685 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
686 // Call waiting tone is stopped by stopTone() method
687 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
688 break;
689 case TONE_BUSY:
690 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
691 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
692 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
693 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800694 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
695 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700696 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800697 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700698 toneType = ToneGenerator.TONE_SUP_BUSY;
699 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
700 toneLengthMillis = 4000;
701 } else {
702 throw new IllegalStateException("Unexpected phone type: " + phoneType);
703 }
704 break;
705 case TONE_CONGESTION:
706 toneType = ToneGenerator.TONE_SUP_CONGESTION;
707 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
708 toneLengthMillis = 4000;
709 break;
710
711 case TONE_CALL_ENDED:
712 toneType = ToneGenerator.TONE_PROP_PROMPT;
713 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
714 toneLengthMillis = 200;
715 break;
716 case TONE_OTA_CALL_END:
717 if (mApplication.cdmaOtaConfigData.otaPlaySuccessFailureTone ==
718 OtaUtils.OTA_PLAY_SUCCESS_FAILURE_TONE_ON) {
719 toneType = ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD;
720 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
721 toneLengthMillis = 750;
722 } else {
723 toneType = ToneGenerator.TONE_PROP_PROMPT;
724 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
725 toneLengthMillis = 200;
726 }
727 break;
728 case TONE_VOICE_PRIVACY:
729 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
730 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
731 toneLengthMillis = 5000;
732 break;
733 case TONE_REORDER:
734 toneType = ToneGenerator.TONE_CDMA_REORDER;
735 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
736 toneLengthMillis = 4000;
737 break;
738 case TONE_INTERCEPT:
739 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
740 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
741 toneLengthMillis = 500;
742 break;
743 case TONE_CDMA_DROP:
744 case TONE_OUT_OF_SERVICE:
745 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
746 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
747 toneLengthMillis = 375;
748 break;
749 case TONE_REDIAL:
750 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
751 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
752 toneLengthMillis = 5000;
753 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 case TONE_UNOBTAINABLE_NUMBER:
755 toneType = ToneGenerator.TONE_SUP_ERROR;
756 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
757 toneLengthMillis = 4000;
758 break;
759 default:
760 throw new IllegalArgumentException("Bad toneId: " + mToneId);
761 }
762
763 // If the mToneGenerator creation fails, just continue without it. It is
764 // a local audio signal, and is not as important.
765 ToneGenerator toneGenerator;
766 try {
767 int stream;
768 if (mBluetoothHeadset != null) {
769 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
770 AudioManager.STREAM_VOICE_CALL;
771 } else {
772 stream = AudioManager.STREAM_VOICE_CALL;
773 }
774 toneGenerator = new ToneGenerator(stream, toneVolume);
775 // if (DBG) log("- created toneGenerator: " + toneGenerator);
776 } catch (RuntimeException e) {
777 Log.w(LOG_TAG,
778 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
779 toneGenerator = null;
780 }
781
782 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
783 // CONGESTION tones at least), the ToneGenerator itself knows
784 // the right pattern of tones to play; we do NOT need to
785 // manually start/stop each individual tone, or manually
786 // insert the correct delay between tones. (We just start it
787 // and let it run for however long we want the tone pattern to
788 // continue.)
789 //
790 // TODO: When we stop the ToneGenerator in the middle of a
791 // "tone pattern", it sounds bad if we cut if off while the
792 // tone is actually playing. Consider adding API to the
793 // ToneGenerator to say "stop at the next silent part of the
794 // pattern", or simply "play the pattern N times and then
795 // stop."
796 boolean needToStopTone = true;
797 boolean okToPlayTone = false;
798
799 if (toneGenerator != null) {
800 int ringerMode = mAudioManager.getRingerMode();
801 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
802 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
803 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
804 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
805 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
806 okToPlayTone = true;
807 needToStopTone = false;
808 }
809 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
810 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
811 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
812 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
813 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
814 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
815 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
816 okToPlayTone = true;
817 needToStopTone = false;
818 }
819 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
820 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
821 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
822 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
823 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
824 okToPlayTone = true;
825 needToStopTone = false;
826 }
827 } else { // For the rest of the tones, always OK to play.
828 okToPlayTone = true;
829 }
830 } else { // Not "CDMA"
831 okToPlayTone = true;
832 }
833
834 synchronized (this) {
835 if (okToPlayTone && mState != TONE_STOPPED) {
836 mState = TONE_ON;
837 toneGenerator.startTone(toneType);
838 try {
839 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
840 } catch (InterruptedException e) {
841 Log.w(LOG_TAG,
842 "InCallTonePlayer stopped: " + e);
843 }
844 if (needToStopTone) {
845 toneGenerator.stopTone();
846 }
847 }
848 // if (DBG) log("- InCallTonePlayer: done playing.");
849 toneGenerator.release();
850 mState = TONE_OFF;
851 }
852 }
853
854 // Finally, do the same cleanup we otherwise would have done
855 // in onDisconnect().
856 //
857 // (But watch out: do NOT do this if the phone is in use,
858 // since some of our tones get played *during* a call (like
859 // CALL_WAITING) and we definitely *don't*
860 // want to reset the audio mode / speaker / bluetooth after
861 // playing those!
862 // This call is really here for use with tones that get played
863 // *after* a call disconnects, like "busy" or "congestion" or
864 // "call ended", where the phone has already become idle but
865 // we need to defer the resetAudioStateAfterDisconnect() call
866 // till the tone finishes playing.)
867 if (mCM.getState() == PhoneConstants.State.IDLE) {
868 resetAudioStateAfterDisconnect();
869 }
870 }
871
872 public void stopTone() {
873 synchronized (this) {
874 if (mState == TONE_ON) {
875 notify();
876 }
877 mState = TONE_STOPPED;
878 }
879 }
880 }
881
882 /**
883 * Displays a notification when the phone receives a DisplayInfo record.
884 */
885 private void onDisplayInfo(AsyncResult r) {
886 // Extract the DisplayInfo String from the message
887 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
888
889 if (displayInfoRec != null) {
890 String displayInfo = displayInfoRec.alpha;
891 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800892 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893
Anthony Leee9468532014-11-15 15:21:00 -0800894 // start a timer that kills the dialog
895 sendEmptyMessageDelayed(CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
896 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 }
898 }
899
900 /**
Anthony Leee9468532014-11-15 15:21:00 -0800901 * Displays a notification when the phone receives a notice that a supplemental
902 * service has failed.
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800903 * TODO: This is a NOOP if it isn't for conferences or resuming call failures right now.
Anthony Leee9468532014-11-15 15:21:00 -0800904 */
905 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800906 if (r.result != Phone.SuppService.CONFERENCE && r.result != Phone.SuppService.RESUME) {
907 if (DBG) log("onSuppServiceFailed: not a merge or resume failure event");
Anthony Leee9468532014-11-15 15:21:00 -0800908 return;
909 }
910
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800911 String mergeFailedString = "";
912 if (r.result == Phone.SuppService.CONFERENCE) {
913 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
914 mergeFailedString = mApplication.getResources().getString(
915 R.string.incall_error_supp_service_conference);
916 } else if (r.result == Phone.SuppService.RESUME) {
917 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
918 mergeFailedString = mApplication.getResources().getString(
919 R.string.incall_error_supp_service_switch);
920 }
Anthony Leee9468532014-11-15 15:21:00 -0800921 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
922
923 // start a timer that kills the dialog
924 sendEmptyMessageDelayed(CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
925 SHOW_MESSAGE_NOTIFICATION_TIME);
926 }
927
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800928 public void updatePhoneStateListeners() {
929 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
930
931 // Unregister phone listeners for inactive subscriptions.
932 Iterator<Integer> itr = mPhoneStateListeners.keySet().iterator();
933 while (itr.hasNext()) {
934 int subId = itr.next();
935 if (subInfos == null || !containsSubId(subInfos, subId)) {
936 // Hide the outstanding notifications.
937 mApplication.notificationMgr.updateMwi(subId, false);
938 mApplication.notificationMgr.updateCfi(subId, false);
939
940 // Listening to LISTEN_NONE removes the listener.
941 mTelephonyManager.listen(
942 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
943 itr.remove();
944 }
945 }
946
947 if (subInfos == null) {
948 return;
949 }
950
951 // Register new phone listeners for active subscriptions.
952 for (int i = 0; i < subInfos.size(); i++) {
953 int subId = subInfos.get(i).getSubscriptionId();
954 if (!mPhoneStateListeners.containsKey(subId)) {
955 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
956 mTelephonyManager.listen(listener,
957 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
958 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
959 mPhoneStateListeners.put(subId, listener);
960 }
961 }
962 }
963
964 /**
965 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
966 */
967 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
968 if (subInfos == null) {
969 return false;
970 }
971
972 for (int i = 0; i < subInfos.size(); i++) {
973 if (subInfos.get(i).getSubscriptionId() == subId) {
974 return true;
975 }
976 }
977 return false;
978 }
979
Anthony Leee9468532014-11-15 15:21:00 -0800980 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800981 * Displays a notification when the phone receives a notice that TTY mode
982 * has changed on remote end.
983 */
984 private void onTtyModeReceived(AsyncResult r) {
985 if (DBG) log("TtyModeReceived: displaying notification message");
986
987 int resId = 0;
988 switch (((Integer)r.result).intValue()) {
989 case TelecomManager.TTY_MODE_FULL:
990 resId = com.android.internal.R.string.peerTtyModeFull;
991 break;
992 case TelecomManager.TTY_MODE_HCO:
993 resId = com.android.internal.R.string.peerTtyModeHco;
994 break;
995 case TelecomManager.TTY_MODE_VCO:
996 resId = com.android.internal.R.string.peerTtyModeVco;
997 break;
998 case TelecomManager.TTY_MODE_OFF:
999 resId = com.android.internal.R.string.peerTtyModeOff;
1000 break;
1001 default:
1002 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
1003 break;
1004 }
1005 if (resId != 0) {
1006 PhoneDisplayMessage.displayNetworkMessage(mApplication,
1007 mApplication.getResources().getString(resId));
1008
1009 // start a timer that kills the dialog
1010 sendEmptyMessageDelayed(
1011 CallStateMonitor.INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
1012 SHOW_MESSAGE_NOTIFICATION_TIME);
1013 }
1014 }
1015
1016 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 * Helper class to play SignalInfo tones using the ToneGenerator.
1018 *
1019 * To use, just instantiate a new SignalInfoTonePlayer
1020 * (passing in the ToneID constant for the tone you want)
1021 * and start() it.
1022 */
1023 private class SignalInfoTonePlayer extends Thread {
1024 private int mToneId;
1025
1026 SignalInfoTonePlayer(int toneId) {
1027 super();
1028 mToneId = toneId;
1029 }
1030
1031 @Override
1032 public void run() {
1033 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -07001034 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 if (mSignalInfoToneGenerator != null) {
1036 //First stop any ongoing SignalInfo tone
1037 mSignalInfoToneGenerator.stopTone();
1038
1039 //Start playing the new tone if its a valid tone
1040 mSignalInfoToneGenerator.startTone(mToneId);
1041 }
1042 }
1043 }
1044
1045 /**
1046 * Plays a tone when the phone receives a SignalInfo record.
1047 */
1048 private void onSignalInfo(AsyncResult r) {
1049 // Signal Info are totally ignored on non-voice-capable devices.
1050 if (!PhoneGlobals.sVoiceCapable) {
1051 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
1052 return;
1053 }
1054
1055 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
1056 // Do not start any new SignalInfo tone when Call state is INCOMING
1057 // and stop any previous SignalInfo tone which is being played
1058 stopSignalInfoTone();
1059 } else {
1060 // Extract the SignalInfo String from the message
1061 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
1062 // Only proceed if a Signal info is present.
1063 if (signalInfoRec != null) {
1064 boolean isPresent = signalInfoRec.isPresent;
1065 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
1066 if (isPresent) {// if tone is valid
1067 int uSignalType = signalInfoRec.signalType;
1068 int uAlertPitch = signalInfoRec.alertPitch;
1069 int uSignal = signalInfoRec.signal;
1070
1071 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
1072 uAlertPitch + ", uSignal=" + uSignal);
1073 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
1074 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
1075 (uSignalType, uAlertPitch, uSignal);
1076
1077 //Create the SignalInfo tone player and pass the ToneID
1078 new SignalInfoTonePlayer(toneID).start();
1079 }
1080 }
1081 }
1082 }
1083
1084 /**
1085 * Stops a SignalInfo tone in the following condition
1086 * 1 - On receiving a New Ringing Call
1087 * 2 - On disconnecting a call
1088 * 3 - On answering a Call Waiting Call
1089 */
1090 /* package */ void stopSignalInfoTone() {
1091 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
1092 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
1093 }
1094
1095 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 * Return the private variable mPreviousCdmaCallState.
1097 */
1098 /* package */ Call.State getPreviousCdmaCallState() {
1099 return mPreviousCdmaCallState;
1100 }
1101
1102 /**
1103 * Return the private variable mVoicePrivacyState.
1104 */
1105 /* package */ boolean getVoicePrivacyState() {
1106 return mVoicePrivacyState;
1107 }
1108
1109 /**
1110 * Return the private variable mIsCdmaRedialCall.
1111 */
1112 /* package */ boolean getIsCdmaRedialCall() {
1113 return mIsCdmaRedialCall;
1114 }
1115
Santos Cordon5c046722014-09-18 15:41:13 -07001116 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -08001117 new BluetoothProfile.ServiceListener() {
1118 public void onServiceConnected(int profile, BluetoothProfile proxy) {
1119 mBluetoothHeadset = (BluetoothHeadset) proxy;
1120 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
1121 }
1122
1123 public void onServiceDisconnected(int profile) {
1124 mBluetoothHeadset = null;
1125 }
1126 };
1127
1128 private class CallNotifierPhoneStateListener extends PhoneStateListener {
1129 public CallNotifierPhoneStateListener(int subId) {
1130 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001131 }
1132
Andrew Lee2fcb6c32014-12-04 14:52:35 -08001133 @Override
1134 public void onMessageWaitingIndicatorChanged(boolean visible) {
1135 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
1136 mApplication.notificationMgr.updateMwi(this.mSubId, visible);
1137 }
1138
1139 @Override
1140 public void onCallForwardingIndicatorChanged(boolean visible) {
1141 if (VDBG) log("onCallForwardingIndicatorChanged(): " + this.mSubId + " " + visible);
1142 mApplication.notificationMgr.updateCfi(this.mSubId, visible);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001143 }
1144 };
1145
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001146 private void log(String msg) {
1147 Log.d(LOG_TAG, msg);
1148 }
1149}