blob: 6e55c13904d2e4201c957a393d0cc47505c0f1eb [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 /**
256 * Resets the audio mode and speaker state when a call ends.
257 */
258 private void resetAudioStateAfterDisconnect() {
259 if (VDBG) log("resetAudioStateAfterDisconnect()...");
260
261 if (mBluetoothHeadset != null) {
262 mBluetoothHeadset.disconnectAudio();
263 }
264
265 // call turnOnSpeaker() with state=false and store=true even if speaker
266 // is already off to reset user requested speaker state.
267 PhoneUtils.turnOnSpeaker(mApplication, false, true);
268
269 PhoneUtils.setAudioMode(mCM);
270 }
271
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 * Helper class to play tones through the earpiece (or speaker / BT)
274 * during a call, using the ToneGenerator.
275 *
276 * To use, just instantiate a new InCallTonePlayer
277 * (passing in the TONE_* constant for the tone you want)
278 * and start() it.
279 *
280 * When we're done playing the tone, if the phone is idle at that
281 * point, we'll reset the audio routing and speaker state.
282 * (That means that for tones that get played *after* a call
283 * disconnects, like "busy" or "congestion" or "call ended", you
284 * should NOT call resetAudioStateAfterDisconnect() yourself.
285 * Instead, just start the InCallTonePlayer, which will automatically
286 * defer the resetAudioStateAfterDisconnect() call until the tone
287 * finishes playing.)
288 */
289 private class InCallTonePlayer extends Thread {
290 private int mToneId;
291 private int mState;
292 // The possible tones we can play.
293 public static final int TONE_NONE = 0;
294 public static final int TONE_CALL_WAITING = 1;
295 public static final int TONE_BUSY = 2;
296 public static final int TONE_CONGESTION = 3;
297 public static final int TONE_CALL_ENDED = 4;
298 public static final int TONE_VOICE_PRIVACY = 5;
299 public static final int TONE_REORDER = 6;
300 public static final int TONE_INTERCEPT = 7;
301 public static final int TONE_CDMA_DROP = 8;
302 public static final int TONE_OUT_OF_SERVICE = 9;
303 public static final int TONE_REDIAL = 10;
304 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
306
307 // The tone volume relative to other sounds in the stream
308 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
309 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
310 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
311
312 // Buffer time (in msec) to add on to tone timeout value.
313 // Needed mainly when the timeout value for a tone is the
314 // exact duration of the tone itself.
315 static final int TONE_TIMEOUT_BUFFER = 20;
316
317 // The tone state
318 static final int TONE_OFF = 0;
319 static final int TONE_ON = 1;
320 static final int TONE_STOPPED = 2;
321
322 InCallTonePlayer(int toneId) {
323 super();
324 mToneId = toneId;
325 mState = TONE_OFF;
326 }
327
328 @Override
329 public void run() {
330 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
331
332 int toneType = 0; // passed to ToneGenerator.startTone()
333 int toneVolume; // passed to the ToneGenerator constructor
334 int toneLengthMillis;
335 int phoneType = mCM.getFgPhone().getPhoneType();
336
337 switch (mToneId) {
338 case TONE_CALL_WAITING:
339 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
340 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
341 // Call waiting tone is stopped by stopTone() method
342 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
343 break;
344 case TONE_BUSY:
345 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
346 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
347 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
348 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800349 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
350 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700351 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800352 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 toneType = ToneGenerator.TONE_SUP_BUSY;
354 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
355 toneLengthMillis = 4000;
356 } else {
357 throw new IllegalStateException("Unexpected phone type: " + phoneType);
358 }
359 break;
360 case TONE_CONGESTION:
361 toneType = ToneGenerator.TONE_SUP_CONGESTION;
362 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
363 toneLengthMillis = 4000;
364 break;
365
366 case TONE_CALL_ENDED:
367 toneType = ToneGenerator.TONE_PROP_PROMPT;
368 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
369 toneLengthMillis = 200;
370 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 case TONE_VOICE_PRIVACY:
372 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
373 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
374 toneLengthMillis = 5000;
375 break;
376 case TONE_REORDER:
377 toneType = ToneGenerator.TONE_CDMA_REORDER;
378 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
379 toneLengthMillis = 4000;
380 break;
381 case TONE_INTERCEPT:
382 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
383 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
384 toneLengthMillis = 500;
385 break;
386 case TONE_CDMA_DROP:
387 case TONE_OUT_OF_SERVICE:
388 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
389 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
390 toneLengthMillis = 375;
391 break;
392 case TONE_REDIAL:
393 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
394 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
395 toneLengthMillis = 5000;
396 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 case TONE_UNOBTAINABLE_NUMBER:
398 toneType = ToneGenerator.TONE_SUP_ERROR;
399 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
400 toneLengthMillis = 4000;
401 break;
402 default:
403 throw new IllegalArgumentException("Bad toneId: " + mToneId);
404 }
405
406 // If the mToneGenerator creation fails, just continue without it. It is
407 // a local audio signal, and is not as important.
408 ToneGenerator toneGenerator;
409 try {
410 int stream;
411 if (mBluetoothHeadset != null) {
412 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
413 AudioManager.STREAM_VOICE_CALL;
414 } else {
415 stream = AudioManager.STREAM_VOICE_CALL;
416 }
417 toneGenerator = new ToneGenerator(stream, toneVolume);
418 // if (DBG) log("- created toneGenerator: " + toneGenerator);
419 } catch (RuntimeException e) {
420 Log.w(LOG_TAG,
421 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
422 toneGenerator = null;
423 }
424
425 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
426 // CONGESTION tones at least), the ToneGenerator itself knows
427 // the right pattern of tones to play; we do NOT need to
428 // manually start/stop each individual tone, or manually
429 // insert the correct delay between tones. (We just start it
430 // and let it run for however long we want the tone pattern to
431 // continue.)
432 //
433 // TODO: When we stop the ToneGenerator in the middle of a
434 // "tone pattern", it sounds bad if we cut if off while the
435 // tone is actually playing. Consider adding API to the
436 // ToneGenerator to say "stop at the next silent part of the
437 // pattern", or simply "play the pattern N times and then
438 // stop."
439 boolean needToStopTone = true;
440 boolean okToPlayTone = false;
441
442 if (toneGenerator != null) {
443 int ringerMode = mAudioManager.getRingerMode();
444 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
445 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
446 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
447 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
448 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
449 okToPlayTone = true;
450 needToStopTone = false;
451 }
452 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
453 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
454 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
455 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
456 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
457 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
458 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
459 okToPlayTone = true;
460 needToStopTone = false;
461 }
462 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
463 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
464 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
465 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
466 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
467 okToPlayTone = true;
468 needToStopTone = false;
469 }
470 } else { // For the rest of the tones, always OK to play.
471 okToPlayTone = true;
472 }
473 } else { // Not "CDMA"
474 okToPlayTone = true;
475 }
476
477 synchronized (this) {
478 if (okToPlayTone && mState != TONE_STOPPED) {
479 mState = TONE_ON;
480 toneGenerator.startTone(toneType);
481 try {
482 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
483 } catch (InterruptedException e) {
484 Log.w(LOG_TAG,
485 "InCallTonePlayer stopped: " + e);
486 }
487 if (needToStopTone) {
488 toneGenerator.stopTone();
489 }
490 }
491 // if (DBG) log("- InCallTonePlayer: done playing.");
492 toneGenerator.release();
493 mState = TONE_OFF;
494 }
495 }
496
497 // Finally, do the same cleanup we otherwise would have done
498 // in onDisconnect().
499 //
500 // (But watch out: do NOT do this if the phone is in use,
501 // since some of our tones get played *during* a call (like
502 // CALL_WAITING) and we definitely *don't*
503 // want to reset the audio mode / speaker / bluetooth after
504 // playing those!
505 // This call is really here for use with tones that get played
506 // *after* a call disconnects, like "busy" or "congestion" or
507 // "call ended", where the phone has already become idle but
508 // we need to defer the resetAudioStateAfterDisconnect() call
509 // till the tone finishes playing.)
510 if (mCM.getState() == PhoneConstants.State.IDLE) {
511 resetAudioStateAfterDisconnect();
512 }
513 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700514 }
515
516 /**
517 * Displays a notification when the phone receives a DisplayInfo record.
518 */
519 private void onDisplayInfo(AsyncResult r) {
520 // Extract the DisplayInfo String from the message
521 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
522
523 if (displayInfoRec != null) {
524 String displayInfo = displayInfoRec.alpha;
525 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800526 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527
Anthony Leee9468532014-11-15 15:21:00 -0800528 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800529 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800530 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 }
532 }
533
534 /**
Anthony Leee9468532014-11-15 15:21:00 -0800535 * Displays a notification when the phone receives a notice that a supplemental
536 * service has failed.
Anthony Leee9468532014-11-15 15:21:00 -0800537 */
538 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800539 String mergeFailedString = "";
540 if (r.result == Phone.SuppService.CONFERENCE) {
541 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
542 mergeFailedString = mApplication.getResources().getString(
543 R.string.incall_error_supp_service_conference);
544 } else if (r.result == Phone.SuppService.RESUME) {
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700545 if (DBG) log("onSuppServiceFailed: displaying resume failure message");
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800546 mergeFailedString = mApplication.getResources().getString(
Tyler Gunn93f9ff52018-04-11 13:53:04 -0700547 R.string.incall_error_supp_service_resume);
Anju Mathapati4effeb22016-01-25 22:25:09 -0800548 } else if (r.result == Phone.SuppService.HOLD) {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700549 if (DBG) log("onSuppServiceFailed: displaying hold failure message");
Anju Mathapati4effeb22016-01-25 22:25:09 -0800550 mergeFailedString = mApplication.getResources().getString(
551 R.string.incall_error_supp_service_hold);
Tyler Gunna7de7d32017-06-09 16:21:00 -0700552 } else if (r.result == Phone.SuppService.TRANSFER) {
553 if (DBG) log("onSuppServiceFailed: displaying transfer failure message");
554 mergeFailedString = mApplication.getResources().getString(
555 R.string.incall_error_supp_service_transfer);
556 } else if (r.result == Phone.SuppService.SEPARATE) {
557 if (DBG) log("onSuppServiceFailed: displaying separate failure message");
558 mergeFailedString = mApplication.getResources().getString(
559 R.string.incall_error_supp_service_separate);
560 } else if (r.result == Phone.SuppService.SWITCH) {
561 if (DBG) log("onSuppServiceFailed: displaying switch failure message");
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530562 mergeFailedString = mApplication.getResources().getString(
Tyler Gunna7de7d32017-06-09 16:21:00 -0700563 R.string.incall_error_supp_service_switch);
564 } else if (r.result == Phone.SuppService.REJECT) {
565 if (DBG) log("onSuppServiceFailed: displaying reject 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_reject);
Shashidhar Vithalrao Kulkarnia557d622018-05-10 13:41:42 +0530568 } else if (r.result == Phone.SuppService.HANGUP) {
569 mergeFailedString = mApplication.getResources().getString(
570 R.string.incall_error_supp_service_hangup);
571 } else {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700572 if (DBG) log("onSuppServiceFailed: unknown failure");
573 return;
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800574 }
Tyler Gunna7de7d32017-06-09 16:21:00 -0700575
Anthony Leee9468532014-11-15 15:21:00 -0800576 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
577
578 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800579 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800580 SHOW_MESSAGE_NOTIFICATION_TIME);
581 }
582
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900583 public void updatePhoneStateListeners(boolean isRefresh) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530584 updatePhoneStateListeners(isRefresh, UPDATE_TYPE_MWI_CFI,
585 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
586 }
587
588 public void updatePhoneStateListeners(boolean isRefresh, int updateType, int subIdToUpdate) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800589 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
590
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900591 // Sort sub id list based on slot id, so that CFI/MWI notifications will be updated for
592 // slot 0 first then slot 1. This is needed to ensure that when CFI or MWI is enabled for
593 // both slots, user always sees icon related to slot 0 on left side followed by that of
594 // slot 1.
595 List<Integer> subIdList = new ArrayList<Integer>(mPhoneStateListeners.keySet());
596 Collections.sort(subIdList, new Comparator<Integer>() {
597 public int compare(Integer sub1, Integer sub2) {
598 int slotId1 = SubscriptionController.getInstance().getSlotIndex(sub1);
599 int slotId2 = SubscriptionController.getInstance().getSlotIndex(sub2);
600 return slotId1 > slotId2 ? 0 : -1;
601 }
602 });
603
604 for (int subIdCounter = (subIdList.size() - 1); subIdCounter >= 0; subIdCounter--) {
605 int subId = subIdList.get(subIdCounter);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800606 if (subInfos == null || !containsSubId(subInfos, subId)) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900607 Log.d(LOG_TAG, "updatePhoneStateListeners: Hide the outstanding notifications.");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800608 // Hide the outstanding notifications.
609 mApplication.notificationMgr.updateMwi(subId, false);
610 mApplication.notificationMgr.updateCfi(subId, false);
611
612 // Listening to LISTEN_NONE removes the listener.
613 mTelephonyManager.listen(
614 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900615 mPhoneStateListeners.remove(subId);
616 } else {
617 Log.d(LOG_TAG, "updatePhoneStateListeners: update CF notifications.");
618
619 if (mCFIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530620 if ((updateType == UPDATE_TYPE_CFI) && (subId == subIdToUpdate)) {
621 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId),
622 isRefresh);
623 } else {
624 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId), true);
625 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900626 }
627 if (mMWIStatus.containsKey(subId)) {
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530628 if ((updateType == UPDATE_TYPE_MWI) && (subId == subIdToUpdate)) {
629 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId),
630 isRefresh);
631 } else {
632 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId), true);
633 }
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900634 }
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800635 }
636 }
637
638 if (subInfos == null) {
639 return;
640 }
641
642 // Register new phone listeners for active subscriptions.
643 for (int i = 0; i < subInfos.size(); i++) {
644 int subId = subInfos.get(i).getSubscriptionId();
645 if (!mPhoneStateListeners.containsKey(subId)) {
646 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
647 mTelephonyManager.listen(listener,
648 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
649 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
650 mPhoneStateListeners.put(subId, listener);
651 }
652 }
653 }
654
655 /**
656 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
657 */
658 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
659 if (subInfos == null) {
660 return false;
661 }
662
663 for (int i = 0; i < subInfos.size(); i++) {
664 if (subInfos.get(i).getSubscriptionId() == subId) {
665 return true;
666 }
667 }
668 return false;
669 }
670
Anthony Leee9468532014-11-15 15:21:00 -0800671 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800672 * Displays a notification when the phone receives a notice that TTY mode
673 * has changed on remote end.
674 */
675 private void onTtyModeReceived(AsyncResult r) {
676 if (DBG) log("TtyModeReceived: displaying notification message");
677
678 int resId = 0;
679 switch (((Integer)r.result).intValue()) {
680 case TelecomManager.TTY_MODE_FULL:
681 resId = com.android.internal.R.string.peerTtyModeFull;
682 break;
683 case TelecomManager.TTY_MODE_HCO:
684 resId = com.android.internal.R.string.peerTtyModeHco;
685 break;
686 case TelecomManager.TTY_MODE_VCO:
687 resId = com.android.internal.R.string.peerTtyModeVco;
688 break;
689 case TelecomManager.TTY_MODE_OFF:
690 resId = com.android.internal.R.string.peerTtyModeOff;
691 break;
692 default:
693 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
694 break;
695 }
696 if (resId != 0) {
697 PhoneDisplayMessage.displayNetworkMessage(mApplication,
698 mApplication.getResources().getString(resId));
699
700 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800701 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800702 SHOW_MESSAGE_NOTIFICATION_TIME);
703 }
704 }
705
706 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 * Helper class to play SignalInfo tones using the ToneGenerator.
708 *
709 * To use, just instantiate a new SignalInfoTonePlayer
710 * (passing in the ToneID constant for the tone you want)
711 * and start() it.
712 */
713 private class SignalInfoTonePlayer extends Thread {
714 private int mToneId;
715
716 SignalInfoTonePlayer(int toneId) {
717 super();
718 mToneId = toneId;
719 }
720
721 @Override
722 public void run() {
723 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700724 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 if (mSignalInfoToneGenerator != null) {
726 //First stop any ongoing SignalInfo tone
727 mSignalInfoToneGenerator.stopTone();
728
729 //Start playing the new tone if its a valid tone
730 mSignalInfoToneGenerator.startTone(mToneId);
731 }
732 }
733 }
734
735 /**
736 * Plays a tone when the phone receives a SignalInfo record.
737 */
738 private void onSignalInfo(AsyncResult r) {
739 // Signal Info are totally ignored on non-voice-capable devices.
740 if (!PhoneGlobals.sVoiceCapable) {
741 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
742 return;
743 }
744
745 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
746 // Do not start any new SignalInfo tone when Call state is INCOMING
747 // and stop any previous SignalInfo tone which is being played
748 stopSignalInfoTone();
749 } else {
750 // Extract the SignalInfo String from the message
751 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
752 // Only proceed if a Signal info is present.
753 if (signalInfoRec != null) {
754 boolean isPresent = signalInfoRec.isPresent;
755 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
756 if (isPresent) {// if tone is valid
757 int uSignalType = signalInfoRec.signalType;
758 int uAlertPitch = signalInfoRec.alertPitch;
759 int uSignal = signalInfoRec.signal;
760
761 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
762 uAlertPitch + ", uSignal=" + uSignal);
763 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
764 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
765 (uSignalType, uAlertPitch, uSignal);
766
767 //Create the SignalInfo tone player and pass the ToneID
768 new SignalInfoTonePlayer(toneID).start();
769 }
770 }
771 }
772 }
773
774 /**
775 * Stops a SignalInfo tone in the following condition
776 * 1 - On receiving a New Ringing Call
777 * 2 - On disconnecting a call
778 * 3 - On answering a Call Waiting Call
779 */
780 /* package */ void stopSignalInfoTone() {
781 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
782 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
783 }
784
Santos Cordon5c046722014-09-18 15:41:13 -0700785 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800786 new BluetoothProfile.ServiceListener() {
787 public void onServiceConnected(int profile, BluetoothProfile proxy) {
788 mBluetoothHeadset = (BluetoothHeadset) proxy;
789 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
790 }
791
792 public void onServiceDisconnected(int profile) {
793 mBluetoothHeadset = null;
794 }
795 };
796
797 private class CallNotifierPhoneStateListener extends PhoneStateListener {
798 public CallNotifierPhoneStateListener(int subId) {
799 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 }
801
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800802 @Override
803 public void onMessageWaitingIndicatorChanged(boolean visible) {
804 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900805 mMWIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530806 updatePhoneStateListeners(false, UPDATE_TYPE_MWI, this.mSubId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800807 }
808
809 @Override
810 public void onCallForwardingIndicatorChanged(boolean visible) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700811 Log.i(LOG_TAG, "onCallForwardingIndicatorChanged(): subId=" + this.mSubId
812 + ", visible=" + (visible ? "Y" : "N"));
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900813 mCFIStatus.put(this.mSubId, visible);
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530814 updatePhoneStateListeners(false, UPDATE_TYPE_CFI, this.mSubId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700815 }
816 };
817
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700818 private void log(String msg) {
819 Log.d(LOG_TAG, msg);
820 }
821}