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