blob: 82f94fa3a21ccc81d6251bc1bb079c1961c2a039 [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright 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 Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Santos Cordon5b7b9b32014-03-26 14:00:22 -070019import android.content.ComponentName;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Ben Gilad2313e622014-02-06 12:02:25 -080021import android.os.Handler;
22import android.os.Looper;
Santos Cordonc499c1c2014-04-14 17:13:14 -070023
24import android.os.Message;
Sailesh Nepal810735e2014-03-18 18:15:46 -070025import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080026import android.telecomm.CallServiceDescriptor;
Evan Charltona05805b2014-03-05 08:21:46 -080027import android.telecomm.TelecommConstants;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Santos Cordon5b7b9b32014-03-26 14:00:22 -070029import com.google.common.base.Preconditions;
30import com.google.common.collect.ImmutableCollection;
31import com.google.common.collect.ImmutableList;
32import com.google.common.collect.Sets;
33
34import java.util.Collection;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080035import java.util.Iterator;
Ben Gilad0407fb22014-01-09 16:18:41 -080036import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080037
Santos Cordon8e8b8d22013-12-19 14:14:05 -080038/**
Sailesh Nepal18386a82014-03-19 10:22:40 -070039 * Switchboard is responsible for:
Santos Cordon5b7b9b32014-03-26 14:00:22 -070040 * - gathering the {@link CallServiceWrapper}s and {@link CallServiceSelectorWrapper}s through
41 * which to place outgoing calls
42 * - starting outgoing calls (via {@link OutgoingCallsManager}
43 * - switching active calls between call services.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080044 */
Ben Giladd17443c2014-01-06 11:04:15 -080045final class Switchboard {
Santos Cordonc499c1c2014-04-14 17:13:14 -070046 private final static int MSG_EXPIRE_STALE_CALL = 1;
Ben Gilad2495d572014-01-09 17:26:19 -080047
Santos Cordon681663d2014-01-30 04:32:15 -080048 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080049
Santos Cordon681663d2014-01-30 04:32:15 -080050 /** Used to place outgoing calls. */
51 private final OutgoingCallsManager mOutgoingCallsManager;
52
Santos Cordon493e8f22014-02-19 03:15:12 -080053 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080054 private final IncomingCallsManager mIncomingCallsManager;
55
Santos Cordonc195e362014-02-11 17:05:31 -080056 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080057
Santos Cordonc195e362014-02-11 17:05:31 -080058 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080059
60 /** Used to schedule tasks on the main (UI) thread. */
Santos Cordonc499c1c2014-04-14 17:13:14 -070061 private final Handler mHandler = new Handler() {
Ben Gilad2313e622014-02-06 12:02:25 -080062 @Override
Santos Cordonc499c1c2014-04-14 17:13:14 -070063 public void handleMessage(Message msg) {
64 switch(msg.what) {
65 case MSG_EXPIRE_STALE_CALL:
66 expireStaleCall((Call) msg.obj);
67 break;
68 default:
69 Log.wtf(Switchboard.this, "Unexpected message %d.", msg.what);
Ben Gilad2313e622014-02-06 12:02:25 -080070 }
71 }
72 };
73
74 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
75
76 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080077
Ben Gilad03292d42014-01-16 15:06:16 -080078 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080079 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080080 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
81 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080082 */
Evan Charltonf02e9882014-03-06 12:54:52 -080083 private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080084
Ben Gilad03292d42014-01-16 15:06:16 -080085 /**
86 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080087 * see {@link CallServiceSelectorRepository}.
Evan Charltonf02e9882014-03-06 12:54:52 -080088 * TODO(gilad): Clear once the active-call count goes to zero.
Ben Gilad03292d42014-01-16 15:06:16 -080089 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070090 private ImmutableCollection<CallServiceSelectorWrapper> mSelectors = ImmutableList.of();
Ben Gilad0407fb22014-01-09 16:18:41 -080091
Santos Cordon681663d2014-01-30 04:32:15 -080092 /**
Santos Cordonc195e362014-02-11 17:05:31 -080093 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080094 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -080095 */
96 private int mLookupId = 0;
97
98 /**
Santos Cordon681663d2014-01-30 04:32:15 -080099 * Persists the specified parameters and initializes Switchboard.
100 */
Santos Cordon6242b132014-02-07 16:24:42 -0800101 Switchboard(CallsManager callsManager) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700102 ThreadUtil.checkOnMainThread();
103
Santos Cordon6242b132014-02-07 16:24:42 -0800104 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800105 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800106 mIncomingCallsManager = new IncomingCallsManager(this);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700107 mSelectorRepository = new CallServiceSelectorRepository(this, mOutgoingCallsManager);
Santos Cordon493e8f22014-02-19 03:15:12 -0800108 mCallServiceRepository =
109 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Santos Cordon681663d2014-01-30 04:32:15 -0800110 }
Ben Giladd17443c2014-01-06 11:04:15 -0800111
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800112 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800113 * Starts the process of placing an outgoing call by searching for available call services
114 * through which the call can be placed. After a lookup for those services completes, execution
115 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800116 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800117 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800118 */
Santos Cordonc195e362014-02-11 17:05:31 -0800119 void placeOutgoingCall(Call call) {
Ben Giladbb167cd2014-02-25 16:24:05 -0800120 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
121 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
122 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
123 // reset, (5) the selector lookup completes but the call-services are missing. This should
124 // be okay since the call-service lookup completed. Specifically the already-available call
125 // services are cached and will be provided in response to the second lookup cycle.
Evan Charltonf02e9882014-03-06 12:54:52 -0800126 mCallServices.clear();
Sailesh Nepal18386a82014-03-19 10:22:40 -0700127 mSelectors = ImmutableList.of();
Ben Giladbb167cd2014-02-25 16:24:05 -0800128
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800129 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800130
Ben Giladebd9b662014-02-19 16:03:44 -0800131 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800132 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800133 mCallServiceRepository.initiateLookup(mLookupId);
134 mSelectorRepository.initiateLookup(mLookupId);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700135
136 Message msg = mHandler.obtainMessage(MSG_EXPIRE_STALE_CALL, call);
137 mHandler.sendMessageDelayed(msg, Timeouts.getNewOutgoingCallMillis());
Ben Gilad0407fb22014-01-09 16:18:41 -0800138 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800139
Ben Gilad0407fb22014-01-09 16:18:41 -0800140 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800141 * Retrieves details about the incoming call through the incoming call manager. The incoming
142 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
143 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800144 *
145 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800146 * @param descriptor The relevant call-service descriptor.
Evan Charltona05805b2014-03-05 08:21:46 -0800147 * @param extras The optional extras passed via
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700148 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800149 */
Evan Charltona05805b2014-03-05 08:21:46 -0800150 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800151 Log.d(this, "retrieveIncomingCall");
Ben Giladc5b22692014-02-18 20:03:22 -0800152 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800153 call.setCallService(callService);
Evan Charltona05805b2014-03-05 08:21:46 -0800154 mIncomingCallsManager.retrieveIncomingCall(call, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800155 }
156
157 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800158 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800159 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800160 *
Ben Gilad03292d42014-01-16 15:06:16 -0800161 * @param callServices The potentially-partial set of call services. Partial since the lookup
162 * process is time-boxed, such that some providers/call-services may be slow to respond and
163 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800164 */
Santos Cordonc195e362014-02-11 17:05:31 -0800165 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charltonf02e9882014-03-06 12:54:52 -0800166 mCallServices.clear();
167 mCallServices.addAll(callServices);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800168 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800169 }
170
171 /**
172 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800173 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800174 *
Sailesh Nepal18386a82014-03-19 10:22:40 -0700175 * @param selectors Collection of selectors. The order of the collection determines the order in
176 * which the selectors are tried.
Ben Giladb59769e2014-01-16 11:41:10 -0800177 */
Sailesh Nepal18386a82014-03-19 10:22:40 -0700178 void setSelectors(ImmutableCollection<CallServiceSelectorWrapper> selectors) {
Ben Giladb59769e2014-01-16 11:41:10 -0800179 ThreadUtil.checkOnMainThread();
Sailesh Nepal18386a82014-03-19 10:22:40 -0700180 Preconditions.checkNotNull(selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800181
Ben Gilad134cf092014-01-16 18:26:12 -0800182 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
183 // emergency calls) and order the entire set prior to the assignment below. If the
184 // built-in selectors can be implemented in a manner that does not require binding,
Sailesh Nepal18386a82014-03-19 10:22:40 -0700185 // that's probably preferred.
186 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800187 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800188 }
189
190 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800191 * Handles the case where an outgoing call has been successfully placed,
192 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800193 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800194 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800195 Log.d(this, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800196
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800197 mCallsManager.handleSuccessfulOutgoingCall(call);
198 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800199 }
200
201 /**
202 * Handles the case where an outgoing call could not be proceed by any of the
203 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
204 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700205 void handleFailedOutgoingCall(Call call, boolean isAborted) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800206 Log.d(this, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800207
Sailesh Nepal810735e2014-03-18 18:15:46 -0700208 mCallsManager.handleUnsuccessfulOutgoingCall(call, isAborted);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800209 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800210 }
211
212 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800213 * Handles the case where we successfully receive details of an incoming call. Hands the
214 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
215 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800216 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700217 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800218 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800219
Sailesh Nepal810735e2014-03-18 18:15:46 -0700220 mCallsManager.handleSuccessfulIncomingCall(call, callInfo);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800221 }
222
223 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800224 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
225 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800226 *
227 * @param call The call.
228 */
229 void handleFailedIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800230 Log.d(this, "handleFailedIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800231
Santos Cordon493e8f22014-02-19 03:15:12 -0800232 // Since we set the call service before calling into incoming-calls manager, we clear it for
233 // good measure if an error is reported.
234 call.clearCallService();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700235 mCallsManager.handleUnsuccessfulIncomingCall(call);
236
Santos Cordon493e8f22014-02-19 03:15:12 -0800237 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
238 // a future date bind to the in-call app optimistically during the incoming-call sequence
239 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
240 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800241 }
242
243 /**
Ihab Awad4e0f1922014-04-14 17:35:38 -0700244 * Ensures any state regarding a call is cleaned up.
245 *
246 * @param call The call.
247 */
248 void abortCall(Call call) {
249 Log.d(this, "abortCall");
250 mOutgoingCallsManager.abort(call);
251 }
252
253 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800254 * Attempts to process the next new outgoing calls that have not yet been expired.
255 */
256 private void processNewOutgoingCalls() {
Evan Charltonf02e9882014-03-06 12:54:52 -0800257 if (mCallServices.isEmpty() || mSelectors.isEmpty()) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800258 // At least one call service and one selector are required to process outgoing calls.
259 return;
260 }
261
262 if (!mNewOutgoingCalls.isEmpty()) {
263 Call call = mNewOutgoingCalls.iterator().next();
264 mNewOutgoingCalls.remove(call);
265 mPendingOutgoingCalls.add(call);
266
267 // Specifically only attempt to place one call at a time such that call services
268 // can be freed from needing to deal with concurrent requests.
269 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800270 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800271 }
272
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800273 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800274 * Attempts to place the specified call.
275 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800276 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800277 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800278 private void processNewOutgoingCall(Call call) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700279 Collection<CallServiceSelectorWrapper> selectors;
280
281 // Use the call's selector if it's already tied to one. This is the case for handoff calls.
282 if (call.getCallServiceSelector() != null) {
283 selectors = ImmutableList.of(call.getCallServiceSelector());
284 } else {
285 selectors = mSelectors;
286 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700287
288 boolean useEmergencySelector =
289 EmergencyCallServiceSelector.shouldUseSelector(call.getHandle());
290 Log.d(this, "processNewOutgoingCall, isEmergency=%b", useEmergencySelector);
291
292 if (useEmergencySelector) {
293 // This is potentially an emergency call so add the emergency selector before the
294 // other selectors.
295 ImmutableList.Builder<CallServiceSelectorWrapper> selectorsBuilder =
296 ImmutableList.builder();
297
298 ComponentName componentName = new ComponentName(
299 TelecommApp.getInstance(), EmergencyCallServiceSelector.class);
300 CallServiceSelectorWrapper emergencySelector =
301 new CallServiceSelectorWrapper(
302 componentName.flattenToShortString(),
303 componentName,
304 mCallsManager,
305 mOutgoingCallsManager);
306
307 selectorsBuilder.add(emergencySelector);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700308 selectorsBuilder.addAll(selectors);
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700309 selectors = selectorsBuilder.build();
310 }
311
312 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800313 }
314
315 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800316 * Finalizes the outgoing-call sequence, regardless if it succeeded or failed.
317 */
318 private void finalizeOutgoingCall(Call call) {
319 mPendingOutgoingCalls.remove(call);
320
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800321 processNewOutgoingCalls(); // Process additional (new) calls, if any.
322 }
323
324 /**
Santos Cordonc499c1c2014-04-14 17:13:14 -0700325 * Expires calls which are taking too long to connect.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800326 *
Santos Cordonc499c1c2014-04-14 17:13:14 -0700327 * @param call The call to expire.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800328 */
Santos Cordonc499c1c2014-04-14 17:13:14 -0700329 private void expireStaleCall(Call call) {
330 final long newCallTimeoutMillis = Timeouts.getNewOutgoingCallMillis();
331
332 if (call.getAgeMillis() < newCallTimeoutMillis) {
333 Log.wtf(this, "Expiring a call early. Age: %d, Time since attempt: %d",
334 call.getAgeMillis(), newCallTimeoutMillis);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800335 }
336
Santos Cordonc499c1c2014-04-14 17:13:14 -0700337 if (mNewOutgoingCalls.remove(call)) {
338 // The call had not yet been processed so all we have to do is report the
339 // failure.
340 handleFailedOutgoingCall(call, true /* isAborted */);
341 } else if (mPendingOutgoingCalls.remove(call)) {
342 if (!mOutgoingCallsManager.abort(call)) {
343 Log.wtf(this, "Pending call failed to abort, call: %s.", call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800344 }
345 }
346 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800347}