blob: 476ff064bfee7946b435087cc8c05a9c93de7c95 [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
Andrew Lee2fcb6c32014-12-04 14:52:35 -080047import java.util.Iterator;
48import java.util.List;
49import java.util.Map;
50
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051/**
52 * Phone app module that listens for phone state changes and various other
53 * events from the telephony layer, and triggers any resulting UI behavior
Santos Cordon5422a8d2014-09-12 04:20:56 -070054 * (like starting the Incoming Call UI, playing in-call tones,
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055 * updating notifications, writing call log entries, etc.)
56 */
Santos Cordon5422a8d2014-09-12 04:20:56 -070057public class CallNotifier extends Handler {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058 private static final String LOG_TAG = "CallNotifier";
59 private static final boolean DBG =
60 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
61 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
62
Anthony Leee9468532014-11-15 15:21:00 -080063 // Time to display the message from the underlying phone layers.
64 private static final int SHOW_MESSAGE_NOTIFICATION_TIME = 3000; // msec
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065
66 /** The singleton instance. */
67 private static CallNotifier sInstance;
68
Andrew Lee2fcb6c32014-12-04 14:52:35 -080069 private Map<Integer, CallNotifierPhoneStateListener> mPhoneStateListeners =
70 new ArrayMap<Integer, CallNotifierPhoneStateListener>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072 private PhoneGlobals mApplication;
73 private CallManager mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074 private BluetoothHeadset mBluetoothHeadset;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
76 // ToneGenerator instance for playing SignalInfo tones
77 private ToneGenerator mSignalInfoToneGenerator;
78
79 // The tone volume relative to other sounds in the stream SignalInfo
80 private static final int TONE_RELATIVE_VOLUME_SIGNALINFO = 80;
81
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082 private boolean mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 // Cached AudioManager
85 private AudioManager mAudioManager;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080086 private SubscriptionManager mSubscriptionManager;
87 private TelephonyManager mTelephonyManager;
Santos Cordon27a3c1f2013-08-06 07:49:27 -070088
Brad Ebingera9c6b6d2016-01-07 17:24:16 -080089 // Events from the Phone object:
90 public static final int PHONE_DISCONNECT = 3;
91 public static final int PHONE_STATE_DISPLAYINFO = 6;
92 public static final int PHONE_STATE_SIGNALINFO = 7;
93 public static final int PHONE_ENHANCED_VP_ON = 9;
94 public static final int PHONE_ENHANCED_VP_OFF = 10;
95 public static final int PHONE_SUPP_SERVICE_FAILED = 14;
96 public static final int PHONE_TTY_MODE_RECEIVED = 15;
97 // Events generated internally.
98 // We should store all the possible event type values in one place to make sure that
99 // they don't step on each others' toes.
100 public static final int INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE = 22;
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800101
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 /**
103 * Initialize the singleton CallNotifier instance.
104 * This is only done once, at startup, from PhoneApp.onCreate().
105 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800106 /* package */ static CallNotifier init(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800107 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 synchronized (CallNotifier.class) {
109 if (sInstance == null) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800110 sInstance = new CallNotifier(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 } else {
112 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
113 }
114 return sInstance;
115 }
116 }
117
118 /** Private constructor; @see init() */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800119 private CallNotifier(
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800120 PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 mApplication = app;
122 mCM = app.mCM;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123
124 mAudioManager = (AudioManager) mApplication.getSystemService(Context.AUDIO_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800125 mTelephonyManager =
126 (TelephonyManager) mApplication.getSystemService(Context.TELEPHONY_SERVICE);
127 mSubscriptionManager = (SubscriptionManager) mApplication.getSystemService(
128 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800130 registerForNotifications();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
133 if (adapter != null) {
134 adapter.getProfileProxy(mApplication.getApplicationContext(),
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800135 mBluetoothProfileServiceListener,
136 BluetoothProfile.HEADSET);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137 }
138
Wink Savillef67832f2015-01-12 16:51:50 -0800139 mSubscriptionManager.addOnSubscriptionsChangedListener(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800140 new OnSubscriptionsChangedListener() {
141 @Override
142 public void onSubscriptionsChanged() {
143 updatePhoneStateListeners();
144 }
145 });
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 }
147
148 private void createSignalInfoToneGenerator() {
149 // Instantiate the ToneGenerator for SignalInfo and CallWaiting
150 // TODO: We probably don't need the mSignalInfoToneGenerator instance
151 // around forever. Need to change it so as to create a ToneGenerator instance only
152 // when a tone is being played and releases it after its done playing.
153 if (mSignalInfoToneGenerator == null) {
154 try {
155 mSignalInfoToneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL,
156 TONE_RELATIVE_VOLUME_SIGNALINFO);
157 Log.d(LOG_TAG, "CallNotifier: mSignalInfoToneGenerator created when toneplay");
158 } catch (RuntimeException e) {
159 Log.w(LOG_TAG, "CallNotifier: Exception caught while creating " +
160 "mSignalInfoToneGenerator: " + e);
161 mSignalInfoToneGenerator = null;
162 }
163 } else {
164 Log.d(LOG_TAG, "mSignalInfoToneGenerator created already, hence skipping");
165 }
166 }
167
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800168 /**
169 * Register for call state notifications with the CallManager.
170 */
171 private void registerForNotifications() {
172 mCM.registerForDisconnect(this, PHONE_DISCONNECT, null);
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800173 mCM.registerForDisplayInfo(this, PHONE_STATE_DISPLAYINFO, null);
174 mCM.registerForSignalInfo(this, PHONE_STATE_SIGNALINFO, null);
175 mCM.registerForInCallVoicePrivacyOn(this, PHONE_ENHANCED_VP_ON, null);
176 mCM.registerForInCallVoicePrivacyOff(this, PHONE_ENHANCED_VP_OFF, null);
177 mCM.registerForSuppServiceFailed(this, PHONE_SUPP_SERVICE_FAILED, null);
178 mCM.registerForTtyModeReceived(this, PHONE_TTY_MODE_RECEIVED, null);
179 }
180
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 @Override
182 public void handleMessage(Message msg) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800183 if (DBG) {
184 Log.d(LOG_TAG, "handleMessage(" + msg.what + ")");
185 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 switch (msg.what) {
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800187 case PHONE_DISCONNECT:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 if (DBG) log("DISCONNECT");
Roshan Pius19f39cf2015-08-12 10:44:38 -0700189 // Stop any signalInfo tone being played when a call gets ended, the rest of the
190 // disconnect functionality in onDisconnect() is handled in ConnectionService.
191 stopSignalInfoTone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 break;
193
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800194 case PHONE_STATE_DISPLAYINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 if (DBG) log("Received PHONE_STATE_DISPLAYINFO event");
196 onDisplayInfo((AsyncResult) msg.obj);
197 break;
198
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800199 case PHONE_STATE_SIGNALINFO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 if (DBG) log("Received PHONE_STATE_SIGNALINFO event");
201 onSignalInfo((AsyncResult) msg.obj);
202 break;
203
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800204 case INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 if (DBG) log("Received Display Info notification done event ...");
Anthony Leee9468532014-11-15 15:21:00 -0800206 PhoneDisplayMessage.dismissMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 break;
208
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800209 case PHONE_ENHANCED_VP_ON:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 if (DBG) log("PHONE_ENHANCED_VP_ON...");
211 if (!mVoicePrivacyState) {
212 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
213 new InCallTonePlayer(toneToPlay).start();
214 mVoicePrivacyState = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 }
216 break;
217
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800218 case PHONE_ENHANCED_VP_OFF:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219 if (DBG) log("PHONE_ENHANCED_VP_OFF...");
220 if (mVoicePrivacyState) {
221 int toneToPlay = InCallTonePlayer.TONE_VOICE_PRIVACY;
222 new InCallTonePlayer(toneToPlay).start();
223 mVoicePrivacyState = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 }
225 break;
226
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800227 case PHONE_SUPP_SERVICE_FAILED:
Anthony Leee9468532014-11-15 15:21:00 -0800228 if (DBG) log("PHONE_SUPP_SERVICE_FAILED...");
229 onSuppServiceFailed((AsyncResult) msg.obj);
230 break;
231
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800232 case PHONE_TTY_MODE_RECEIVED:
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800233 if (DBG) log("Received PHONE_TTY_MODE_RECEIVED event");
234 onTtyModeReceived((AsyncResult) msg.obj);
235 break;
236
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 default:
238 // super.handleMessage(msg);
239 }
240 }
241
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 void updateCallNotifierRegistrationsAfterRadioTechnologyChange() {
243 if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
244
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245 // Instantiate mSignalInfoToneGenerator
246 createSignalInfoToneGenerator();
247 }
248
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 /**
250 * Resets the audio mode and speaker state when a call ends.
251 */
252 private void resetAudioStateAfterDisconnect() {
253 if (VDBG) log("resetAudioStateAfterDisconnect()...");
254
255 if (mBluetoothHeadset != null) {
256 mBluetoothHeadset.disconnectAudio();
257 }
258
259 // call turnOnSpeaker() with state=false and store=true even if speaker
260 // is already off to reset user requested speaker state.
261 PhoneUtils.turnOnSpeaker(mApplication, false, true);
262
263 PhoneUtils.setAudioMode(mCM);
264 }
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 * Helper class to play tones through the earpiece (or speaker / BT)
268 * during a call, using the ToneGenerator.
269 *
270 * To use, just instantiate a new InCallTonePlayer
271 * (passing in the TONE_* constant for the tone you want)
272 * and start() it.
273 *
274 * When we're done playing the tone, if the phone is idle at that
275 * point, we'll reset the audio routing and speaker state.
276 * (That means that for tones that get played *after* a call
277 * disconnects, like "busy" or "congestion" or "call ended", you
278 * should NOT call resetAudioStateAfterDisconnect() yourself.
279 * Instead, just start the InCallTonePlayer, which will automatically
280 * defer the resetAudioStateAfterDisconnect() call until the tone
281 * finishes playing.)
282 */
283 private class InCallTonePlayer extends Thread {
284 private int mToneId;
285 private int mState;
286 // The possible tones we can play.
287 public static final int TONE_NONE = 0;
288 public static final int TONE_CALL_WAITING = 1;
289 public static final int TONE_BUSY = 2;
290 public static final int TONE_CONGESTION = 3;
291 public static final int TONE_CALL_ENDED = 4;
292 public static final int TONE_VOICE_PRIVACY = 5;
293 public static final int TONE_REORDER = 6;
294 public static final int TONE_INTERCEPT = 7;
295 public static final int TONE_CDMA_DROP = 8;
296 public static final int TONE_OUT_OF_SERVICE = 9;
297 public static final int TONE_REDIAL = 10;
298 public static final int TONE_OTA_CALL_END = 11;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 public static final int TONE_UNOBTAINABLE_NUMBER = 13;
300
301 // The tone volume relative to other sounds in the stream
302 static final int TONE_RELATIVE_VOLUME_EMERGENCY = 100;
303 static final int TONE_RELATIVE_VOLUME_HIPRI = 80;
304 static final int TONE_RELATIVE_VOLUME_LOPRI = 50;
305
306 // Buffer time (in msec) to add on to tone timeout value.
307 // Needed mainly when the timeout value for a tone is the
308 // exact duration of the tone itself.
309 static final int TONE_TIMEOUT_BUFFER = 20;
310
311 // The tone state
312 static final int TONE_OFF = 0;
313 static final int TONE_ON = 1;
314 static final int TONE_STOPPED = 2;
315
316 InCallTonePlayer(int toneId) {
317 super();
318 mToneId = toneId;
319 mState = TONE_OFF;
320 }
321
322 @Override
323 public void run() {
324 log("InCallTonePlayer.run(toneId = " + mToneId + ")...");
325
326 int toneType = 0; // passed to ToneGenerator.startTone()
327 int toneVolume; // passed to the ToneGenerator constructor
328 int toneLengthMillis;
329 int phoneType = mCM.getFgPhone().getPhoneType();
330
331 switch (mToneId) {
332 case TONE_CALL_WAITING:
333 toneType = ToneGenerator.TONE_SUP_CALL_WAITING;
334 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
335 // Call waiting tone is stopped by stopTone() method
336 toneLengthMillis = Integer.MAX_VALUE - TONE_TIMEOUT_BUFFER;
337 break;
338 case TONE_BUSY:
339 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
340 toneType = ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT;
341 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
342 toneLengthMillis = 1000;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800343 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM
344 || phoneType == PhoneConstants.PHONE_TYPE_SIP
Etan Cohen0ca1c802014-07-07 15:35:48 -0700345 || phoneType == PhoneConstants.PHONE_TYPE_IMS
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800346 || phoneType == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 toneType = ToneGenerator.TONE_SUP_BUSY;
348 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
349 toneLengthMillis = 4000;
350 } else {
351 throw new IllegalStateException("Unexpected phone type: " + phoneType);
352 }
353 break;
354 case TONE_CONGESTION:
355 toneType = ToneGenerator.TONE_SUP_CONGESTION;
356 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
357 toneLengthMillis = 4000;
358 break;
359
360 case TONE_CALL_ENDED:
361 toneType = ToneGenerator.TONE_PROP_PROMPT;
362 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
363 toneLengthMillis = 200;
364 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 case TONE_VOICE_PRIVACY:
366 toneType = ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE;
367 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
368 toneLengthMillis = 5000;
369 break;
370 case TONE_REORDER:
371 toneType = ToneGenerator.TONE_CDMA_REORDER;
372 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
373 toneLengthMillis = 4000;
374 break;
375 case TONE_INTERCEPT:
376 toneType = ToneGenerator.TONE_CDMA_ABBR_INTERCEPT;
377 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
378 toneLengthMillis = 500;
379 break;
380 case TONE_CDMA_DROP:
381 case TONE_OUT_OF_SERVICE:
382 toneType = ToneGenerator.TONE_CDMA_CALLDROP_LITE;
383 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
384 toneLengthMillis = 375;
385 break;
386 case TONE_REDIAL:
387 toneType = ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE;
388 toneVolume = TONE_RELATIVE_VOLUME_LOPRI;
389 toneLengthMillis = 5000;
390 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 case TONE_UNOBTAINABLE_NUMBER:
392 toneType = ToneGenerator.TONE_SUP_ERROR;
393 toneVolume = TONE_RELATIVE_VOLUME_HIPRI;
394 toneLengthMillis = 4000;
395 break;
396 default:
397 throw new IllegalArgumentException("Bad toneId: " + mToneId);
398 }
399
400 // If the mToneGenerator creation fails, just continue without it. It is
401 // a local audio signal, and is not as important.
402 ToneGenerator toneGenerator;
403 try {
404 int stream;
405 if (mBluetoothHeadset != null) {
406 stream = mBluetoothHeadset.isAudioOn() ? AudioManager.STREAM_BLUETOOTH_SCO:
407 AudioManager.STREAM_VOICE_CALL;
408 } else {
409 stream = AudioManager.STREAM_VOICE_CALL;
410 }
411 toneGenerator = new ToneGenerator(stream, toneVolume);
412 // if (DBG) log("- created toneGenerator: " + toneGenerator);
413 } catch (RuntimeException e) {
414 Log.w(LOG_TAG,
415 "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
416 toneGenerator = null;
417 }
418
419 // Using the ToneGenerator (with the CALL_WAITING / BUSY /
420 // CONGESTION tones at least), the ToneGenerator itself knows
421 // the right pattern of tones to play; we do NOT need to
422 // manually start/stop each individual tone, or manually
423 // insert the correct delay between tones. (We just start it
424 // and let it run for however long we want the tone pattern to
425 // continue.)
426 //
427 // TODO: When we stop the ToneGenerator in the middle of a
428 // "tone pattern", it sounds bad if we cut if off while the
429 // tone is actually playing. Consider adding API to the
430 // ToneGenerator to say "stop at the next silent part of the
431 // pattern", or simply "play the pattern N times and then
432 // stop."
433 boolean needToStopTone = true;
434 boolean okToPlayTone = false;
435
436 if (toneGenerator != null) {
437 int ringerMode = mAudioManager.getRingerMode();
438 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
439 if (toneType == ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD) {
440 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
441 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
442 if (DBG) log("- InCallTonePlayer: start playing call tone=" + toneType);
443 okToPlayTone = true;
444 needToStopTone = false;
445 }
446 } else if ((toneType == ToneGenerator.TONE_CDMA_NETWORK_BUSY_ONE_SHOT) ||
447 (toneType == ToneGenerator.TONE_CDMA_REORDER) ||
448 (toneType == ToneGenerator.TONE_CDMA_ABBR_REORDER) ||
449 (toneType == ToneGenerator.TONE_CDMA_ABBR_INTERCEPT) ||
450 (toneType == ToneGenerator.TONE_CDMA_CALLDROP_LITE)) {
451 if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
452 if (DBG) log("InCallTonePlayer:playing call fail tone:" + toneType);
453 okToPlayTone = true;
454 needToStopTone = false;
455 }
456 } else if ((toneType == ToneGenerator.TONE_CDMA_ALERT_AUTOREDIAL_LITE) ||
457 (toneType == ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE)) {
458 if ((ringerMode != AudioManager.RINGER_MODE_SILENT) &&
459 (ringerMode != AudioManager.RINGER_MODE_VIBRATE)) {
460 if (DBG) log("InCallTonePlayer:playing tone for toneType=" + toneType);
461 okToPlayTone = true;
462 needToStopTone = false;
463 }
464 } else { // For the rest of the tones, always OK to play.
465 okToPlayTone = true;
466 }
467 } else { // Not "CDMA"
468 okToPlayTone = true;
469 }
470
471 synchronized (this) {
472 if (okToPlayTone && mState != TONE_STOPPED) {
473 mState = TONE_ON;
474 toneGenerator.startTone(toneType);
475 try {
476 wait(toneLengthMillis + TONE_TIMEOUT_BUFFER);
477 } catch (InterruptedException e) {
478 Log.w(LOG_TAG,
479 "InCallTonePlayer stopped: " + e);
480 }
481 if (needToStopTone) {
482 toneGenerator.stopTone();
483 }
484 }
485 // if (DBG) log("- InCallTonePlayer: done playing.");
486 toneGenerator.release();
487 mState = TONE_OFF;
488 }
489 }
490
491 // Finally, do the same cleanup we otherwise would have done
492 // in onDisconnect().
493 //
494 // (But watch out: do NOT do this if the phone is in use,
495 // since some of our tones get played *during* a call (like
496 // CALL_WAITING) and we definitely *don't*
497 // want to reset the audio mode / speaker / bluetooth after
498 // playing those!
499 // This call is really here for use with tones that get played
500 // *after* a call disconnects, like "busy" or "congestion" or
501 // "call ended", where the phone has already become idle but
502 // we need to defer the resetAudioStateAfterDisconnect() call
503 // till the tone finishes playing.)
504 if (mCM.getState() == PhoneConstants.State.IDLE) {
505 resetAudioStateAfterDisconnect();
506 }
507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 }
509
510 /**
511 * Displays a notification when the phone receives a DisplayInfo record.
512 */
513 private void onDisplayInfo(AsyncResult r) {
514 // Extract the DisplayInfo String from the message
515 CdmaDisplayInfoRec displayInfoRec = (CdmaDisplayInfoRec)(r.result);
516
517 if (displayInfoRec != null) {
518 String displayInfo = displayInfoRec.alpha;
519 if (DBG) log("onDisplayInfo: displayInfo=" + displayInfo);
Anthony Leee9468532014-11-15 15:21:00 -0800520 PhoneDisplayMessage.displayNetworkMessage(mApplication, displayInfo);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521
Anthony Leee9468532014-11-15 15:21:00 -0800522 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800523 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800524 SHOW_MESSAGE_NOTIFICATION_TIME);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 }
526 }
527
528 /**
Anthony Leee9468532014-11-15 15:21:00 -0800529 * Displays a notification when the phone receives a notice that a supplemental
530 * service has failed.
Anthony Leee9468532014-11-15 15:21:00 -0800531 */
532 private void onSuppServiceFailed(AsyncResult r) {
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800533 String mergeFailedString = "";
534 if (r.result == Phone.SuppService.CONFERENCE) {
535 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
536 mergeFailedString = mApplication.getResources().getString(
537 R.string.incall_error_supp_service_conference);
538 } else if (r.result == Phone.SuppService.RESUME) {
539 if (DBG) log("onSuppServiceFailed: displaying merge failure message");
540 mergeFailedString = mApplication.getResources().getString(
541 R.string.incall_error_supp_service_switch);
Anju Mathapati4effeb22016-01-25 22:25:09 -0800542 } else if (r.result == Phone.SuppService.HOLD) {
Tyler Gunna7de7d32017-06-09 16:21:00 -0700543 if (DBG) log("onSuppServiceFailed: displaying hold failure message");
Anju Mathapati4effeb22016-01-25 22:25:09 -0800544 mergeFailedString = mApplication.getResources().getString(
545 R.string.incall_error_supp_service_hold);
Tyler Gunna7de7d32017-06-09 16:21:00 -0700546 } else if (r.result == Phone.SuppService.TRANSFER) {
547 if (DBG) log("onSuppServiceFailed: displaying transfer failure message");
548 mergeFailedString = mApplication.getResources().getString(
549 R.string.incall_error_supp_service_transfer);
550 } else if (r.result == Phone.SuppService.SEPARATE) {
551 if (DBG) log("onSuppServiceFailed: displaying separate failure message");
552 mergeFailedString = mApplication.getResources().getString(
553 R.string.incall_error_supp_service_separate);
554 } else if (r.result == Phone.SuppService.SWITCH) {
555 if (DBG) log("onSuppServiceFailed: displaying switch failure message");
556 mApplication.getResources().getString(
557 R.string.incall_error_supp_service_switch);
558 } else if (r.result == Phone.SuppService.REJECT) {
559 if (DBG) log("onSuppServiceFailed: displaying reject failure message");
560 mApplication.getResources().getString(
561 R.string.incall_error_supp_service_reject);
562 } else {
563 if (DBG) log("onSuppServiceFailed: unknown failure");
564 return;
Tyler Gunn9dd07d02014-12-08 10:52:57 -0800565 }
Tyler Gunna7de7d32017-06-09 16:21:00 -0700566
Anthony Leee9468532014-11-15 15:21:00 -0800567 PhoneDisplayMessage.displayErrorMessage(mApplication, mergeFailedString);
568
569 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800570 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Anthony Leee9468532014-11-15 15:21:00 -0800571 SHOW_MESSAGE_NOTIFICATION_TIME);
572 }
573
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800574 public void updatePhoneStateListeners() {
575 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
576
577 // Unregister phone listeners for inactive subscriptions.
578 Iterator<Integer> itr = mPhoneStateListeners.keySet().iterator();
579 while (itr.hasNext()) {
580 int subId = itr.next();
581 if (subInfos == null || !containsSubId(subInfos, subId)) {
582 // Hide the outstanding notifications.
583 mApplication.notificationMgr.updateMwi(subId, false);
584 mApplication.notificationMgr.updateCfi(subId, false);
585
586 // Listening to LISTEN_NONE removes the listener.
587 mTelephonyManager.listen(
588 mPhoneStateListeners.get(subId), PhoneStateListener.LISTEN_NONE);
589 itr.remove();
590 }
591 }
592
593 if (subInfos == null) {
594 return;
595 }
596
597 // Register new phone listeners for active subscriptions.
598 for (int i = 0; i < subInfos.size(); i++) {
599 int subId = subInfos.get(i).getSubscriptionId();
600 if (!mPhoneStateListeners.containsKey(subId)) {
601 CallNotifierPhoneStateListener listener = new CallNotifierPhoneStateListener(subId);
602 mTelephonyManager.listen(listener,
603 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
604 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR);
605 mPhoneStateListeners.put(subId, listener);
606 }
607 }
608 }
609
610 /**
611 * @return {@code true} if the list contains SubscriptionInfo with the given subscription id.
612 */
613 private boolean containsSubId(List<SubscriptionInfo> subInfos, int subId) {
614 if (subInfos == null) {
615 return false;
616 }
617
618 for (int i = 0; i < subInfos.size(); i++) {
619 if (subInfos.get(i).getSubscriptionId() == subId) {
620 return true;
621 }
622 }
623 return false;
624 }
625
Anthony Leee9468532014-11-15 15:21:00 -0800626 /**
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800627 * Displays a notification when the phone receives a notice that TTY mode
628 * has changed on remote end.
629 */
630 private void onTtyModeReceived(AsyncResult r) {
631 if (DBG) log("TtyModeReceived: displaying notification message");
632
633 int resId = 0;
634 switch (((Integer)r.result).intValue()) {
635 case TelecomManager.TTY_MODE_FULL:
636 resId = com.android.internal.R.string.peerTtyModeFull;
637 break;
638 case TelecomManager.TTY_MODE_HCO:
639 resId = com.android.internal.R.string.peerTtyModeHco;
640 break;
641 case TelecomManager.TTY_MODE_VCO:
642 resId = com.android.internal.R.string.peerTtyModeVco;
643 break;
644 case TelecomManager.TTY_MODE_OFF:
645 resId = com.android.internal.R.string.peerTtyModeOff;
646 break;
647 default:
648 Log.e(LOG_TAG, "Unsupported TTY mode: " + r.result);
649 break;
650 }
651 if (resId != 0) {
652 PhoneDisplayMessage.displayNetworkMessage(mApplication,
653 mApplication.getResources().getString(resId));
654
655 // start a timer that kills the dialog
Brad Ebingera9c6b6d2016-01-07 17:24:16 -0800656 sendEmptyMessageDelayed(INTERNAL_SHOW_MESSAGE_NOTIFICATION_DONE,
Pavel Zhamaitsiak82256c02014-12-10 17:11:40 -0800657 SHOW_MESSAGE_NOTIFICATION_TIME);
658 }
659 }
660
661 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 * Helper class to play SignalInfo tones using the ToneGenerator.
663 *
664 * To use, just instantiate a new SignalInfoTonePlayer
665 * (passing in the ToneID constant for the tone you want)
666 * and start() it.
667 */
668 private class SignalInfoTonePlayer extends Thread {
669 private int mToneId;
670
671 SignalInfoTonePlayer(int toneId) {
672 super();
673 mToneId = toneId;
674 }
675
676 @Override
677 public void run() {
678 log("SignalInfoTonePlayer.run(toneId = " + mToneId + ")...");
Yorke Lee65cbd162014-10-08 11:26:02 -0700679 createSignalInfoToneGenerator();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 if (mSignalInfoToneGenerator != null) {
681 //First stop any ongoing SignalInfo tone
682 mSignalInfoToneGenerator.stopTone();
683
684 //Start playing the new tone if its a valid tone
685 mSignalInfoToneGenerator.startTone(mToneId);
686 }
687 }
688 }
689
690 /**
691 * Plays a tone when the phone receives a SignalInfo record.
692 */
693 private void onSignalInfo(AsyncResult r) {
694 // Signal Info are totally ignored on non-voice-capable devices.
695 if (!PhoneGlobals.sVoiceCapable) {
696 Log.w(LOG_TAG, "Got onSignalInfo() on non-voice-capable device! Ignoring...");
697 return;
698 }
699
700 if (PhoneUtils.isRealIncomingCall(mCM.getFirstActiveRingingCall().getState())) {
701 // Do not start any new SignalInfo tone when Call state is INCOMING
702 // and stop any previous SignalInfo tone which is being played
703 stopSignalInfoTone();
704 } else {
705 // Extract the SignalInfo String from the message
706 CdmaSignalInfoRec signalInfoRec = (CdmaSignalInfoRec)(r.result);
707 // Only proceed if a Signal info is present.
708 if (signalInfoRec != null) {
709 boolean isPresent = signalInfoRec.isPresent;
710 if (DBG) log("onSignalInfo: isPresent=" + isPresent);
711 if (isPresent) {// if tone is valid
712 int uSignalType = signalInfoRec.signalType;
713 int uAlertPitch = signalInfoRec.alertPitch;
714 int uSignal = signalInfoRec.signal;
715
716 if (DBG) log("onSignalInfo: uSignalType=" + uSignalType + ", uAlertPitch=" +
717 uAlertPitch + ", uSignal=" + uSignal);
718 //Map the Signal to a ToneGenerator ToneID only if Signal info is present
719 int toneID = SignalToneUtil.getAudioToneFromSignalInfo
720 (uSignalType, uAlertPitch, uSignal);
721
722 //Create the SignalInfo tone player and pass the ToneID
723 new SignalInfoTonePlayer(toneID).start();
724 }
725 }
726 }
727 }
728
729 /**
730 * Stops a SignalInfo tone in the following condition
731 * 1 - On receiving a New Ringing Call
732 * 2 - On disconnecting a call
733 * 3 - On answering a Call Waiting Call
734 */
735 /* package */ void stopSignalInfoTone() {
736 if (DBG) log("stopSignalInfoTone: Stopping SignalInfo tone player");
737 new SignalInfoTonePlayer(ToneGenerator.TONE_CDMA_SIGNAL_OFF).start();
738 }
739
Santos Cordon5c046722014-09-18 15:41:13 -0700740 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800741 new BluetoothProfile.ServiceListener() {
742 public void onServiceConnected(int profile, BluetoothProfile proxy) {
743 mBluetoothHeadset = (BluetoothHeadset) proxy;
744 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
745 }
746
747 public void onServiceDisconnected(int profile) {
748 mBluetoothHeadset = null;
749 }
750 };
751
752 private class CallNotifierPhoneStateListener extends PhoneStateListener {
753 public CallNotifierPhoneStateListener(int subId) {
754 super(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 }
756
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800757 @Override
758 public void onMessageWaitingIndicatorChanged(boolean visible) {
759 if (VDBG) log("onMessageWaitingIndicatorChanged(): " + this.mSubId + " " + visible);
760 mApplication.notificationMgr.updateMwi(this.mSubId, visible);
761 }
762
763 @Override
764 public void onCallForwardingIndicatorChanged(boolean visible) {
765 if (VDBG) log("onCallForwardingIndicatorChanged(): " + this.mSubId + " " + visible);
766 mApplication.notificationMgr.updateCfi(this.mSubId, visible);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 }
768 };
769
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 private void log(String msg) {
771 Log.d(LOG_TAG, msg);
772 }
773}