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