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; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
| 22 | import android.os.RemoteException; |
| 23 | import android.telecomm.ICallService; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.telecomm.CallServiceFinder.CallServiceSearchCallback; |
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; |
| 31 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 32 | /** |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 38 | class Switchboard { |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 39 | /** Used to identify log entries by this class */ |
| 40 | private static final String TAG = Switchboard.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 41 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 42 | /** |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 53 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 54 | 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 66 | /** |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 128 | /** |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 138 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 139 | } |