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 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 21 | import android.telecomm.CallAudioState; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 22 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 23 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 24 | import android.telecomm.GatewayInfo; |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 25 | import android.telecomm.PhoneAccount; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 26 | import android.telephony.DisconnectCause; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 27 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 28 | import com.google.common.base.Preconditions; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableCollection; |
| 30 | import com.google.common.collect.ImmutableList; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 31 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 32 | import java.util.HashSet; |
| 33 | import java.util.LinkedHashSet; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 34 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Singleton. |
| 38 | * |
| 39 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 40 | * access from other packages specifically refraining from passing the CallsManager instance |
| 41 | * beyond the com.android.telecomm package boundary. |
| 42 | */ |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 43 | public final class CallsManager extends Call.ListenerBase { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 45 | // TODO(santoscordon): Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 46 | interface CallsManagerListener { |
| 47 | void onCallAdded(Call call); |
| 48 | void onCallRemoved(Call call); |
| 49 | void onCallStateChanged(Call call, CallState oldState, CallState newState); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 50 | void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 51 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 52 | ConnectionServiceWrapper oldService, |
| 53 | ConnectionServiceWrapper newService); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 54 | void onIncomingCallAnswered(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 55 | void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 56 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 57 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 58 | void onRequestingRingback(Call call, boolean ringback); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 59 | void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable); |
| 60 | void onIsConferencedChanged(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 61 | void onCannedSmsResponsesLoaded(Call call); |
Nancy Chen | a65d41f | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 62 | void onCallVideoProviderChanged(Call call); |
Tyler Gunn | e19cc00 | 2014-07-01 11:32:53 -0700 | [diff] [blame] | 63 | void onFeaturesChanged(Call call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 66 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 67 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 68 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 69 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 70 | * calls are added to the map and removed when the calls move to the disconnected state. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 71 | */ |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 72 | private final Set<Call> mCalls = new LinkedHashSet<>(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 73 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 74 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(); |
| 75 | private final InCallController mInCallController = new InCallController(); |
| 76 | private final CallAudioManager mCallAudioManager; |
| 77 | private final Ringer mRinger; |
| 78 | private final Set<CallsManagerListener> mListeners = new HashSet<>(); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 79 | private final HeadsetMediaButton mHeadsetMediaButton; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 82 | * The call the user is currently interacting with. This is the call that should have audio |
| 83 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 84 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 85 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 86 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 87 | /** Singleton accessor. */ |
| 88 | static CallsManager getInstance() { |
| 89 | return INSTANCE; |
| 90 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 91 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 92 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 93 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 94 | */ |
| 95 | private CallsManager() { |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 96 | TelecommApp app = TelecommApp.getInstance(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 97 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 98 | StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this); |
| 99 | mCallAudioManager = new CallAudioManager(app, statusBarNotifier); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 100 | InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 101 | mRinger = new Ringer(mCallAudioManager, this, playerFactory, app); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 102 | mHeadsetMediaButton = new HeadsetMediaButton(app, this); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 103 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 104 | mListeners.add(statusBarNotifier); |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 105 | mListeners.add(new CallLogManager(app)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 106 | mListeners.add(new PhoneStateBroadcaster()); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 107 | mListeners.add(mInCallController); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 108 | mListeners.add(mRinger); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 109 | mListeners.add(new RingbackPlayer(this, playerFactory)); |
| 110 | mListeners.add(new InCallToneMonitor(playerFactory, this)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 111 | mListeners.add(mCallAudioManager); |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 112 | mListeners.add(app.getMissedCallNotifier()); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 113 | mListeners.add(mDtmfLocalTonePlayer); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 114 | mListeners.add(mHeadsetMediaButton); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 115 | mListeners.add(RespondViaSmsManager.getInstance()); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public void onSuccessfulOutgoingCall(Call call) { |
| 120 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
| 121 | if (mCalls.contains(call)) { |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 122 | // The call's ConnectionService has been updated. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 123 | for (CallsManagerListener listener : mListeners) { |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 124 | listener.onConnectionServiceChanged(call, null, call.getConnectionService()); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 125 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 126 | } else { |
| 127 | Log.wtf(this, "unexpected successful call notification: %s", call); |
| 128 | return; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 129 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 130 | |
| 131 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 135 | public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) { |
| 136 | Log.v(this, "onFailedOutgoingCall, call: %s", call); |
| 137 | // TODO: Replace disconnect cause with more specific disconnect causes. |
| 138 | markCallAsDisconnected(call, errorCode, errorMsg); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onCancelledOutgoingCall(Call call) { |
| 143 | Log.v(this, "onCancelledOutgoingCall, call: %s", call); |
| 144 | setCallState(call, CallState.ABORTED); |
| 145 | removeCall(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 149 | public void onSuccessfulIncomingCall(Call call) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 150 | Log.d(this, "onSuccessfulIncomingCall"); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 151 | setCallState(call, CallState.RINGING); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 152 | addCall(call); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onFailedIncomingCall(Call call) { |
| 157 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 160 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 161 | public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) { |
| 162 | for (CallsManagerListener listener : mListeners) { |
| 163 | listener.onIsConferenceCapableChanged(call, isConferenceCapable); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 168 | public void onRequestingRingback(Call call, boolean ringback) { |
| 169 | for (CallsManagerListener listener : mListeners) { |
| 170 | listener.onRequestingRingback(call, ringback); |
| 171 | } |
| 172 | } |
| 173 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 174 | @Override |
| 175 | public void onPostDialWait(Call call, String remaining) { |
| 176 | mInCallController.onPostDialWait(call, remaining); |
| 177 | } |
| 178 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public void onExpiredConferenceCall(Call call) { |
| 181 | call.removeListener(this); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onConfirmedConferenceCall(Call call) { |
| 186 | addCall(call); |
| 187 | Log.v(this, "confirming Conf call %s", call); |
| 188 | for (CallsManagerListener listener : mListeners) { |
| 189 | listener.onIsConferencedChanged(call); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onParentChanged(Call call) { |
| 195 | for (CallsManagerListener listener : mListeners) { |
| 196 | listener.onIsConferencedChanged(call); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onChildrenChanged(Call call) { |
| 202 | for (CallsManagerListener listener : mListeners) { |
| 203 | listener.onIsConferencedChanged(call); |
| 204 | } |
| 205 | } |
| 206 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public void onCannedSmsResponsesLoaded(Call call) { |
| 209 | for (CallsManagerListener listener : mListeners) { |
| 210 | listener.onCannedSmsResponsesLoaded(call); |
| 211 | } |
| 212 | } |
| 213 | |
Nancy Chen | a65d41f | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public void onCallVideoProviderChanged(Call call) { |
| 216 | for (CallsManagerListener listener : mListeners) { |
| 217 | listener.onCallVideoProviderChanged(call); |
| 218 | } |
| 219 | } |
| 220 | |
Tyler Gunn | e19cc00 | 2014-07-01 11:32:53 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public void onFeaturesChanged(Call call) { |
| 223 | Log.v(this, "onFeaturesChanged: %d", call.getFeatures()); |
| 224 | for (CallsManagerListener listener : mListeners) { |
| 225 | listener.onFeaturesChanged(call); |
| 226 | } |
| 227 | } |
| 228 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 229 | ImmutableCollection<Call> getCalls() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 230 | return ImmutableList.copyOf(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | Call getForegroundCall() { |
| 234 | return mForegroundCall; |
| 235 | } |
| 236 | |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 237 | Ringer getRinger() { |
| 238 | return mRinger; |
| 239 | } |
| 240 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 241 | InCallController getInCallController() { |
| 242 | return mInCallController; |
| 243 | } |
| 244 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 245 | boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 246 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 247 | if (call.isEmergencyCall()) { |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | CallAudioState getAudioState() { |
| 255 | return mCallAudioManager.getAudioState(); |
| 256 | } |
| 257 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 258 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 259 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 260 | * specified call; using the specified call service descriptor. Upon success, execution returns |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 261 | * to {@link #onSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 262 | * |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 263 | * @param descriptor The descriptor of the connection service to use for this incoming call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 264 | * @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] | 265 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 266 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 267 | Log.d(this, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 268 | // Create a call with no handle. Eventually, switchboard will update the call with |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 269 | // additional information from the connection service, but for now we just need one to pass |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 270 | // around. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 271 | Call call = new Call(true /* isIncoming */, false /* isConference */); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 272 | // TODO(santoscordon): Move this to be a part of addCall() |
| 273 | call.addListener(this); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 274 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 275 | call.startIncoming(descriptor, extras); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 279 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 280 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 281 | * @param handle Handle to connect the call with. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 282 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 283 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 284 | * actual dialed handle via a gateway provider. May be null. |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 285 | * @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^] | 286 | * @param videoState The desired video state for the outgoing call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 287 | */ |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 288 | void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame^] | 289 | PhoneAccount account, boolean speakerphoneOn, int videoState) { |
| 290 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 291 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle(); |
| 292 | |
| 293 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 294 | 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] | 295 | } else { |
| 296 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 297 | Log.pii(uriHandle), Log.pii(handle)); |
| 298 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 299 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 300 | Call call = new Call( |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 301 | uriHandle, gatewayInfo, account, |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 302 | false /* isIncoming */, false /* isConference */); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 303 | call.setStartWithSpeakerphoneOn(speakerphoneOn); |
Tyler Gunn | c4abd91 | 2014-07-08 14:22:10 -0700 | [diff] [blame^] | 304 | call.setVideoState(videoState); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 305 | |
| 306 | // TODO(santoscordon): Move this to be a part of addCall() |
| 307 | call.addListener(this); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 308 | addCall(call); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 309 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 310 | call.startOutgoing(); |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 314 | * Attempts to start a conference call for the specified call. |
| 315 | * |
| 316 | * @param call The call to conference with. |
| 317 | */ |
| 318 | void conference(Call call) { |
| 319 | Call conferenceCall = new Call(false, true); |
| 320 | conferenceCall.addListener(this); |
| 321 | call.conferenceInto(conferenceCall); |
| 322 | } |
| 323 | |
| 324 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 325 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 326 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 327 | * the user opting to answer said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 328 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 329 | void answerCall(Call call) { |
| 330 | if (!mCalls.contains(call)) { |
| 331 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 332 | } else { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 333 | // If the foreground call is not the ringing call and it is currently isActive() or |
| 334 | // DIALING, put it on hold before answering the call. |
| 335 | if (mForegroundCall != null && mForegroundCall != call && |
| 336 | (mForegroundCall.isActive() || |
| 337 | mForegroundCall.getState() == CallState.DIALING)) { |
| 338 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
| 339 | mForegroundCall, call); |
| 340 | mForegroundCall.hold(); |
| 341 | // TODO(santoscordon): Wait until we get confirmation of the active call being |
| 342 | // on-hold before answering the new call. |
| 343 | // TODO(santoscordon): Import logic from CallManager.acceptCall() |
| 344 | } |
| 345 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 346 | for (CallsManagerListener listener : mListeners) { |
| 347 | listener.onIncomingCallAnswered(call); |
| 348 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 349 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 350 | // 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] | 351 | // {@link #markCallAsActive}. |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 352 | call.answer(); |
| 353 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 358 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 359 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 360 | */ |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 361 | void rejectCall(Call call, boolean rejectWithMessage, String textMessage) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 362 | if (!mCalls.contains(call)) { |
| 363 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 364 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 365 | for (CallsManagerListener listener : mListeners) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 366 | listener.onIncomingCallRejected(call, rejectWithMessage, textMessage); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 367 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 368 | call.reject(rejectWithMessage, textMessage); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 369 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 373 | * Instructs Telecomm to play the specified DTMF tone within the specified call. |
| 374 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 375 | * @param digit The DTMF digit to play. |
| 376 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 377 | void playDtmfTone(Call call, char digit) { |
| 378 | if (!mCalls.contains(call)) { |
| 379 | 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] | 380 | } else { |
| 381 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 382 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Instructs Telecomm to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 388 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 389 | void stopDtmfTone(Call call) { |
| 390 | if (!mCalls.contains(call)) { |
| 391 | 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] | 392 | } else { |
| 393 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 394 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 399 | * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 400 | */ |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 401 | void postDialContinue(Call call, boolean proceed) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 402 | if (!mCalls.contains(call)) { |
| 403 | 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] | 404 | } else { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 405 | call.postDialContinue(proceed); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 410 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 411 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 412 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 413 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 414 | void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 415 | Log.v(this, "disconnectCall %s", call); |
| 416 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 417 | if (!mCalls.contains(call)) { |
| 418 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 419 | } else { |
| 420 | call.disconnect(); |
| 421 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 422 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 423 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 424 | /** |
| 425 | * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the |
| 426 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 427 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 428 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 429 | void holdCall(Call call) { |
| 430 | if (!mCalls.contains(call)) { |
| 431 | 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] | 432 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 433 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 434 | call.hold(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Instructs Telecomm to release the specified call from hold. Intended to be invoked by |
| 440 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 441 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 442 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 443 | void unholdCall(Call call) { |
| 444 | if (!mCalls.contains(call)) { |
| 445 | 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] | 446 | } else { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 447 | Log.d(this, "unholding call: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 448 | call.unhold(); |
| 449 | } |
| 450 | } |
| 451 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 452 | /** Called by the in-call UI to change the mute state. */ |
| 453 | void mute(boolean shouldMute) { |
| 454 | mCallAudioManager.mute(shouldMute); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 459 | * speaker phone. |
| 460 | */ |
| 461 | void setAudioRoute(int route) { |
| 462 | mCallAudioManager.setAudioRoute(route); |
| 463 | } |
| 464 | |
Sailesh Nepal | 77da19e | 2014-07-02 21:31:16 -0700 | [diff] [blame] | 465 | void phoneAccountClicked(Call call) { |
| 466 | if (!mCalls.contains(call)) { |
| 467 | Log.i(this, "phoneAccountClicked in a non-existent call %s", call); |
| 468 | } else { |
| 469 | call.phoneAccountClicked(); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 470 | } |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 473 | /** Called when the audio state changes. */ |
| 474 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
| 475 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 476 | for (CallsManagerListener listener : mListeners) { |
| 477 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 478 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 481 | void markCallAsRinging(Call call) { |
| 482 | setCallState(call, CallState.RINGING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 485 | void markCallAsDialing(Call call) { |
| 486 | setCallState(call, CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 489 | void markCallAsActive(Call call) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 490 | if (call.getConnectTimeMillis() == 0) { |
| 491 | call.setConnectTimeMillis(System.currentTimeMillis()); |
| 492 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 493 | setCallState(call, CallState.ACTIVE); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 494 | |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 495 | if (call.getStartWithSpeakerphoneOn()) { |
| 496 | setAudioRoute(CallAudioState.ROUTE_SPEAKER); |
| 497 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 500 | void markCallAsOnHold(Call call) { |
| 501 | setCallState(call, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 504 | /** |
| 505 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 506 | * live call, then also disconnect from the in-call controller. |
| 507 | * |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 508 | * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}. |
| 509 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 510 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 511 | void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) { |
| 512 | call.setDisconnectCause(disconnectCause, disconnectMessage); |
| 513 | setCallState(call, CallState.DISCONNECTED); |
Sailesh Nepal | 77da19e | 2014-07-02 21:31:16 -0700 | [diff] [blame] | 514 | removeCall(call); |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 517 | /** |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 518 | * Cleans up any calls currently associated with the specified connection service when the |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 519 | * call-service binder disconnects unexpectedly. |
| 520 | * |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 521 | * @param service The connection service that disconnected. |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 522 | */ |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 523 | void handleConnectionServiceDeath(ConnectionServiceWrapper service) { |
| 524 | Preconditions.checkNotNull(service); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 525 | for (Call call : ImmutableList.copyOf(mCalls)) { |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 526 | if (call.getConnectionService() == service) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 527 | markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null); |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 532 | boolean hasAnyCalls() { |
| 533 | return !mCalls.isEmpty(); |
| 534 | } |
| 535 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 536 | boolean hasActiveOrHoldingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 537 | return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | boolean hasRingingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 541 | return getFirstCallWithState(CallState.RINGING) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 544 | boolean onMediaButton(int type) { |
| 545 | if (hasAnyCalls()) { |
| 546 | if (HeadsetMediaButton.SHORT_PRESS == type) { |
| 547 | Call ringingCall = getFirstCallWithState(CallState.RINGING); |
| 548 | if (ringingCall == null) { |
| 549 | mCallAudioManager.toggleMute(); |
| 550 | return true; |
| 551 | } else { |
| 552 | ringingCall.answer(); |
| 553 | return true; |
| 554 | } |
| 555 | } else if (HeadsetMediaButton.LONG_PRESS == type) { |
| 556 | Log.d(this, "handleHeadsetHook: longpress -> hangup"); |
| 557 | Call callToHangup = getFirstCallWithState( |
| 558 | CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD); |
| 559 | if (callToHangup != null) { |
| 560 | callToHangup.disconnect(); |
| 561 | return true; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | /** |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 569 | * Checks to see if the specified call is the only high-level call and if so, enable the |
| 570 | * "Add-call" button. We allow you to add a second call but not a third or beyond. |
| 571 | * |
| 572 | * @param call The call to test for add-call. |
| 573 | * @return Whether the add-call feature should be enabled for the call. |
| 574 | */ |
| 575 | protected boolean isAddCallCapable(Call call) { |
| 576 | if (call.getParentCall() != null) { |
| 577 | // Never true for child calls. |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | // Loop through all the other calls and there exists a top level (has no parent) call |
| 582 | // that is not the specified call, return false. |
| 583 | for (Call otherCall : mCalls) { |
| 584 | if (call != otherCall && otherCall.getParentCall() == null) { |
| 585 | return false; |
| 586 | } |
| 587 | } |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | /** |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 592 | * Returns the first call that it finds with the given states. The states are treated as having |
| 593 | * priority order so that any call with the first state will be returned before any call with |
| 594 | * states listed later in the parameter list. |
| 595 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 596 | Call getFirstCallWithState(CallState... states) { |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 597 | for (CallState currentState : states) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 598 | // check the foreground first |
| 599 | if (mForegroundCall != null && mForegroundCall.getState() == currentState) { |
| 600 | return mForegroundCall; |
| 601 | } |
| 602 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 603 | for (Call call : mCalls) { |
| 604 | if (currentState == call.getState()) { |
| 605 | return call; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | return null; |
| 610 | } |
| 611 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 612 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 613 | * Adds the specified call to the main list of live calls. |
| 614 | * |
| 615 | * @param call The call to add. |
| 616 | */ |
| 617 | private void addCall(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 618 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 619 | |
| 620 | // TODO(santoscordon): Update mForegroundCall prior to invoking |
| 621 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 622 | for (CallsManagerListener listener : mListeners) { |
| 623 | listener.onCallAdded(call); |
| 624 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 625 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 628 | private void removeCall(Call call) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 629 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 630 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 631 | call.removeListener(this); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 632 | call.clearConnectionService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 633 | |
| 634 | boolean shouldNotify = false; |
| 635 | if (mCalls.contains(call)) { |
| 636 | mCalls.remove(call); |
| 637 | shouldNotify = true; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 638 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 639 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 640 | // Only broadcast changes for calls that are being tracked. |
| 641 | if (shouldNotify) { |
| 642 | for (CallsManagerListener listener : mListeners) { |
| 643 | listener.onCallRemoved(call); |
| 644 | } |
| 645 | updateForegroundCall(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 648 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 649 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 650 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 651 | * |
| 652 | * @param call The call. |
| 653 | * @param newState The new state of the call. |
| 654 | */ |
| 655 | private void setCallState(Call call, CallState newState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 656 | Preconditions.checkNotNull(newState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 657 | CallState oldState = call.getState(); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 658 | Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 659 | if (newState != oldState) { |
| 660 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 661 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 662 | // sense (e.g., ACTIVE -> RINGING). |
| 663 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 664 | // into a well-defined state machine. |
| 665 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 666 | // unexpected transition occurs. |
| 667 | call.setState(newState); |
| 668 | |
| 669 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 670 | if (mCalls.contains(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 671 | for (CallsManagerListener listener : mListeners) { |
| 672 | listener.onCallStateChanged(call, oldState, newState); |
| 673 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 674 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 680 | * 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] | 681 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 682 | private void updateForegroundCall() { |
| 683 | Call newForegroundCall = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 684 | for (Call call : mCalls) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 685 | // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 686 | // of its state will be foreground by default and instead the connection service should |
| 687 | // 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] | 688 | // the call should play audio and listen to microphone if it wants. |
| 689 | |
| 690 | // Active calls have priority. |
| 691 | if (call.isActive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 692 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 693 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 694 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 695 | |
| 696 | if (call.isAlive() || call.getState() == CallState.RINGING) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 697 | newForegroundCall = call; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 698 | // 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] | 699 | } |
| 700 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 701 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 702 | if (newForegroundCall != mForegroundCall) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 703 | Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 704 | Call oldForegroundCall = mForegroundCall; |
| 705 | mForegroundCall = newForegroundCall; |
| 706 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 707 | for (CallsManagerListener listener : mListeners) { |
| 708 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 709 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 710 | } |
| 711 | } |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 712 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 713 | private static boolean areDescriptorsEqual( |
| 714 | CallServiceDescriptor descriptor1, |
| 715 | CallServiceDescriptor descriptor2) { |
| 716 | if (descriptor1 == null) { |
| 717 | return descriptor2 == null; |
| 718 | } |
| 719 | return descriptor1.equals(descriptor2); |
| 720 | } |
| 721 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 722 | private static boolean areUriEqual(Uri handle1, Uri handle2) { |
| 723 | if (handle1 == null) { |
| 724 | return handle2 == null; |
| 725 | } |
| 726 | return handle1.equals(handle2); |
| 727 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 728 | } |