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