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 | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 21 | import android.telecomm.CallInfo; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 22 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 23 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame^] | 24 | import android.telecomm.GatewayInfo; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 25 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 26 | import com.google.common.base.Preconditions; |
| 27 | import com.google.common.base.Strings; |
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; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 30 | import com.google.common.collect.Lists; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 31 | import com.google.common.collect.Maps; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 32 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 33 | import java.util.List; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 34 | import java.util.Map; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Singleton. |
| 38 | * |
| 39 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 40 | * access from other packages specifically refraining from passing the CallsManager instance |
| 41 | * beyond the com.android.telecomm package boundary. |
| 42 | */ |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 43 | public final class CallsManager { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 44 | |
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); |
| 49 | void onIncomingCallAnswered(Call call); |
| 50 | void onIncomingCallRejected(Call call); |
| 51 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
| 52 | } |
| 53 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 54 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 55 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 56 | private final Switchboard mSwitchboard; |
| 57 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 58 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 59 | * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming |
| 60 | * and outgoing calls are added to the map and removed when the calls move to the disconnected |
| 61 | * state. |
| 62 | * TODO(santoscordon): Add new CallId class and use it in place of String. |
| 63 | */ |
| 64 | private final Map<String, Call> mCalls = Maps.newHashMap(); |
| 65 | |
| 66 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 67 | * The call the user is currently interacting with. This is the call that should have audio |
| 68 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 69 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 70 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 71 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 72 | private final CallsManagerListener mCallLogManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 73 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 74 | private final CallsManagerListener mPhoneStateBroadcaster; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 75 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 76 | private final CallsManagerListener mCallAudioManager; |
| 77 | |
| 78 | private final CallsManagerListener mInCallController; |
| 79 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 80 | private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 81 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 82 | private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 83 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 84 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 85 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 86 | */ |
| 87 | private CallsManager() { |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 88 | mSwitchboard = new Switchboard(this); |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 89 | mCallLogManager = new CallLogManager(TelecommApp.getInstance()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 90 | mPhoneStateBroadcaster = new PhoneStateBroadcaster(); |
| 91 | mCallAudioManager = new CallAudioManager(); |
| 92 | mInCallController = new InCallController(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 95 | static CallsManager getInstance() { |
| 96 | return INSTANCE; |
| 97 | } |
| 98 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 99 | ImmutableCollection<Call> getCalls() { |
| 100 | return ImmutableList.copyOf(mCalls.values()); |
| 101 | } |
| 102 | |
| 103 | Call getForegroundCall() { |
| 104 | return mForegroundCall; |
| 105 | } |
| 106 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 107 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 108 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 109 | * specified call; using the specified call service descriptor. Upon success, execution returns |
| 110 | * to {@link #handleSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 111 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 112 | * @param descriptor The descriptor of the call service to use for this incoming call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 113 | * @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] | 114 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 115 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 116 | Log.d(this, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 117 | // Create a call with no handle. Eventually, switchboard will update the call with |
| 118 | // additional information from the call service, but for now we just need one to pass around |
| 119 | // with a unique call ID. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 120 | Call call = new Call(true /* isIncoming */); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 121 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 122 | mSwitchboard.retrieveIncomingCall(call, descriptor, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 126 | * Validates the specified call and, upon no objection to connect it, adds the new call to the |
| 127 | * list of live calls. Also notifies the in-call app so the user can answer or reject the call. |
| 128 | * |
| 129 | * @param call The new incoming call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 130 | * @param callInfo The details of the call. |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 131 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 132 | void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 133 | Log.d(this, "handleSuccessfulIncomingCall"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 134 | Preconditions.checkState(callInfo.getState() == CallState.RINGING); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 135 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 136 | Uri handle = call.getHandle(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 137 | ContactInfo contactInfo = call.getContactInfo(); |
| 138 | for (IncomingCallValidator validator : mIncomingCallValidators) { |
| 139 | if (!validator.isValid(handle, contactInfo)) { |
| 140 | // TODO(gilad): Consider displaying an error message. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 141 | Log.i(this, "Dropping restricted incoming call"); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // No objection to accept the incoming call, proceed with potentially connecting it (based |
| 147 | // on the user's action, or lack thereof). |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 148 | call.setHandle(callInfo.getHandle()); |
| 149 | setCallState(call, callInfo.getState()); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 150 | addCall(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 151 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 152 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Called when an incoming call was not connected. |
| 155 | * |
| 156 | * @param call The incoming call. |
| 157 | */ |
| 158 | void handleUnsuccessfulIncomingCall(Call call) { |
| 159 | setCallState(call, CallState.DISCONNECTED); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame^] | 163 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 164 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame^] | 165 | * @param handle Handle to connect the call with. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 166 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame^] | 167 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
| 168 | * actual dialed handle via a gateway provider. May be null. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 169 | */ |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame^] | 170 | void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) { |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 171 | for (OutgoingCallValidator validator : mOutgoingCallValidators) { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 172 | if (!validator.isValid(handle, contactInfo)) { |
| 173 | // TODO(gilad): Display an error message. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 174 | Log.i(this, "Dropping restricted outgoing call."); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame^] | 179 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle(); |
| 180 | |
| 181 | if (gatewayInfo == null) { |
| 182 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle)); |
| 183 | } else { |
| 184 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 185 | Log.pii(uriHandle), Log.pii(handle)); |
| 186 | } |
| 187 | Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 188 | setCallState(call, CallState.DIALING); |
| 189 | addCall(call); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 190 | mSwitchboard.placeOutgoingCall(call); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 191 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 194 | * Called when a call service acknowledges that it can place a call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 195 | * |
| 196 | * @param call The new outgoing call. |
| 197 | */ |
| 198 | void handleSuccessfulOutgoingCall(Call call) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 199 | Log.v(this, "handleSuccessfulOutgoingCall, %s", call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 203 | * Called when an outgoing call was not placed. |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 204 | * |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 205 | * @param call The outgoing call. |
| 206 | * @param isAborted True if the call was unsuccessful because it was aborted. |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 207 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 208 | void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) { |
| 209 | Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted); |
| 210 | if (isAborted) { |
| 211 | call.abort(); |
| 212 | setCallState(call, CallState.ABORTED); |
| 213 | } else { |
| 214 | setCallState(call, CallState.DISCONNECTED); |
| 215 | } |
| 216 | removeCall(call); |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 220 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 221 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 222 | * the user opting to answer said call. |
| 223 | * |
| 224 | * @param callId The ID of the call. |
| 225 | */ |
| 226 | void answerCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 227 | Call call = mCalls.get(callId); |
| 228 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 229 | Log.i(this, "Request to answer a non-existent call %s", callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 230 | } else { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 231 | mCallLogManager.onIncomingCallAnswered(call); |
| 232 | mPhoneStateBroadcaster.onIncomingCallAnswered(call); |
| 233 | mCallAudioManager.onIncomingCallAnswered(call); |
| 234 | mInCallController.onIncomingCallAnswered(call); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 235 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 236 | // We do not update the UI until we get confirmation of the answer() through |
| 237 | // {@link #markCallAsActive}. However, if we ever change that to look more responsive, |
| 238 | // then we need to make sure we add a timeout for the answer() in case the call never |
| 239 | // comes out of RINGING. |
| 240 | call.answer(); |
| 241 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 246 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 247 | * the user opting to reject said call. |
| 248 | * |
| 249 | * @param callId The ID of the call. |
| 250 | */ |
| 251 | void rejectCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 252 | Call call = mCalls.get(callId); |
| 253 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 254 | Log.i(this, "Request to reject a non-existent call %s", callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 255 | } else { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 256 | mCallLogManager.onIncomingCallRejected(call); |
| 257 | mPhoneStateBroadcaster.onIncomingCallRejected(call); |
| 258 | mCallAudioManager.onIncomingCallRejected(call); |
| 259 | mInCallController.onIncomingCallRejected(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 260 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 261 | call.reject(); |
| 262 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 267 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 268 | * the user hitting the end-call button. |
| 269 | * |
| 270 | * @param callId The ID of the call. |
| 271 | */ |
| 272 | void disconnectCall(String callId) { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 273 | Call call = mCalls.get(callId); |
| 274 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 275 | Log.w(this, "Unknown call (%s) asked to disconnect", callId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 276 | } else { |
| 277 | call.disconnect(); |
| 278 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 279 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 280 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the |
| 283 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 284 | * the user hitting the hold button during an active call. |
| 285 | * |
| 286 | * @param callId The ID of the call. |
| 287 | */ |
| 288 | void holdCall(String callId) { |
| 289 | Call call = mCalls.get(callId); |
| 290 | if (call == null) { |
| 291 | Log.w(this, "Unknown call (%s) asked to be put on hold", callId); |
| 292 | } else { |
| 293 | Log.d(this, "Putting call on hold: (%s)", callId); |
| 294 | call.hold(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Instructs Telecomm to release the specified call from hold. Intended to be invoked by |
| 300 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 301 | * by the user hitting the hold button during a held call. |
| 302 | * |
| 303 | * @param callId The ID of the call |
| 304 | */ |
| 305 | void unholdCall(String callId) { |
| 306 | Call call = mCalls.get(callId); |
| 307 | if (call == null) { |
| 308 | Log.w(this, "Unknown call (%s) asked to be removed from hold", callId); |
| 309 | } else { |
| 310 | Log.d(this, "Removing call from hold: (%s)", callId); |
| 311 | call.unhold(); |
| 312 | } |
| 313 | } |
| 314 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 315 | void markCallAsRinging(String callId) { |
| 316 | setCallState(callId, CallState.RINGING); |
| 317 | } |
| 318 | |
| 319 | void markCallAsDialing(String callId) { |
| 320 | setCallState(callId, CallState.DIALING); |
| 321 | } |
| 322 | |
| 323 | void markCallAsActive(String callId) { |
| 324 | setCallState(callId, CallState.ACTIVE); |
| 325 | } |
| 326 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 327 | void markCallAsOnHold(String callId) { |
| 328 | setCallState(callId, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 331 | /** |
| 332 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 333 | * live call, then also disconnect from the in-call controller. |
| 334 | * |
| 335 | * @param callId The ID of the call. |
| 336 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 337 | void markCallAsDisconnected(String callId) { |
| 338 | setCallState(callId, CallState.DISCONNECTED); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 339 | removeCall(mCalls.remove(callId)); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 343 | * @return True if there exists a call with the specific state. |
| 344 | */ |
| 345 | boolean hasCallWithState(CallState... states) { |
| 346 | for (Call call : mCalls.values()) { |
| 347 | for (CallState state : states) { |
| 348 | if (call.getState() == state) { |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | /** |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 358 | * Cleans up any calls currently associated with the specified call service when the |
| 359 | * call-service binder disconnects unexpectedly. |
| 360 | * |
| 361 | * @param callService The call service that disconnected. |
| 362 | */ |
| 363 | void handleCallServiceDeath(CallServiceWrapper callService) { |
| 364 | Preconditions.checkNotNull(callService); |
| 365 | for (Call call : ImmutableList.copyOf(mCalls.values())) { |
| 366 | if (call.getCallService() == callService) { |
| 367 | markCallAsDisconnected(call.getId()); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 373 | * Adds the specified call to the main list of live calls. |
| 374 | * |
| 375 | * @param call The call to add. |
| 376 | */ |
| 377 | private void addCall(Call call) { |
| 378 | mCalls.put(call.getId(), call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 379 | |
| 380 | mCallLogManager.onCallAdded(call); |
| 381 | mPhoneStateBroadcaster.onCallAdded(call); |
| 382 | mCallAudioManager.onCallAdded(call); |
| 383 | mInCallController.onCallAdded(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 384 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 387 | private void removeCall(Call call) { |
| 388 | call.clearCallService(); |
| 389 | |
| 390 | mCallLogManager.onCallRemoved(call); |
| 391 | mPhoneStateBroadcaster.onCallRemoved(call); |
| 392 | mCallAudioManager.onCallRemoved(call); |
| 393 | mInCallController.onCallRemoved(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 394 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 398 | * Sets the specified state on the specified call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 399 | * |
| 400 | * @param callId The ID of the call to update. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 401 | * @param newState The new state of the call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 402 | */ |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 403 | private void setCallState(String callId, CallState newState) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 404 | Preconditions.checkState(!Strings.isNullOrEmpty(callId)); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 405 | Preconditions.checkNotNull(newState); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 406 | |
| 407 | Call call = mCalls.get(callId); |
| 408 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 409 | Log.w(this, "Call %s was not found while attempting to update the state to %s.", |
| 410 | callId, newState ); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 411 | } else { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 412 | setCallState(call, newState); |
| 413 | } |
| 414 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 415 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 416 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 417 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 418 | * |
| 419 | * @param call The call. |
| 420 | * @param newState The new state of the call. |
| 421 | */ |
| 422 | private void setCallState(Call call, CallState newState) { |
| 423 | CallState oldState = call.getState(); |
| 424 | if (newState != oldState) { |
| 425 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 426 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 427 | // sense (e.g., ACTIVE -> RINGING). |
| 428 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 429 | // into a well-defined state machine. |
| 430 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 431 | // unexpected transition occurs. |
| 432 | call.setState(newState); |
| 433 | |
| 434 | // Only broadcast state change for calls that are being tracked. |
| 435 | if (mCalls.containsKey(call.getId())) { |
| 436 | mCallLogManager.onCallStateChanged(call, oldState, newState); |
| 437 | mPhoneStateBroadcaster.onCallStateChanged(call, oldState, newState); |
| 438 | mCallAudioManager.onCallStateChanged(call, oldState, newState); |
| 439 | mInCallController.onCallStateChanged(call, oldState, newState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 440 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 446 | * 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] | 447 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 448 | private void updateForegroundCall() { |
| 449 | Call newForegroundCall = null; |
| 450 | for (Call call : mCalls.values()) { |
| 451 | // Incoming ringing calls have priority. |
| 452 | if (call.getState() == CallState.RINGING) { |
| 453 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 454 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 455 | } |
| 456 | if (call.getState() == CallState.ACTIVE) { |
| 457 | newForegroundCall = call; |
| 458 | // Don't break in case there's a ringing call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 461 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 462 | if (newForegroundCall != mForegroundCall) { |
| 463 | Call oldForegroundCall = mForegroundCall; |
| 464 | mForegroundCall = newForegroundCall; |
| 465 | |
| 466 | mCallLogManager.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 467 | mPhoneStateBroadcaster.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 468 | mCallAudioManager.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 469 | mInCallController.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 472 | } |