blob: ebef00db956001f87f5d59c32cfb88838480cb21 [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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070019import com.android.internal.telephony.CallManager;
fionaxu80126972017-02-01 17:01:26 -080020
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import com.android.internal.telephony.Phone;
22import com.android.internal.telephony.PhoneConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaDisplayInfoRec;
24import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaSignalInfoRec;
25import com.android.internal.telephony.cdma.SignalToneUtil;
26
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.bluetooth.BluetoothAdapter;
28import android.bluetooth.BluetoothHeadset;
29import android.bluetooth.BluetoothProfile;
30import android.content.Context;
31import android.media.AudioManager;
32import android.media.ToneGenerator;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.AsyncResult;
34import android.os.Handler;
35import android.os.Message;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.SystemProperties;
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -080037import android.telecom.TelecomManager;
fionaxu80126972017-02-01 17:01:26 -080038
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.telephony.PhoneStateListener;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080040import android.telephony.SubscriptionInfo;
41import android.telephony.SubscriptionManager;
42import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.TelephonyManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080044import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.util.Log;
46
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090047import java.util.ArrayList;
48import java.util.Collections;
49import java.util.Comparator;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080050import java.util.Iterator;
51import java.util.List;
52import java.util.Map;
53
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090054import com.android.internal.telephony.SubscriptionController;
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>();
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090076 private Map<Integer, Boolean> mCFIStatus = new ArrayMap<Integer, Boolean>();
77 private Map<Integer, Boolean> mMWIStatus = new ArrayMap<Integer, Boolean>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078 private PhoneGlobals mApplication;
79 private CallManager mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080 private BluetoothHeadset mBluetoothHeadset;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081
82 // ToneGenerator instance for playing SignalInfo tones
83 private ToneGenerator mSignalInfoToneGenerator;
84
85 // The tone volume relative to other sounds in the stream SignalInfo
86 private static final int TONE_RELATIVE_VOLUME_SIGNALINFO = 80;
87
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 private boolean mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 // Cached AudioManager
91 private AudioManager mAudioManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080092 private SubscriptionManager mSubscriptionManager;
93 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -070094
Brad Ebingera9c6b6d2016-01-07 17:24:16 -080095 // Events from the Phone object:
96 public static final int PHONE_DISCONNECT = 3;
97 public static final int PHONE_STATE_DISPLAYINFO = 6;
98 public static final int PHONE_STATE_SIGNALINFO = 7;
99 public static final int PHONE_ENHANCED_VP_ON = 9;
100 public static final int PHONE_ENHANCED_VP_OFF = 10;
101 public static final int PHONE_SUPP_SERVICE_FAILED = 14;
102 public static final int PHONE_TTY_MODE_RECEIVED = 15;
103 // Events generated internally.
104 // We should store all the possible event type values in one place to make sure that
105 // they don't step on each others' toes.
106 public static final int INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE = 22;
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800107
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530108 public static final int UPDATE_TYPE_MWI = 0;
109 public static final int UPDATE_TYPE_CFI = 1;
110 public static final int UPDATE_TYPE_MWI_CFI = 2;
111
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(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800117 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 synchronized (CallNotifier.class) {
119 if (sInstance == null) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800120 sInstance = new CallNotifier(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 } else {
122 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
123 }
124 return sInstance;
125 }
126 }
127
128 /** Private constructor; @see init() */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800129 private CallNotifier(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800130 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131 mApplication = app;
132 mCM = app.mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
134 mAudioManager = (AudioManager) mApplication.getSystemService(Context.AUDIO_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800135 mTelephonyManager =
136 (TelephonyManager) mApplication.getSystemService(Context.TELEPHONY_SERVICE);
137 mSubscriptionManager = (SubscriptionManager) mApplication.getSystemService(
138 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800140 registerForNotifications();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
143 if (adapter != null) {
144 adapter.getProfileProxy(mApplication.getApplicationContext(),
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800145 mBluetoothProfileServiceListener,
146 BluetoothProfile.HEADSET);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 }
148
Wink Savillef67832f2015-01-12 16:51:50 -0800149 mSubscriptionManager.addOnSubscriptionsChangedListener(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800150 new OnSubscriptionsChangedListener() {
151 @Override
152 public void onSubscriptionsChanged() {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900153 updatePhoneStateListeners(true);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800154 }
155 });
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 }
157
158 private void createSignalInfoToneGenerator() {
159 // Instantiate the ToneGenerator for SignalInfo and CallWaiting
160 // TODO: We probably don't need the mSignalInfoToneGenerator instance
161 // around forever. Need to change it so as to create a ToneGenerator instance only
162 // when a tone is being played and releases it after its done playing.
163 if (mSignalInfoToneGenerator == null) {
164 try {
165 mSignalInfoToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL,
166 TONE_RELATIVE_VOLUME_SIGNALINFO);
167 Log.d(LOG_TAG, "CallNotifier: mSignalInfoToneGenerator created when toneplay");
168 } catch (RuntimeException e) {
169 Log.w(LOG_TAG, "CallNotifier: Exception caught while creating " +
170 "mSignalInfoToneGenerator: " + e);
171 mSignalInfoToneGenerator = null;
172 }
173 } else {
174 Log.d(LOG_TAG, "mSignalInfoToneGenerator created already, hence skipping");
175 }
176 }
177
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800178 /**
179 * Register for call state notifications with the CallManager.
180 */
181 private void registerForNotifications() {
182 mCM.registerForDisconnect(this, PHONE_DISCONNECT, null);
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800183 mCM.registerForDisplayInfo(this, PHONE_STATE_DISPLAYINFO, null);
184 mCM.registerForSignalInfo(this, PHONE_STATE_SIGNALINFO, null);
185 mCM.registerForInCallVoicePrivacyOn(this, PHONE_ENHANCED_VP_ON, null);
186 mCM.registerForInCallVoicePrivacyOff(this, PHONE_ENHANCED_VP_OFF, null);
187 mCM.registerForSuppServiceFailed(this, PHONE_SUPP_SERVICE_FAILED, null);
188 mCM.registerForTtyModeReceived(this, PHONE_TTY_MODE_RECEIVED, null);
189 }
190
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 @Override
192 public void handleMessage(Message msg) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800193 if (DBG) {
194 Log.d(LOG_TAG, "handleMessage(" + msg.what + ")");
195 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 switch (msg.what) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800197 case PHONE_DISCONNECT:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 if (DBG) log("DISCONNECT");
Roshan Pius19f39cf2015-08-12 10:44:38 -0700199 // Stop any signalInfo tone being played when a call gets ended, the rest of the
200 // disconnect functionality in onDisconnect() is handled in ConnectionService.
201 stopSignalInfoTone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 break;
203
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800204 case PHONE_STATE_DISPLAYINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 if (DBG) log("Received PHONE_STATE_DISPLAYINFO event");
206 onDisplayInfo((AsyncResult) msg.obj);
207 break;
208
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800209 case PHONE_STATE_SIGNALINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 if (DBG) log("Received PHONE_STATE_SIGNALINFO event");
211 onSignalInfo((AsyncResult) msg.obj);
212 break;
213
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800214 case INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 if (DBG) log("Received Display Info notification done event ...");
Anthony Leee9468532014-11-15 15:21:00 -0800216 PhoneDisplayMessage.dismissMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 break;
218
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800219 case PHONE_ENHANCED_VP_ON:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 if (DBG) log("PHONE_ENHANCED_VP_ON...");
221 if (!mVoicePrivacyState) {
222 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
223 new InCallTonePlayer(toneToPlay).start();
224 mVoicePrivacyState = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 }
226 break;
227
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800228 case PHONE_ENHANCED_VP_OFF:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 if (DBG) log("PHONE_ENHANCED_VP_OFF...");
230 if (mVoicePrivacyState) {
231 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
232 new InCallTonePlayer(toneToPlay).start();
233 mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234 }
235 break;
236
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800237 case PHONE_SUPP_SERVICE_FAILED:
Anthony Leee9468532014-11-15 15:21:00 -0800238 if (DBG) log("PHONE_SUPP_SERVICE_FAILED...");
239 onSuppServiceFailed((AsyncResult) msg.obj);
240 break;
241
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800242 case PHONE_TTY_MODE_RECEIVED:
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800243 if (DBG) log("Received PHONE_TTY_MODE_RECEIVED event");
244 onTtyModeReceived((AsyncResult) msg.obj);
245 break;
246
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 default:
248 // super.handleMessage(msg);
249 }
250 }
251
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 void updateCallNotifierRegistrationsAfterRadioTechnologyChange() {
253 if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 // Instantiate mSignalInfoToneGenerator
256 createSignalInfoToneGenerator();
257 }
258
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 /**
260 * Resets the audio mode and speaker state when a call ends.
261 */
262 private void resetAudioStateAfterDisconnect() {
263 if (VDBG) log("resetAudioStateAfterDisconnect()...");
264
265 if (mBluetoothHeadset != null) {
266 mBluetoothHeadset.disconnectAudio();
267 }
268
269 // call turnOnSpeaker() with state=false and store=true even if speaker
270 // is already off to reset user requested speaker state.
271 PhoneUtils.turnOnSpeaker(mApplication, false, true);
272
273 PhoneUtils.setAudioMode(mCM);
274 }
275
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277 * Helper class to play tones through the earpiece (or speaker / BT)
278 * during a call, using the ToneGenerator.
279 *
280 * To use, just instantiate a new InCallTonePlayer
281 * (passing in the TONE_* constant for the tone you want)
282 * and start() it.
283 *
284 * When we're done playing the tone, if the phone is idle at that
285 * point, we'll reset the audio routing and speaker state.
286 * (That means that for tones that get played *after* a call
287 * disconnects, like "busy" or "congestion" or "call ended", you
288 * should NOT call resetAudioStateAfterDisconnect() yourself.
289 * Instead, just start the InCallTonePlayer, which will automatically
290 * defer the resetAudioStateAfterDisconnect() call until the tone
291 * finishes playing.)
292 */
293 private class InCallTonePlayer extends Thread {
294 private int mToneId;
295 private int mState;
296 // The possible tones we can play.
297 public static final int TONE_NONE = 0;
298 public static final int TONE_CALL_WAITING = 1;
299 public static final int TONE_BUSY = 2;
300 public static final int TONE_CONGESTION = 3;
301 public static final int TONE_CALL_ENDED = 4;
302 public static final int TONE_VOICE_PRIVACY = 5;
303 public static final int TONE_REORDER = 6;
304 public static final int TONE_INTERCEPT = 7;
305 public static final int TONE_CDMA_DROP = 8;
306 public static final int TONE_OUT_OF_SERVICE = 9;
307 public static final int TONE_REDIAL = 10;
308 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
310
311 // The tone volume relative to other sounds in the stream
312 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
313 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
314 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
315
316 // Buffer time (in msec) to add on to tone timeout value.
317 // Needed mainly when the timeout value for a tone is the
318 // exact duration of the tone itself.
319 static final int TONE_TIMEOUT_BUFFER = 20;
320
321 // The tone state
322 static final int TONE_OFF = 0;
323 static final int TONE_ON = 1;
324 static final int TONE_STOPPED = 2;
325
326 InCallTonePlayer(int toneId) {
327 super();
328 mToneId = toneId;
329 mState = TONE_OFF;
330 }
331
332 @Override
333 public void run() {
334 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
335
336 int toneType = 0; // passed to ToneGenerator.startTone()
337 int toneVolume; // passed to the ToneGenerator constructor
338 int toneLengthMillis;
339 int phoneType = mCM.getFgPhone().getPhoneType();
340
341 switch (mToneId) {
342 case TONE_CALL_WAITING:
343 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
344 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
345 // Call waiting tone is stopped by stopTone() method
346 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
347 break;
348 case TONE_BUSY:
349 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
350 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
351 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
352 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800353 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
354 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700355 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800356 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 toneType = ToneGenerator.TONE_SUP_BUSY;
358 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
359 toneLengthMillis = 4000;
360 } else {
361 throw new IllegalStateException("Unexpected phone type: " + phoneType);
362 }
363 break;
364 case TONE_CONGESTION:
365 toneType = ToneGenerator.TONE_SUP_CONGESTION;
366 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
367 toneLengthMillis = 4000;
368 break;
369
370 case TONE_CALL_ENDED:
371 toneType = ToneGenerator.TONE_PROP_PROMPT;
372 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
373 toneLengthMillis = 200;
374 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 case TONE_VOICE_PRIVACY:
376 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
377 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
378 toneLengthMillis = 5000;
379 break;
380 case TONE_REORDER:
381 toneType = ToneGenerator.TONE_CDMA_REORDER;
382 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
383 toneLengthMillis = 4000;
384 break;
385 case TONE_INTERCEPT:
386 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
387 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
388 toneLengthMillis = 500;
389 break;
390 case TONE_CDMA_DROP:
391 case TONE_OUT_OF_SERVICE:
392 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
393 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
394 toneLengthMillis = 375;
395 break;
396 case TONE_REDIAL:
397 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
398 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
399 toneLengthMillis = 5000;
400 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 case TONE_UNOBTAINABLE_NUMBER:
402 toneType = ToneGenerator.TONE_SUP_ERROR;
403 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
404 toneLengthMillis = 4000;
405 break;
406 default:
407 throw new IllegalArgumentException("Bad toneId: " + mToneId);
408 }
409
410 // If the mToneGenerator creation fails, just continue without it. It is
411 // a local audio signal, and is not as important.
412 ToneGenerator toneGenerator;
413 try {
414 int stream;
415 if (mBluetoothHeadset != null) {
416 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
417 AudioManager.STREAM_VOICE_CALL;
418 } else {
419 stream = AudioManager.STREAM_VOICE_CALL;
420 }
421 toneGenerator = new ToneGenerator(stream, toneVolume);
422 // if (DBG) log("- created toneGenerator: " + toneGenerator);
423 } catch (RuntimeException e) {
424 Log.w(LOG_TAG,
425 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
426 toneGenerator = null;
427 }
428
429 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
430 // CONGESTION tones at least), the ToneGenerator itself knows
431 // the right pattern of tones to play; we do NOT need to
432 // manually start/stop each individual tone, or manually
433 // insert the correct delay between tones. (We just start it
434 // and let it run for however long we want the tone pattern to
435 // continue.)
436 //
437 // TODO: When we stop the ToneGenerator in the middle of a
438 // "tone pattern", it sounds bad if we cut if off while the
439 // tone is actually playing. Consider adding API to the
440 // ToneGenerator to say "stop at the next silent part of the
441 // pattern", or simply "play the pattern N times and then
442 // stop."
443 boolean needToStopTone = true;
444 boolean okToPlayTone = false;
445
446 if (toneGenerator != null) {
447 int ringerMode = mAudioManager.getRingerMode();
448 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
449 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
450 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
451 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
452 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
453 okToPlayTone = true;
454 needToStopTone = false;
455 }
456 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
457 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
458 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
459 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
460 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
461 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
462 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
463 okToPlayTone = true;
464 needToStopTone = false;
465 }
466 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
467 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
468 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
469 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
470 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
471 okToPlayTone = true;
472 needToStopTone = false;
473 }
474 } else { // For the rest of the tones, always OK to play.
475 okToPlayTone = true;
476 }
477 } else { // Not "CDMA"
478 okToPlayTone = true;
479 }
480
481 synchronized (this) {
482 if (okToPlayTone && mState != TONE_STOPPED) {
483 mState = TONE_ON;
484 toneGenerator.startTone(toneType);
485 try {
486 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
487 } catch (InterruptedException e) {
488 Log.w(LOG_TAG,
489 "InCallTonePlayer stopped: " + e);
490 }
491 if (needToStopTone) {
492 toneGenerator.stopTone();
493 }
494 }
495 // if (DBG) log("- InCallTonePlayer: done playing.");
496 toneGenerator.release();
497 mState = TONE_OFF;
498 }
499 }
500
501 // Finally, do the same cleanup we otherwise would have done
502 // in onDisconnect().
503 //
504 // (But watch out: do NOT do this if the phone is in use,
505 // since some of our tones get played *during* a call (like
506 // CALL_WAITING) and we definitely *don't*
507 // want to reset the audio mode / speaker / bluetooth after
508 // playing those!
509 // This call is really here for use with tones that get played
510 // *after* a call disconnects, like "busy" or "congestion" or
511 // "call ended", where the phone has already become idle but
512 // we need to defer the resetAudioStateAfterDisconnect() call
513 // till the tone finishes playing.)
514 if (mCM.getState() == PhoneConstants.State.IDLE) {
515 resetAudioStateAfterDisconnect();
516 }
517 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 }
519
520 /**
521 * Displays a notification when the phone receives a DisplayInfo record.
522 */
523 private void onDisplayInfo(AsyncResult r) {
524 // Extract the DisplayInfo String from the message
525 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
526
527 if (displayInfoRec != null) {
528 String displayInfo = displayInfoRec.alpha;
529 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800530 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531
Anthony Leee9468532014-11-15 15:21:00 -0800532 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800533 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800534 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700535 }
536 }
537
538 /**
Anthony Leee9468532014-11-15 15:21:00 -0800539 * Displays a notification when the phone receives a notice that a supplemental
540 * service has failed.
Anthony Leee9468532014-11-15 15:21:00 -0800541 */
542 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800543 String mergeFailedString = "";
544 if (r.result == Phone.SuppService.CONFERENCE) {
545 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
546 mergeFailedString = mApplication.getResources().getString(
547 R.string.incall_error_supp_service_conference);
548 } else if (r.result == Phone.SuppService.RESUME) {
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700549 if (DBG) log("onSuppServiceFailed: displaying resume failure message");
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800550 mergeFailedString = mApplication.getResources().getString(
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700551 R.string.incall_error_supp_service_resume);
Anju Mathapati4effeb22016-01-25 22:25:09 -0800552 } else if (r.result == Phone.SuppService.HOLD) {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700553 if (DBG) log("onSuppServiceFailed: displaying hold failure message");
Anju Mathapati4effeb22016-01-25 22:25:09 -0800554 mergeFailedString = mApplication.getResources().getString(
555 R.string.incall_error_supp_service_hold);
Tyler Gunna7de7d32017-06-09 16:21:00 -0700556 } else if (r.result == Phone.SuppService.TRANSFER) {
557 if (DBG) log("onSuppServiceFailed: displaying transfer failure message");
558 mergeFailedString = mApplication.getResources().getString(
559 R.string.incall_error_supp_service_transfer);
560 } else if (r.result == Phone.SuppService.SEPARATE) {
561 if (DBG) log("onSuppServiceFailed: displaying separate failure message");
562 mergeFailedString = mApplication.getResources().getString(
563 R.string.incall_error_supp_service_separate);
564 } else if (r.result == Phone.SuppService.SWITCH) {
565 if (DBG) log("onSuppServiceFailed: displaying switch failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530566 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700567 R.string.incall_error_supp_service_switch);
568 } else if (r.result == Phone.SuppService.REJECT) {
569 if (DBG) log("onSuppServiceFailed: displaying reject failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530570 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700571 R.string.incall_error_supp_service_reject);
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530572 } else if (r.result == Phone.SuppService.HANGUP) {
573 mergeFailedString = mApplication.getResources().getString(
574 R.string.incall_error_supp_service_hangup);
575 } else {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700576 if (DBG) log("onSuppServiceFailed: unknown failure");
577 return;
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800578 }
Tyler Gunna7de7d32017-06-09 16:21:00 -0700579
Anthony Leee9468532014-11-15 15:21:00 -0800580 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
581
582 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800583 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800584 SHOW_MESSAGE_NOTIFICATION_TIME);
585 }
586
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900587 public void updatePhoneStateListeners(boolean isRefresh) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530588 updatePhoneStateListeners(isRefresh, UPDATE_TYPE_MWI_CFI,
589 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
590 }
591
592 public void updatePhoneStateListeners(boolean isRefresh, int updateType, int subIdToUpdate) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800593 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
594
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900595 // Sort sub id list based on slot id, so that CFI/MWI notifications will be updated for
596 // slot 0 first then slot 1. This is needed to ensure that when CFI or MWI is enabled for
597 // both slots, user always sees icon related to slot 0 on left side followed by that of
598 // slot 1.
599 List<Integer> subIdList = new ArrayList<Integer>(mPhoneStateListeners.keySet());
600 Collections.sort(subIdList, new Comparator<Integer>() {
601 public int compare(Integer sub1, Integer sub2) {
602 int slotId1 = SubscriptionController.getInstance().getSlotIndex(sub1);
603 int slotId2 = SubscriptionController.getInstance().getSlotIndex(sub2);
604 return slotId1 > slotId2 ? 0 : -1;
605 }
606 });
607
608 for (int subIdCounter = (subIdList.size() - 1); subIdCounter >= 0; subIdCounter--) {
609 int subId = subIdList.get(subIdCounter);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800610 if (subInfos == null || !containsSubId(subInfos, subId)) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900611 Log.d(LOG_TAG, "updatePhoneStateListeners: Hide the outstanding notifications.");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800612 // Hide the outstanding notifications.
613 mApplication.notificationMgr.updateMwi(subId, false);
614 mApplication.notificationMgr.updateCfi(subId, false);
615
616 // Listening to LISTEN_NONE removes the listener.
617 mTelephonyManager.listen(
618 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900619 mPhoneStateListeners.remove(subId);
620 } else {
621 Log.d(LOG_TAG, "updatePhoneStateListeners: update CF notifications.");
622
623 if (mCFIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530624 if ((updateType == UPDATE_TYPE_CFI) && (subId == subIdToUpdate)) {
625 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId),
626 isRefresh);
627 } else {
628 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId), true);
629 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900630 }
631 if (mMWIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530632 if ((updateType == UPDATE_TYPE_MWI) && (subId == subIdToUpdate)) {
633 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId),
634 isRefresh);
635 } else {
636 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId), true);
637 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900638 }
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800639 }
640 }
641
642 if (subInfos == null) {
643 return;
644 }
645
646 // Register new phone listeners for active subscriptions.
647 for (int i = 0; i < subInfos.size(); i++) {
648 int subId = subInfos.get(i).getSubscriptionId();
649 if (!mPhoneStateListeners.containsKey(subId)) {
650 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
651 mTelephonyManager.listen(listener,
652 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
653 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
654 mPhoneStateListeners.put(subId, listener);
655 }
656 }
657 }
658
659 /**
660 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
661 */
662 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
663 if (subInfos == null) {
664 return false;
665 }
666
667 for (int i = 0; i < subInfos.size(); i++) {
668 if (subInfos.get(i).getSubscriptionId() == subId) {
669 return true;
670 }
671 }
672 return false;
673 }
674
Anthony Leee9468532014-11-15 15:21:00 -0800675 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800676 * Displays a notification when the phone receives a notice that TTY mode
677 * has changed on remote end.
678 */
679 private void onTtyModeReceived(AsyncResult r) {
680 if (DBG) log("TtyModeReceived: displaying notification message");
681
682 int resId = 0;
683 switch (((Integer)r.result).intValue()) {
684 case TelecomManager.TTY_MODE_FULL:
685 resId = com.android.internal.R.string.peerTtyModeFull;
686 break;
687 case TelecomManager.TTY_MODE_HCO:
688 resId = com.android.internal.R.string.peerTtyModeHco;
689 break;
690 case TelecomManager.TTY_MODE_VCO:
691 resId = com.android.internal.R.string.peerTtyModeVco;
692 break;
693 case TelecomManager.TTY_MODE_OFF:
694 resId = com.android.internal.R.string.peerTtyModeOff;
695 break;
696 default:
697 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
698 break;
699 }
700 if (resId != 0) {
701 PhoneDisplayMessage.displayNetworkMessage(mApplication,
702 mApplication.getResources().getString(resId));
703
704 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800705 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800706 SHOW_MESSAGE_NOTIFICATION_TIME);
707 }
708 }
709
710 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 * Helper class to play SignalInfo tones using the ToneGenerator.
712 *
713 * To use, just instantiate a new SignalInfoTonePlayer
714 * (passing in the ToneID constant for the tone you want)
715 * and start() it.
716 */
717 private class SignalInfoTonePlayer extends Thread {
718 private int mToneId;
719
720 SignalInfoTonePlayer(int toneId) {
721 super();
722 mToneId = toneId;
723 }
724
725 @Override
726 public void run() {
727 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700728 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 if (mSignalInfoToneGenerator != null) {
730 //First stop any ongoing SignalInfo tone
731 mSignalInfoToneGenerator.stopTone();
732
733 //Start playing the new tone if its a valid tone
734 mSignalInfoToneGenerator.startTone(mToneId);
735 }
736 }
737 }
738
739 /**
740 * Plays a tone when the phone receives a SignalInfo record.
741 */
742 private void onSignalInfo(AsyncResult r) {
743 // Signal Info are totally ignored on non-voice-capable devices.
744 if (!PhoneGlobals.sVoiceCapable) {
745 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
746 return;
747 }
748
749 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
750 // Do not start any new SignalInfo tone when Call state is INCOMING
751 // and stop any previous SignalInfo tone which is being played
752 stopSignalInfoTone();
753 } else {
754 // Extract the SignalInfo String from the message
755 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
756 // Only proceed if a Signal info is present.
757 if (signalInfoRec != null) {
758 boolean isPresent = signalInfoRec.isPresent;
759 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
760 if (isPresent) {// if tone is valid
761 int uSignalType = signalInfoRec.signalType;
762 int uAlertPitch = signalInfoRec.alertPitch;
763 int uSignal = signalInfoRec.signal;
764
765 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
766 uAlertPitch + ", uSignal=" + uSignal);
767 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
768 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
769 (uSignalType, uAlertPitch, uSignal);
770
771 //Create the SignalInfo tone player and pass the ToneID
772 new SignalInfoTonePlayer(toneID).start();
773 }
774 }
775 }
776 }
777
778 /**
779 * Stops a SignalInfo tone in the following condition
780 * 1 - On receiving a New Ringing Call
781 * 2 - On disconnecting a call
782 * 3 - On answering a Call Waiting Call
783 */
784 /* package */ void stopSignalInfoTone() {
785 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
786 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
787 }
788
Santos Cordon5c046722014-09-18 15:41:13 -0700789 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800790 new BluetoothProfile.ServiceListener() {
791 public void onServiceConnected(int profile, BluetoothProfile proxy) {
792 mBluetoothHeadset = (BluetoothHeadset) proxy;
793 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
794 }
795
796 public void onServiceDisconnected(int profile) {
797 mBluetoothHeadset = null;
798 }
799 };
800
801 private class CallNotifierPhoneStateListener extends PhoneStateListener {
802 public CallNotifierPhoneStateListener(int subId) {
803 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 }
805
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800806 @Override
807 public void onMessageWaitingIndicatorChanged(boolean visible) {
808 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900809 mMWIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530810 updatePhoneStateListeners(false, UPDATE_TYPE_MWI, this.mSubId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800811 }
812
813 @Override
814 public void onCallForwardingIndicatorChanged(boolean visible) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700815 Log.i(LOG_TAG, "onCallForwardingIndicatorChanged(): subId=" + this.mSubId
816 + ", visible=" + (visible ? "Y" : "N"));
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900817 mCFIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530818 updatePhoneStateListeners(false, UPDATE_TYPE_CFI, this.mSubId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700819 }
820 };
821
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700822 private void log(String msg) {
823 Log.d(LOG_TAG, msg);
824 }
825}