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