blob: d084e929648df0abbc6ff51952b3bc7dd9cbe739 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Santos Cordon7d4ddf62013-07-10 11:58:08 -070019import com.android.internal.telephony.CallManager;
fionaxu80126972017-02-01 17:01:26 -080020
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import com.android.internal.telephony.Phone;
22import com.android.internal.telephony.PhoneConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaDisplayInfoRec;
24import com.android.internal.telephony.cdma.CdmaInformationRecords.CdmaSignalInfoRec;
25import com.android.internal.telephony.cdma.SignalToneUtil;
26
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.bluetooth.BluetoothAdapter;
28import android.bluetooth.BluetoothHeadset;
29import android.bluetooth.BluetoothProfile;
30import android.content.Context;
31import android.media.AudioManager;
32import android.media.ToneGenerator;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.AsyncResult;
34import android.os.Handler;
35import android.os.Message;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.SystemProperties;
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -080037import android.telecom.TelecomManager;
fionaxu80126972017-02-01 17:01:26 -080038
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.telephony.PhoneStateListener;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080040import android.telephony.SubscriptionInfo;
41import android.telephony.SubscriptionManager;
42import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.TelephonyManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080044import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.util.Log;
46
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090047import java.util.ArrayList;
48import java.util.Collections;
49import java.util.Comparator;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080050import java.util.Iterator;
51import java.util.List;
52import java.util.Map;
53
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090054import com.android.internal.telephony.SubscriptionController;
55
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056/**
57 * Phone app module that listens for phone state changes and various other
58 * events from the telephony layer, and triggers any resulting UI behavior
Santos Cordon5422a8d2014-09-12 04:20:56 -070059 * (like starting the Incoming Call UI, playing in-call tones,
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060 * updating notifications, writing call log entries, etc.)
61 */
Santos Cordon5422a8d2014-09-12 04:20:56 -070062public class CallNotifier extends Handler {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063 private static final String LOG_TAG = "CallNotifier";
64 private static final boolean DBG =
65 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
66 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
67
Anthony Leee9468532014-11-15 15:21:00 -080068 // Time to display the message from the underlying phone layers.
69 private static final int SHOW_MESSAGE_NOTIFICATION_TIME = 3000; // msec
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070
71 /** The singleton instance. */
72 private static CallNotifier sInstance;
73
Andrew Lee2fcb6c32014-12-04 14:52:35 -080074 private Map<Integer, CallNotifierPhoneStateListener> mPhoneStateListeners =
75 new ArrayMap<Integer, CallNotifierPhoneStateListener>();
Kazuya Ohshiro263737d2017-10-06 19:42:03 +090076 private Map<Integer, Boolean> mCFIStatus = new ArrayMap<Integer, Boolean>();
77 private Map<Integer, Boolean> mMWIStatus = new ArrayMap<Integer, Boolean>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078 private PhoneGlobals mApplication;
79 private CallManager mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080 private BluetoothHeadset mBluetoothHeadset;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081
82 // ToneGenerator instance for playing SignalInfo tones
83 private ToneGenerator mSignalInfoToneGenerator;
84
85 // The tone volume relative to other sounds in the stream SignalInfo
86 private static final int TONE_RELATIVE_VOLUME_SIGNALINFO = 80;
87
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 private boolean mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 // Cached AudioManager
91 private AudioManager mAudioManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080092 private SubscriptionManager mSubscriptionManager;
93 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -070094
Brad Ebingera9c6b6d2016-01-07 17:24:16 -080095 // Events from the Phone object:
96 public static final int PHONE_DISCONNECT = 3;
97 public static final int PHONE_STATE_DISPLAYINFO = 6;
98 public static final int PHONE_STATE_SIGNALINFO = 7;
99 public static final int PHONE_ENHANCED_VP_ON = 9;
100 public static final int PHONE_ENHANCED_VP_OFF = 10;
101 public static final int PHONE_SUPP_SERVICE_FAILED = 14;
102 public static final int PHONE_TTY_MODE_RECEIVED = 15;
103 // Events generated internally.
104 // We should store all the possible event type values in one place to make sure that
105 // they don't step on each others' toes.
106 public static final int INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE = 22;
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800107
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) {
545 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
546 mergeFailedString = mApplication.getResources().getString(
547 R.string.incall_error_supp_service_switch);
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");
562 mApplication.getResources().getString(
563 R.string.incall_error_supp_service_switch);
564 } else if (r.result == Phone.SuppService.REJECT) {
565 if (DBG) log("onSuppServiceFailed: displaying reject failure message");
566 mApplication.getResources().getString(
567 R.string.incall_error_supp_service_reject);
568 } else {
569 if (DBG) log("onSuppServiceFailed: unknown failure");
570 return;
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800571 }
Tyler Gunna7de7d32017-06-09 16:21:00 -0700572
Anthony Leee9468532014-11-15 15:21:00 -0800573 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
574
575 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800576 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800577 SHOW_MESSAGE_NOTIFICATION_TIME);
578 }
579
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900580 public void updatePhoneStateListeners(boolean isRefresh) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800581 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
582
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900583 // Sort sub id list based on slot id, so that CFI/MWI notifications will be updated for
584 // slot 0 first then slot 1. This is needed to ensure that when CFI or MWI is enabled for
585 // both slots, user always sees icon related to slot 0 on left side followed by that of
586 // slot 1.
587 List<Integer> subIdList = new ArrayList<Integer>(mPhoneStateListeners.keySet());
588 Collections.sort(subIdList, new Comparator<Integer>() {
589 public int compare(Integer sub1, Integer sub2) {
590 int slotId1 = SubscriptionController.getInstance().getSlotIndex(sub1);
591 int slotId2 = SubscriptionController.getInstance().getSlotIndex(sub2);
592 return slotId1 > slotId2 ? 0 : -1;
593 }
594 });
595
596 for (int subIdCounter = (subIdList.size() - 1); subIdCounter >= 0; subIdCounter--) {
597 int subId = subIdList.get(subIdCounter);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800598 if (subInfos == null || !containsSubId(subInfos, subId)) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900599 Log.d(LOG_TAG, "updatePhoneStateListeners: Hide the outstanding notifications.");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800600 // Hide the outstanding notifications.
601 mApplication.notificationMgr.updateMwi(subId, false);
602 mApplication.notificationMgr.updateCfi(subId, false);
603
604 // Listening to LISTEN_NONE removes the listener.
605 mTelephonyManager.listen(
606 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900607 mPhoneStateListeners.remove(subId);
608 } else {
609 Log.d(LOG_TAG, "updatePhoneStateListeners: update CF notifications.");
610
611 if (mCFIStatus.containsKey(subId)) {
612 mApplication.notificationMgr.updateCfi(subId, mCFIStatus.get(subId));
613 }
614 if (mMWIStatus.containsKey(subId)) {
615 mApplication.notificationMgr.updateMwi(subId, mMWIStatus.get(subId), isRefresh);
616 }
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800617 }
618 }
619
620 if (subInfos == null) {
621 return;
622 }
623
624 // Register new phone listeners for active subscriptions.
625 for (int i = 0; i < subInfos.size(); i++) {
626 int subId = subInfos.get(i).getSubscriptionId();
627 if (!mPhoneStateListeners.containsKey(subId)) {
628 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
629 mTelephonyManager.listen(listener,
630 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
631 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
632 mPhoneStateListeners.put(subId, listener);
633 }
634 }
635 }
636
637 /**
638 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
639 */
640 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
641 if (subInfos == null) {
642 return false;
643 }
644
645 for (int i = 0; i < subInfos.size(); i++) {
646 if (subInfos.get(i).getSubscriptionId() == subId) {
647 return true;
648 }
649 }
650 return false;
651 }
652
Anthony Leee9468532014-11-15 15:21:00 -0800653 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800654 * Displays a notification when the phone receives a notice that TTY mode
655 * has changed on remote end.
656 */
657 private void onTtyModeReceived(AsyncResult r) {
658 if (DBG) log("TtyModeReceived: displaying notification message");
659
660 int resId = 0;
661 switch (((Integer)r.result).intValue()) {
662 case TelecomManager.TTY_MODE_FULL:
663 resId = com.android.internal.R.string.peerTtyModeFull;
664 break;
665 case TelecomManager.TTY_MODE_HCO:
666 resId = com.android.internal.R.string.peerTtyModeHco;
667 break;
668 case TelecomManager.TTY_MODE_VCO:
669 resId = com.android.internal.R.string.peerTtyModeVco;
670 break;
671 case TelecomManager.TTY_MODE_OFF:
672 resId = com.android.internal.R.string.peerTtyModeOff;
673 break;
674 default:
675 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
676 break;
677 }
678 if (resId != 0) {
679 PhoneDisplayMessage.displayNetworkMessage(mApplication,
680 mApplication.getResources().getString(resId));
681
682 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800683 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800684 SHOW_MESSAGE_NOTIFICATION_TIME);
685 }
686 }
687
688 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 * Helper class to play SignalInfo tones using the ToneGenerator.
690 *
691 * To use, just instantiate a new SignalInfoTonePlayer
692 * (passing in the ToneID constant for the tone you want)
693 * and start() it.
694 */
695 private class SignalInfoTonePlayer extends Thread {
696 private int mToneId;
697
698 SignalInfoTonePlayer(int toneId) {
699 super();
700 mToneId = toneId;
701 }
702
703 @Override
704 public void run() {
705 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700706 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 if (mSignalInfoToneGenerator != null) {
708 //First stop any ongoing SignalInfo tone
709 mSignalInfoToneGenerator.stopTone();
710
711 //Start playing the new tone if its a valid tone
712 mSignalInfoToneGenerator.startTone(mToneId);
713 }
714 }
715 }
716
717 /**
718 * Plays a tone when the phone receives a SignalInfo record.
719 */
720 private void onSignalInfo(AsyncResult r) {
721 // Signal Info are totally ignored on non-voice-capable devices.
722 if (!PhoneGlobals.sVoiceCapable) {
723 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
724 return;
725 }
726
727 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
728 // Do not start any new SignalInfo tone when Call state is INCOMING
729 // and stop any previous SignalInfo tone which is being played
730 stopSignalInfoTone();
731 } else {
732 // Extract the SignalInfo String from the message
733 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
734 // Only proceed if a Signal info is present.
735 if (signalInfoRec != null) {
736 boolean isPresent = signalInfoRec.isPresent;
737 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
738 if (isPresent) {// if tone is valid
739 int uSignalType = signalInfoRec.signalType;
740 int uAlertPitch = signalInfoRec.alertPitch;
741 int uSignal = signalInfoRec.signal;
742
743 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
744 uAlertPitch + ", uSignal=" + uSignal);
745 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
746 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
747 (uSignalType, uAlertPitch, uSignal);
748
749 //Create the SignalInfo tone player and pass the ToneID
750 new SignalInfoTonePlayer(toneID).start();
751 }
752 }
753 }
754 }
755
756 /**
757 * Stops a SignalInfo tone in the following condition
758 * 1 - On receiving a New Ringing Call
759 * 2 - On disconnecting a call
760 * 3 - On answering a Call Waiting Call
761 */
762 /* package */ void stopSignalInfoTone() {
763 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
764 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
765 }
766
Santos Cordon5c046722014-09-18 15:41:13 -0700767 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800768 new BluetoothProfile.ServiceListener() {
769 public void onServiceConnected(int profile, BluetoothProfile proxy) {
770 mBluetoothHeadset = (BluetoothHeadset) proxy;
771 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
772 }
773
774 public void onServiceDisconnected(int profile) {
775 mBluetoothHeadset = null;
776 }
777 };
778
779 private class CallNotifierPhoneStateListener extends PhoneStateListener {
780 public CallNotifierPhoneStateListener(int subId) {
781 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 }
783
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800784 @Override
785 public void onMessageWaitingIndicatorChanged(boolean visible) {
786 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900787 mMWIStatus.put(this.mSubId, visible);
788 updatePhoneStateListeners(false);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800789 }
790
791 @Override
792 public void onCallForwardingIndicatorChanged(boolean visible) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700793 Log.i(LOG_TAG, "onCallForwardingIndicatorChanged(): subId=" + this.mSubId
794 + ", visible=" + (visible ? "Y" : "N"));
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900795 mCFIStatus.put(this.mSubId, visible);
796 updatePhoneStateListeners(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700797 }
798 };
799
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 private void log(String msg) {
801 Log.d(LOG_TAG, msg);
802 }
803}