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