blob: c4c602849f08bab994de2e8e48aa93f6f8bba097 [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
Ben Gilad134cf092014-01-16 18:26:12 -0800128 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
129 // emergency calls) and order the entire set prior to the assignment below. If the
130 // built-in selectors can be implemented in a manner that does not require binding,
131 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800132 mSelectors = selectors;
133 processPendingOutgoingCalls();
134 }
135
136 /**
137 * Attempts to process any pending outgoing calls that have not yet been expired.
138 */
139 void processPendingOutgoingCalls() {
140 for (Call call : mPendingOutgoingCalls) {
141 placeOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800142 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800143 }
144
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800146 * Attempts to place the specified call.
147 *
148 * @param call The call to put through.
149 */
150 private void placeOutgoingCall(Call call) {
151 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
152 // At least one call service and one selector are required to process outgoing calls.
153 return;
154 }
155
156 // TODO(gilad): Iterate through the prioritized list of selectors passing to each selector
157 // (read-only versions of) the call object and all available call services. Break out once
158 // a matching selector is found. Calls with no matching selectors will eventually be killed
159 // by the cleanup/monitor thread, see the "monitor" to-do at the top of the file.
160
161 // Psuedo code (assuming connect to be a future switchboard method):
162 //
163 // FOR selector IN prioritizedSelectors:
164 // prioritizedCallServices = selector.select(mCallServices, call)
165 // IF notEmpty(prioritizedCallServices):
166 // FOR callService IN prioritizedCallServices:
167 // TRY
168 // connect(call, callService, selector)
169 // mPendingOutgoingCalls.remove(call)
170 // BREAK
171 }
172
173 /**
174 * Determines whether or not the specified collection is either null or empty.
175 *
176 * @param collection Either null or the collection object to be evaluated.
177 * @return True if the collection is null or empty.
178 */
179 @SuppressWarnings("rawtypes")
180 private boolean isNullOrEmpty(Collection collection) {
181 return collection == null || collection.isEmpty();
182 }
183
184 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800185 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
186 * select one and place a call to the handle.
187 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -0800188 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800189 *
190 * @param handle The handle to dial.
191 * @param contactInfo Information about the entity being called.
192 * @param callServices The list of available {@link ICallService}s.
193 */
Ben Giladd17443c2014-01-06 11:04:15 -0800194// private void placeOutgoingCallInternal(
195// String handle,
196// ContactInfo contactInfo,
197// List<ICallService> callServices) throws CallServiceUnavailableException {
198//
199// Log.i(TAG, "Placing and outgoing call.");
200//
201// if (callServices.isEmpty()) {
202// // No call services, bail out.
203// // TODO(contacts-team): Add logging?
204// // TODO(santoscordon): Does this actually go anywhere considering this method is now
205// // asynchronous?
206// throw new CallServiceUnavailableException("No CallService found.");
207// }
208//
209// List<ICallService> compatibleCallServices = Lists.newArrayList();
210// for (ICallService service : callServices) {
211// // TODO(santoscordon): This code needs to be updated to an asynchronous response
212// // callback from isCompatibleWith().
213// /* if (service.isCompatibleWith(handle)) {
214// // NOTE(android-contacts): If we end up taking the liberty to issue
215// // calls not using the explicit user input (in case one is provided)
216// // and instead pull an alternative method of communication from the
217// // specified user-info object, it may be desirable to give precedence
218// // to services that can in fact respect the user's intent.
219// compatibleCallServices.add(service);
220// }
221// */
222// }
223//
224// if (compatibleCallServices.isEmpty()) {
225// // None of the available call services is suitable for making this call.
226// // TODO(contacts-team): Same here re logging.
227// throw new CallServiceUnavailableException("No compatible CallService found.");
228// }
229//
230// // NOTE(android-team): At this point we can also prompt the user for
231// // preference, i.e. instead of the logic just below.
232// if (compatibleCallServices.size() > 1) {
233// compatibleCallServices = sort(compatibleCallServices);
234// }
235// for (ICallService service : compatibleCallServices) {
236// try {
237// service.call(handle);
238// return;
239// } catch (RemoteException e) {
240// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
241// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
242// // handles this.
243// }
244// // catch (OutgoingCallException ignored) {
245// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
246// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
247// }
248// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800249}