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