blob: 6ee4d220cd671d5ece75d19c4cec7cfdd275b92a [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;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080020
21import android.content.Context;
22import android.os.RemoteException;
23import android.telecomm.ICallService;
24import android.util.Log;
25
26import com.android.telecomm.CallServiceFinder.CallServiceSearchCallback;
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;
31
Santos Cordon8e8b8d22013-12-19 14:14:05 -080032/**
33 * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make
34 * outgoing calls and (2) switching active calls between transports (each ICallService is
35 * considered a different transport type).
36 * TODO(santoscordon): Need to add comments on the switchboard optimizer once that it is place.
37 */
Ben Gilad9f2bed32013-12-12 17:43:26 -080038class Switchboard {
Santos Cordon8e8b8d22013-12-19 14:14:05 -080039 /** Used to identify log entries by this class */
40 private static final String TAG = Switchboard.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080041
Santos Cordon8e8b8d22013-12-19 14:14:05 -080042 /**
43 * Places an outgoing call to the handle passed in. Method asynchronously collects
44 * {@link ICallService} implementations and passes them along with the handle and contactInfo
45 * to {@link #placeOutgoingCallInternal} to actually place the call.
46 *
47 * @param handle The handle to dial.
48 * @param contactInfo Information about the entity being called.
49 * @param context The application context.
50 */
51 void placeOutgoingCall(final String handle, final ContactInfo contactInfo,
52 final Context context) {
Ben Gilad9f2bed32013-12-12 17:43:26 -080053
Santos Cordon8e8b8d22013-12-19 14:14:05 -080054 CallServiceFinder.findCallServices(context, new CallServiceSearchCallback() {
55 @Override
56 public void onSearchComplete(List<ICallService> callServices) {
57 try {
58 placeOutgoingCallInternal(handle, contactInfo, callServices);
59 } catch (CallServiceUnavailableException e) {
60 // TODO(santoscordon): Handle error
61 }
62 }
63 });
Ben Gilad9f2bed32013-12-12 17:43:26 -080064 }
65
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 /**
67 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
68 * select one and place a call to the handle.
69 * TODO(santoscordon): How does the CallService selection process work?
70 *
71 * @param handle The handle to dial.
72 * @param contactInfo Information about the entity being called.
73 * @param callServices The list of available {@link ICallService}s.
74 */
75 private void placeOutgoingCallInternal(String handle, ContactInfo contactInfo,
76 List<ICallService> callServices) throws CallServiceUnavailableException {
77 Log.i(TAG, "Placing and outgoing call.");
78
79 if (callServices.isEmpty()) {
80 // No call services, bail out.
81 // TODO(contacts-team): Add logging?
82 // TODO(santoscordon): Does this actually go anywhere considering this method is now
83 // asynchronous?
84 throw new CallServiceUnavailableException("No CallService found.");
85 }
86
87 List<ICallService> compatibleCallServices = Lists.newArrayList();
88 for (ICallService service : callServices) {
89 // TODO(santoscordon): This code needs to be updated to an asynchronous response
90 // callback from isCompatibleWith().
91 /* if (service.isCompatibleWith(handle)) {
92 // NOTE(android-contacts): If we end up taking the liberty to issue
93 // calls not using the explicit user input (in case one is provided)
94 // and instead pull an alternative method of communication from the
95 // specified user-info object, it may be desirable to give precedence
96 // to services that can in fact respect the user's intent.
97 compatibleCallServices.add(service);
98 }
99 */
100 }
101
102 if (compatibleCallServices.isEmpty()) {
103 // None of the available call services is suitable for making this call.
104 // TODO(contacts-team): Same here re logging.
105 throw new CallServiceUnavailableException("No compatible CallService found.");
106 }
107
108 // NOTE(android-team): At this point we can also prompt the user for
109 // preference, i.e. instead of the logic just below.
110 if (compatibleCallServices.size() > 1) {
111 compatibleCallServices = sort(compatibleCallServices);
112 }
113 for (ICallService service : compatibleCallServices) {
114 try {
115 service.call(handle);
116 return;
117 } catch (RemoteException e) {
118 // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
119 // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
120 // handles this.
121 }
122 // catch (OutgoingCallException ignored) {
123 // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
124 // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
125 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800126 }
127
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800128 /**
129 * Sorts a list of {@link ICallService} ordered by the prefered service for dialing the call.
130 *
131 * @param callServices The list to order.
132 */
133 private List<ICallService> sort(List<ICallService> callServices) {
134 // TODO(android-contacts): Sort by reliability, cost, and ultimately
135 // the desirability to issue a given call over each of the specified
136 // call services.
137 return callServices;
Ben Gilad9f2bed32013-12-12 17:43:26 -0800138 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800139}