Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 1 | /* |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Evan Charlton | ac1aa9e | 2014-01-03 13:47:12 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 21 | |
| 22 | import android.content.Context; |
| 23 | import android.os.RemoteException; |
| 24 | import android.telecomm.ICallService; |
| 25 | import android.util.Log; |
| 26 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 27 | import com.android.telecomm.exceptions.CallServiceUnavailableException; |
| 28 | import com.android.telecomm.exceptions.OutgoingCallException; |
| 29 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 30 | import java.util.List; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 31 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 32 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 33 | /** |
| 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 38 | * 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 Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 40 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 41 | final class Switchboard { |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 42 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 43 | private static final String TAG = Switchboard.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 44 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 45 | private CallServiceFinder callServiceFinder = new CallServiceFinder(this); |
| 46 | |
| 47 | private Set<Call> mPendingOutgoingCalls = Sets.newHashSet(); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 48 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 49 | /** |
| 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 54 | * @param handle The handle to dial. |
| 55 | * @param contactInfo Information about the entity being called. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 56 | * @param context The application context. |
| 57 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 58 | void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame^] | 59 | ThreadUtil.checkOnMainThread(); |
| 60 | mPendingOutgoingCalls.add(new Call(handle, contactInfo)); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 61 | callServiceFinder.initiateLookup(context); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 62 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 63 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 64 | /** |
| 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 Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame^] | 75 | 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 82 | |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame^] | 83 | // 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 91 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 94 | /** |
| 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 Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 98 | * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 99 | * |
| 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 Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 104 | // 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 159 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 160 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 161 | * Sorts a list of {@link ICallService} ordered by the preferred service for dialing the call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 162 | * |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 170 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 171 | } |