blob: cfffb940f4de7fbbe3e1d1b0e53fcfa490499416 [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
71 /** The singleton instance. */
72 private static CallNotifier sInstance;
73
Andrew Lee2fcb6c32014-12-04 14:52:35 -080074 private Map<Integer, CallNotifierPhoneStateListener> mPhoneStateListeners =
75 new ArrayMap<Integer, CallNotifierPhoneStateListener>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077 private PhoneGlobals mApplication;
78 private CallManager mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079 private BluetoothHeadset mBluetoothHeadset;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080
81 // ToneGenerator instance for playing SignalInfo tones
82 private ToneGenerator mSignalInfoToneGenerator;
83
84 // The tone volume relative to other sounds in the stream SignalInfo
85 private static final int TONE_RELATIVE_VOLUME_SIGNALINFO = 80;
86
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087 private boolean mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089 // Cached AudioManager
90 private AudioManager mAudioManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080091 private SubscriptionManager mSubscriptionManager;
92 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -070093
Brad Ebingera9c6b6d2016-01-07 17:24:16 -080094 // Events from the Phone object:
95 public static final int PHONE_DISCONNECT = 3;
96 public static final int PHONE_STATE_DISPLAYINFO = 6;
97 public static final int PHONE_STATE_SIGNALINFO = 7;
98 public static final int PHONE_ENHANCED_VP_ON = 9;
99 public static final int PHONE_ENHANCED_VP_OFF = 10;
100 public static final int PHONE_SUPP_SERVICE_FAILED = 14;
101 public static final int PHONE_TTY_MODE_RECEIVED = 15;
102 // Events generated internally.
103 // We should store all the possible event type values in one place to make sure that
104 // they don't step on each others' toes.
105 public static final int INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE = 22;
106 // Other events from call manager
107 public static final int EVENT_OTA_PROVISION_CHANGE = 20;
108
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 /**
110 * Initialize the singleton CallNotifier instance.
111 * This is only done once, at startup, from PhoneApp.onCreate().
112 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800113 /* package */ static CallNotifier init(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800114 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 synchronized (CallNotifier.class) {
116 if (sInstance == null) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800117 sInstance = new CallNotifier(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 } else {
119 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
120 }
121 return sInstance;
122 }
123 }
124
125 /** Private constructor; @see init() */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800126 private CallNotifier(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800127 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 mApplication = app;
129 mCM = app.mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130
131 mAudioManager = (AudioManager) mApplication.getSystemService(Context.AUDIO_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800132 mTelephonyManager =
133 (TelephonyManager) mApplication.getSystemService(Context.TELEPHONY_SERVICE);
134 mSubscriptionManager = (SubscriptionManager) mApplication.getSystemService(
135 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800137 registerForNotifications();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
140 if (adapter != null) {
141 adapter.getProfileProxy(mApplication.getApplicationContext(),
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800142 mBluetoothProfileServiceListener,
143 BluetoothProfile.HEADSET);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144 }
145
Wink Savillef67832f2015-01-12 16:51:50 -0800146 mSubscriptionManager.addOnSubscriptionsChangedListener(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800147 new OnSubscriptionsChangedListener() {
148 @Override
149 public void onSubscriptionsChanged() {
150 updatePhoneStateListeners();
151 }
152 });
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 }
154
155 private void createSignalInfoToneGenerator() {
156 // Instantiate the ToneGenerator for SignalInfo and CallWaiting
157 // TODO: We probably don't need the mSignalInfoToneGenerator instance
158 // around forever. Need to change it so as to create a ToneGenerator instance only
159 // when a tone is being played and releases it after its done playing.
160 if (mSignalInfoToneGenerator == null) {
161 try {
162 mSignalInfoToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL,
163 TONE_RELATIVE_VOLUME_SIGNALINFO);
164 Log.d(LOG_TAG, "CallNotifier: mSignalInfoToneGenerator created when toneplay");
165 } catch (RuntimeException e) {
166 Log.w(LOG_TAG, "CallNotifier: Exception caught while creating " +
167 "mSignalInfoToneGenerator: " + e);
168 mSignalInfoToneGenerator = null;
169 }
170 } else {
171 Log.d(LOG_TAG, "mSignalInfoToneGenerator created already, hence skipping");
172 }
173 }
174
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800175 /**
176 * Register for call state notifications with the CallManager.
177 */
178 private void registerForNotifications() {
179 mCM.registerForDisconnect(this, PHONE_DISCONNECT, null);
180 mCM.registerForCdmaOtaStatusChange(this, EVENT_OTA_PROVISION_CHANGE, null);
181 mCM.registerForDisplayInfo(this, PHONE_STATE_DISPLAYINFO, null);
182 mCM.registerForSignalInfo(this, PHONE_STATE_SIGNALINFO, null);
183 mCM.registerForInCallVoicePrivacyOn(this, PHONE_ENHANCED_VP_ON, null);
184 mCM.registerForInCallVoicePrivacyOff(this, PHONE_ENHANCED_VP_OFF, null);
185 mCM.registerForSuppServiceFailed(this, PHONE_SUPP_SERVICE_FAILED, null);
186 mCM.registerForTtyModeReceived(this, PHONE_TTY_MODE_RECEIVED, null);
187 }
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 @Override
190 public void handleMessage(Message msg) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800191 if (DBG) {
192 Log.d(LOG_TAG, "handleMessage(" + msg.what + ")");
193 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 switch (msg.what) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800195 case PHONE_DISCONNECT:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 if (DBG) log("DISCONNECT");
Roshan Pius19f39cf2015-08-12 10:44:38 -0700197 // Stop any signalInfo tone being played when a call gets ended, the rest of the
198 // disconnect functionality in onDisconnect() is handled in ConnectionService.
199 stopSignalInfoTone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 break;
201
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800202 case PHONE_STATE_DISPLAYINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 if (DBG) log("Received PHONE_STATE_DISPLAYINFO event");
204 onDisplayInfo((AsyncResult) msg.obj);
205 break;
206
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800207 case PHONE_STATE_SIGNALINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 if (DBG) log("Received PHONE_STATE_SIGNALINFO event");
209 onSignalInfo((AsyncResult) msg.obj);
210 break;
211
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800212 case INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 if (DBG) log("Received Display Info notification done event ...");
Anthony Leee9468532014-11-15 15:21:00 -0800214 PhoneDisplayMessage.dismissMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 break;
216
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800217 case EVENT_OTA_PROVISION_CHANGE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 if (DBG) log("EVENT_OTA_PROVISION_CHANGE...");
219 mApplication.handleOtaspEvent(msg);
220 break;
221
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800222 case PHONE_ENHANCED_VP_ON:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 if (DBG) log("PHONE_ENHANCED_VP_ON...");
224 if (!mVoicePrivacyState) {
225 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
226 new InCallTonePlayer(toneToPlay).start();
227 mVoicePrivacyState = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 }
229 break;
230
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800231 case PHONE_ENHANCED_VP_OFF:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 if (DBG) log("PHONE_ENHANCED_VP_OFF...");
233 if (mVoicePrivacyState) {
234 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
235 new InCallTonePlayer(toneToPlay).start();
236 mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 }
238 break;
239
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800240 case PHONE_SUPP_SERVICE_FAILED:
Anthony Leee9468532014-11-15 15:21:00 -0800241 if (DBG) log("PHONE_SUPP_SERVICE_FAILED...");
242 onSuppServiceFailed((AsyncResult) msg.obj);
243 break;
244
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800245 case PHONE_TTY_MODE_RECEIVED:
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800246 if (DBG) log("Received PHONE_TTY_MODE_RECEIVED event");
247 onTtyModeReceived((AsyncResult) msg.obj);
248 break;
249
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 default:
251 // super.handleMessage(msg);
252 }
253 }
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 void updateCallNotifierRegistrationsAfterRadioTechnologyChange() {
256 if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 // Instantiate mSignalInfoToneGenerator
259 createSignalInfoToneGenerator();
260 }
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 /**
263 * Resets the audio mode and speaker state when a call ends.
264 */
265 private void resetAudioStateAfterDisconnect() {
266 if (VDBG) log("resetAudioStateAfterDisconnect()...");
267
268 if (mBluetoothHeadset != null) {
269 mBluetoothHeadset.disconnectAudio();
270 }
271
272 // call turnOnSpeaker() with state=false and store=true even if speaker
273 // is already off to reset user requested speaker state.
274 PhoneUtils.turnOnSpeaker(mApplication, false, true);
275
276 PhoneUtils.setAudioMode(mCM);
277 }
278
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 * Helper class to play tones through the earpiece (or speaker / BT)
281 * during a call, using the ToneGenerator.
282 *
283 * To use, just instantiate a new InCallTonePlayer
284 * (passing in the TONE_* constant for the tone you want)
285 * and start() it.
286 *
287 * When we're done playing the tone, if the phone is idle at that
288 * point, we'll reset the audio routing and speaker state.
289 * (That means that for tones that get played *after* a call
290 * disconnects, like "busy" or "congestion" or "call ended", you
291 * should NOT call resetAudioStateAfterDisconnect() yourself.
292 * Instead, just start the InCallTonePlayer, which will automatically
293 * defer the resetAudioStateAfterDisconnect() call until the tone
294 * finishes playing.)
295 */
296 private class InCallTonePlayer extends Thread {
297 private int mToneId;
298 private int mState;
299 // The possible tones we can play.
300 public static final int TONE_NONE = 0;
301 public static final int TONE_CALL_WAITING = 1;
302 public static final int TONE_BUSY = 2;
303 public static final int TONE_CONGESTION = 3;
304 public static final int TONE_CALL_ENDED = 4;
305 public static final int TONE_VOICE_PRIVACY = 5;
306 public static final int TONE_REORDER = 6;
307 public static final int TONE_INTERCEPT = 7;
308 public static final int TONE_CDMA_DROP = 8;
309 public static final int TONE_OUT_OF_SERVICE = 9;
310 public static final int TONE_REDIAL = 10;
311 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
313
314 // The tone volume relative to other sounds in the stream
315 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
316 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
317 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
318
319 // Buffer time (in msec) to add on to tone timeout value.
320 // Needed mainly when the timeout value for a tone is the
321 // exact duration of the tone itself.
322 static final int TONE_TIMEOUT_BUFFER = 20;
323
324 // The tone state
325 static final int TONE_OFF = 0;
326 static final int TONE_ON = 1;
327 static final int TONE_STOPPED = 2;
328
329 InCallTonePlayer(int toneId) {
330 super();
331 mToneId = toneId;
332 mState = TONE_OFF;
333 }
334
335 @Override
336 public void run() {
337 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
338
339 int toneType = 0; // passed to ToneGenerator.startTone()
340 int toneVolume; // passed to the ToneGenerator constructor
341 int toneLengthMillis;
342 int phoneType = mCM.getFgPhone().getPhoneType();
343
344 switch (mToneId) {
345 case TONE_CALL_WAITING:
346 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
347 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
348 // Call waiting tone is stopped by stopTone() method
349 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
350 break;
351 case TONE_BUSY:
352 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
353 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
354 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
355 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800356 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
357 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700358 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800359 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 toneType = ToneGenerator.TONE_SUP_BUSY;
361 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
362 toneLengthMillis = 4000;
363 } else {
364 throw new IllegalStateException("Unexpected phone type: " + phoneType);
365 }
366 break;
367 case TONE_CONGESTION:
368 toneType = ToneGenerator.TONE_SUP_CONGESTION;
369 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
370 toneLengthMillis = 4000;
371 break;
372
373 case TONE_CALL_ENDED:
374 toneType = ToneGenerator.TONE_PROP_PROMPT;
375 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
376 toneLengthMillis = 200;
377 break;
378 case TONE_OTA_CALL_END:
379 if (mApplication.cdmaOtaConfigData.otaPlaySuccessFailureTone ==
380 OtaUtils.OTA_PLAY_SUCCESS_FAILURE_TONE_ON) {
381 toneType = ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD;
382 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
383 toneLengthMillis = 750;
384 } else {
385 toneType = ToneGenerator.TONE_PROP_PROMPT;
386 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
387 toneLengthMillis = 200;
388 }
389 break;
390 case TONE_VOICE_PRIVACY:
391 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
392 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
393 toneLengthMillis = 5000;
394 break;
395 case TONE_REORDER:
396 toneType = ToneGenerator.TONE_CDMA_REORDER;
397 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
398 toneLengthMillis = 4000;
399 break;
400 case TONE_INTERCEPT:
401 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
402 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
403 toneLengthMillis = 500;
404 break;
405 case TONE_CDMA_DROP:
406 case TONE_OUT_OF_SERVICE:
407 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
408 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
409 toneLengthMillis = 375;
410 break;
411 case TONE_REDIAL:
412 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
413 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
414 toneLengthMillis = 5000;
415 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 case TONE_UNOBTAINABLE_NUMBER:
417 toneType = ToneGenerator.TONE_SUP_ERROR;
418 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
419 toneLengthMillis = 4000;
420 break;
421 default:
422 throw new IllegalArgumentException("Bad toneId: " + mToneId);
423 }
424
425 // If the mToneGenerator creation fails, just continue without it. It is
426 // a local audio signal, and is not as important.
427 ToneGenerator toneGenerator;
428 try {
429 int stream;
430 if (mBluetoothHeadset != null) {
431 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
432 AudioManager.STREAM_VOICE_CALL;
433 } else {
434 stream = AudioManager.STREAM_VOICE_CALL;
435 }
436 toneGenerator = new ToneGenerator(stream, toneVolume);
437 // if (DBG) log("- created toneGenerator: " + toneGenerator);
438 } catch (RuntimeException e) {
439 Log.w(LOG_TAG,
440 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
441 toneGenerator = null;
442 }
443
444 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
445 // CONGESTION tones at least), the ToneGenerator itself knows
446 // the right pattern of tones to play; we do NOT need to
447 // manually start/stop each individual tone, or manually
448 // insert the correct delay between tones. (We just start it
449 // and let it run for however long we want the tone pattern to
450 // continue.)
451 //
452 // TODO: When we stop the ToneGenerator in the middle of a
453 // "tone pattern", it sounds bad if we cut if off while the
454 // tone is actually playing. Consider adding API to the
455 // ToneGenerator to say "stop at the next silent part of the
456 // pattern", or simply "play the pattern N times and then
457 // stop."
458 boolean needToStopTone = true;
459 boolean okToPlayTone = false;
460
461 if (toneGenerator != null) {
462 int ringerMode = mAudioManager.getRingerMode();
463 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
464 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
465 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
466 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
467 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
468 okToPlayTone = true;
469 needToStopTone = false;
470 }
471 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
472 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
473 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
474 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
475 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
476 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
477 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
478 okToPlayTone = true;
479 needToStopTone = false;
480 }
481 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
482 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
483 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
484 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
485 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
486 okToPlayTone = true;
487 needToStopTone = false;
488 }
489 } else { // For the rest of the tones, always OK to play.
490 okToPlayTone = true;
491 }
492 } else { // Not "CDMA"
493 okToPlayTone = true;
494 }
495
496 synchronized (this) {
497 if (okToPlayTone && mState != TONE_STOPPED) {
498 mState = TONE_ON;
499 toneGenerator.startTone(toneType);
500 try {
501 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
502 } catch (InterruptedException e) {
503 Log.w(LOG_TAG,
504 "InCallTonePlayer stopped: " + e);
505 }
506 if (needToStopTone) {
507 toneGenerator.stopTone();
508 }
509 }
510 // if (DBG) log("- InCallTonePlayer: done playing.");
511 toneGenerator.release();
512 mState = TONE_OFF;
513 }
514 }
515
516 // Finally, do the same cleanup we otherwise would have done
517 // in onDisconnect().
518 //
519 // (But watch out: do NOT do this if the phone is in use,
520 // since some of our tones get played *during* a call (like
521 // CALL_WAITING) and we definitely *don't*
522 // want to reset the audio mode / speaker / bluetooth after
523 // playing those!
524 // This call is really here for use with tones that get played
525 // *after* a call disconnects, like "busy" or "congestion" or
526 // "call ended", where the phone has already become idle but
527 // we need to defer the resetAudioStateAfterDisconnect() call
528 // till the tone finishes playing.)
529 if (mCM.getState() == PhoneConstants.State.IDLE) {
530 resetAudioStateAfterDisconnect();
531 }
532 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 }
534
535 /**
536 * Displays a notification when the phone receives a DisplayInfo record.
537 */
538 private void onDisplayInfo(AsyncResult r) {
539 // Extract the DisplayInfo String from the message
540 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
541
542 if (displayInfoRec != null) {
543 String displayInfo = displayInfoRec.alpha;
544 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800545 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546
Anthony Leee9468532014-11-15 15:21:00 -0800547 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800548 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800549 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700550 }
551 }
552
553 /**
Anthony Leee9468532014-11-15 15:21:00 -0800554 * Displays a notification when the phone receives a notice that a supplemental
555 * service has failed.
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800556 * TODO: This is a NOOP if it isn't for conferences or resuming call failures right now.
Anthony Leee9468532014-11-15 15:21:00 -0800557 */
558 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800559 if (r.result != Phone.SuppService.CONFERENCE && r.result != Phone.SuppService.RESUME) {
560 if (DBG) log("onSuppServiceFailed: not a merge or resume failure event");
Anthony Leee9468532014-11-15 15:21:00 -0800561 return;
562 }
563
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800564 String mergeFailedString = "";
565 if (r.result == Phone.SuppService.CONFERENCE) {
566 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
567 mergeFailedString = mApplication.getResources().getString(
568 R.string.incall_error_supp_service_conference);
569 } else if (r.result == Phone.SuppService.RESUME) {
570 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
571 mergeFailedString = mApplication.getResources().getString(
572 R.string.incall_error_supp_service_switch);
573 }
Anthony Leee9468532014-11-15 15:21:00 -0800574 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
575
576 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800577 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800578 SHOW_MESSAGE_NOTIFICATION_TIME);
579 }
580
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800581 public void updatePhoneStateListeners() {
582 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
583
584 // Unregister phone listeners for inactive subscriptions.
585 Iterator<Integer> itr = mPhoneStateListeners.keySet().iterator();
586 while (itr.hasNext()) {
587 int subId = itr.next();
588 if (subInfos == null || !containsSubId(subInfos, subId)) {
589 // Hide the outstanding notifications.
590 mApplication.notificationMgr.updateMwi(subId, false);
591 mApplication.notificationMgr.updateCfi(subId, false);
592
593 // Listening to LISTEN_NONE removes the listener.
594 mTelephonyManager.listen(
595 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
596 itr.remove();
597 }
598 }
599
600 if (subInfos == null) {
601 return;
602 }
603
604 // Register new phone listeners for active subscriptions.
605 for (int i = 0; i < subInfos.size(); i++) {
606 int subId = subInfos.get(i).getSubscriptionId();
607 if (!mPhoneStateListeners.containsKey(subId)) {
608 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
609 mTelephonyManager.listen(listener,
610 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
611 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
612 mPhoneStateListeners.put(subId, listener);
613 }
614 }
615 }
616
617 /**
618 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
619 */
620 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
621 if (subInfos == null) {
622 return false;
623 }
624
625 for (int i = 0; i < subInfos.size(); i++) {
626 if (subInfos.get(i).getSubscriptionId() == subId) {
627 return true;
628 }
629 }
630 return false;
631 }
632
Anthony Leee9468532014-11-15 15:21:00 -0800633 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800634 * Displays a notification when the phone receives a notice that TTY mode
635 * has changed on remote end.
636 */
637 private void onTtyModeReceived(AsyncResult r) {
638 if (DBG) log("TtyModeReceived: displaying notification message");
639
640 int resId = 0;
641 switch (((Integer)r.result).intValue()) {
642 case TelecomManager.TTY_MODE_FULL:
643 resId = com.android.internal.R.string.peerTtyModeFull;
644 break;
645 case TelecomManager.TTY_MODE_HCO:
646 resId = com.android.internal.R.string.peerTtyModeHco;
647 break;
648 case TelecomManager.TTY_MODE_VCO:
649 resId = com.android.internal.R.string.peerTtyModeVco;
650 break;
651 case TelecomManager.TTY_MODE_OFF:
652 resId = com.android.internal.R.string.peerTtyModeOff;
653 break;
654 default:
655 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
656 break;
657 }
658 if (resId != 0) {
659 PhoneDisplayMessage.displayNetworkMessage(mApplication,
660 mApplication.getResources().getString(resId));
661
662 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800663 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800664 SHOW_MESSAGE_NOTIFICATION_TIME);
665 }
666 }
667
668 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 * Helper class to play SignalInfo tones using the ToneGenerator.
670 *
671 * To use, just instantiate a new SignalInfoTonePlayer
672 * (passing in the ToneID constant for the tone you want)
673 * and start() it.
674 */
675 private class SignalInfoTonePlayer extends Thread {
676 private int mToneId;
677
678 SignalInfoTonePlayer(int toneId) {
679 super();
680 mToneId = toneId;
681 }
682
683 @Override
684 public void run() {
685 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700686 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700687 if (mSignalInfoToneGenerator != null) {
688 //First stop any ongoing SignalInfo tone
689 mSignalInfoToneGenerator.stopTone();
690
691 //Start playing the new tone if its a valid tone
692 mSignalInfoToneGenerator.startTone(mToneId);
693 }
694 }
695 }
696
697 /**
698 * Plays a tone when the phone receives a SignalInfo record.
699 */
700 private void onSignalInfo(AsyncResult r) {
701 // Signal Info are totally ignored on non-voice-capable devices.
702 if (!PhoneGlobals.sVoiceCapable) {
703 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
704 return;
705 }
706
707 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
708 // Do not start any new SignalInfo tone when Call state is INCOMING
709 // and stop any previous SignalInfo tone which is being played
710 stopSignalInfoTone();
711 } else {
712 // Extract the SignalInfo String from the message
713 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
714 // Only proceed if a Signal info is present.
715 if (signalInfoRec != null) {
716 boolean isPresent = signalInfoRec.isPresent;
717 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
718 if (isPresent) {// if tone is valid
719 int uSignalType = signalInfoRec.signalType;
720 int uAlertPitch = signalInfoRec.alertPitch;
721 int uSignal = signalInfoRec.signal;
722
723 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
724 uAlertPitch + ", uSignal=" + uSignal);
725 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
726 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
727 (uSignalType, uAlertPitch, uSignal);
728
729 //Create the SignalInfo tone player and pass the ToneID
730 new SignalInfoTonePlayer(toneID).start();
731 }
732 }
733 }
734 }
735
736 /**
737 * Stops a SignalInfo tone in the following condition
738 * 1 - On receiving a New Ringing Call
739 * 2 - On disconnecting a call
740 * 3 - On answering a Call Waiting Call
741 */
742 /* package */ void stopSignalInfoTone() {
743 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
744 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
745 }
746
Santos Cordon5c046722014-09-18 15:41:13 -0700747 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800748 new BluetoothProfile.ServiceListener() {
749 public void onServiceConnected(int profile, BluetoothProfile proxy) {
750 mBluetoothHeadset = (BluetoothHeadset) proxy;
751 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
752 }
753
754 public void onServiceDisconnected(int profile) {
755 mBluetoothHeadset = null;
756 }
757 };
758
759 private class CallNotifierPhoneStateListener extends PhoneStateListener {
760 public CallNotifierPhoneStateListener(int subId) {
761 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 }
763
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800764 @Override
765 public void onMessageWaitingIndicatorChanged(boolean visible) {
766 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
767 mApplication.notificationMgr.updateMwi(this.mSubId, visible);
768 }
769
770 @Override
771 public void onCallForwardingIndicatorChanged(boolean visible) {
772 if (VDBG) log("onCallForwardingIndicatorChanged(): " + this.mSubId + " " + visible);
773 mApplication.notificationMgr.updateCfi(this.mSubId, visible);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774 }
775 };
776
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 private void log(String msg) {
778 Log.d(LOG_TAG, msg);
779 }
780}