blob: e9f90336333c042968e6521a827cdcd7dce7592a [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;
25import android.util.Log;
26
Ben Gilad9f2bed32013-12-12 17:43:26 -080027import com.android.telecomm.exceptions.CallServiceUnavailableException;
28import com.android.telecomm.exceptions.OutgoingCallException;
29
Ben Gilad9f2bed32013-12-12 17:43:26 -080030import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080031import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033/**
34 * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make
35 * outgoing calls and (2) switching active calls between transports (each ICallService is
36 * considered a different transport type).
37 * TODO(santoscordon): Need to add comments on the switchboard optimizer once that it is place.
Ben Gilad0407fb22014-01-09 16:18:41 -080038 * TODO(gilad): Add a monitor thread to wake up periodically and check for stale pending calls
39 * that may need to be terminated, see mPendingOutgoingCalls.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080040 */
Ben Giladd17443c2014-01-06 11:04:15 -080041final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080042
Santos Cordon8e8b8d22013-12-19 14:14:05 -080043 private static final String TAG = Switchboard.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080044
Ben Gilad0407fb22014-01-09 16:18:41 -080045 private CallServiceFinder callServiceFinder = new CallServiceFinder(this);
46
47 private Set<Call> mPendingOutgoingCalls = Sets.newHashSet();
Ben Giladd17443c2014-01-06 11:04:15 -080048
Santos Cordon8e8b8d22013-12-19 14:14:05 -080049 /**
50 * Places an outgoing call to the handle passed in. Method asynchronously collects
51 * {@link ICallService} implementations and passes them along with the handle and contactInfo
52 * to {@link #placeOutgoingCallInternal} to actually place the call.
53 *
Ben Gilad0407fb22014-01-09 16:18:41 -080054 * @param handle The handle to dial.
55 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080056 * @param context The application context.
57 */
Ben Giladd17443c2014-01-06 11:04:15 -080058 void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -080059 ThreadUtil.checkOnMainThread();
60 mPendingOutgoingCalls.add(new Call(handle, contactInfo));
Ben Giladd17443c2014-01-06 11:04:15 -080061 callServiceFinder.initiateLookup(context);
Ben Gilad0407fb22014-01-09 16:18:41 -080062 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080063
Ben Gilad0407fb22014-01-09 16:18:41 -080064 /**
65 * Persists the specified list of call services and attempts to connect any pending outgoing
66 * calls still waiting for a matching call-service to be initiated.
67 *
68 * @param callServices The potentially-partial list of call services the switchboard should
69 * feel free to make use of. Partial since the lookup procedure is time-boxed such that
70 * some providers/call-services may be too slow to respond and hence effectively omitted
71 * from the specified list. If the switchboard has previous/reliable knowledge of other
72 * call-services, it should be at liberty to use these just as well.
73 */
74 void setCallServices(List<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -080075 ThreadUtil.checkOnMainThread();
76 for (Call pendingCall : mPendingOutgoingCalls) {
77 // TODO(gilad): Iterate through the prioritized list of switchboard policies passing
78 // to each policy the call object as well as all known call services. Break out of
79 // the inner/policy loop as soon as the first matching policy for the call is found.
80 // Calls for which no matching policy can be found will be killed by cleanup/monitor
81 // thread, see the "monitor" to-do at the top of the file.
Ben Gilad0407fb22014-01-09 16:18:41 -080082
Evan Charlton0958f532014-01-10 16:58:02 -080083 // Psuedo code (assuming connect to be a future switchboard method):
84 //
85 // FOR policy IN prioritizedPolicies:
86 // IF policy.is_applicable_to(pendingCall, callServices):
87 // TRY
88 // connect(pendingCall, callServices, policy)
89 // mPendingOutgoingCalls.remove(pendingCall)
90 // BREAK
Ben Gilad0407fb22014-01-09 16:18:41 -080091 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080092 }
93
Santos Cordon8e8b8d22013-12-19 14:14:05 -080094 /**
95 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
96 * select one and place a call to the handle.
97 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -080098 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 *
100 * @param handle The handle to dial.
101 * @param contactInfo Information about the entity being called.
102 * @param callServices The list of available {@link ICallService}s.
103 */
Ben Giladd17443c2014-01-06 11:04:15 -0800104// private void placeOutgoingCallInternal(
105// String handle,
106// ContactInfo contactInfo,
107// List<ICallService> callServices) throws CallServiceUnavailableException {
108//
109// Log.i(TAG, "Placing and outgoing call.");
110//
111// if (callServices.isEmpty()) {
112// // No call services, bail out.
113// // TODO(contacts-team): Add logging?
114// // TODO(santoscordon): Does this actually go anywhere considering this method is now
115// // asynchronous?
116// throw new CallServiceUnavailableException("No CallService found.");
117// }
118//
119// List<ICallService> compatibleCallServices = Lists.newArrayList();
120// for (ICallService service : callServices) {
121// // TODO(santoscordon): This code needs to be updated to an asynchronous response
122// // callback from isCompatibleWith().
123// /* if (service.isCompatibleWith(handle)) {
124// // NOTE(android-contacts): If we end up taking the liberty to issue
125// // calls not using the explicit user input (in case one is provided)
126// // and instead pull an alternative method of communication from the
127// // specified user-info object, it may be desirable to give precedence
128// // to services that can in fact respect the user's intent.
129// compatibleCallServices.add(service);
130// }
131// */
132// }
133//
134// if (compatibleCallServices.isEmpty()) {
135// // None of the available call services is suitable for making this call.
136// // TODO(contacts-team): Same here re logging.
137// throw new CallServiceUnavailableException("No compatible CallService found.");
138// }
139//
140// // NOTE(android-team): At this point we can also prompt the user for
141// // preference, i.e. instead of the logic just below.
142// if (compatibleCallServices.size() > 1) {
143// compatibleCallServices = sort(compatibleCallServices);
144// }
145// for (ICallService service : compatibleCallServices) {
146// try {
147// service.call(handle);
148// return;
149// } catch (RemoteException e) {
150// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
151// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
152// // handles this.
153// }
154// // catch (OutgoingCallException ignored) {
155// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
156// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
157// }
158// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800159
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800160 /**
Ben Giladd17443c2014-01-06 11:04:15 -0800161 * Sorts a list of {@link ICallService} ordered by the preferred service for dialing the call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800162 *
163 * @param callServices The list to order.
164 */
165 private List<ICallService> sort(List<ICallService> callServices) {
166 // TODO(android-contacts): Sort by reliability, cost, and ultimately
167 // the desirability to issue a given call over each of the specified
168 // call services.
169 return callServices;
Ben Gilad9f2bed32013-12-12 17:43:26 -0800170 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800171}