blob: 0af1b23dd25966ae6e9b848edb1342d53ffb5bde [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 android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothHeadset;
21import android.bluetooth.BluetoothProfile;
22import android.content.Context;
23import android.media.AudioManager;
24import android.media.ToneGenerator;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.os.AsyncResult;
26import android.os.Handler;
27import android.os.Message;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.os.SystemProperties;
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -080029import android.telecom.TelecomManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.telephony.PhoneStateListener;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080031import android.telephony.SubscriptionInfo;
32import android.telephony.SubscriptionManager;
33import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.telephony.TelephonyManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080035import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.util.Log;
37
Aravind Sreekumarafc08c52018-04-10 15:34:32 -070038import com.android.internal.telephony.CallManager;
39import com.android.internal.telephony.Phone;
40import com.android.internal.telephony.PhoneConstants;
41import com.android.internal.telephony.SubscriptionController;
42import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaDisplayInfoRec;
43import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaSignalInfoRec;
44import com.android.internal.telephony.cdma.SignalToneUtil;
45
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090046import java.util.ArrayList;
47import java.util.Collections;
48import java.util.Comparator;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080049import java.util.List;
50import java.util.Map;
51
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052/**
53 * Phone app module that listens for phone state changes and various other
54 * events from the telephony layer, and triggers any resulting UI behavior
Santos Cordon5422a8d2014-09-12 04:20:56 -070055 * (like starting the Incoming Call UI, playing in-call tones,
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056 * updating notifications, writing call log entries, etc.)
57 */
Santos Cordon5422a8d2014-09-12 04:20:56 -070058public class CallNotifier extends Handler {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059 private static final String LOG_TAG = "CallNotifier";
60 private static final boolean DBG =
61 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
62 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
63
Anthony Leee9468532014-11-15 15:21:00 -080064 // Time to display the message from the underlying phone layers.
65 private static final int SHOW_MESSAGE_NOTIFICATION_TIME = 3000; // msec
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066
67 /** The singleton instance. */
68 private static CallNotifier sInstance;
69
Andrew Lee2fcb6c32014-12-04 14:52:35 -080070 private Map<Integer, CallNotifierPhoneStateListener> mPhoneStateListeners =
71 new ArrayMap<Integer, CallNotifierPhoneStateListener>();
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090072 private Map<Integer, Boolean> mCFIStatus = new ArrayMap<Integer, Boolean>();
73 private Map<Integer, Boolean> mMWIStatus = new ArrayMap<Integer, Boolean>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074 private PhoneGlobals mApplication;
75 private CallManager mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 private BluetoothHeadset mBluetoothHeadset;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
78 // ToneGenerator instance for playing SignalInfo tones
79 private ToneGenerator mSignalInfoToneGenerator;
80
81 // The tone volume relative to other sounds in the stream SignalInfo
82 private static final int TONE_RELATIVE_VOLUME_SIGNALINFO = 80;
83
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 private boolean mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086 // Cached AudioManager
87 private AudioManager mAudioManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080088 private SubscriptionManager mSubscriptionManager;
89 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -070090
Brad Ebingera9c6b6d2016-01-07 17:24:16 -080091 // Events from the Phone object:
92 public static final int PHONE_DISCONNECT = 3;
93 public static final int PHONE_STATE_DISPLAYINFO = 6;
94 public static final int PHONE_STATE_SIGNALINFO = 7;
95 public static final int PHONE_ENHANCED_VP_ON = 9;
96 public static final int PHONE_ENHANCED_VP_OFF = 10;
97 public static final int PHONE_SUPP_SERVICE_FAILED = 14;
98 public static final int PHONE_TTY_MODE_RECEIVED = 15;
99 // Events generated internally.
100 // We should store all the possible event type values in one place to make sure that
101 // they don't step on each others' toes.
102 public static final int INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE = 22;
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800103
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530104 public static final int UPDATE_TYPE_MWI = 0;
105 public static final int UPDATE_TYPE_CFI = 1;
106 public static final int UPDATE_TYPE_MWI_CFI = 2;
107
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 /**
109 * Initialize the singleton CallNotifier instance.
110 * This is only done once, at startup, from PhoneApp.onCreate().
111 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800112 /* package */ static CallNotifier init(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800113 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 synchronized (CallNotifier.class) {
115 if (sInstance == null) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800116 sInstance = new CallNotifier(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117 } else {
118 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
119 }
120 return sInstance;
121 }
122 }
123
124 /** Private constructor; @see init() */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800125 private CallNotifier(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800126 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 mApplication = app;
128 mCM = app.mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129
130 mAudioManager = (AudioManager) mApplication.getSystemService(Context.AUDIO_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800131 mTelephonyManager =
132 (TelephonyManager) mApplication.getSystemService(Context.TELEPHONY_SERVICE);
133 mSubscriptionManager = (SubscriptionManager) mApplication.getSystemService(
134 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800136 registerForNotifications();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
139 if (adapter != null) {
140 adapter.getProfileProxy(mApplication.getApplicationContext(),
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800141 mBluetoothProfileServiceListener,
142 BluetoothProfile.HEADSET);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 }
144
Wink Savillef67832f2015-01-12 16:51:50 -0800145 mSubscriptionManager.addOnSubscriptionsChangedListener(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800146 new OnSubscriptionsChangedListener() {
147 @Override
148 public void onSubscriptionsChanged() {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900149 updatePhoneStateListeners(true);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800150 }
151 });
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 }
153
154 private void createSignalInfoToneGenerator() {
155 // Instantiate the ToneGenerator for SignalInfo and CallWaiting
156 // TODO: We probably don't need the mSignalInfoToneGenerator instance
157 // around forever. Need to change it so as to create a ToneGenerator instance only
158 // when a tone is being played and releases it after its done playing.
159 if (mSignalInfoToneGenerator == null) {
160 try {
161 mSignalInfoToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL,
162 TONE_RELATIVE_VOLUME_SIGNALINFO);
163 Log.d(LOG_TAG, "CallNotifier: mSignalInfoToneGenerator created when toneplay");
164 } catch (RuntimeException e) {
165 Log.w(LOG_TAG, "CallNotifier: Exception caught while creating " +
166 "mSignalInfoToneGenerator: " + e);
167 mSignalInfoToneGenerator = null;
168 }
169 } else {
170 Log.d(LOG_TAG, "mSignalInfoToneGenerator created already, hence skipping");
171 }
172 }
173
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800174 /**
175 * Register for call state notifications with the CallManager.
176 */
177 private void registerForNotifications() {
178 mCM.registerForDisconnect(this, PHONE_DISCONNECT, null);
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800179 mCM.registerForDisplayInfo(this, PHONE_STATE_DISPLAYINFO, null);
180 mCM.registerForSignalInfo(this, PHONE_STATE_SIGNALINFO, null);
181 mCM.registerForInCallVoicePrivacyOn(this, PHONE_ENHANCED_VP_ON, null);
182 mCM.registerForInCallVoicePrivacyOff(this, PHONE_ENHANCED_VP_OFF, null);
183 mCM.registerForSuppServiceFailed(this, PHONE_SUPP_SERVICE_FAILED, null);
184 mCM.registerForTtyModeReceived(this, PHONE_TTY_MODE_RECEIVED, null);
185 }
186
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 @Override
188 public void handleMessage(Message msg) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800189 if (DBG) {
190 Log.d(LOG_TAG, "handleMessage(" + msg.what + ")");
191 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 switch (msg.what) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800193 case PHONE_DISCONNECT:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 if (DBG) log("DISCONNECT");
Roshan Pius19f39cf2015-08-12 10:44:38 -0700195 // Stop any signalInfo tone being played when a call gets ended, the rest of the
196 // disconnect functionality in onDisconnect() is handled in ConnectionService.
197 stopSignalInfoTone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 break;
199
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800200 case PHONE_STATE_DISPLAYINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 if (DBG) log("Received PHONE_STATE_DISPLAYINFO event");
202 onDisplayInfo((AsyncResult) msg.obj);
203 break;
204
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800205 case PHONE_STATE_SIGNALINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 if (DBG) log("Received PHONE_STATE_SIGNALINFO event");
207 onSignalInfo((AsyncResult) msg.obj);
208 break;
209
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800210 case INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 if (DBG) log("Received Display Info notification done event ...");
Anthony Leee9468532014-11-15 15:21:00 -0800212 PhoneDisplayMessage.dismissMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 break;
214
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800215 case PHONE_ENHANCED_VP_ON:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 if (DBG) log("PHONE_ENHANCED_VP_ON...");
217 if (!mVoicePrivacyState) {
218 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
219 new InCallTonePlayer(toneToPlay).start();
220 mVoicePrivacyState = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 }
222 break;
223
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800224 case PHONE_ENHANCED_VP_OFF:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 if (DBG) log("PHONE_ENHANCED_VP_OFF...");
226 if (mVoicePrivacyState) {
227 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
228 new InCallTonePlayer(toneToPlay).start();
229 mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 }
231 break;
232
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800233 case PHONE_SUPP_SERVICE_FAILED:
Anthony Leee9468532014-11-15 15:21:00 -0800234 if (DBG) log("PHONE_SUPP_SERVICE_FAILED...");
235 onSuppServiceFailed((AsyncResult) msg.obj);
236 break;
237
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800238 case PHONE_TTY_MODE_RECEIVED:
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800239 if (DBG) log("Received PHONE_TTY_MODE_RECEIVED event");
240 onTtyModeReceived((AsyncResult) msg.obj);
241 break;
242
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 default:
244 // super.handleMessage(msg);
245 }
246 }
247
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 void updateCallNotifierRegistrationsAfterRadioTechnologyChange() {
249 if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
250
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 // Instantiate mSignalInfoToneGenerator
252 createSignalInfoToneGenerator();
253 }
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256 * Helper class to play tones through the earpiece (or speaker / BT)
257 * during a call, using the ToneGenerator.
258 *
259 * To use, just instantiate a new InCallTonePlayer
260 * (passing in the TONE_* constant for the tone you want)
261 * and start() it.
262 *
263 * When we're done playing the tone, if the phone is idle at that
264 * point, we'll reset the audio routing and speaker state.
265 * (That means that for tones that get played *after* a call
266 * disconnects, like "busy" or "congestion" or "call ended", you
267 * should NOT call resetAudioStateAfterDisconnect() yourself.
268 * Instead, just start the InCallTonePlayer, which will automatically
269 * defer the resetAudioStateAfterDisconnect() call until the tone
270 * finishes playing.)
271 */
272 private class InCallTonePlayer extends Thread {
273 private int mToneId;
274 private int mState;
275 // The possible tones we can play.
276 public static final int TONE_NONE = 0;
277 public static final int TONE_CALL_WAITING = 1;
278 public static final int TONE_BUSY = 2;
279 public static final int TONE_CONGESTION = 3;
280 public static final int TONE_CALL_ENDED = 4;
281 public static final int TONE_VOICE_PRIVACY = 5;
282 public static final int TONE_REORDER = 6;
283 public static final int TONE_INTERCEPT = 7;
284 public static final int TONE_CDMA_DROP = 8;
285 public static final int TONE_OUT_OF_SERVICE = 9;
286 public static final int TONE_REDIAL = 10;
287 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
289
290 // The tone volume relative to other sounds in the stream
291 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
292 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
293 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
294
295 // Buffer time (in msec) to add on to tone timeout value.
296 // Needed mainly when the timeout value for a tone is the
297 // exact duration of the tone itself.
298 static final int TONE_TIMEOUT_BUFFER = 20;
299
300 // The tone state
301 static final int TONE_OFF = 0;
302 static final int TONE_ON = 1;
303 static final int TONE_STOPPED = 2;
304
305 InCallTonePlayer(int toneId) {
306 super();
307 mToneId = toneId;
308 mState = TONE_OFF;
309 }
310
311 @Override
312 public void run() {
313 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
314
315 int toneType = 0; // passed to ToneGenerator.startTone()
316 int toneVolume; // passed to the ToneGenerator constructor
317 int toneLengthMillis;
318 int phoneType = mCM.getFgPhone().getPhoneType();
319
320 switch (mToneId) {
321 case TONE_CALL_WAITING:
322 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
323 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
324 // Call waiting tone is stopped by stopTone() method
325 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
326 break;
327 case TONE_BUSY:
328 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
329 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
330 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
331 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800332 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
333 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700334 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800335 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 toneType = ToneGenerator.TONE_SUP_BUSY;
337 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
338 toneLengthMillis = 4000;
339 } else {
340 throw new IllegalStateException("Unexpected phone type: " + phoneType);
341 }
342 break;
343 case TONE_CONGESTION:
344 toneType = ToneGenerator.TONE_SUP_CONGESTION;
345 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
346 toneLengthMillis = 4000;
347 break;
348
349 case TONE_CALL_ENDED:
350 toneType = ToneGenerator.TONE_PROP_PROMPT;
351 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
352 toneLengthMillis = 200;
353 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354 case TONE_VOICE_PRIVACY:
355 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
356 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
357 toneLengthMillis = 5000;
358 break;
359 case TONE_REORDER:
360 toneType = ToneGenerator.TONE_CDMA_REORDER;
361 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
362 toneLengthMillis = 4000;
363 break;
364 case TONE_INTERCEPT:
365 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
366 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
367 toneLengthMillis = 500;
368 break;
369 case TONE_CDMA_DROP:
370 case TONE_OUT_OF_SERVICE:
371 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
372 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
373 toneLengthMillis = 375;
374 break;
375 case TONE_REDIAL:
376 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
377 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
378 toneLengthMillis = 5000;
379 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 case TONE_UNOBTAINABLE_NUMBER:
381 toneType = ToneGenerator.TONE_SUP_ERROR;
382 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
383 toneLengthMillis = 4000;
384 break;
385 default:
386 throw new IllegalArgumentException("Bad toneId: " + mToneId);
387 }
388
389 // If the mToneGenerator creation fails, just continue without it. It is
390 // a local audio signal, and is not as important.
391 ToneGenerator toneGenerator;
392 try {
393 int stream;
394 if (mBluetoothHeadset != null) {
Jordan Liu1c85e772019-08-06 16:06:19 -0700395 stream = isBluetoothAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO :
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 AudioManager.STREAM_VOICE_CALL;
397 } else {
398 stream = AudioManager.STREAM_VOICE_CALL;
399 }
400 toneGenerator = new ToneGenerator(stream, toneVolume);
401 // if (DBG) log("- created toneGenerator: " + toneGenerator);
402 } catch (RuntimeException e) {
403 Log.w(LOG_TAG,
404 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
405 toneGenerator = null;
406 }
407
408 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
409 // CONGESTION tones at least), the ToneGenerator itself knows
410 // the right pattern of tones to play; we do NOT need to
411 // manually start/stop each individual tone, or manually
412 // insert the correct delay between tones. (We just start it
413 // and let it run for however long we want the tone pattern to
414 // continue.)
415 //
416 // TODO: When we stop the ToneGenerator in the middle of a
417 // "tone pattern", it sounds bad if we cut if off while the
418 // tone is actually playing. Consider adding API to the
419 // ToneGenerator to say "stop at the next silent part of the
420 // pattern", or simply "play the pattern N times and then
421 // stop."
422 boolean needToStopTone = true;
423 boolean okToPlayTone = false;
424
425 if (toneGenerator != null) {
426 int ringerMode = mAudioManager.getRingerMode();
427 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
428 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
429 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
430 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
431 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
432 okToPlayTone = true;
433 needToStopTone = false;
434 }
435 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
436 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
437 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
438 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
439 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
440 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
441 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
442 okToPlayTone = true;
443 needToStopTone = false;
444 }
445 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
446 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
447 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
448 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
449 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
450 okToPlayTone = true;
451 needToStopTone = false;
452 }
453 } else { // For the rest of the tones, always OK to play.
454 okToPlayTone = true;
455 }
456 } else { // Not "CDMA"
457 okToPlayTone = true;
458 }
459
460 synchronized (this) {
461 if (okToPlayTone && mState != TONE_STOPPED) {
462 mState = TONE_ON;
463 toneGenerator.startTone(toneType);
464 try {
465 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
466 } catch (InterruptedException e) {
467 Log.w(LOG_TAG,
468 "InCallTonePlayer stopped: " + e);
469 }
470 if (needToStopTone) {
471 toneGenerator.stopTone();
472 }
473 }
474 // if (DBG) log("- InCallTonePlayer: done playing.");
475 toneGenerator.release();
476 mState = TONE_OFF;
477 }
478 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 }
481
Jordan Liu1c85e772019-08-06 16:06:19 -0700482 // Returns whether there are any connected Bluetooth audio devices
483 private boolean isBluetoothAudioOn() {
484 return mBluetoothHeadset.getConnectedDevices().size() > 0;
485 }
486
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487 /**
488 * Displays a notification when the phone receives a DisplayInfo record.
489 */
490 private void onDisplayInfo(AsyncResult r) {
491 // Extract the DisplayInfo String from the message
492 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
493
494 if (displayInfoRec != null) {
495 String displayInfo = displayInfoRec.alpha;
496 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800497 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700498
Anthony Leee9468532014-11-15 15:21:00 -0800499 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800500 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800501 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 }
503 }
504
505 /**
Anthony Leee9468532014-11-15 15:21:00 -0800506 * Displays a notification when the phone receives a notice that a supplemental
507 * service has failed.
Anthony Leee9468532014-11-15 15:21:00 -0800508 */
509 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800510 String mergeFailedString = "";
511 if (r.result == Phone.SuppService.CONFERENCE) {
512 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
513 mergeFailedString = mApplication.getResources().getString(
514 R.string.incall_error_supp_service_conference);
515 } else if (r.result == Phone.SuppService.RESUME) {
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700516 if (DBG) log("onSuppServiceFailed: displaying resume failure message");
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800517 mergeFailedString = mApplication.getResources().getString(
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700518 R.string.incall_error_supp_service_resume);
Anju Mathapati4effeb22016-01-25 22:25:09 -0800519 } else if (r.result == Phone.SuppService.HOLD) {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700520 if (DBG) log("onSuppServiceFailed: displaying hold failure message");
Anju Mathapati4effeb22016-01-25 22:25:09 -0800521 mergeFailedString = mApplication.getResources().getString(
522 R.string.incall_error_supp_service_hold);
Tyler Gunna7de7d32017-06-09 16:21:00 -0700523 } else if (r.result == Phone.SuppService.TRANSFER) {
524 if (DBG) log("onSuppServiceFailed: displaying transfer failure message");
525 mergeFailedString = mApplication.getResources().getString(
526 R.string.incall_error_supp_service_transfer);
527 } else if (r.result == Phone.SuppService.SEPARATE) {
528 if (DBG) log("onSuppServiceFailed: displaying separate failure message");
529 mergeFailedString = mApplication.getResources().getString(
530 R.string.incall_error_supp_service_separate);
531 } else if (r.result == Phone.SuppService.SWITCH) {
532 if (DBG) log("onSuppServiceFailed: displaying switch failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530533 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700534 R.string.incall_error_supp_service_switch);
535 } else if (r.result == Phone.SuppService.REJECT) {
536 if (DBG) log("onSuppServiceFailed: displaying reject failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530537 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700538 R.string.incall_error_supp_service_reject);
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530539 } else if (r.result == Phone.SuppService.HANGUP) {
540 mergeFailedString = mApplication.getResources().getString(
541 R.string.incall_error_supp_service_hangup);
542 } else {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700543 if (DBG) log("onSuppServiceFailed: unknown failure");
544 return;
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800545 }
Tyler Gunna7de7d32017-06-09 16:21:00 -0700546
Anthony Leee9468532014-11-15 15:21:00 -0800547 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
548
549 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800550 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800551 SHOW_MESSAGE_NOTIFICATION_TIME);
552 }
553
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900554 public void updatePhoneStateListeners(boolean isRefresh) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530555 updatePhoneStateListeners(isRefresh, UPDATE_TYPE_MWI_CFI,
556 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
557 }
558
559 public void updatePhoneStateListeners(boolean isRefresh, int updateType, int subIdToUpdate) {
Malcolm Chena336e2a2019-02-12 18:51:37 -0800560 List<SubscriptionInfo> subInfos = SubscriptionController.getInstance()
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800561 .getActiveSubscriptionInfoList(mApplication.getOpPackageName(),
562 null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800563
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900564 // Sort sub id list based on slot id, so that CFI/MWI notifications will be updated for
565 // slot 0 first then slot 1. This is needed to ensure that when CFI or MWI is enabled for
566 // both slots, user always sees icon related to slot 0 on left side followed by that of
567 // slot 1.
568 List<Integer> subIdList = new ArrayList<Integer>(mPhoneStateListeners.keySet());
569 Collections.sort(subIdList, new Comparator<Integer>() {
570 public int compare(Integer sub1, Integer sub2) {
571 int slotId1 = SubscriptionController.getInstance().getSlotIndex(sub1);
572 int slotId2 = SubscriptionController.getInstance().getSlotIndex(sub2);
573 return slotId1 > slotId2 ? 0 : -1;
574 }
575 });
576
577 for (int subIdCounter = (subIdList.size() - 1); subIdCounter >= 0; subIdCounter--) {
578 int subId = subIdList.get(subIdCounter);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800579 if (subInfos == null || !containsSubId(subInfos, subId)) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900580 Log.d(LOG_TAG, "updatePhoneStateListeners: Hide the outstanding notifications.");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800581 // Hide the outstanding notifications.
582 mApplication.notificationMgr.updateMwi(subId, false);
583 mApplication.notificationMgr.updateCfi(subId, false);
584
585 // Listening to LISTEN_NONE removes the listener.
586 mTelephonyManager.listen(
587 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900588 mPhoneStateListeners.remove(subId);
589 } else {
590 Log.d(LOG_TAG, "updatePhoneStateListeners: update CF notifications.");
591
592 if (mCFIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530593 if ((updateType == UPDATE_TYPE_CFI) && (subId == subIdToUpdate)) {
594 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId),
595 isRefresh);
596 } else {
597 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId), true);
598 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900599 }
600 if (mMWIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530601 if ((updateType == UPDATE_TYPE_MWI) && (subId == subIdToUpdate)) {
602 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId),
603 isRefresh);
604 } else {
605 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId), true);
606 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900607 }
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800608 }
609 }
610
611 if (subInfos == null) {
612 return;
613 }
614
615 // Register new phone listeners for active subscriptions.
616 for (int i = 0; i < subInfos.size(); i++) {
617 int subId = subInfos.get(i).getSubscriptionId();
618 if (!mPhoneStateListeners.containsKey(subId)) {
chen xu06946472019-03-20 14:55:59 -0700619 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener();
620 mTelephonyManager.createForSubscriptionId(subId).listen(listener,
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800621 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
622 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
623 mPhoneStateListeners.put(subId, listener);
624 }
625 }
626 }
627
628 /**
629 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
630 */
631 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
632 if (subInfos == null) {
633 return false;
634 }
635
636 for (int i = 0; i < subInfos.size(); i++) {
637 if (subInfos.get(i).getSubscriptionId() == subId) {
638 return true;
639 }
640 }
641 return false;
642 }
643
Anthony Leee9468532014-11-15 15:21:00 -0800644 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800645 * Displays a notification when the phone receives a notice that TTY mode
646 * has changed on remote end.
647 */
648 private void onTtyModeReceived(AsyncResult r) {
649 if (DBG) log("TtyModeReceived: displaying notification message");
650
651 int resId = 0;
652 switch (((Integer)r.result).intValue()) {
653 case TelecomManager.TTY_MODE_FULL:
654 resId = com.android.internal.R.string.peerTtyModeFull;
655 break;
656 case TelecomManager.TTY_MODE_HCO:
657 resId = com.android.internal.R.string.peerTtyModeHco;
658 break;
659 case TelecomManager.TTY_MODE_VCO:
660 resId = com.android.internal.R.string.peerTtyModeVco;
661 break;
662 case TelecomManager.TTY_MODE_OFF:
663 resId = com.android.internal.R.string.peerTtyModeOff;
664 break;
665 default:
666 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
667 break;
668 }
669 if (resId != 0) {
670 PhoneDisplayMessage.displayNetworkMessage(mApplication,
671 mApplication.getResources().getString(resId));
672
673 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800674 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800675 SHOW_MESSAGE_NOTIFICATION_TIME);
676 }
677 }
678
679 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 * Helper class to play SignalInfo tones using the ToneGenerator.
681 *
682 * To use, just instantiate a new SignalInfoTonePlayer
683 * (passing in the ToneID constant for the tone you want)
684 * and start() it.
685 */
686 private class SignalInfoTonePlayer extends Thread {
687 private int mToneId;
688
689 SignalInfoTonePlayer(int toneId) {
690 super();
691 mToneId = toneId;
692 }
693
694 @Override
695 public void run() {
696 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700697 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700698 if (mSignalInfoToneGenerator != null) {
699 //First stop any ongoing SignalInfo tone
700 mSignalInfoToneGenerator.stopTone();
701
702 //Start playing the new tone if its a valid tone
703 mSignalInfoToneGenerator.startTone(mToneId);
704 }
705 }
706 }
707
708 /**
709 * Plays a tone when the phone receives a SignalInfo record.
710 */
711 private void onSignalInfo(AsyncResult r) {
712 // Signal Info are totally ignored on non-voice-capable devices.
713 if (!PhoneGlobals.sVoiceCapable) {
714 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
715 return;
716 }
717
718 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
719 // Do not start any new SignalInfo tone when Call state is INCOMING
720 // and stop any previous SignalInfo tone which is being played
721 stopSignalInfoTone();
722 } else {
723 // Extract the SignalInfo String from the message
724 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
725 // Only proceed if a Signal info is present.
726 if (signalInfoRec != null) {
727 boolean isPresent = signalInfoRec.isPresent;
728 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
729 if (isPresent) {// if tone is valid
730 int uSignalType = signalInfoRec.signalType;
731 int uAlertPitch = signalInfoRec.alertPitch;
732 int uSignal = signalInfoRec.signal;
733
734 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
735 uAlertPitch + ", uSignal=" + uSignal);
736 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
737 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
738 (uSignalType, uAlertPitch, uSignal);
739
740 //Create the SignalInfo tone player and pass the ToneID
741 new SignalInfoTonePlayer(toneID).start();
742 }
743 }
744 }
745 }
746
747 /**
748 * Stops a SignalInfo tone in the following condition
749 * 1 - On receiving a New Ringing Call
750 * 2 - On disconnecting a call
751 * 3 - On answering a Call Waiting Call
752 */
753 /* package */ void stopSignalInfoTone() {
754 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
755 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
756 }
757
Santos Cordon5c046722014-09-18 15:41:13 -0700758 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800759 new BluetoothProfile.ServiceListener() {
760 public void onServiceConnected(int profile, BluetoothProfile proxy) {
761 mBluetoothHeadset = (BluetoothHeadset) proxy;
762 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
763 }
764
765 public void onServiceDisconnected(int profile) {
766 mBluetoothHeadset = null;
767 }
768 };
769
770 private class CallNotifierPhoneStateListener extends PhoneStateListener {
chen xu06946472019-03-20 14:55:59 -0700771 public CallNotifierPhoneStateListener() {
772 super();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 }
774
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800775 @Override
776 public void onMessageWaitingIndicatorChanged(boolean visible) {
777 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900778 mMWIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530779 updatePhoneStateListeners(false, UPDATE_TYPE_MWI, this.mSubId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800780 }
781
782 @Override
783 public void onCallForwardingIndicatorChanged(boolean visible) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700784 Log.i(LOG_TAG, "onCallForwardingIndicatorChanged(): subId=" + this.mSubId
785 + ", visible=" + (visible ? "Y" : "N"));
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900786 mCFIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530787 updatePhoneStateListeners(false, UPDATE_TYPE_CFI, this.mSubId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 }
789 };
790
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 private void log(String msg) {
792 Log.d(LOG_TAG, msg);
793 }
794}