blob: cc01941d20a1f2198858647a9008b785cafc2ca5 [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) {
Ben Gilad0407fb22014-01-09 16:18:41 -080059 synchronized (mPendingOutgoingCalls) {
60 mPendingOutgoingCalls.add(new Call(handle, contactInfo));
61 }
Ben Giladd17443c2014-01-06 11:04:15 -080062 callServiceFinder.initiateLookup(context);
Ben Gilad0407fb22014-01-09 16:18:41 -080063 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080064
Ben Gilad0407fb22014-01-09 16:18:41 -080065 /**
66 * Persists the specified list of call services and attempts to connect any pending outgoing
67 * calls still waiting for a matching call-service to be initiated.
68 *
69 * @param callServices The potentially-partial list of call services the switchboard should
70 * feel free to make use of. Partial since the lookup procedure is time-boxed such that
71 * some providers/call-services may be too slow to respond and hence effectively omitted
72 * from the specified list. If the switchboard has previous/reliable knowledge of other
73 * call-services, it should be at liberty to use these just as well.
74 */
75 void setCallServices(List<ICallService> callServices) {
76 synchronized (mPendingOutgoingCalls) {
77 for (Call pendingCall : mPendingOutgoingCalls) {
78 // TODO(gilad): Iterate through the prioritized list of switchboard policies passing
79 // to each policy the call object as well as all known call services. Break out of
80 // the inner/policy loop as soon as the first matching policy for the call is found.
81 // Calls for which no matching policy can be found will be killed by cleanup/monitor
82 // thread, see the "monitor" to-do at the top of the file.
83
84 // Psuedo code (assuming connect to be a future switchboard method):
85 //
86 // FOR policy IN prioritizedPolicies:
87 // IF policy.is_applicable_to(pendingCall, callServices):
88 // TRY
89 // connect(pendingCall, callServices, policy)
90 // mPendingOutgoingCalls.remove(pendingCall)
91 // BREAK
92 }
93 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080094 }
95
Santos Cordon8e8b8d22013-12-19 14:14:05 -080096 /**
97 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
98 * select one and place a call to the handle.
99 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -0800100 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 *
102 * @param handle The handle to dial.
103 * @param contactInfo Information about the entity being called.
104 * @param callServices The list of available {@link ICallService}s.
105 */
Ben Giladd17443c2014-01-06 11:04:15 -0800106// private void placeOutgoingCallInternal(
107// String handle,
108// ContactInfo contactInfo,
109// List<ICallService> callServices) throws CallServiceUnavailableException {
110//
111// Log.i(TAG, "Placing and outgoing call.");
112//
113// if (callServices.isEmpty()) {
114// // No call services, bail out.
115// // TODO(contacts-team): Add logging?
116// // TODO(santoscordon): Does this actually go anywhere considering this method is now
117// // asynchronous?
118// throw new CallServiceUnavailableException("No CallService found.");
119// }
120//
121// List<ICallService> compatibleCallServices = Lists.newArrayList();
122// for (ICallService service : callServices) {
123// // TODO(santoscordon): This code needs to be updated to an asynchronous response
124// // callback from isCompatibleWith().
125// /* if (service.isCompatibleWith(handle)) {
126// // NOTE(android-contacts): If we end up taking the liberty to issue
127// // calls not using the explicit user input (in case one is provided)
128// // and instead pull an alternative method of communication from the
129// // specified user-info object, it may be desirable to give precedence
130// // to services that can in fact respect the user's intent.
131// compatibleCallServices.add(service);
132// }
133// */
134// }
135//
136// if (compatibleCallServices.isEmpty()) {
137// // None of the available call services is suitable for making this call.
138// // TODO(contacts-team): Same here re logging.
139// throw new CallServiceUnavailableException("No compatible CallService found.");
140// }
141//
142// // NOTE(android-team): At this point we can also prompt the user for
143// // preference, i.e. instead of the logic just below.
144// if (compatibleCallServices.size() > 1) {
145// compatibleCallServices = sort(compatibleCallServices);
146// }
147// for (ICallService service : compatibleCallServices) {
148// try {
149// service.call(handle);
150// return;
151// } catch (RemoteException e) {
152// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
153// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
154// // handles this.
155// }
156// // catch (OutgoingCallException ignored) {
157// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
158// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
159// }
160// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800161
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800162 /**
Ben Giladd17443c2014-01-06 11:04:15 -0800163 * Sorts a list of {@link ICallService} ordered by the preferred service for dialing the call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800164 *
165 * @param callServices The list to order.
166 */
167 private List<ICallService> sort(List<ICallService> callServices) {
168 // TODO(android-contacts): Sort by reliability, cost, and ultimately
169 // the desirability to issue a given call over each of the specified
170 // call services.
171 return callServices;
Ben Gilad9f2bed32013-12-12 17:43:26 -0800172 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800173}