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 | |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 21 | import android.os.Handler; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 22 | import android.os.Message; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 23 | import android.telecomm.CallInfo; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 24 | import android.telecomm.CallServiceDescriptor; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 25 | import android.telecomm.TelecommConstants; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 26 | |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 27 | import com.google.common.base.Preconditions; |
| 28 | import com.google.common.collect.ImmutableCollection; |
| 29 | import com.google.common.collect.ImmutableList; |
| 30 | import com.google.common.collect.Sets; |
| 31 | |
| 32 | import java.util.Collection; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 33 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 34 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 35 | /** |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 36 | * Switchboard is responsible for: |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 37 | * - gathering the {@link CallServiceWrapper}s and {@link CallServiceSelectorWrapper}s through |
| 38 | * which to place outgoing calls |
| 39 | * - starting outgoing calls (via {@link OutgoingCallsManager} |
| 40 | * - switching active calls between call services. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 41 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 42 | final class Switchboard { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 43 | private final static int MSG_EXPIRE_STALE_CALL = 1; |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 45 | private final static Switchboard sInstance = new Switchboard(); |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 46 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 47 | /** Used to place outgoing calls. */ |
| 48 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 49 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 50 | /** Used to retrieve incoming call details. */ |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 51 | private final IncomingCallsManager mIncomingCallsManager; |
| 52 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 53 | private final CallServiceRepository mCallServiceRepository; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 54 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 55 | private final CallServiceSelectorRepository mSelectorRepository; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 56 | |
| 57 | /** Used to schedule tasks on the main (UI) thread. */ |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 58 | private final Handler mHandler = new Handler() { |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 59 | @Override |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 60 | public void handleMessage(Message msg) { |
| 61 | switch(msg.what) { |
| 62 | case MSG_EXPIRE_STALE_CALL: |
| 63 | expireStaleCall((Call) msg.obj); |
| 64 | break; |
| 65 | default: |
| 66 | Log.wtf(Switchboard.this, "Unexpected message %d.", msg.what); |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet(); |
| 72 | |
| 73 | private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 74 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 75 | /** |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 76 | * The set of currently available call-service implementations, see |
Ben Gilad | 3758723 | 2014-02-19 16:03:44 -0800 | [diff] [blame] | 77 | * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the |
| 78 | * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls. |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 79 | */ |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 80 | private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 81 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 82 | /** |
| 83 | * The set of currently available call-service-selector implementations, |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 84 | * see {@link CallServiceSelectorRepository}. |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 85 | * TODO(gilad): Clear once the active-call count goes to zero. |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 86 | */ |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 87 | private ImmutableCollection<CallServiceSelectorWrapper> mSelectors = ImmutableList.of(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 88 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 89 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 90 | * 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] | 91 | * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup(). |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 92 | */ |
| 93 | private int mLookupId = 0; |
| 94 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 95 | /** Singleton accessor. */ |
| 96 | static Switchboard getInstance() { |
| 97 | return sInstance; |
| 98 | } |
| 99 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 100 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 101 | * Persists the specified parameters and initializes Switchboard. |
| 102 | */ |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 103 | private Switchboard() { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 104 | ThreadUtil.checkOnMainThread(); |
| 105 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 106 | mOutgoingCallsManager = new OutgoingCallsManager(this); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 107 | mIncomingCallsManager = new IncomingCallsManager(this); |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 108 | mSelectorRepository = new CallServiceSelectorRepository(this, mOutgoingCallsManager); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 109 | mCallServiceRepository = |
| 110 | new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager); |
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 | 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(); |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 128 | mSelectors = ImmutableList.of(); |
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); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 136 | |
| 137 | Message msg = mHandler.obtainMessage(MSG_EXPIRE_STALE_CALL, call); |
| 138 | mHandler.sendMessageDelayed(msg, Timeouts.getNewOutgoingCallMillis()); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 139 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 140 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 141 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 142 | * Retrieves details about the incoming call through the incoming call manager. The incoming |
| 143 | * call manager will invoke either {@link #handleSuccessfulIncomingCall} or |
| 144 | * {@link #handleFailedIncomingCall} depending on the result of the retrieval. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 145 | * |
| 146 | * @param call The call object. |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 147 | * @param descriptor The relevant call-service descriptor. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 148 | * @param extras The optional extras passed via |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 149 | * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS} |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 150 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 151 | void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 152 | Log.d(this, "retrieveIncomingCall"); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 153 | CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 154 | call.setCallService(callService); |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 155 | mIncomingCallsManager.retrieveIncomingCall(call, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 159 | * 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] | 160 | * calls. Intended to be invoked by {@link CallServiceRepository} exclusively. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 161 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 162 | * @param callServices The potentially-partial set of call services. Partial since the lookup |
| 163 | * process is time-boxed, such that some providers/call-services may be slow to respond and |
| 164 | * hence effectively omitted from the specified list. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 165 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 166 | void setCallServices(Set<CallServiceWrapper> callServices) { |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 167 | mCallServices.clear(); |
| 168 | mCallServices.addAll(callServices); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 169 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * 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] | 174 | * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 175 | * |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 176 | * @param selectors Collection of selectors. The order of the collection determines the order in |
| 177 | * which the selectors are tried. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 178 | */ |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 179 | void setSelectors(ImmutableCollection<CallServiceSelectorWrapper> selectors) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 180 | ThreadUtil.checkOnMainThread(); |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 181 | Preconditions.checkNotNull(selectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 182 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 183 | // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with |
| 184 | // emergency calls) and order the entire set prior to the assignment below. If the |
| 185 | // 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] | 186 | // that's probably preferred. |
| 187 | mSelectors = selectors; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 188 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 192 | * Handles the case where an outgoing call has been successfully placed, |
| 193 | * see {@link OutgoingCallProcessor}. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 194 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 195 | void handleSuccessfulOutgoingCall(Call call) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 196 | Log.d(this, "handleSuccessfulOutgoingCall"); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 197 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 198 | finalizeOutgoingCall(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 199 | call.handleSuccessfulOutgoing(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Handles the case where an outgoing call could not be proceed by any of the |
| 204 | * selector/call-service implementations, see {@link OutgoingCallProcessor}. |
| 205 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 206 | void handleFailedOutgoingCall(Call call, boolean isAborted) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 207 | Log.d(this, "handleFailedOutgoingCall"); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 208 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 209 | finalizeOutgoingCall(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 210 | call.handleFailedOutgoing(isAborted); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 214 | * Handles the case where we successfully receive details of an incoming call. Hands the |
| 215 | * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that |
| 216 | * point, {@link CallsManager} should bring up the incoming-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 217 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 218 | void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 219 | Log.d(this, "handleSuccessfulIncomingCall"); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 220 | call.handleSuccessfulIncoming(callInfo); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 224 | * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call |
| 225 | * intent via {@link CallActivity}. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 226 | * |
| 227 | * @param call The call. |
| 228 | */ |
| 229 | void handleFailedIncomingCall(Call call) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 230 | Log.d(this, "handleFailedIncomingCall"); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 231 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 232 | // Since we set the call service before calling into incoming-calls manager, we clear it for |
| 233 | // good measure if an error is reported. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 234 | call.handleFailedIncoming(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 235 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 236 | // At the moment there is nothing more to do if an incoming call is not retrieved. We may at |
| 237 | // a future date bind to the in-call app optimistically during the incoming-call sequence |
| 238 | // and this method could tell {@link CallsManager} to unbind from the in-call app if the |
| 239 | // incoming call was not retrieved. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
Ihab Awad | 4e0f192 | 2014-04-14 17:35:38 -0700 | [diff] [blame] | 243 | * Ensures any state regarding a call is cleaned up. |
| 244 | * |
| 245 | * @param call The call. |
| 246 | */ |
| 247 | void abortCall(Call call) { |
| 248 | Log.d(this, "abortCall"); |
| 249 | mOutgoingCallsManager.abort(call); |
| 250 | } |
| 251 | |
| 252 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 253 | * Attempts to process the next new outgoing calls that have not yet been expired. |
| 254 | */ |
| 255 | private void processNewOutgoingCalls() { |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 256 | if (mCallServices.isEmpty() || mSelectors.isEmpty()) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 257 | // At least one call service and one selector are required to process outgoing calls. |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (!mNewOutgoingCalls.isEmpty()) { |
| 262 | Call call = mNewOutgoingCalls.iterator().next(); |
| 263 | mNewOutgoingCalls.remove(call); |
| 264 | mPendingOutgoingCalls.add(call); |
| 265 | |
| 266 | // Specifically only attempt to place one call at a time such that call services |
| 267 | // can be freed from needing to deal with concurrent requests. |
| 268 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 269 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 272 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 273 | * Attempts to place the specified call. |
| 274 | * |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 275 | * @param call The call to place. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 276 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 277 | private void processNewOutgoingCall(Call call) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 278 | Collection<CallServiceSelectorWrapper> selectors; |
| 279 | |
| 280 | // Use the call's selector if it's already tied to one. This is the case for handoff calls. |
| 281 | if (call.getCallServiceSelector() != null) { |
| 282 | selectors = ImmutableList.of(call.getCallServiceSelector()); |
| 283 | } else { |
| 284 | selectors = mSelectors; |
| 285 | } |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 286 | |
| 287 | boolean useEmergencySelector = |
| 288 | EmergencyCallServiceSelector.shouldUseSelector(call.getHandle()); |
| 289 | Log.d(this, "processNewOutgoingCall, isEmergency=%b", useEmergencySelector); |
| 290 | |
| 291 | if (useEmergencySelector) { |
| 292 | // This is potentially an emergency call so add the emergency selector before the |
| 293 | // other selectors. |
| 294 | ImmutableList.Builder<CallServiceSelectorWrapper> selectorsBuilder = |
| 295 | ImmutableList.builder(); |
| 296 | |
| 297 | ComponentName componentName = new ComponentName( |
| 298 | TelecommApp.getInstance(), EmergencyCallServiceSelector.class); |
| 299 | CallServiceSelectorWrapper emergencySelector = |
| 300 | new CallServiceSelectorWrapper( |
| 301 | componentName.flattenToShortString(), |
| 302 | componentName, |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 303 | CallsManager.getInstance(), |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 304 | mOutgoingCallsManager); |
| 305 | |
| 306 | selectorsBuilder.add(emergencySelector); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 307 | selectorsBuilder.addAll(selectors); |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 308 | selectors = selectorsBuilder.build(); |
| 309 | } |
| 310 | |
| 311 | mOutgoingCallsManager.placeCall(call, mCallServices, selectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 315 | * Finalizes the outgoing-call sequence, regardless if it succeeded or failed. |
| 316 | */ |
| 317 | private void finalizeOutgoingCall(Call call) { |
| 318 | mPendingOutgoingCalls.remove(call); |
| 319 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 320 | processNewOutgoingCalls(); // Process additional (new) calls, if any. |
| 321 | } |
| 322 | |
| 323 | /** |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 324 | * Expires calls which are taking too long to connect. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 325 | * |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 326 | * @param call The call to expire. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 327 | */ |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 328 | private void expireStaleCall(Call call) { |
| 329 | final long newCallTimeoutMillis = Timeouts.getNewOutgoingCallMillis(); |
| 330 | |
| 331 | if (call.getAgeMillis() < newCallTimeoutMillis) { |
| 332 | Log.wtf(this, "Expiring a call early. Age: %d, Time since attempt: %d", |
| 333 | call.getAgeMillis(), newCallTimeoutMillis); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 336 | if (mNewOutgoingCalls.remove(call)) { |
| 337 | // The call had not yet been processed so all we have to do is report the |
| 338 | // failure. |
| 339 | handleFailedOutgoingCall(call, true /* isAborted */); |
| 340 | } else if (mPendingOutgoingCalls.remove(call)) { |
| 341 | if (!mOutgoingCallsManager.abort(call)) { |
| 342 | Log.wtf(this, "Pending call failed to abort, call: %s.", call); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 346 | } |