blob: 4abebce61216f9b16d1b6afb3b4a12e3070c83a1 [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
51 /** TODO(gilad): Add comment, may also want to use a set instead. */
52 /** TODO(gilad): Null out once the active-call count goes to zero. */
53 private List<ICallService> mCallServices;
54
55 /** TODO(gilad): Add comment, may also want to use a set instead. */
56 /** TODO(gilad): Null out once the active-call count goes to zero. */
57 private List<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080058
59 private Set<Call> mPendingOutgoingCalls = Sets.newHashSet();
Ben Giladd17443c2014-01-06 11:04:15 -080060
Santos Cordon8e8b8d22013-12-19 14:14:05 -080061 /**
62 * Places an outgoing call to the handle passed in. Method asynchronously collects
63 * {@link ICallService} implementations and passes them along with the handle and contactInfo
64 * to {@link #placeOutgoingCallInternal} to actually place the call.
Ben Giladb59769e2014-01-16 11:41:10 -080065 * TODO(gilad): Update.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 *
Ben Gilad0407fb22014-01-09 16:18:41 -080067 * @param handle The handle to dial.
68 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080069 * @param context The application context.
70 */
Ben Giladd17443c2014-01-06 11:04:15 -080071 void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -080072 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -080073
74 // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager.
75 Call call = new Call(handle, contactInfo);
76 boolean bailout = false;
77 if (isNullOrEmpty(mCallServices)) {
78 mCallServiceFinder.initiateLookup(context);
79 bailout = true;
80 }
81 if (isNullOrEmpty(mSelectors)) {
82 mSelectorFinder.initiateLookup(context);
83 bailout = true;
84 }
85
86 if (bailout) {
87 // Unable to process the call without either call service, selectors, or both.
88 // Store the call for deferred processing and bail out.
89 mPendingOutgoingCalls.add(call);
90 return;
91 }
92
93 placeOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -080094 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080095
Ben Gilad0407fb22014-01-09 16:18:41 -080096 /**
97 * Persists the specified list of call services and attempts to connect any pending outgoing
Ben Giladb59769e2014-01-16 11:41:10 -080098 * calls still waiting for a matching call-service to be initiated. Intended to be called by
99 * {@link CallServiceFinder} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800100 *
Ben Giladb59769e2014-01-16 11:41:10 -0800101 * @param callServices The potentially-partial list of call services. Partial since the
102 * lookup procedure is time-boxed, such that some providers/call-services may be slow
103 * to respond and hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800104 */
105 void setCallServices(List<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800106 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800107
Ben Giladb59769e2014-01-16 11:41:10 -0800108 mCallServices = callServices;
109 processPendingOutgoingCalls();
110 }
111
112 /**
113 * Persists the specified list of selectors and attempts to connect any pending outgoing
114 * calls. Intended to be called by {@link CallServiceSelectorFinder} exclusively.
115 *
116 * @param selectors The potentially-partial list of selectors. Partial since the lookup
117 * procedure is time-boxed, such that some selectors may be slow to respond and hence
118 * effectively omitted from the specified list.
119 */
120 void setSelectors(List<ICallServiceSelector> selectors) {
121 ThreadUtil.checkOnMainThread();
122
123 mSelectors = selectors;
124 processPendingOutgoingCalls();
125 }
126
127 /**
128 * Attempts to process any pending outgoing calls that have not yet been expired.
129 */
130 void processPendingOutgoingCalls() {
131 for (Call call : mPendingOutgoingCalls) {
132 placeOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800133 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800134 }
135
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800136 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800137 * Attempts to place the specified call.
138 *
139 * @param call The call to put through.
140 */
141 private void placeOutgoingCall(Call call) {
142 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
143 // At least one call service and one selector are required to process outgoing calls.
144 return;
145 }
146
147 // TODO(gilad): Iterate through the prioritized list of selectors passing to each selector
148 // (read-only versions of) the call object and all available call services. Break out once
149 // a matching selector is found. Calls with no matching selectors will eventually be killed
150 // by the cleanup/monitor thread, see the "monitor" to-do at the top of the file.
151
152 // Psuedo code (assuming connect to be a future switchboard method):
153 //
154 // FOR selector IN prioritizedSelectors:
155 // prioritizedCallServices = selector.select(mCallServices, call)
156 // IF notEmpty(prioritizedCallServices):
157 // FOR callService IN prioritizedCallServices:
158 // TRY
159 // connect(call, callService, selector)
160 // mPendingOutgoingCalls.remove(call)
161 // BREAK
162 }
163
164 /**
165 * Determines whether or not the specified collection is either null or empty.
166 *
167 * @param collection Either null or the collection object to be evaluated.
168 * @return True if the collection is null or empty.
169 */
170 @SuppressWarnings("rawtypes")
171 private boolean isNullOrEmpty(Collection collection) {
172 return collection == null || collection.isEmpty();
173 }
174
175 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800176 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
177 * select one and place a call to the handle.
178 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -0800179 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800180 *
181 * @param handle The handle to dial.
182 * @param contactInfo Information about the entity being called.
183 * @param callServices The list of available {@link ICallService}s.
184 */
Ben Giladd17443c2014-01-06 11:04:15 -0800185// private void placeOutgoingCallInternal(
186// String handle,
187// ContactInfo contactInfo,
188// List<ICallService> callServices) throws CallServiceUnavailableException {
189//
190// Log.i(TAG, "Placing and outgoing call.");
191//
192// if (callServices.isEmpty()) {
193// // No call services, bail out.
194// // TODO(contacts-team): Add logging?
195// // TODO(santoscordon): Does this actually go anywhere considering this method is now
196// // asynchronous?
197// throw new CallServiceUnavailableException("No CallService found.");
198// }
199//
200// List<ICallService> compatibleCallServices = Lists.newArrayList();
201// for (ICallService service : callServices) {
202// // TODO(santoscordon): This code needs to be updated to an asynchronous response
203// // callback from isCompatibleWith().
204// /* if (service.isCompatibleWith(handle)) {
205// // NOTE(android-contacts): If we end up taking the liberty to issue
206// // calls not using the explicit user input (in case one is provided)
207// // and instead pull an alternative method of communication from the
208// // specified user-info object, it may be desirable to give precedence
209// // to services that can in fact respect the user's intent.
210// compatibleCallServices.add(service);
211// }
212// */
213// }
214//
215// if (compatibleCallServices.isEmpty()) {
216// // None of the available call services is suitable for making this call.
217// // TODO(contacts-team): Same here re logging.
218// throw new CallServiceUnavailableException("No compatible CallService found.");
219// }
220//
221// // NOTE(android-team): At this point we can also prompt the user for
222// // preference, i.e. instead of the logic just below.
223// if (compatibleCallServices.size() > 1) {
224// compatibleCallServices = sort(compatibleCallServices);
225// }
226// for (ICallService service : compatibleCallServices) {
227// try {
228// service.call(handle);
229// return;
230// } catch (RemoteException e) {
231// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
232// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
233// // handles this.
234// }
235// // catch (OutgoingCallException ignored) {
236// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
237// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
238// }
239// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800240}