blob: bd97b698b640dc9c9bcae3171f3ba412d0b6678d [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) {
395 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
396 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
482 /**
483 * Displays a notification when the phone receives a DisplayInfo record.
484 */
485 private void onDisplayInfo(AsyncResult r) {
486 // Extract the DisplayInfo String from the message
487 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
488
489 if (displayInfoRec != null) {
490 String displayInfo = displayInfoRec.alpha;
491 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800492 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493
Anthony Leee9468532014-11-15 15:21:00 -0800494 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800495 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800496 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700497 }
498 }
499
500 /**
Anthony Leee9468532014-11-15 15:21:00 -0800501 * Displays a notification when the phone receives a notice that a supplemental
502 * service has failed.
Anthony Leee9468532014-11-15 15:21:00 -0800503 */
504 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800505 String mergeFailedString = "";
506 if (r.result == Phone.SuppService.CONFERENCE) {
507 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
508 mergeFailedString = mApplication.getResources().getString(
509 R.string.incall_error_supp_service_conference);
510 } else if (r.result == Phone.SuppService.RESUME) {
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700511 if (DBG) log("onSuppServiceFailed: displaying resume failure message");
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800512 mergeFailedString = mApplication.getResources().getString(
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700513 R.string.incall_error_supp_service_resume);
Anju Mathapati4effeb22016-01-25 22:25:09 -0800514 } else if (r.result == Phone.SuppService.HOLD) {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700515 if (DBG) log("onSuppServiceFailed: displaying hold failure message");
Anju Mathapati4effeb22016-01-25 22:25:09 -0800516 mergeFailedString = mApplication.getResources().getString(
517 R.string.incall_error_supp_service_hold);
Tyler Gunna7de7d32017-06-09 16:21:00 -0700518 } else if (r.result == Phone.SuppService.TRANSFER) {
519 if (DBG) log("onSuppServiceFailed: displaying transfer failure message");
520 mergeFailedString = mApplication.getResources().getString(
521 R.string.incall_error_supp_service_transfer);
522 } else if (r.result == Phone.SuppService.SEPARATE) {
523 if (DBG) log("onSuppServiceFailed: displaying separate failure message");
524 mergeFailedString = mApplication.getResources().getString(
525 R.string.incall_error_supp_service_separate);
526 } else if (r.result == Phone.SuppService.SWITCH) {
527 if (DBG) log("onSuppServiceFailed: displaying switch failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530528 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700529 R.string.incall_error_supp_service_switch);
530 } else if (r.result == Phone.SuppService.REJECT) {
531 if (DBG) log("onSuppServiceFailed: displaying reject failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530532 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700533 R.string.incall_error_supp_service_reject);
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530534 } else if (r.result == Phone.SuppService.HANGUP) {
535 mergeFailedString = mApplication.getResources().getString(
536 R.string.incall_error_supp_service_hangup);
537 } else {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700538 if (DBG) log("onSuppServiceFailed: unknown failure");
539 return;
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800540 }
Tyler Gunna7de7d32017-06-09 16:21:00 -0700541
Anthony Leee9468532014-11-15 15:21:00 -0800542 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
543
544 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800545 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800546 SHOW_MESSAGE_NOTIFICATION_TIME);
547 }
548
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900549 public void updatePhoneStateListeners(boolean isRefresh) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530550 updatePhoneStateListeners(isRefresh, UPDATE_TYPE_MWI_CFI,
551 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
552 }
553
554 public void updatePhoneStateListeners(boolean isRefresh, int updateType, int subIdToUpdate) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800555 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
556
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900557 // Sort sub id list based on slot id, so that CFI/MWI notifications will be updated for
558 // slot 0 first then slot 1. This is needed to ensure that when CFI or MWI is enabled for
559 // both slots, user always sees icon related to slot 0 on left side followed by that of
560 // slot 1.
561 List<Integer> subIdList = new ArrayList<Integer>(mPhoneStateListeners.keySet());
562 Collections.sort(subIdList, new Comparator<Integer>() {
563 public int compare(Integer sub1, Integer sub2) {
564 int slotId1 = SubscriptionController.getInstance().getSlotIndex(sub1);
565 int slotId2 = SubscriptionController.getInstance().getSlotIndex(sub2);
566 return slotId1 > slotId2 ? 0 : -1;
567 }
568 });
569
570 for (int subIdCounter = (subIdList.size() - 1); subIdCounter >= 0; subIdCounter--) {
571 int subId = subIdList.get(subIdCounter);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800572 if (subInfos == null || !containsSubId(subInfos, subId)) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900573 Log.d(LOG_TAG, "updatePhoneStateListeners: Hide the outstanding notifications.");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800574 // Hide the outstanding notifications.
575 mApplication.notificationMgr.updateMwi(subId, false);
576 mApplication.notificationMgr.updateCfi(subId, false);
577
578 // Listening to LISTEN_NONE removes the listener.
579 mTelephonyManager.listen(
580 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900581 mPhoneStateListeners.remove(subId);
582 } else {
583 Log.d(LOG_TAG, "updatePhoneStateListeners: update CF notifications.");
584
585 if (mCFIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530586 if ((updateType == UPDATE_TYPE_CFI) && (subId == subIdToUpdate)) {
587 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId),
588 isRefresh);
589 } else {
590 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId), true);
591 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900592 }
593 if (mMWIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530594 if ((updateType == UPDATE_TYPE_MWI) && (subId == subIdToUpdate)) {
595 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId),
596 isRefresh);
597 } else {
598 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId), true);
599 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900600 }
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800601 }
602 }
603
604 if (subInfos == null) {
605 return;
606 }
607
608 // Register new phone listeners for active subscriptions.
609 for (int i = 0; i < subInfos.size(); i++) {
610 int subId = subInfos.get(i).getSubscriptionId();
611 if (!mPhoneStateListeners.containsKey(subId)) {
612 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
613 mTelephonyManager.listen(listener,
614 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
615 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
616 mPhoneStateListeners.put(subId, listener);
617 }
618 }
619 }
620
621 /**
622 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
623 */
624 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
625 if (subInfos == null) {
626 return false;
627 }
628
629 for (int i = 0; i < subInfos.size(); i++) {
630 if (subInfos.get(i).getSubscriptionId() == subId) {
631 return true;
632 }
633 }
634 return false;
635 }
636
Anthony Leee9468532014-11-15 15:21:00 -0800637 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800638 * Displays a notification when the phone receives a notice that TTY mode
639 * has changed on remote end.
640 */
641 private void onTtyModeReceived(AsyncResult r) {
642 if (DBG) log("TtyModeReceived: displaying notification message");
643
644 int resId = 0;
645 switch (((Integer)r.result).intValue()) {
646 case TelecomManager.TTY_MODE_FULL:
647 resId = com.android.internal.R.string.peerTtyModeFull;
648 break;
649 case TelecomManager.TTY_MODE_HCO:
650 resId = com.android.internal.R.string.peerTtyModeHco;
651 break;
652 case TelecomManager.TTY_MODE_VCO:
653 resId = com.android.internal.R.string.peerTtyModeVco;
654 break;
655 case TelecomManager.TTY_MODE_OFF:
656 resId = com.android.internal.R.string.peerTtyModeOff;
657 break;
658 default:
659 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
660 break;
661 }
662 if (resId != 0) {
663 PhoneDisplayMessage.displayNetworkMessage(mApplication,
664 mApplication.getResources().getString(resId));
665
666 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800667 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800668 SHOW_MESSAGE_NOTIFICATION_TIME);
669 }
670 }
671
672 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 * Helper class to play SignalInfo tones using the ToneGenerator.
674 *
675 * To use, just instantiate a new SignalInfoTonePlayer
676 * (passing in the ToneID constant for the tone you want)
677 * and start() it.
678 */
679 private class SignalInfoTonePlayer extends Thread {
680 private int mToneId;
681
682 SignalInfoTonePlayer(int toneId) {
683 super();
684 mToneId = toneId;
685 }
686
687 @Override
688 public void run() {
689 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700690 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 if (mSignalInfoToneGenerator != null) {
692 //First stop any ongoing SignalInfo tone
693 mSignalInfoToneGenerator.stopTone();
694
695 //Start playing the new tone if its a valid tone
696 mSignalInfoToneGenerator.startTone(mToneId);
697 }
698 }
699 }
700
701 /**
702 * Plays a tone when the phone receives a SignalInfo record.
703 */
704 private void onSignalInfo(AsyncResult r) {
705 // Signal Info are totally ignored on non-voice-capable devices.
706 if (!PhoneGlobals.sVoiceCapable) {
707 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
708 return;
709 }
710
711 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
712 // Do not start any new SignalInfo tone when Call state is INCOMING
713 // and stop any previous SignalInfo tone which is being played
714 stopSignalInfoTone();
715 } else {
716 // Extract the SignalInfo String from the message
717 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
718 // Only proceed if a Signal info is present.
719 if (signalInfoRec != null) {
720 boolean isPresent = signalInfoRec.isPresent;
721 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
722 if (isPresent) {// if tone is valid
723 int uSignalType = signalInfoRec.signalType;
724 int uAlertPitch = signalInfoRec.alertPitch;
725 int uSignal = signalInfoRec.signal;
726
727 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
728 uAlertPitch + ", uSignal=" + uSignal);
729 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
730 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
731 (uSignalType, uAlertPitch, uSignal);
732
733 //Create the SignalInfo tone player and pass the ToneID
734 new SignalInfoTonePlayer(toneID).start();
735 }
736 }
737 }
738 }
739
740 /**
741 * Stops a SignalInfo tone in the following condition
742 * 1 - On receiving a New Ringing Call
743 * 2 - On disconnecting a call
744 * 3 - On answering a Call Waiting Call
745 */
746 /* package */ void stopSignalInfoTone() {
747 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
748 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
749 }
750
Santos Cordon5c046722014-09-18 15:41:13 -0700751 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800752 new BluetoothProfile.ServiceListener() {
753 public void onServiceConnected(int profile, BluetoothProfile proxy) {
754 mBluetoothHeadset = (BluetoothHeadset) proxy;
755 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
756 }
757
758 public void onServiceDisconnected(int profile) {
759 mBluetoothHeadset = null;
760 }
761 };
762
763 private class CallNotifierPhoneStateListener extends PhoneStateListener {
764 public CallNotifierPhoneStateListener(int subId) {
765 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 }
767
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800768 @Override
769 public void onMessageWaitingIndicatorChanged(boolean visible) {
770 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900771 mMWIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530772 updatePhoneStateListeners(false, UPDATE_TYPE_MWI, this.mSubId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800773 }
774
775 @Override
776 public void onCallForwardingIndicatorChanged(boolean visible) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700777 Log.i(LOG_TAG, "onCallForwardingIndicatorChanged(): subId=" + this.mSubId
778 + ", visible=" + (visible ? "Y" : "N"));
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900779 mCFIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530780 updatePhoneStateListeners(false, UPDATE_TYPE_CFI, this.mSubId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 }
782 };
783
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 private void log(String msg) {
785 Log.d(LOG_TAG, msg);
786 }
787}