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