blob: 52bacd909fe7ec9c6596802066b05315d59636f0 [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
Evan Charltonac1aa9e2014-01-03 13:47:12 -080019import com.google.common.collect.Lists;
Ben Gilad0407fb22014-01-09 16:18:41 -080020import com.google.common.collect.Sets;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080021
22import android.content.Context;
23import android.os.RemoteException;
24import android.telecomm.ICallService;
Ben Giladb59769e2014-01-16 11:41:10 -080025import android.telecomm.ICallServiceSelector;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080026import android.util.Log;
27
Ben Gilad9f2bed32013-12-12 17:43:26 -080028import com.android.telecomm.exceptions.CallServiceUnavailableException;
29import com.android.telecomm.exceptions.OutgoingCallException;
30
Ben Giladb59769e2014-01-16 11:41:10 -080031import java.util.Collection;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080033import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Santos Cordon8e8b8d22013-12-19 14:14:05 -080035/**
36 * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make
37 * outgoing calls and (2) switching active calls between transports (each ICallService is
38 * considered a different transport type).
39 * TODO(santoscordon): Need to add comments on the switchboard optimizer once that it is place.
Ben Gilad0407fb22014-01-09 16:18:41 -080040 * TODO(gilad): Add a monitor thread to wake up periodically and check for stale pending calls
41 * that may need to be terminated, see mPendingOutgoingCalls.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080042 */
Ben Giladd17443c2014-01-06 11:04:15 -080043final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080044
Santos Cordon8e8b8d22013-12-19 14:14:05 -080045 private static final String TAG = Switchboard.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080046
Ben Giladb59769e2014-01-16 11:41:10 -080047 private CallServiceFinder mCallServiceFinder = new CallServiceFinder(this);
48
49 private CallServiceSelectorFinder mSelectorFinder = new CallServiceSelectorFinder(this);
50
Ben Gilad03292d42014-01-16 15:06:16 -080051 /**
52 * The set of currently available call-service implementations, see {@link CallServiceFinder}.
53 * TODO(gilad): Null out once the active-call count goes to zero.
54 */
55 private Set<ICallService> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080056
Ben Gilad03292d42014-01-16 15:06:16 -080057 /**
58 * The set of currently available call-service-selector implementations,
59 * see {@link CallServiceSelectorFinder}.
60 * TODO(gilad): Null out once the active-call count goes to zero.
61 */
62 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080063
64 private Set<Call> mPendingOutgoingCalls = Sets.newHashSet();
Ben Giladd17443c2014-01-06 11:04:15 -080065
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 /**
67 * Places an outgoing call to the handle passed in. Method asynchronously collects
68 * {@link ICallService} implementations and passes them along with the handle and contactInfo
69 * to {@link #placeOutgoingCallInternal} to actually place the call.
Ben Giladb59769e2014-01-16 11:41:10 -080070 * TODO(gilad): Update.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080071 *
Ben Gilad0407fb22014-01-09 16:18:41 -080072 * @param handle The handle to dial.
73 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080074 * @param context The application context.
75 */
Ben Giladd17443c2014-01-06 11:04:15 -080076 void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -080077 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -080078
79 // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager.
80 Call call = new Call(handle, contactInfo);
81 boolean bailout = false;
82 if (isNullOrEmpty(mCallServices)) {
83 mCallServiceFinder.initiateLookup(context);
84 bailout = true;
85 }
86 if (isNullOrEmpty(mSelectors)) {
87 mSelectorFinder.initiateLookup(context);
88 bailout = true;
89 }
90
91 if (bailout) {
92 // Unable to process the call without either call service, selectors, or both.
93 // Store the call for deferred processing and bail out.
94 mPendingOutgoingCalls.add(call);
95 return;
96 }
97
98 placeOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -080099 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800100
Ben Gilad0407fb22014-01-09 16:18:41 -0800101 /**
Ben Gilad03292d42014-01-16 15:06:16 -0800102 * Persists the specified set of call services and attempts to connect any pending outgoing
103 * calls (still waiting for a matching call-service to be initiated). Intended to be called
104 * by {@link CallServiceFinder} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800105 *
Ben Gilad03292d42014-01-16 15:06:16 -0800106 * @param callServices The potentially-partial set of call services. Partial since the lookup
107 * process is time-boxed, such that some providers/call-services may be slow to respond and
108 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800109 */
Ben Gilad03292d42014-01-16 15:06:16 -0800110 void setCallServices(Set<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800111 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800112
Ben Giladb59769e2014-01-16 11:41:10 -0800113 mCallServices = callServices;
114 processPendingOutgoingCalls();
115 }
116
117 /**
118 * Persists the specified list of selectors and attempts to connect any pending outgoing
119 * calls. Intended to be called by {@link CallServiceSelectorFinder} exclusively.
120 *
Ben Gilad03292d42014-01-16 15:06:16 -0800121 * @param selectors The potentially-partial set of selectors. Partial since the lookup
122 * procedure is time-boxed such that some selectors may be slow to respond and hence
123 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800124 */
Ben Gilad03292d42014-01-16 15:06:16 -0800125 void setSelectors(Set<ICallServiceSelector> selectors) {
Ben Giladb59769e2014-01-16 11:41:10 -0800126 ThreadUtil.checkOnMainThread();
127
128 mSelectors = selectors;
129 processPendingOutgoingCalls();
130 }
131
132 /**
133 * Attempts to process any pending outgoing calls that have not yet been expired.
134 */
135 void processPendingOutgoingCalls() {
136 for (Call call : mPendingOutgoingCalls) {
137 placeOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800138 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800139 }
140
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800141 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800142 * Attempts to place the specified call.
143 *
144 * @param call The call to put through.
145 */
146 private void placeOutgoingCall(Call call) {
147 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
148 // At least one call service and one selector are required to process outgoing calls.
149 return;
150 }
151
152 // TODO(gilad): Iterate through the prioritized list of selectors passing to each selector
153 // (read-only versions of) the call object and all available call services. Break out once
154 // a matching selector is found. Calls with no matching selectors will eventually be killed
155 // by the cleanup/monitor thread, see the "monitor" to-do at the top of the file.
156
157 // Psuedo code (assuming connect to be a future switchboard method):
158 //
159 // FOR selector IN prioritizedSelectors:
160 // prioritizedCallServices = selector.select(mCallServices, call)
161 // IF notEmpty(prioritizedCallServices):
162 // FOR callService IN prioritizedCallServices:
163 // TRY
164 // connect(call, callService, selector)
165 // mPendingOutgoingCalls.remove(call)
166 // BREAK
167 }
168
169 /**
170 * Determines whether or not the specified collection is either null or empty.
171 *
172 * @param collection Either null or the collection object to be evaluated.
173 * @return True if the collection is null or empty.
174 */
175 @SuppressWarnings("rawtypes")
176 private boolean isNullOrEmpty(Collection collection) {
177 return collection == null || collection.isEmpty();
178 }
179
180 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800181 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
182 * select one and place a call to the handle.
183 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -0800184 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800185 *
186 * @param handle The handle to dial.
187 * @param contactInfo Information about the entity being called.
188 * @param callServices The list of available {@link ICallService}s.
189 */
Ben Giladd17443c2014-01-06 11:04:15 -0800190// private void placeOutgoingCallInternal(
191// String handle,
192// ContactInfo contactInfo,
193// List<ICallService> callServices) throws CallServiceUnavailableException {
194//
195// Log.i(TAG, "Placing and outgoing call.");
196//
197// if (callServices.isEmpty()) {
198// // No call services, bail out.
199// // TODO(contacts-team): Add logging?
200// // TODO(santoscordon): Does this actually go anywhere considering this method is now
201// // asynchronous?
202// throw new CallServiceUnavailableException("No CallService found.");
203// }
204//
205// List<ICallService> compatibleCallServices = Lists.newArrayList();
206// for (ICallService service : callServices) {
207// // TODO(santoscordon): This code needs to be updated to an asynchronous response
208// // callback from isCompatibleWith().
209// /* if (service.isCompatibleWith(handle)) {
210// // NOTE(android-contacts): If we end up taking the liberty to issue
211// // calls not using the explicit user input (in case one is provided)
212// // and instead pull an alternative method of communication from the
213// // specified user-info object, it may be desirable to give precedence
214// // to services that can in fact respect the user's intent.
215// compatibleCallServices.add(service);
216// }
217// */
218// }
219//
220// if (compatibleCallServices.isEmpty()) {
221// // None of the available call services is suitable for making this call.
222// // TODO(contacts-team): Same here re logging.
223// throw new CallServiceUnavailableException("No compatible CallService found.");
224// }
225//
226// // NOTE(android-team): At this point we can also prompt the user for
227// // preference, i.e. instead of the logic just below.
228// if (compatibleCallServices.size() > 1) {
229// compatibleCallServices = sort(compatibleCallServices);
230// }
231// for (ICallService service : compatibleCallServices) {
232// try {
233// service.call(handle);
234// return;
235// } catch (RemoteException e) {
236// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
237// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
238// // handles this.
239// }
240// // catch (OutgoingCallException ignored) {
241// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
242// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
243// }
244// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800245}