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 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 22 | import android.os.Bundle; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 25 | import android.telecomm.CallServiceDescriptor; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 26 | import android.telecomm.ICallServiceSelector; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 27 | import android.telecomm.TelecommConstants; |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 29 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 30 | import java.util.Iterator; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 31 | import java.util.List; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 32 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 33 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 34 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 35 | * Switchboard is responsible for (1) gathering the {@link CallServiceWrapper}s and |
| 36 | * {@link ICallServiceSelector}s through which to place outgoing calls, (2) starting outgoing calls |
| 37 | * (via {@link OutgoingCallsManager} and (3) switching active calls between call services. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 38 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 39 | final class Switchboard { |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 40 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 41 | private final static String TAG = Switchboard.class.getSimpleName(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 42 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 43 | private final CallsManager mCallsManager; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 45 | /** Used to place outgoing calls. */ |
| 46 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 47 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 48 | /** Used to retrieve incoming call details. */ |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 49 | private final IncomingCallsManager mIncomingCallsManager; |
| 50 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 51 | private final CallServiceRepository mCallServiceRepository; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 52 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 53 | private final CallServiceSelectorRepository mSelectorRepository; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 54 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 55 | private final BinderDeallocator mBinderDeallocator; |
| 56 | |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 57 | /** Used to schedule tasks on the main (UI) thread. */ |
| 58 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 59 | |
| 60 | /** |
| 61 | * Executes a single tick task and potentially schedules the next such that polling continues |
| 62 | * as long as necessary. |
| 63 | * NOTE(gilad): by design no two tick invocations should ever overlap. |
| 64 | */ |
| 65 | private final Runnable mTicker = new Runnable() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | tick(); |
| 69 | if (isTicking()) { |
| 70 | scheduleNextTick(); |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet(); |
| 76 | |
| 77 | private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 78 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 79 | /** |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 80 | * The set of currently available call-service implementations, see |
Ben Gilad | 3758723 | 2014-02-19 16:03:44 -0800 | [diff] [blame] | 81 | * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the |
| 82 | * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls. |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 83 | */ |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 84 | private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 85 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 86 | /** |
| 87 | * The set of currently available call-service-selector implementations, |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 88 | * see {@link CallServiceSelectorRepository}. |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 89 | * TODO(gilad): Clear once the active-call count goes to zero. |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 90 | */ |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 91 | private final Set<ICallServiceSelector> mSelectors = Sets.newHashSet(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 92 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 93 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 94 | * The current lookup-cycle ID used with the repositories. Incremented with each invocation |
Ben Gilad | 3758723 | 2014-02-19 16:03:44 -0800 | [diff] [blame] | 95 | * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup(). |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 96 | */ |
| 97 | private int mLookupId = 0; |
| 98 | |
| 99 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 100 | * Persists the specified parameters and initializes Switchboard. |
| 101 | */ |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 102 | Switchboard(CallsManager callsManager) { |
| 103 | mCallsManager = callsManager; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 104 | mOutgoingCallsManager = new OutgoingCallsManager(this); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 105 | mIncomingCallsManager = new IncomingCallsManager(this); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 106 | mSelectorRepository = new CallServiceSelectorRepository(this); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 107 | mCallServiceRepository = |
| 108 | new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 109 | |
| 110 | mBinderDeallocator = new BinderDeallocator(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 111 | } |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 112 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 113 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 114 | * Starts the process of placing an outgoing call by searching for available call services |
| 115 | * through which the call can be placed. After a lookup for those services completes, execution |
| 116 | * returns to {@link #setCallServices} where the process of placing the call continues. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 117 | * |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 118 | * @param call The yet-to-be-connected outgoing-call object. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 119 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 120 | void placeOutgoingCall(Call call) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 121 | mBinderDeallocator.acquireUsePermit(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 122 | |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 123 | // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall |
| 124 | // is invoked with call A, (2) the call-service lookup completes, but the one for selectors |
| 125 | // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is |
| 126 | // reset, (5) the selector lookup completes but the call-services are missing. This should |
| 127 | // be okay since the call-service lookup completed. Specifically the already-available call |
| 128 | // services are cached and will be provided in response to the second lookup cycle. |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 129 | mCallServices.clear(); |
| 130 | mSelectors.clear(); |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 131 | |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 132 | mNewOutgoingCalls.add(call); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 133 | |
Ben Gilad | ebd9b66 | 2014-02-19 16:03:44 -0800 | [diff] [blame] | 134 | // Initiate a lookup every time to account for newly-installed apps and/or updated settings. |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 135 | mLookupId++; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 136 | mCallServiceRepository.initiateLookup(mLookupId); |
| 137 | mSelectorRepository.initiateLookup(mLookupId); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 138 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 139 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 140 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 141 | * Retrieves details about the incoming call through the incoming call manager. The incoming |
| 142 | * call manager will invoke either {@link #handleSuccessfulIncomingCall} or |
| 143 | * {@link #handleFailedIncomingCall} depending on the result of the retrieval. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 144 | * |
| 145 | * @param call The call object. |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 146 | * @param descriptor The relevant call-service descriptor. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 147 | * @param extras The optional extras passed via |
| 148 | * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS} |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 149 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 150 | void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) { |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 151 | Log.d(TAG, "retrieveIncomingCall"); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 152 | mBinderDeallocator.acquireUsePermit(); |
| 153 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 154 | CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 155 | call.setCallService(callService); |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 156 | mIncomingCallsManager.retrieveIncomingCall(call, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 160 | * Persists the specified set of call services and attempts to place any pending outgoing |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 161 | * calls. Intended to be invoked by {@link CallServiceRepository} exclusively. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 162 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 163 | * @param callServices The potentially-partial set of call services. Partial since the lookup |
| 164 | * process is time-boxed, such that some providers/call-services may be slow to respond and |
| 165 | * hence effectively omitted from the specified list. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 166 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 167 | void setCallServices(Set<CallServiceWrapper> callServices) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 168 | mBinderDeallocator.updateBinders(mCallServices); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 169 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 170 | mCallServices.clear(); |
| 171 | mCallServices.addAll(callServices); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 172 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Persists the specified list of selectors and attempts to connect any pending outgoing |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 177 | * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 178 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 179 | * @param selectors The potentially-partial set of selectors. Partial since the lookup |
| 180 | * procedure is time-boxed such that some selectors may be slow to respond and hence |
| 181 | * effectively omitted from the specified set. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 182 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 183 | void setSelectors(Set<ICallServiceSelector> selectors) { |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 184 | // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 185 | // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. Also need |
| 186 | // to invoke updateBinders(selectors) once this to-do is addressed. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 187 | ThreadUtil.checkOnMainThread(); |
| 188 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 189 | // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with |
| 190 | // emergency calls) and order the entire set prior to the assignment below. If the |
| 191 | // built-in selectors can be implemented in a manner that does not require binding, |
| 192 | // that's probably preferred. May want to use a LinkedHashSet for the sorted set. |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 193 | mSelectors.clear(); |
| 194 | mSelectors.addAll(selectors); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 195 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 199 | * Handles the case where an outgoing call has been successfully placed, |
| 200 | * see {@link OutgoingCallProcessor}. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 201 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 202 | void handleSuccessfulOutgoingCall(Call call) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 203 | Log.d(TAG, "handleSuccessfulOutgoingCall"); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 204 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 205 | mCallsManager.handleSuccessfulOutgoingCall(call); |
| 206 | finalizeOutgoingCall(call); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Handles the case where an outgoing call could not be proceed by any of the |
| 211 | * selector/call-service implementations, see {@link OutgoingCallProcessor}. |
| 212 | */ |
| 213 | void handleFailedOutgoingCall(Call call) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 214 | Log.d(TAG, "handleFailedOutgoingCall"); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 215 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 216 | // TODO(gilad): Notify mCallsManager. |
| 217 | |
| 218 | finalizeOutgoingCall(call); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 222 | * Handles the case where we successfully receive details of an incoming call. Hands the |
| 223 | * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that |
| 224 | * point, {@link CallsManager} should bring up the incoming-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 225 | */ |
| 226 | void handleSuccessfulIncomingCall(Call call) { |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 227 | Log.d(TAG, "handleSuccessfulIncomingCall"); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 228 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 229 | mCallsManager.handleSuccessfulIncomingCall(call); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 230 | mBinderDeallocator.releaseUsePermit(); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 234 | * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call |
| 235 | * intent via {@link CallActivity}. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 236 | * |
| 237 | * @param call The call. |
| 238 | */ |
| 239 | void handleFailedIncomingCall(Call call) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 240 | Log.d(TAG, "handleFailedIncomingCall"); |
| 241 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 242 | // Since we set the call service before calling into incoming-calls manager, we clear it for |
| 243 | // good measure if an error is reported. |
| 244 | call.clearCallService(); |
| 245 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 246 | mBinderDeallocator.releaseUsePermit(); |
| 247 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 248 | // At the moment there is nothing more to do if an incoming call is not retrieved. We may at |
| 249 | // a future date bind to the in-call app optimistically during the incoming-call sequence |
| 250 | // and this method could tell {@link CallsManager} to unbind from the in-call app if the |
| 251 | // incoming call was not retrieved. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 255 | * @return True if ticking should continue (or be resumed) and false otherwise. |
| 256 | */ |
| 257 | private boolean isTicking() { |
| 258 | // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting |
| 259 | // to be connected by a call service) and also when at least one active call is switch-able |
| 260 | // between call services, see {@link ICallServiceSelector#isSwitchable}. |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Schedules the next tick invocation. |
| 266 | */ |
| 267 | private void scheduleNextTick() { |
Evan Charlton | 1063e28 | 2014-03-05 19:31:21 -0800 | [diff] [blame] | 268 | mHandler.postDelayed(mTicker, Timeouts.getTickMs()); |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 272 | * Attempts to process the next new outgoing calls that have not yet been expired. |
| 273 | */ |
| 274 | private void processNewOutgoingCalls() { |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 275 | if (mCallServices.isEmpty() || mSelectors.isEmpty()) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 276 | // At least one call service and one selector are required to process outgoing calls. |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | if (!mNewOutgoingCalls.isEmpty()) { |
| 281 | Call call = mNewOutgoingCalls.iterator().next(); |
| 282 | mNewOutgoingCalls.remove(call); |
| 283 | mPendingOutgoingCalls.add(call); |
| 284 | |
| 285 | // Specifically only attempt to place one call at a time such that call services |
| 286 | // can be freed from needing to deal with concurrent requests. |
| 287 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 288 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 291 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 292 | * Attempts to place the specified call. |
| 293 | * |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 294 | * @param call The call to place. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 295 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 296 | private void processNewOutgoingCall(Call call) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 297 | // Convert to (duplicate-free) list to aid index-based iteration, see the comment under |
| 298 | // setSelectors regarding using LinkedHashSet instead. |
| 299 | List<ICallServiceSelector> selectors = Lists.newArrayList(); |
| 300 | selectors.addAll(mSelectors); |
| 301 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 302 | mOutgoingCallsManager.placeCall(call, mCallServices, selectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 306 | * Finalizes the outgoing-call sequence, regardless if it succeeded or failed. |
| 307 | */ |
| 308 | private void finalizeOutgoingCall(Call call) { |
| 309 | mPendingOutgoingCalls.remove(call); |
| 310 | |
| 311 | mBinderDeallocator.releaseUsePermit(); |
| 312 | processNewOutgoingCalls(); // Process additional (new) calls, if any. |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Performs the set of tasks that needs to be executed on polling basis. |
| 317 | */ |
| 318 | private void tick() { |
| 319 | // TODO(gilad): Add the necessary logic to support switching. |
| 320 | |
| 321 | expireStaleOutgoingCalls(mNewOutgoingCalls); |
| 322 | expireStaleOutgoingCalls(mPendingOutgoingCalls); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Identifies stale calls and takes the necessary steps to mark these as expired. |
| 327 | * |
| 328 | * @param calls The set of calls to iterate through. |
| 329 | */ |
| 330 | private void expireStaleOutgoingCalls(Set<Call> calls) { |
| 331 | if (calls.isEmpty()) { |
| 332 | return; |
| 333 | } |
| 334 | |
Evan Charlton | 1063e28 | 2014-03-05 19:31:21 -0800 | [diff] [blame] | 335 | final long newCallTimeoutMs = Timeouts.getNewOutgoingCallMs(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 336 | Iterator<Call> iterator = calls.iterator(); |
| 337 | while (iterator.hasNext()) { |
| 338 | Call call = iterator.next(); |
Evan Charlton | 1063e28 | 2014-03-05 19:31:21 -0800 | [diff] [blame] | 339 | if (call.getAgeInMilliseconds() >= newCallTimeoutMs) { |
Evan Charlton | 26d6727 | 2014-03-06 18:35:19 -0800 | [diff] [blame] | 340 | Log.d(TAG, "Call " + call.getId() + " timed out."); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 341 | mOutgoingCallsManager.abort(call); |
| 342 | calls.remove(call); |
| 343 | |
| 344 | // TODO(gilad): We may also have expired calls that are not yet associated with an |
| 345 | // OutgoingCallProcessor (e.g. when newer calls are "blocked" on older-yet-expired |
| 346 | // ones), in which case call.abort may need to be invoked directly. Alternatively |
| 347 | // we can also create an OutgoingCallsManager instance for every new call at intent- |
| 348 | // processing time. |
| 349 | |
| 350 | // TODO(gilad): Notify the user in the relevant cases (at least outgoing). |
| 351 | |
| 352 | mBinderDeallocator.releaseUsePermit(); |
| 353 | } |
| 354 | } |
| 355 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 356 | } |