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 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 19 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 20 | import android.telecomm.CallState; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 21 | import android.util.Log; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 22 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 23 | import com.google.common.base.Preconditions; |
| 24 | import com.google.common.base.Strings; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 26 | import com.google.common.collect.Maps; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 27 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 28 | import java.util.List; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 29 | import java.util.Map; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 30 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Singleton. |
| 33 | * |
| 34 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 35 | * access from other packages specifically refraining from passing the CallsManager instance |
| 36 | * beyond the com.android.telecomm package boundary. |
| 37 | */ |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 38 | public final class CallsManager { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 39 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 40 | private static final String TAG = CallsManager.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 41 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 42 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 43 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 44 | private final Switchboard mSwitchboard; |
| 45 | |
| 46 | /** Used to control the in-call app. */ |
| 47 | private final InCallController mInCallController; |
| 48 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 49 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 50 | * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming |
| 51 | * and outgoing calls are added to the map and removed when the calls move to the disconnected |
| 52 | * state. |
| 53 | * TODO(santoscordon): Add new CallId class and use it in place of String. |
| 54 | */ |
| 55 | private final Map<String, Call> mCalls = Maps.newHashMap(); |
| 56 | |
| 57 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 58 | * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set |
| 59 | * of CallsManager APIs that need to be exposed to the dialer (or any application firing call |
| 60 | * intents) may be empty. |
| 61 | */ |
| 62 | private DialerAdapter mDialerAdapter; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 63 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 64 | private InCallAdapter mInCallAdapter; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 65 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 66 | private CallLogManager mCallLogManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 67 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 68 | private VoicemailManager mVoicemailManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 69 | |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 70 | private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 71 | |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 72 | private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 73 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 74 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 75 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 76 | */ |
| 77 | private CallsManager() { |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 78 | mSwitchboard = new Switchboard(this); |
| 79 | mInCallController = new InCallController(this); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 82 | static CallsManager getInstance() { |
| 83 | return INSTANCE; |
| 84 | } |
| 85 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 86 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 87 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 88 | * specified call; using the specified call service descriptor. Upon success, execution returns |
| 89 | * to {@link #handleSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 90 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 91 | * @param descriptor The descriptor of the call service to use for this incoming call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 92 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 93 | void processIncomingCallIntent(CallServiceDescriptor descriptor) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 94 | Log.d(TAG, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 95 | // Create a call with no handle. Eventually, switchboard will update the call with |
| 96 | // additional information from the call service, but for now we just need one to pass around |
| 97 | // with a unique call ID. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 98 | Call call = new Call(); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 99 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 100 | mSwitchboard.retrieveIncomingCall(call, descriptor); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 104 | * Validates the specified call and, upon no objection to connect it, adds the new call to the |
| 105 | * list of live calls. Also notifies the in-call app so the user can answer or reject the call. |
| 106 | * |
| 107 | * @param call The new incoming call. |
| 108 | */ |
| 109 | void handleSuccessfulIncomingCall(Call call) { |
| 110 | Log.d(TAG, "handleSuccessfulIncomingCall"); |
| 111 | Preconditions.checkState(call.getState() == CallState.RINGING); |
| 112 | |
| 113 | String handle = call.getHandle(); |
| 114 | ContactInfo contactInfo = call.getContactInfo(); |
| 115 | for (IncomingCallValidator validator : mIncomingCallValidators) { |
| 116 | if (!validator.isValid(handle, contactInfo)) { |
| 117 | // TODO(gilad): Consider displaying an error message. |
| 118 | Log.i(TAG, "Dropping restricted incoming call"); |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // No objection to accept the incoming call, proceed with potentially connecting it (based |
| 124 | // on the user's action, or lack thereof). |
| 125 | addCall(call); |
| 126 | } |
| 127 | |
| 128 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 129 | * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint, |
| 130 | * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED, |
| 131 | * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes |
| 132 | * this method. |
| 133 | * |
| 134 | * @param handle The handle to dial. |
| 135 | * @param contactInfo Information about the entity being called. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 136 | */ |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 137 | void processOutgoingCallIntent(String handle, ContactInfo contactInfo) { |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 138 | for (OutgoingCallValidator validator : mOutgoingCallValidators) { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 139 | if (!validator.isValid(handle, contactInfo)) { |
| 140 | // TODO(gilad): Display an error message. |
| 141 | Log.i(TAG, "Dropping restricted outgoing call."); |
| 142 | return; |
| 143 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // No objection to issue the call, proceed with trying to put it through. |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 147 | Call call = new Call(handle, contactInfo); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 148 | mSwitchboard.placeOutgoingCall(call); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 149 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 152 | * Adds a new outgoing call to the list of live calls and notifies the in-call app. |
| 153 | * |
| 154 | * @param call The new outgoing call. |
| 155 | */ |
| 156 | void handleSuccessfulOutgoingCall(Call call) { |
| 157 | // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the |
| 158 | // placed call from the call service so there is no need to set it here. Instead, check that |
| 159 | // the state is appropriate. |
| 160 | Preconditions.checkState(call.getState() == CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 161 | addCall(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 165 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 166 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 167 | * the user opting to answer said call. |
| 168 | * |
| 169 | * @param callId The ID of the call. |
| 170 | */ |
| 171 | void answerCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 172 | Call call = mCalls.get(callId); |
| 173 | if (call == null) { |
| 174 | Log.i(TAG, "Request to answer a non-existent call " + callId); |
| 175 | } else { |
| 176 | // We do not update the UI until we get confirmation of the answer() through |
| 177 | // {@link #markCallAsActive}. However, if we ever change that to look more responsive, |
| 178 | // then we need to make sure we add a timeout for the answer() in case the call never |
| 179 | // comes out of RINGING. |
| 180 | call.answer(); |
| 181 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 186 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 187 | * the user opting to reject said call. |
| 188 | * |
| 189 | * @param callId The ID of the call. |
| 190 | */ |
| 191 | void rejectCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 192 | Call call = mCalls.get(callId); |
| 193 | if (call == null) { |
| 194 | Log.i(TAG, "Request to reject a non-existent call " + callId); |
| 195 | } else { |
| 196 | call.reject(); |
| 197 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 202 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 203 | * the user hitting the end-call button. |
| 204 | * |
| 205 | * @param callId The ID of the call. |
| 206 | */ |
| 207 | void disconnectCall(String callId) { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 208 | Call call = mCalls.get(callId); |
| 209 | if (call == null) { |
| 210 | Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect"); |
| 211 | } else { |
| 212 | call.disconnect(); |
| 213 | } |
| 214 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 215 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 216 | |
| 217 | void markCallAsRinging(String callId) { |
| 218 | setCallState(callId, CallState.RINGING); |
| 219 | } |
| 220 | |
| 221 | void markCallAsDialing(String callId) { |
| 222 | setCallState(callId, CallState.DIALING); |
| 223 | } |
| 224 | |
| 225 | void markCallAsActive(String callId) { |
| 226 | setCallState(callId, CallState.ACTIVE); |
Sailesh Nepal | fc511ca | 2014-02-20 18:48:53 -0800 | [diff] [blame] | 227 | mInCallController.markCallAsActive(callId); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 230 | /** |
| 231 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 232 | * live call, then also disconnect from the in-call controller. |
| 233 | * |
| 234 | * @param callId The ID of the call. |
| 235 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 236 | void markCallAsDisconnected(String callId) { |
| 237 | setCallState(callId, CallState.DISCONNECTED); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 238 | |
| 239 | Call call = mCalls.remove(callId); |
| 240 | // At this point the call service has confirmed that the call is disconnected to it is |
| 241 | // safe to disassociate the call from its call service. |
| 242 | call.clearCallService(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 243 | |
| 244 | // Notify the in-call UI |
| 245 | mInCallController.markCallAsDisconnected(callId); |
| 246 | if (mCalls.isEmpty()) { |
| 247 | mInCallController.unbind(); |
| 248 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 252 | * Sends all the live calls to the in-call app if any exist. If there are no live calls, then |
| 253 | * tells the in-call controller to unbind since it is not needed. |
| 254 | */ |
| 255 | void updateInCall() { |
| 256 | if (mCalls.isEmpty()) { |
| 257 | mInCallController.unbind(); |
| 258 | } else { |
| 259 | for (Call call : mCalls.values()) { |
| 260 | addInCallEntry(call); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Adds the specified call to the main list of live calls. |
| 267 | * |
| 268 | * @param call The call to add. |
| 269 | */ |
| 270 | private void addCall(Call call) { |
| 271 | mCalls.put(call.getId(), call); |
| 272 | addInCallEntry(call); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Notifies the in-call app of the specified (new) call. |
| 277 | */ |
| 278 | private void addInCallEntry(Call call) { |
| 279 | mInCallController.addCall(call.toCallInfo()); |
| 280 | } |
| 281 | |
| 282 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 283 | * Sets the specified state on the specified call. |
| 284 | * |
| 285 | * @param callId The ID of the call to update. |
| 286 | * @param state The new state of the call. |
| 287 | */ |
| 288 | private void setCallState(String callId, CallState state) { |
| 289 | Preconditions.checkState(!Strings.isNullOrEmpty(callId)); |
| 290 | Preconditions.checkNotNull(state); |
| 291 | |
| 292 | Call call = mCalls.get(callId); |
| 293 | if (call == null) { |
| 294 | Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " + |
| 295 | state + "."); |
| 296 | } else { |
| 297 | // Unfortunately, in the telephony world, the radio is king. So if the call notifies us |
| 298 | // that the call is in a particular state, we allow it even if it doesn't make sense |
| 299 | // (e.g., ACTIVE -> RINGING). |
| 300 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState into |
| 301 | // a well-defined state machine. |
| 302 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 303 | // unexpected transition occurs. |
| 304 | call.setState(state); |
| 305 | // TODO(santoscordon): Notify the in-call app whenever a call changes state. |
| 306 | } |
| 307 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 308 | } |