Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 18 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 20 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 22 | import android.os.Handler; |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 23 | import android.os.Looper; |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 24 | import android.os.SystemProperties; |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 25 | import android.os.Trace; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 26 | import android.provider.CallLog.Calls; |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 27 | import android.telecom.CallAudioState; |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 28 | import android.telecom.Conference; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 29 | import android.telecom.Connection; |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 30 | import android.telecom.DisconnectCause; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 31 | import android.telecom.GatewayInfo; |
| 32 | import android.telecom.ParcelableConference; |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 33 | import android.telecom.ParcelableConnection; |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 34 | import android.telecom.PhoneAccount; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 35 | import android.telecom.PhoneAccountHandle; |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 36 | import android.telecom.TelecomManager; |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 37 | import android.telecom.VideoProfile; |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 38 | import android.telephony.PhoneNumberUtils; |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 39 | import android.telephony.TelephonyManager; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 40 | |
Ihab Awad | a365390 | 2015-01-23 13:44:46 -0800 | [diff] [blame] | 41 | import com.android.internal.annotations.VisibleForTesting; |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 42 | import com.android.internal.telephony.PhoneConstants; |
| 43 | import com.android.internal.telephony.TelephonyProperties; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 44 | import com.android.internal.util.IndentingPrintWriter; |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 45 | |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 46 | import java.util.Collection; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 47 | import java.util.Collections; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 48 | import java.util.HashSet; |
Santos Cordon | 7cdb909 | 2014-07-24 21:33:33 -0700 | [diff] [blame] | 49 | import java.util.List; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 50 | import java.util.Objects; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 51 | import java.util.Set; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 52 | import java.util.concurrent.ConcurrentHashMap; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 53 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Singleton. |
| 56 | * |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 57 | * NOTE: by design most APIs are package private, use the relevant adapter/s to allow |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 58 | * access from other packages specifically refraining from passing the CallsManager instance |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 59 | * beyond the com.android.server.telecom package boundary. |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 60 | */ |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 61 | @VisibleForTesting |
| 62 | public class CallsManager extends Call.ListenerBase { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 63 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 64 | // TODO: Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 65 | interface CallsManagerListener { |
| 66 | void onCallAdded(Call call); |
| 67 | void onCallRemoved(Call call); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 68 | void onCallStateChanged(Call call, int oldState, int newState); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 69 | void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 70 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 71 | ConnectionServiceWrapper oldService, |
| 72 | ConnectionServiceWrapper newService); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 73 | void onIncomingCallAnswered(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 74 | void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 75 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 76 | void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 77 | void onRingbackRequested(Call call, boolean ringback); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 78 | void onIsConferencedChanged(Call call); |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 79 | void onIsVoipAudioModeChanged(Call call); |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 80 | void onVideoStateChanged(Call call); |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 81 | void onCanAddCallChanged(boolean canAddCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 84 | private static final String TAG = "CallsManager"; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 85 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 86 | private static final int MAXIMUM_LIVE_CALLS = 1; |
| 87 | private static final int MAXIMUM_HOLD_CALLS = 1; |
| 88 | private static final int MAXIMUM_RINGING_CALLS = 1; |
| 89 | private static final int MAXIMUM_OUTGOING_CALLS = 1; |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 90 | private static final int MAXIMUM_TOP_LEVEL_CALLS = 2; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 91 | |
| 92 | private static final int[] OUTGOING_CALL_STATES = |
Santos Cordon | 9269451 | 2015-04-23 14:47:28 -0700 | [diff] [blame] | 93 | {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING}; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 94 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 95 | private static final int[] LIVE_CALL_STATES = |
Santos Cordon | 9269451 | 2015-04-23 14:47:28 -0700 | [diff] [blame] | 96 | {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING, CallState.ACTIVE}; |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 97 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 98 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 99 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 100 | * calls are added to the map and removed when the calls move to the disconnected state. |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 101 | * |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 102 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 103 | * load factor before resizing, 1 means we only expect a single thread to |
| 104 | * access the map so make only a single shard |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 105 | */ |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 106 | private final Set<Call> mCalls = Collections.newSetFromMap( |
| 107 | new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1)); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 108 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 109 | private final ConnectionServiceRepository mConnectionServiceRepository; |
| 110 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer; |
| 111 | private final InCallController mInCallController; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 112 | private final CallAudioManager mCallAudioManager; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 113 | private RespondViaSmsManager mRespondViaSmsManager; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 114 | private final Ringer mRinger; |
Santos Cordon | 61e6f35 | 2014-12-03 02:53:34 -0800 | [diff] [blame] | 115 | private final InCallWakeLockController mInCallWakeLockController; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 116 | // For this set initial table size to 16 because we add 13 listeners in |
| 117 | // the CallsManager constructor. |
| 118 | private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap( |
| 119 | new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1)); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 120 | private final HeadsetMediaButton mHeadsetMediaButton; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 121 | private final WiredHeadsetManager mWiredHeadsetManager; |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 122 | private final DockManager mDockManager; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 123 | private final TtyManager mTtyManager; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 124 | private final ProximitySensorManager mProximitySensorManager; |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 125 | private final PhoneStateBroadcaster mPhoneStateBroadcaster; |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 126 | private final CallLogManager mCallLogManager; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 127 | private final Context mContext; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 128 | private final TelecomSystem.SyncRoot mLock; |
| 129 | private final ContactsAsyncHelper mContactsAsyncHelper; |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 130 | private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 131 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
| 132 | private final MissedCallNotifier mMissedCallNotifier; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 133 | private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 134 | private final Set<Call> mPendingCallsToDisconnect = new HashSet<>(); |
| 135 | /* Handler tied to thread in which CallManager was initialized. */ |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 136 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 137 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 138 | private boolean mCanAddCall = true; |
| 139 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 140 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 141 | * The call the user is currently interacting with. This is the call that should have audio |
| 142 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 143 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 144 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 145 | |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 146 | private Runnable mStopTone; |
| 147 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 148 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 149 | * Initializes the required Telecom components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 150 | */ |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 151 | CallsManager( |
| 152 | Context context, |
| 153 | TelecomSystem.SyncRoot lock, |
| 154 | ContactsAsyncHelper contactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 155 | CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 156 | MissedCallNotifier missedCallNotifier, |
| 157 | PhoneAccountRegistrar phoneAccountRegistrar, |
| 158 | HeadsetMediaButtonFactory headsetMediaButtonFactory, |
| 159 | ProximitySensorManagerFactory proximitySensorManagerFactory, |
| 160 | InCallWakeLockControllerFactory inCallWakeLockControllerFactory) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 161 | mContext = context; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 162 | mLock = lock; |
| 163 | mContactsAsyncHelper = contactsAsyncHelper; |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 164 | mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 165 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
| 166 | mMissedCallNotifier = missedCallNotifier; |
| 167 | StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this); |
| 168 | mWiredHeadsetManager = new WiredHeadsetManager(context); |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 169 | mDockManager = new DockManager(context); |
| 170 | mCallAudioManager = new CallAudioManager( |
Ihab Awad | b60f006 | 2015-05-26 16:20:32 -0700 | [diff] [blame] | 171 | context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this); |
Vinit Deshpande | 73ff372 | 2015-04-14 17:58:09 -0700 | [diff] [blame] | 172 | InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 173 | mRinger = new Ringer(mCallAudioManager, this, playerFactory, context); |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 174 | mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 175 | mTtyManager = new TtyManager(context, mWiredHeadsetManager); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 176 | mProximitySensorManager = proximitySensorManagerFactory.create(context, this); |
| 177 | mPhoneStateBroadcaster = new PhoneStateBroadcaster(this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 178 | mCallLogManager = new CallLogManager(context); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 179 | mInCallController = new InCallController(context, mLock, this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 180 | mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 181 | mConnectionServiceRepository = |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 182 | new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this); |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 183 | mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 184 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 185 | mListeners.add(statusBarNotifier); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 186 | mListeners.add(mCallLogManager); |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 187 | mListeners.add(mPhoneStateBroadcaster); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 188 | mListeners.add(mInCallController); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 189 | mListeners.add(mRinger); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 190 | mListeners.add(new RingbackPlayer(this, playerFactory)); |
| 191 | mListeners.add(new InCallToneMonitor(playerFactory, this)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 192 | mListeners.add(mCallAudioManager); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 193 | mListeners.add(missedCallNotifier); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 194 | mListeners.add(mDtmfLocalTonePlayer); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 195 | mListeners.add(mHeadsetMediaButton); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 196 | mListeners.add(mProximitySensorManager); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 197 | |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 198 | mMissedCallNotifier.updateOnStartup( |
| 199 | mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 202 | public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) { |
| 203 | if (mRespondViaSmsManager != null) { |
| 204 | mListeners.remove(mRespondViaSmsManager); |
| 205 | } |
| 206 | mRespondViaSmsManager = respondViaSmsManager; |
| 207 | mListeners.add(respondViaSmsManager); |
| 208 | } |
| 209 | |
| 210 | public RespondViaSmsManager getRespondViaSmsManager() { |
| 211 | return mRespondViaSmsManager; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 214 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 215 | public void onSuccessfulOutgoingCall(Call call, int callState) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 216 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 217 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 218 | setCallState(call, callState, "successful outgoing call"); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 219 | if (!mCalls.contains(call)) { |
| 220 | // Call was not added previously in startOutgoingCall due to it being a potential MMI |
| 221 | // code, so add it now. |
| 222 | addCall(call); |
| 223 | } |
| 224 | |
| 225 | // The call's ConnectionService has been updated. |
| 226 | for (CallsManagerListener listener : mListeners) { |
| 227 | listener.onConnectionServiceChanged(call, null, call.getConnectionService()); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 228 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 229 | |
| 230 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 234 | public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) { |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 235 | Log.v(this, "onFailedOutgoingCall, call: %s", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 236 | |
Andrew Lee | e6288a7 | 2014-10-01 13:50:02 -0700 | [diff] [blame] | 237 | markCallAsRemoved(call); |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 241 | public void onSuccessfulIncomingCall(Call incomingCall) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 242 | Log.d(this, "onSuccessfulIncomingCall"); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 243 | setCallState(incomingCall, CallState.RINGING, "successful incoming call"); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 244 | |
| 245 | if (hasMaximumRingingCalls()) { |
| 246 | incomingCall.reject(false, null); |
| 247 | // since the call was not added to the list of calls, we have to call the missed |
| 248 | // call notifier and the call logger manually. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 249 | mMissedCallNotifier.showMissedCallNotification(incomingCall); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 250 | mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE); |
| 251 | } else { |
| 252 | addCall(incomingCall); |
| 253 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void onFailedIncomingCall(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 258 | setCallState(call, CallState.DISCONNECTED, "failed incoming call"); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 259 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 262 | @Override |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 263 | public void onSuccessfulUnknownCall(Call call, int callState) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 264 | setCallState(call, callState, "successful unknown call"); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 265 | Log.i(this, "onSuccessfulUnknownCall for call %s", call); |
| 266 | addCall(call); |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void onFailedUnknownCall(Call call) { |
| 271 | Log.i(this, "onFailedUnknownCall for call %s", call); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 272 | setCallState(call, CallState.DISCONNECTED, "failed unknown call"); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 273 | call.removeListener(this); |
| 274 | } |
| 275 | |
| 276 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 277 | public void onRingbackRequested(Call call, boolean ringback) { |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 278 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 279 | listener.onRingbackRequested(call, ringback); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 283 | @Override |
| 284 | public void onPostDialWait(Call call, String remaining) { |
| 285 | mInCallController.onPostDialWait(call, remaining); |
| 286 | } |
| 287 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 288 | @Override |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 289 | public void onPostDialChar(final Call call, char nextChar) { |
| 290 | if (PhoneNumberUtils.is12Key(nextChar)) { |
| 291 | // Play tone if it is one of the dialpad digits, canceling out the previously queued |
| 292 | // up stopTone runnable since playing a new tone automatically stops the previous tone. |
| 293 | if (mStopTone != null) { |
| 294 | mHandler.removeCallbacks(mStopTone); |
| 295 | } |
| 296 | |
| 297 | mDtmfLocalTonePlayer.playTone(call, nextChar); |
| 298 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 299 | // TODO: Create a LockedRunnable class that does the synchronization automatically. |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 300 | mStopTone = new Runnable() { |
| 301 | @Override |
| 302 | public void run() { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 303 | synchronized (mLock) { |
| 304 | // Set a timeout to stop the tone in case there isn't another tone to follow. |
| 305 | mDtmfLocalTonePlayer.stopTone(call); |
| 306 | } |
Nancy Chen | a469f76 | 2014-12-09 18:29:20 -0800 | [diff] [blame] | 307 | } |
| 308 | }; |
| 309 | mHandler.postDelayed( |
| 310 | mStopTone, |
| 311 | Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver())); |
| 312 | } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT || |
| 313 | nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) { |
| 314 | // Stop the tone if a tone is playing, removing any other stopTone callbacks since |
| 315 | // the previous tone is being stopped anyway. |
| 316 | if (mStopTone != null) { |
| 317 | mHandler.removeCallbacks(mStopTone); |
| 318 | } |
| 319 | mDtmfLocalTonePlayer.stopTone(call); |
| 320 | } else { |
| 321 | Log.w(this, "onPostDialChar: invalid value %d", nextChar); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 326 | public void onParentChanged(Call call) { |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 327 | // parent-child relationship affects which call should be foreground, so do an update. |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 328 | updateCallsManagerState(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 329 | for (CallsManagerListener listener : mListeners) { |
| 330 | listener.onIsConferencedChanged(call); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void onChildrenChanged(Call call) { |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 336 | // parent-child relationship affects which call should be foreground, so do an update. |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 337 | updateCallsManagerState(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 338 | for (CallsManagerListener listener : mListeners) { |
| 339 | listener.onIsConferencedChanged(call); |
| 340 | } |
| 341 | } |
| 342 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 343 | @Override |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 344 | public void onIsVoipAudioModeChanged(Call call) { |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 345 | for (CallsManagerListener listener : mListeners) { |
Andrew Lee | 5be64bc | 2014-09-08 18:35:15 -0700 | [diff] [blame] | 346 | listener.onIsVoipAudioModeChanged(call); |
Sailesh Nepal | 7e66957 | 2014-07-08 21:29:12 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 350 | @Override |
| 351 | public void onVideoStateChanged(Call call) { |
| 352 | for (CallsManagerListener listener : mListeners) { |
| 353 | listener.onVideoStateChanged(call); |
| 354 | } |
| 355 | } |
| 356 | |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 357 | @Override |
| 358 | public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) { |
| 359 | mPendingCallsToDisconnect.add(call); |
| 360 | mHandler.postDelayed(new Runnable() { |
| 361 | @Override |
| 362 | public void run() { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 363 | synchronized (mLock) { |
| 364 | if (mPendingCallsToDisconnect.remove(call)) { |
| 365 | Log.i(this, "Delayed disconnection of call: %s", call); |
| 366 | call.disconnect(); |
| 367 | } |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver())); |
| 371 | |
| 372 | return true; |
| 373 | } |
| 374 | |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 375 | Collection<Call> getCalls() { |
| 376 | return Collections.unmodifiableCollection(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | Call getForegroundCall() { |
| 380 | return mForegroundCall; |
| 381 | } |
| 382 | |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 383 | Ringer getRinger() { |
| 384 | return mRinger; |
| 385 | } |
| 386 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 387 | InCallController getInCallController() { |
| 388 | return mInCallController; |
| 389 | } |
| 390 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 391 | boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 392 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 393 | if (call.isEmergencyCall()) { |
| 394 | return true; |
| 395 | } |
| 396 | } |
| 397 | return false; |
| 398 | } |
| 399 | |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 400 | boolean hasVideoCall() { |
| 401 | for (Call call : mCalls) { |
Tyler Gunn | 467b64f | 2015-06-09 13:48:47 -0700 | [diff] [blame] | 402 | if (VideoProfile.isVideo(call.getVideoState())) { |
Andrew Lee | 4550623 | 2014-10-22 17:54:33 -0700 | [diff] [blame] | 403 | return true; |
| 404 | } |
| 405 | } |
| 406 | return false; |
| 407 | } |
| 408 | |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 409 | CallAudioState getAudioState() { |
| 410 | return mCallAudioManager.getCallAudioState(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 413 | boolean isTtySupported() { |
| 414 | return mTtyManager.isTtySupported(); |
| 415 | } |
| 416 | |
| 417 | int getCurrentTtyMode() { |
| 418 | return mTtyManager.getCurrentTtyMode(); |
| 419 | } |
| 420 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 421 | void addListener(CallsManagerListener listener) { |
| 422 | mListeners.add(listener); |
| 423 | } |
| 424 | |
| 425 | void removeListener(CallsManagerListener listener) { |
| 426 | mListeners.remove(listener); |
| 427 | } |
| 428 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 429 | /** |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 430 | * Starts the process to attach the call to a connection service. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 431 | * |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 432 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 433 | * connection service to use for this call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 434 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 435 | */ |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 436 | void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 437 | Log.d(this, "processIncomingCallIntent"); |
Yorke Lee | 69fa897 | 2015-06-08 11:25:32 -0700 | [diff] [blame] | 438 | Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS); |
| 439 | if (handle == null) { |
| 440 | // Required for backwards compatibility |
| 441 | handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER); |
| 442 | } |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 443 | Call call = new Call( |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 444 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 445 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 446 | mLock, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 447 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 448 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 449 | mCallerInfoAsyncQueryFactory, |
Sailesh Nepal | 8aa6a00 | 2014-09-12 10:52:49 -0700 | [diff] [blame] | 450 | handle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 451 | null /* gatewayInfo */, |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 452 | null /* connectionManagerPhoneAccount */, |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 453 | phoneAccountHandle, |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 454 | true /* isIncoming */, |
| 455 | false /* isConference */); |
| 456 | |
Andrew Lee | 1b90665 | 2015-06-01 11:44:55 -0700 | [diff] [blame] | 457 | call.setIntentExtras(extras); |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 458 | // TODO: Move this to be a part of addCall() |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 459 | call.addListener(this); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 460 | call.startCreateConnection(mPhoneAccountRegistrar); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 463 | void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
| 464 | Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE); |
| 465 | Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle)); |
| 466 | Call call = new Call( |
| 467 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 468 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 469 | mLock, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 470 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 471 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 472 | mCallerInfoAsyncQueryFactory, |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 473 | handle, |
| 474 | null /* gatewayInfo */, |
| 475 | null /* connectionManagerPhoneAccount */, |
| 476 | phoneAccountHandle, |
| 477 | // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach |
| 478 | // to the existing connection instead of trying to create a new one. |
| 479 | true /* isIncoming */, |
| 480 | false /* isConference */); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 481 | call.setIsUnknown(true); |
Andrew Lee | 1b90665 | 2015-06-01 11:44:55 -0700 | [diff] [blame] | 482 | call.setIntentExtras(extras); |
Yorke Lee | 9250e5f | 2014-10-01 13:39:09 -0700 | [diff] [blame] | 483 | call.addListener(this); |
| 484 | call.startCreateConnection(mPhoneAccountRegistrar); |
| 485 | } |
| 486 | |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 487 | private Call getNewOutgoingCall(Uri handle) { |
| 488 | // First check to see if we can reuse any of the calls that are waiting to disconnect. |
| 489 | // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information. |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 490 | Call reusedCall = null; |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 491 | for (Call pendingCall : mPendingCallsToDisconnect) { |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 492 | if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) { |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 493 | mPendingCallsToDisconnect.remove(pendingCall); |
| 494 | Log.i(this, "Reusing disconnected call %s", pendingCall); |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 495 | reusedCall = pendingCall; |
| 496 | } else { |
| 497 | pendingCall.disconnect(); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 500 | if (reusedCall != null) { |
| 501 | return reusedCall; |
| 502 | } |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 503 | |
| 504 | // Create a call with original handle. The handle may be changed when the call is attached |
| 505 | // to a connection service, but in most cases will remain the same. |
| 506 | return new Call( |
| 507 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 508 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 509 | mLock, |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 510 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 511 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 512 | mCallerInfoAsyncQueryFactory, |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 513 | handle, |
| 514 | null /* gatewayInfo */, |
| 515 | null /* connectionManagerPhoneAccount */, |
| 516 | null /* phoneAccountHandle */, |
| 517 | false /* isIncoming */, |
| 518 | false /* isConference */); |
| 519 | } |
| 520 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 521 | /** |
| 522 | * Kicks off the first steps to creating an outgoing call so that InCallUI can launch. |
| 523 | * |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 524 | * @param handle Handle to connect the call with. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 525 | * @param phoneAccountHandle The phone account which contains the component name of the |
| 526 | * connection service to use for this call. |
| 527 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 528 | */ |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 529 | Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) { |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 530 | Call call = getNewOutgoingCall(handle); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 531 | |
| 532 | List<PhoneAccountHandle> accounts = |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 533 | mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme(), false); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 534 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 535 | Log.v(this, "startOutgoingCall found accounts = " + accounts); |
| 536 | |
Nancy Chen | bc95db9 | 2014-11-24 13:19:18 -0800 | [diff] [blame] | 537 | if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) { |
| 538 | // If there is an ongoing call, use the same phone account to place this new call. |
| 539 | phoneAccountHandle = mForegroundCall.getTargetPhoneAccount(); |
| 540 | } |
| 541 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 542 | // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call |
| 543 | // as if a phoneAccount was not specified (does the default behavior instead). |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 544 | // Note: We will not attempt to dial with a requested phoneAccount if it is disabled. |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 545 | if (phoneAccountHandle != null) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 546 | if (!accounts.contains(phoneAccountHandle)) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 547 | phoneAccountHandle = null; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (phoneAccountHandle == null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 552 | // No preset account, check if default exists that supports the URI scheme for the |
| 553 | // handle. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 554 | phoneAccountHandle = |
| 555 | mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(handle.getScheme()); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 558 | call.setTargetPhoneAccount(phoneAccountHandle); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 559 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 560 | boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext, |
| 561 | call.getHandle()); |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 562 | boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 563 | |
| 564 | // Do not support any more live calls. Our options are to move a call to hold, disconnect |
| 565 | // a call, or cancel this call altogether. |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 566 | if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 567 | // just cancel at this point. |
Yorke Lee | 5997951 | 2014-12-02 01:03:40 -0800 | [diff] [blame] | 568 | Log.i(this, "No remaining room for outgoing call: %s", call); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 569 | if (mCalls.contains(call)) { |
| 570 | // This call can already exist if it is a reused call, |
| 571 | // See {@link #getNewOutgoingCall}. |
| 572 | call.disconnect(); |
| 573 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 574 | return null; |
| 575 | } |
| 576 | |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 577 | boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 && |
| 578 | !isEmergencyCall; |
| 579 | |
| 580 | if (needsAccountSelection) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 581 | // This is the state where the user is expected to select an account |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 582 | call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection"); |
Pawit Pornkitprasan | 1b2fd28 | 2015-03-15 08:33:32 +0700 | [diff] [blame] | 583 | // Create our own instance to modify (since extras may be Bundle.EMPTY) |
| 584 | extras = new Bundle(extras); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 585 | extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 586 | } else { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 587 | call.setState(CallState.CONNECTING, phoneAccountHandle.toString()); |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 588 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 589 | |
Andrew Lee | 1b90665 | 2015-06-01 11:44:55 -0700 | [diff] [blame] | 590 | call.setIntentExtras(extras); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 591 | |
Tyler Gunn | 504eb83 | 2014-10-13 14:15:27 -0700 | [diff] [blame] | 592 | // Do not add the call if it is a potential MMI code. |
Nancy Chen | 8d92f45 | 2014-10-20 21:43:44 -0700 | [diff] [blame] | 593 | if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) { |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 594 | call.addListener(this); |
Santos Cordon | cf5b291 | 2014-11-05 21:57:54 -0800 | [diff] [blame] | 595 | } else if (!mCalls.contains(call)) { |
| 596 | // We check if mCalls already contains the call because we could potentially be reusing |
| 597 | // a call which was previously added (See {@link #getNewOutgoingCall}). |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 598 | addCall(call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 599 | } |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 600 | |
| 601 | return call; |
| 602 | } |
| 603 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 604 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 605 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 606 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 607 | * @param handle Handle to connect the call with. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 608 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 609 | * actual dialed handle via a gateway provider. May be null. |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 610 | * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects. |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame] | 611 | * @param videoState The desired video state for the outgoing call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 612 | */ |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 613 | void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn, |
| 614 | int videoState) { |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 615 | if (call == null) { |
| 616 | // don't do anything if the call no longer exists |
| 617 | Log.i(this, "Canceling unknown call."); |
Santos Cordon | b7af09e | 2014-08-06 04:30:01 -0700 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | |
Nancy Chen | 201b437 | 2014-09-08 14:18:24 -0700 | [diff] [blame] | 621 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 622 | |
| 623 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 624 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 625 | } else { |
| 626 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 627 | Log.pii(uriHandle), Log.pii(handle)); |
| 628 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 629 | |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 630 | call.setHandle(uriHandle); |
| 631 | call.setGatewayInfo(gatewayInfo); |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame] | 632 | call.setVideoState(videoState); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 633 | |
Santos Cordon | a04bdca | 2015-03-04 16:57:55 -0800 | [diff] [blame] | 634 | if (speakerphoneOn) { |
| 635 | Log.i(this, "%s Starting with speakerphone as requested", call); |
| 636 | } else { |
| 637 | Log.i(this, "%s Starting with speakerphone because car is docked.", call); |
| 638 | } |
| 639 | call.setStartWithSpeakerphoneOn(speakerphoneOn || mDockManager.isDocked()); |
| 640 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 641 | boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext, |
| 642 | call.getHandle()); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 643 | if (isEmergencyCall) { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 644 | // Emergency -- CreateConnectionProcessor will choose accounts automatically |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 645 | call.setTargetPhoneAccount(null); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 646 | } |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 647 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 648 | if (call.getTargetPhoneAccount() != null || isEmergencyCall) { |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 649 | // If the account has been set, proceed to place the outgoing call. |
| 650 | // Otherwise the connection will be initiated when the account is set by the user. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 651 | call.startCreateConnection(mPhoneAccountRegistrar); |
Nancy Chen | d9de92c | 2014-07-21 16:09:13 -0700 | [diff] [blame] | 652 | } |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 656 | * Attempts to start a conference call for the specified call. |
| 657 | * |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 658 | * @param call The call to conference. |
| 659 | * @param otherCall The other call to conference with. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 660 | */ |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 661 | void conference(Call call, Call otherCall) { |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 662 | call.conferenceWith(otherCall); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 666 | * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call |
| 667 | * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 668 | * the user opting to answer said call. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 669 | * |
| 670 | * @param call The call to answer. |
| 671 | * @param videoState The video state in which to answer the call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 672 | */ |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 673 | void answerCall(Call call, int videoState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 674 | if (!mCalls.contains(call)) { |
| 675 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 676 | } else { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 677 | // If the foreground call is not the ringing call and it is currently isActive() or |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 678 | // STATE_DIALING, put it on hold before answering the call. |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 679 | if (mForegroundCall != null && mForegroundCall != call && |
| 680 | (mForegroundCall.isActive() || |
| 681 | mForegroundCall.getState() == CallState.DIALING)) { |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 682 | if (0 == (mForegroundCall.getConnectionCapabilities() |
| 683 | & Connection.CAPABILITY_HOLD)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 684 | // This call does not support hold. If it is from a different connection |
| 685 | // service, then disconnect it, otherwise allow the connection service to |
| 686 | // figure out the right states. |
| 687 | if (mForegroundCall.getConnectionService() != call.getConnectionService()) { |
| 688 | mForegroundCall.disconnect(); |
| 689 | } |
| 690 | } else { |
Andrew Lee | e94a8f1 | 2014-12-19 16:19:51 -0800 | [diff] [blame] | 691 | Call heldCall = getHeldCall(); |
| 692 | if (heldCall != null) { |
| 693 | Log.v(this, "Disconnecting held call %s before holding active call.", |
| 694 | heldCall); |
| 695 | heldCall.disconnect(); |
| 696 | } |
| 697 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 698 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
| 699 | mForegroundCall, call); |
| 700 | mForegroundCall.hold(); |
| 701 | } |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 702 | // TODO: Wait until we get confirmation of the active call being |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 703 | // on-hold before answering the new call. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 704 | // TODO: Import logic from CallManager.acceptCall() |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 707 | for (CallsManagerListener listener : mListeners) { |
| 708 | listener.onIncomingCallAnswered(call); |
| 709 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 710 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 711 | // We do not update the UI until we get confirmation of the answer() through |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 712 | // {@link #markCallAsActive}. |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 713 | call.answer(videoState); |
Tyler Gunn | 5b88249 | 2015-06-03 10:03:13 -0700 | [diff] [blame] | 714 | if (VideoProfile.isVideo(videoState) && |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 715 | !mWiredHeadsetManager.isPluggedIn() && |
| 716 | !mCallAudioManager.isBluetoothDeviceAvailable() && |
| 717 | isSpeakerEnabledForVideoCalls()) { |
| 718 | call.setStartWithSpeakerphoneOn(true); |
| 719 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 720 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 723 | private static boolean isSpeakerEnabledForVideoCalls() { |
| 724 | return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT, |
| 725 | PhoneConstants.AUDIO_OUTPUT_DEFAULT) == |
| 726 | PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER); |
| 727 | } |
| 728 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 729 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 730 | * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call |
| 731 | * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 732 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 733 | */ |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 734 | void rejectCall(Call call, boolean rejectWithMessage, String textMessage) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 735 | if (!mCalls.contains(call)) { |
| 736 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 737 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 738 | for (CallsManagerListener listener : mListeners) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 739 | listener.onIncomingCallRejected(call, rejectWithMessage, textMessage); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 740 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 741 | call.reject(rejectWithMessage, textMessage); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 742 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 746 | * Instructs Telecom to play the specified DTMF tone within the specified call. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 747 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 748 | * @param digit The DTMF digit to play. |
| 749 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 750 | void playDtmfTone(Call call, char digit) { |
| 751 | if (!mCalls.contains(call)) { |
| 752 | Log.i(this, "Request to play DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 753 | } else { |
| 754 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 755 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 760 | * Instructs Telecom to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 761 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 762 | void stopDtmfTone(Call call) { |
| 763 | if (!mCalls.contains(call)) { |
| 764 | Log.i(this, "Request to stop DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 765 | } else { |
| 766 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 767 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 772 | * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 773 | */ |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 774 | void postDialContinue(Call call, boolean proceed) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 775 | if (!mCalls.contains(call)) { |
| 776 | Log.i(this, "Request to continue post-dial string in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 777 | } else { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 778 | call.postDialContinue(proceed); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 783 | * Instructs Telecom to disconnect the specified call. Intended to be invoked by the |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 784 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 785 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 786 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 787 | void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 788 | Log.v(this, "disconnectCall %s", call); |
| 789 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 790 | if (!mCalls.contains(call)) { |
| 791 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 792 | } else { |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 793 | mLocallyDisconnectingCalls.add(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 794 | call.disconnect(); |
| 795 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 796 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 797 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 798 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 799 | * Instructs Telecom to disconnect all calls. |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 800 | */ |
| 801 | void disconnectAllCalls() { |
| 802 | Log.v(this, "disconnectAllCalls"); |
| 803 | |
| 804 | for (Call call : mCalls) { |
| 805 | disconnectCall(call); |
| 806 | } |
| 807 | } |
| 808 | |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 809 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 810 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 811 | * Instructs Telecom to put the specified call on hold. Intended to be invoked by the |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 812 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 813 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 814 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 815 | void holdCall(Call call) { |
| 816 | if (!mCalls.contains(call)) { |
| 817 | Log.w(this, "Unknown call (%s) asked to be put on hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 818 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 819 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 820 | call.hold(); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 825 | * Instructs Telecom to release the specified call from hold. Intended to be invoked by |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 826 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 827 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 828 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 829 | void unholdCall(Call call) { |
| 830 | if (!mCalls.contains(call)) { |
| 831 | Log.w(this, "Unknown call (%s) asked to be removed from hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 832 | } else { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 833 | Log.d(this, "unholding call: (%s)", call); |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 834 | for (Call c : mCalls) { |
Tyler Gunn | e44adb4 | 2015-01-02 10:55:35 -0800 | [diff] [blame] | 835 | // Only attempt to hold parent calls and not the individual children. |
| 836 | if (c != null && c.isAlive() && c != call && c.getParentCall() == null) { |
Sai Cheemalapati | 45b3e3f | 2014-08-15 09:33:00 -0700 | [diff] [blame] | 837 | c.hold(); |
| 838 | } |
| 839 | } |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 840 | call.unhold(); |
| 841 | } |
| 842 | } |
| 843 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 844 | /** Called by the in-call UI to change the mute state. */ |
| 845 | void mute(boolean shouldMute) { |
| 846 | mCallAudioManager.mute(shouldMute); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 851 | * speaker phone. |
| 852 | */ |
| 853 | void setAudioRoute(int route) { |
| 854 | mCallAudioManager.setAudioRoute(route); |
| 855 | } |
| 856 | |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 857 | /** Called by the in-call UI to turn the proximity sensor on. */ |
| 858 | void turnOnProximitySensor() { |
| 859 | mProximitySensorManager.turnOn(); |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * Called by the in-call UI to turn the proximity sensor off. |
| 864 | * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise, |
| 865 | * the screen will be kept off until the proximity sensor goes negative. |
| 866 | */ |
| 867 | void turnOffProximitySensor(boolean screenOnImmediately) { |
| 868 | mProximitySensorManager.turnOff(screenOnImmediately); |
| 869 | } |
| 870 | |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 871 | void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) { |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 872 | if (!mCalls.contains(call)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 873 | Log.i(this, "Attempted to add account to unknown call %s", call); |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 874 | } else { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 875 | // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and |
Santos Cordon | 9269451 | 2015-04-23 14:47:28 -0700 | [diff] [blame] | 876 | // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 877 | // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without |
| 878 | // respecting a rewritten number or a canceled number. This is unlikely since |
| 879 | // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting |
| 880 | // a phone account from the in-call UI. |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 881 | call.setTargetPhoneAccount(account); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 882 | |
| 883 | // Note: emergency calls never go through account selection dialog so they never |
| 884 | // arrive here. |
| 885 | if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 886 | call.startCreateConnection(mPhoneAccountRegistrar); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 887 | } else { |
| 888 | call.disconnect(); |
| 889 | } |
Nancy Chen | f5e5d3c | 2014-10-21 18:45:56 -0700 | [diff] [blame] | 890 | |
| 891 | if (setDefault) { |
| 892 | mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account); |
| 893 | } |
Nancy Chen | 53ceedc | 2014-07-08 18:56:51 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 897 | /** Called when the audio state changes. */ |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 898 | void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 899 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 900 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 901 | listener.onCallAudioStateChanged(oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 902 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 905 | void markCallAsRinging(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 906 | setCallState(call, CallState.RINGING, "ringing set explicitly"); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 909 | void markCallAsDialing(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 910 | setCallState(call, CallState.DIALING, "dialing set explicitly"); |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 911 | maybeMoveToSpeakerPhone(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 912 | } |
| 913 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 914 | void markCallAsActive(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 915 | setCallState(call, CallState.ACTIVE, "active set explicitly"); |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 916 | maybeMoveToSpeakerPhone(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 919 | void markCallAsOnHold(Call call) { |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 920 | setCallState(call, CallState.ON_HOLD, "on-hold set explicitly"); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 923 | /** |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 924 | * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the |
| 925 | * last live call, then also disconnect from the in-call controller. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 926 | * |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 927 | * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 928 | */ |
Andrew Lee | 701dc00 | 2014-09-11 21:29:12 -0700 | [diff] [blame] | 929 | void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) { |
| 930 | call.setDisconnectCause(disconnectCause); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 931 | setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly"); |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 934 | /** |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 935 | * Removes an existing disconnected call, and notifies the in-call app. |
| 936 | */ |
| 937 | void markCallAsRemoved(Call call) { |
| 938 | removeCall(call); |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 939 | if (mLocallyDisconnectingCalls.contains(call)) { |
| 940 | mLocallyDisconnectingCalls.remove(call); |
| 941 | if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) { |
| 942 | mForegroundCall.unhold(); |
| 943 | } |
| 944 | } |
Ihab Awad | a02bef5 | 2014-08-13 18:18:42 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /** |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 948 | * Cleans up any calls currently associated with the specified connection service when the |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 949 | * service binder disconnects unexpectedly. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 950 | * |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 951 | * @param service The connection service that disconnected. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 952 | */ |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 953 | void handleConnectionServiceDeath(ConnectionServiceWrapper service) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 954 | if (service != null) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 955 | for (Call call : mCalls) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 956 | if (call.getConnectionService() == service) { |
Yorke Lee | 2af16b6 | 2014-11-11 17:36:57 -0800 | [diff] [blame] | 957 | if (call.getState() != CallState.DISCONNECTED) { |
| 958 | markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR)); |
| 959 | } |
| 960 | markCallAsRemoved(call); |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 961 | } |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 966 | boolean hasAnyCalls() { |
| 967 | return !mCalls.isEmpty(); |
| 968 | } |
| 969 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 970 | boolean hasActiveOrHoldingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 971 | return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | boolean hasRingingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 975 | return getFirstCallWithState(CallState.RINGING) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 978 | boolean onMediaButton(int type) { |
| 979 | if (hasAnyCalls()) { |
| 980 | if (HeadsetMediaButton.SHORT_PRESS == type) { |
| 981 | Call ringingCall = getFirstCallWithState(CallState.RINGING); |
| 982 | if (ringingCall == null) { |
| 983 | mCallAudioManager.toggleMute(); |
| 984 | return true; |
| 985 | } else { |
Andrew Lee | 38931d0 | 2014-07-16 10:17:36 -0700 | [diff] [blame] | 986 | ringingCall.answer(ringingCall.getVideoState()); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 987 | return true; |
| 988 | } |
| 989 | } else if (HeadsetMediaButton.LONG_PRESS == type) { |
| 990 | Log.d(this, "handleHeadsetHook: longpress -> hangup"); |
| 991 | Call callToHangup = getFirstCallWithState( |
| 992 | CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD); |
| 993 | if (callToHangup != null) { |
| 994 | callToHangup.disconnect(); |
| 995 | return true; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1003 | * Returns true if telecom supports adding another top-level call. |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1004 | */ |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1005 | boolean canAddCall() { |
Andrew Lee | 113ad62 | 2014-12-01 18:33:10 -0800 | [diff] [blame] | 1006 | if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) { |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1010 | int count = 0; |
| 1011 | for (Call call : mCalls) { |
| 1012 | if (call.isEmergencyCall()) { |
| 1013 | // We never support add call if one of the calls is an emergency call. |
| 1014 | return false; |
| 1015 | } else if (call.getParentCall() == null) { |
| 1016 | count++; |
| 1017 | } |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1018 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1019 | // We do not check states for canAddCall. We treat disconnected calls the same |
| 1020 | // and wait until they are removed instead. If we didn't count disconnected calls, |
| 1021 | // we could put InCallServices into a state where they are showing two calls but |
| 1022 | // also support add-call. Technically it's right, but overall looks better (UI-wise) |
| 1023 | // and acts better if we wait until the call is removed. |
| 1024 | if (count >= MAXIMUM_TOP_LEVEL_CALLS) { |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1025 | return false; |
| 1026 | } |
| 1027 | } |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
Ihab Awad | a365390 | 2015-01-23 13:44:46 -0800 | [diff] [blame] | 1031 | @VisibleForTesting |
P.Y. Laligand | d35be75 | 2015-03-06 14:12:43 -0800 | [diff] [blame] | 1032 | public Call getRingingCall() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1033 | return getFirstCallWithState(CallState.RINGING); |
| 1034 | } |
| 1035 | |
| 1036 | Call getActiveCall() { |
| 1037 | return getFirstCallWithState(CallState.ACTIVE); |
| 1038 | } |
| 1039 | |
Nancy Chen | 05a9e40 | 2014-09-26 14:14:32 -0700 | [diff] [blame] | 1040 | Call getDialingCall() { |
| 1041 | return getFirstCallWithState(CallState.DIALING); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | Call getHeldCall() { |
| 1045 | return getFirstCallWithState(CallState.ON_HOLD); |
| 1046 | } |
| 1047 | |
Santos Cordon | c0ca76e | 2014-10-21 15:54:19 -0700 | [diff] [blame] | 1048 | int getNumHeldCalls() { |
| 1049 | int count = 0; |
| 1050 | for (Call call : mCalls) { |
| 1051 | if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) { |
| 1052 | count++; |
| 1053 | } |
| 1054 | } |
| 1055 | return count; |
| 1056 | } |
| 1057 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1058 | Call getFirstCallWithState(int... states) { |
| 1059 | return getFirstCallWithState(null, states); |
| 1060 | } |
| 1061 | |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 1062 | /** |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1063 | * Returns the first call that it finds with the given states. The states are treated as having |
| 1064 | * priority order so that any call with the first state will be returned before any call with |
| 1065 | * states listed later in the parameter list. |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1066 | * |
| 1067 | * @param callToSkip Call that this method should skip while searching |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1068 | */ |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1069 | Call getFirstCallWithState(Call callToSkip, int... states) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1070 | for (int currentState : states) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 1071 | // check the foreground first |
| 1072 | if (mForegroundCall != null && mForegroundCall.getState() == currentState) { |
| 1073 | return mForegroundCall; |
| 1074 | } |
| 1075 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1076 | for (Call call : mCalls) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1077 | if (Objects.equals(callToSkip, call)) { |
| 1078 | continue; |
| 1079 | } |
| 1080 | |
| 1081 | // Only operate on top-level calls |
| 1082 | if (call.getParentCall() != null) { |
| 1083 | continue; |
| 1084 | } |
| 1085 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 1086 | if (currentState == call.getState()) { |
| 1087 | return call; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | return null; |
| 1092 | } |
| 1093 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1094 | Call createConferenceCall( |
| 1095 | PhoneAccountHandle phoneAccount, |
| 1096 | ParcelableConference parcelableConference) { |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 1097 | |
| 1098 | // If the parceled conference specifies a connect time, use it; otherwise default to 0, |
| 1099 | // which is the default value for new Calls. |
| 1100 | long connectTime = |
| 1101 | parcelableConference.getConnectTimeMillis() == |
| 1102 | Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 : |
| 1103 | parcelableConference.getConnectTimeMillis(); |
| 1104 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1105 | Call call = new Call( |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1106 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 1107 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 1108 | mLock, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1109 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 1110 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 1111 | mCallerInfoAsyncQueryFactory, |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1112 | null /* handle */, |
| 1113 | null /* gatewayInfo */, |
| 1114 | null /* connectionManagerPhoneAccount */, |
| 1115 | phoneAccount, |
| 1116 | false /* isIncoming */, |
Tyler Gunn | d92e737 | 2015-01-09 15:59:45 -0800 | [diff] [blame] | 1117 | true /* isConference */, |
| 1118 | connectTime); |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1119 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 1120 | setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()), |
| 1121 | "new conference call"); |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1122 | call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities()); |
Rekha Kumar | d240fe8 | 2015-04-01 21:45:57 -0700 | [diff] [blame] | 1123 | call.setVideoState(parcelableConference.getVideoState()); |
| 1124 | call.setVideoProvider(parcelableConference.getVideoProvider()); |
Andrew Lee | 57412d3 | 2015-04-14 13:38:44 -0700 | [diff] [blame] | 1125 | call.setStatusHints(parcelableConference.getStatusHints()); |
Santos Cordon | b3907b3 | 2015-05-27 17:39:59 -0700 | [diff] [blame] | 1126 | call.setExtras(parcelableConference.getExtras()); |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 1127 | |
| 1128 | // TODO: Move this to be a part of addCall() |
| 1129 | call.addListener(this); |
| 1130 | addCall(call); |
| 1131 | return call; |
| 1132 | } |
| 1133 | |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 1134 | /** |
| 1135 | * @return the call state currently tracked by {@link PhoneStateBroadcaster} |
| 1136 | */ |
| 1137 | int getCallState() { |
| 1138 | return mPhoneStateBroadcaster.getCallState(); |
| 1139 | } |
Nancy Chen | bc9ef17 | 2014-08-15 17:11:57 -0700 | [diff] [blame] | 1140 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 1141 | /** |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1142 | * Retrieves the {@link PhoneAccountRegistrar}. |
| 1143 | * |
| 1144 | * @return The {@link PhoneAccountRegistrar}. |
| 1145 | */ |
| 1146 | PhoneAccountRegistrar getPhoneAccountRegistrar() { |
| 1147 | return mPhoneAccountRegistrar; |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * Retrieves the {@link MissedCallNotifier} |
| 1152 | * @return The {@link MissedCallNotifier}. |
| 1153 | */ |
| 1154 | MissedCallNotifier getMissedCallNotifier() { |
| 1155 | return mMissedCallNotifier; |
| 1156 | } |
| 1157 | |
| 1158 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1159 | * Adds the specified call to the main list of live calls. |
| 1160 | * |
| 1161 | * @param call The call to add. |
| 1162 | */ |
| 1163 | private void addCall(Call call) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1164 | Trace.beginSection("addCall"); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1165 | Log.v(this, "addCall(%s)", call); |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1166 | call.addListener(this); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1167 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1168 | |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1169 | // TODO: Update mForegroundCall prior to invoking |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1170 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1171 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1172 | if (Log.SYSTRACE_DEBUG) { |
| 1173 | Trace.beginSection(listener.getClass().toString() + " addCall"); |
| 1174 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1175 | listener.onCallAdded(call); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1176 | if (Log.SYSTRACE_DEBUG) { |
| 1177 | Trace.endSection(); |
| 1178 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1179 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1180 | updateCallsManagerState(); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1181 | Trace.endSection(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1184 | private void removeCall(Call call) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1185 | Trace.beginSection("removeCall"); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 1186 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 1187 | |
Santos Cordon | 672321e | 2014-08-21 14:38:58 -0700 | [diff] [blame] | 1188 | call.setParentCall(null); // need to clean up parent relationship before destroying. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 1189 | call.removeListener(this); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1190 | call.clearConnectionService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1191 | |
| 1192 | boolean shouldNotify = false; |
| 1193 | if (mCalls.contains(call)) { |
| 1194 | mCalls.remove(call); |
| 1195 | shouldNotify = true; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1196 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 1197 | |
Santos Cordon | 2685dab | 2015-04-17 17:12:09 -0700 | [diff] [blame] | 1198 | call.destroy(); |
| 1199 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1200 | // Only broadcast changes for calls that are being tracked. |
| 1201 | if (shouldNotify) { |
| 1202 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1203 | if (Log.SYSTRACE_DEBUG) { |
| 1204 | Trace.beginSection(listener.getClass().toString() + " onCallRemoved"); |
| 1205 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1206 | listener.onCallRemoved(call); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1207 | if (Log.SYSTRACE_DEBUG) { |
| 1208 | Trace.endSection(); |
| 1209 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1210 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1211 | updateCallsManagerState(); |
Yorke Lee | 604a90f | 2014-10-20 12:13:32 -0700 | [diff] [blame] | 1212 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1213 | Trace.endSection(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1214 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1215 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1216 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 1217 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1218 | * |
| 1219 | * @param call The call. |
| 1220 | * @param newState The new state of the call. |
| 1221 | */ |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 1222 | private void setCallState(Call call, int newState, String tag) { |
Jay Shrauner | acb91eb | 2014-08-08 16:04:53 -0700 | [diff] [blame] | 1223 | if (call == null) { |
| 1224 | return; |
| 1225 | } |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1226 | int oldState = call.getState(); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 1227 | Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState), |
| 1228 | CallState.toString(newState), call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1229 | if (newState != oldState) { |
| 1230 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 1231 | // us that the call is in a particular state, we allow it even if it doesn't make |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 1232 | // sense (e.g., STATE_ACTIVE -> STATE_RINGING). |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1233 | // TODO: Consider putting a stop to the above and turning CallState |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1234 | // into a well-defined state machine. |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1235 | // TODO: Define expected state transitions here, and log when an |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1236 | // unexpected transition occurs. |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 1237 | call.setState(newState, tag); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1238 | |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1239 | Trace.beginSection("onCallStateChanged"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1240 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1241 | if (mCalls.contains(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1242 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1243 | if (Log.SYSTRACE_DEBUG) { |
| 1244 | Trace.beginSection(listener.getClass().toString() + " onCallStateChanged"); |
| 1245 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1246 | listener.onCallStateChanged(call, oldState, newState); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1247 | if (Log.SYSTRACE_DEBUG) { |
| 1248 | Trace.endSection(); |
| 1249 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1250 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1251 | updateCallsManagerState(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1252 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1253 | Trace.endSection(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1258 | * Checks which call should be visible to the user and have audio focus. |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1259 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1260 | private void updateForegroundCall() { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1261 | Trace.beginSection("updateForegroundCall"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1262 | Call newForegroundCall = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 1263 | for (Call call : mCalls) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 1264 | // TODO: Foreground-ness needs to be explicitly set. No call, regardless |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 1265 | // of its state will be foreground by default and instead the connection service should |
| 1266 | // be notified when its calls enter and exit foreground state. Foreground will mean that |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1267 | // the call should play audio and listen to microphone if it wants. |
| 1268 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1269 | // Only top-level calls can be in foreground |
| 1270 | if (call.getParentCall() != null) { |
| 1271 | continue; |
| 1272 | } |
| 1273 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1274 | // Active calls have priority. |
| 1275 | if (call.isActive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1276 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 1277 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1278 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1279 | |
| 1280 | if (call.isAlive() || call.getState() == CallState.RINGING) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1281 | newForegroundCall = call; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1282 | // Don't break in case there's an active call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1283 | } |
| 1284 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 1285 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1286 | if (newForegroundCall != mForegroundCall) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 1287 | Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1288 | Call oldForegroundCall = mForegroundCall; |
| 1289 | mForegroundCall = newForegroundCall; |
Ihab Awad | 5b28132 | 2014-09-25 18:25:29 -0700 | [diff] [blame] | 1290 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1291 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1292 | if (Log.SYSTRACE_DEBUG) { |
| 1293 | Trace.beginSection(listener.getClass().toString() + " updateForegroundCall"); |
| 1294 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1295 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1296 | if (Log.SYSTRACE_DEBUG) { |
| 1297 | Trace.endSection(); |
| 1298 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1299 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1300 | } |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1301 | Trace.endSection(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1302 | } |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1303 | |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1304 | private void updateCanAddCall() { |
| 1305 | boolean newCanAddCall = canAddCall(); |
| 1306 | if (newCanAddCall != mCanAddCall) { |
| 1307 | mCanAddCall = newCanAddCall; |
| 1308 | for (CallsManagerListener listener : mListeners) { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1309 | if (Log.SYSTRACE_DEBUG) { |
| 1310 | Trace.beginSection(listener.getClass().toString() + " updateCanAddCall"); |
| 1311 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1312 | listener.onCanAddCallChanged(mCanAddCall); |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 1313 | if (Log.SYSTRACE_DEBUG) { |
| 1314 | Trace.endSection(); |
| 1315 | } |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | private void updateCallsManagerState() { |
| 1321 | updateForegroundCall(); |
| 1322 | updateCanAddCall(); |
| 1323 | } |
| 1324 | |
Yorke Lee | b701f6d | 2014-08-12 14:04:19 -0700 | [diff] [blame] | 1325 | private boolean isPotentialMMICode(Uri handle) { |
| 1326 | return (handle != null && handle.getSchemeSpecificPart() != null |
| 1327 | && handle.getSchemeSpecificPart().contains("#")); |
| 1328 | } |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1329 | |
Tyler Gunn | 5b452df | 2014-10-01 15:02:58 -0700 | [diff] [blame] | 1330 | /** |
| 1331 | * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are |
| 1332 | * MMI codes which can be dialed when one or more calls are in progress. |
| 1333 | * <P> |
| 1334 | * Checks for numbers formatted similar to the MMI codes defined in: |
| 1335 | * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)} |
| 1336 | * and |
| 1337 | * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)} |
| 1338 | * |
| 1339 | * @param handle The URI to call. |
| 1340 | * @return {@code True} if the URI represents a number which could be an in-call MMI code. |
| 1341 | */ |
| 1342 | private boolean isPotentialInCallMMICode(Uri handle) { |
| 1343 | if (handle != null && handle.getSchemeSpecificPart() != null && |
| 1344 | handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) { |
| 1345 | |
| 1346 | String dialedNumber = handle.getSchemeSpecificPart(); |
| 1347 | return (dialedNumber.equals("0") || |
| 1348 | (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) || |
| 1349 | (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) || |
| 1350 | dialedNumber.equals("3") || |
| 1351 | dialedNumber.equals("4") || |
| 1352 | dialedNumber.equals("5")); |
| 1353 | } |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1357 | private int getNumCallsWithState(int... states) { |
| 1358 | int count = 0; |
| 1359 | for (int state : states) { |
| 1360 | for (Call call : mCalls) { |
Santos Cordon | 91bd74c | 2014-11-07 16:10:22 -0800 | [diff] [blame] | 1361 | if (call.getParentCall() == null && call.getState() == state) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1362 | count++; |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | return count; |
| 1367 | } |
| 1368 | |
| 1369 | private boolean hasMaximumLiveCalls() { |
| 1370 | return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES); |
| 1371 | } |
| 1372 | |
| 1373 | private boolean hasMaximumHoldingCalls() { |
| 1374 | return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD); |
| 1375 | } |
| 1376 | |
| 1377 | private boolean hasMaximumRingingCalls() { |
| 1378 | return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING); |
| 1379 | } |
| 1380 | |
| 1381 | private boolean hasMaximumOutgoingCalls() { |
| 1382 | return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES); |
| 1383 | } |
| 1384 | |
| 1385 | private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) { |
| 1386 | if (hasMaximumLiveCalls()) { |
| 1387 | // NOTE: If the amount of live calls changes beyond 1, this logic will probably |
| 1388 | // have to change. |
| 1389 | Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES); |
Anju Mathapati | c88574c | 2014-12-16 14:46:19 +0530 | [diff] [blame] | 1390 | Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " + |
| 1391 | liveCall); |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1392 | |
Santos Cordon | d176650 | 2014-09-26 15:45:39 -0700 | [diff] [blame] | 1393 | if (call == liveCall) { |
| 1394 | // If the call is already the foreground call, then we are golden. |
Santos Cordon | 9269451 | 2015-04-23 14:47:28 -0700 | [diff] [blame] | 1395 | // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT |
Santos Cordon | d176650 | 2014-09-26 15:45:39 -0700 | [diff] [blame] | 1396 | // state since the call was already populated into the list. |
| 1397 | return true; |
| 1398 | } |
| 1399 | |
Andrew Lee | e659518 | 2014-09-23 18:43:05 -0700 | [diff] [blame] | 1400 | if (hasMaximumOutgoingCalls()) { |
Yorke Lee | 867907d | 2015-06-03 12:24:43 -0700 | [diff] [blame] | 1401 | Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES); |
| 1402 | if (isEmergency && !outgoingCall.isEmergencyCall()) { |
| 1403 | // Disconnect the current outgoing call if it's not an emergency call. If the |
| 1404 | // user tries to make two outgoing calls to different emergency call numbers, |
| 1405 | // we will try to connect the first outgoing call. |
| 1406 | outgoingCall.disconnect(); |
| 1407 | return true; |
| 1408 | } |
| 1409 | if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) { |
| 1410 | // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT} |
| 1411 | // state, just disconnect it since the user has explicitly started a new call. |
| 1412 | outgoingCall.disconnect(); |
| 1413 | return true; |
Andrew Lee | e659518 | 2014-09-23 18:43:05 -0700 | [diff] [blame] | 1414 | } |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1418 | if (hasMaximumHoldingCalls()) { |
| 1419 | // There is no more room for any more calls, unless it's an emergency. |
| 1420 | if (isEmergency) { |
| 1421 | // Kill the current active call, this is easier then trying to disconnect a |
| 1422 | // holding call and hold an active call. |
| 1423 | liveCall.disconnect(); |
| 1424 | return true; |
| 1425 | } |
| 1426 | return false; // No more room! |
| 1427 | } |
| 1428 | |
| 1429 | // We have room for at least one more holding call at this point. |
| 1430 | |
| 1431 | // First thing, if we are trying to make a call with the same phone account as the live |
| 1432 | // call, then allow it so that the connection service can make its own decision about |
| 1433 | // how to handle the new call relative to the current one. |
| 1434 | if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) { |
| 1435 | return true; |
| 1436 | } else if (call.getTargetPhoneAccount() == null) { |
| 1437 | // Without a phone account, we can't say reliably that the call will fail. |
| 1438 | // If the user chooses the same phone account as the live call, then it's |
| 1439 | // still possible that the call can be made (like with CDMA calls not supporting |
| 1440 | // hold but they still support adding a call by going immediately into conference |
| 1441 | // mode). Return true here and we'll run this code again after user chooses an |
| 1442 | // account. |
| 1443 | return true; |
| 1444 | } |
| 1445 | |
| 1446 | // Try to hold the live call before attempting the new outgoing call. |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1447 | if (liveCall.can(Connection.CAPABILITY_HOLD)) { |
Santos Cordon | f193ba4 | 2014-09-12 06:37:39 -0700 | [diff] [blame] | 1448 | liveCall.hold(); |
| 1449 | return true; |
| 1450 | } |
| 1451 | |
| 1452 | // The live call cannot be held so we're out of luck here. There's no room. |
| 1453 | return false; |
| 1454 | } |
| 1455 | return true; |
| 1456 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1457 | |
| 1458 | /** |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 1459 | * Checks to see if the call should be on speakerphone and if so, set it. |
| 1460 | */ |
| 1461 | private void maybeMoveToSpeakerPhone(Call call) { |
| 1462 | if (call.getStartWithSpeakerphoneOn()) { |
Yorke Lee | 2a66f7b | 2015-05-13 14:21:19 -0700 | [diff] [blame] | 1463 | setAudioRoute(CallAudioState.ROUTE_SPEAKER); |
Santos Cordon | d6782cd | 2015-04-16 09:57:50 -0700 | [diff] [blame] | 1464 | call.setStartWithSpeakerphoneOn(false); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | /** |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1469 | * Creates a new call for an existing connection. |
| 1470 | * |
| 1471 | * @param callId The id of the new call. |
| 1472 | * @param connection The connection information. |
| 1473 | * @return The new call. |
| 1474 | */ |
| 1475 | Call createCallForExistingConnection(String callId, ParcelableConnection connection) { |
| 1476 | Call call = new Call( |
| 1477 | mContext, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 1478 | this, |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 1479 | mLock, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1480 | mConnectionServiceRepository, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 1481 | mContactsAsyncHelper, |
Ihab Awad | abcbce4 | 2015-04-07 14:04:01 -0700 | [diff] [blame] | 1482 | mCallerInfoAsyncQueryFactory, |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1483 | connection.getHandle() /* handle */, |
| 1484 | null /* gatewayInfo */, |
| 1485 | null /* connectionManagerPhoneAccount */, |
| 1486 | connection.getPhoneAccount(), /* targetPhoneAccountHandle */ |
| 1487 | false /* isIncoming */, |
| 1488 | false /* isConference */); |
| 1489 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame^] | 1490 | setCallState(call, Call.getStateFromConnectionState(connection.getState()), |
| 1491 | "existing connection"); |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1492 | call.setConnectionCapabilities(connection.getConnectionCapabilities()); |
Tyler Gunn | 6e2b94e | 2014-10-30 11:05:22 -0700 | [diff] [blame] | 1493 | call.setCallerDisplayName(connection.getCallerDisplayName(), |
| 1494 | connection.getCallerDisplayNamePresentation()); |
| 1495 | |
| 1496 | call.addListener(this); |
| 1497 | addCall(call); |
| 1498 | |
| 1499 | return call; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1503 | * Dumps the state of the {@link CallsManager}. |
| 1504 | * |
| 1505 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 1506 | */ |
| 1507 | public void dump(IndentingPrintWriter pw) { |
| 1508 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1509 | if (mCalls != null) { |
| 1510 | pw.println("mCalls: "); |
| 1511 | pw.increaseIndent(); |
| 1512 | for (Call call : mCalls) { |
| 1513 | pw.println(call); |
| 1514 | } |
| 1515 | pw.decreaseIndent(); |
| 1516 | } |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 1517 | pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall)); |
| 1518 | |
| 1519 | if (mCallAudioManager != null) { |
| 1520 | pw.println("mCallAudioManager:"); |
| 1521 | pw.increaseIndent(); |
| 1522 | mCallAudioManager.dump(pw); |
| 1523 | pw.decreaseIndent(); |
| 1524 | } |
| 1525 | |
| 1526 | if (mTtyManager != null) { |
| 1527 | pw.println("mTtyManager:"); |
| 1528 | pw.increaseIndent(); |
| 1529 | mTtyManager.dump(pw); |
| 1530 | pw.decreaseIndent(); |
| 1531 | } |
| 1532 | |
| 1533 | if (mInCallController != null) { |
| 1534 | pw.println("mInCallController:"); |
| 1535 | pw.increaseIndent(); |
| 1536 | mInCallController.dump(pw); |
| 1537 | pw.decreaseIndent(); |
| 1538 | } |
| 1539 | |
| 1540 | if (mConnectionServiceRepository != null) { |
| 1541 | pw.println("mConnectionServiceRepository:"); |
| 1542 | pw.increaseIndent(); |
| 1543 | mConnectionServiceRepository.dump(pw); |
| 1544 | pw.decreaseIndent(); |
| 1545 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 1546 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 1547 | } |