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 | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 27 | import com.android.telecomm.BaseRepository.LookupCallback; |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 28 | import com.google.common.base.Preconditions; |
| 29 | import com.google.common.collect.ImmutableCollection; |
| 30 | import com.google.common.collect.ImmutableList; |
| 31 | import com.google.common.collect.Sets; |
| 32 | |
| 33 | import java.util.Collection; |
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: |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 38 | * - gathering the {@link CallServiceWrapper}s and {@link CallServiceSelectorWrapper}s through |
| 39 | * which to place outgoing calls |
| 40 | * - starting outgoing calls (via {@link OutgoingCallsManager} |
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 | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 43 | /** |
| 44 | * Encapsulates a request to place an outgoing call. |
| 45 | * TODO(santoscordon): Move this state into Call and remove this class. |
| 46 | */ |
| 47 | private final class OutgoingCallEntry { |
| 48 | final Call call; |
| 49 | |
| 50 | private Collection<CallServiceWrapper> mCallServices; |
| 51 | private Collection<CallServiceSelectorWrapper> mSelectors; |
| 52 | private boolean mIsCallPending = true; |
| 53 | |
| 54 | OutgoingCallEntry(Call call) { |
| 55 | this.call = call; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sets the call services to attempt for this outgoing call. |
| 60 | * |
| 61 | * @param callServices The call services. |
| 62 | */ |
| 63 | void setCallServices(Collection<CallServiceWrapper> callServices) { |
| 64 | mCallServices = callServices; |
| 65 | onLookupComplete(); |
| 66 | } |
| 67 | |
| 68 | Collection<CallServiceWrapper> getCallServices() { |
| 69 | return mCallServices; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the selectors to attemnpt for this outgoing call. |
| 74 | * |
| 75 | * @param selectors The call-service selectors. |
| 76 | */ |
| 77 | void setSelectors(Collection<CallServiceSelectorWrapper> selectors) { |
| 78 | mSelectors = selectors; |
| 79 | onLookupComplete(); |
| 80 | } |
| 81 | |
| 82 | Collection<CallServiceSelectorWrapper> getSelectors() { |
| 83 | return mSelectors; |
| 84 | } |
| 85 | |
| 86 | /** Expires the pending outgoing call and stops it from being made. */ |
| 87 | void expire() { |
| 88 | // This can be executed in three states: |
| 89 | // 1) We are still waiting for the list of CSs (Call Services) |
| 90 | // 2) We told outgoing calls manager to place the call using the CSs |
| 91 | // 3) Outgoing calls manager already successfully placed the call. |
| 92 | if (mIsCallPending) { |
| 93 | // Handle state (1), tell the call to clean itself up and shut everything down. |
| 94 | mIsCallPending = false; |
| 95 | call.handleFailedOutgoing(true /* isAborted */); |
| 96 | } else { |
| 97 | // Handle states (2) & (3). We can safely call abort() in either case. If the call |
| 98 | // is not yet successful, then it will abort. If the call was already placed, then |
| 99 | // outgoing calls manager will do nothing (and return false which we ignore). |
| 100 | boolean isAborted = mOutgoingCallsManager.abort(call); |
| 101 | Log.v(this, "expire() caused abort: %b", isAborted); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** Initiates processing of the call once call-services and selectors are set. */ |
| 106 | private void onLookupComplete() { |
| 107 | if (mIsCallPending) { |
| 108 | if (mSelectors != null && mCallServices != null) { |
| 109 | mIsCallPending = false; |
| 110 | processNewOutgoingCall(this); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 116 | private final static int MSG_EXPIRE_STALE_CALL = 1; |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 117 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 118 | private final static Switchboard sInstance = new Switchboard(); |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 119 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 120 | /** Used to place outgoing calls. */ |
| 121 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 122 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 123 | /** Used to retrieve incoming call details. */ |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 124 | private final IncomingCallsManager mIncomingCallsManager; |
| 125 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 126 | private final CallServiceRepository mCallServiceRepository; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 127 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 128 | private final CallServiceSelectorRepository mSelectorRepository; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 129 | |
| 130 | /** Used to schedule tasks on the main (UI) thread. */ |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 131 | private final Handler mHandler = new Handler() { |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 132 | @Override |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 133 | public void handleMessage(Message msg) { |
| 134 | switch(msg.what) { |
| 135 | case MSG_EXPIRE_STALE_CALL: |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 136 | ((OutgoingCallEntry) msg.obj).expire(); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 137 | break; |
| 138 | default: |
| 139 | Log.wtf(Switchboard.this, "Unexpected message %d.", msg.what); |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | }; |
| 143 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 144 | /** Singleton accessor. */ |
| 145 | static Switchboard getInstance() { |
| 146 | return sInstance; |
| 147 | } |
| 148 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 149 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 150 | * Persists the specified parameters and initializes Switchboard. |
| 151 | */ |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 152 | private Switchboard() { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 153 | ThreadUtil.checkOnMainThread(); |
| 154 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 155 | mOutgoingCallsManager = new OutgoingCallsManager(); |
| 156 | mIncomingCallsManager = new IncomingCallsManager(); |
| 157 | mSelectorRepository = new CallServiceSelectorRepository(mOutgoingCallsManager); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 158 | mCallServiceRepository = |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 159 | new CallServiceRepository(mOutgoingCallsManager, mIncomingCallsManager); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 160 | } |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 161 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 162 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 163 | * Starts the process of placing an outgoing call by searching for available call services |
| 164 | * through which the call can be placed. After a lookup for those services completes, execution |
| 165 | * returns to {@link #setCallServices} where the process of placing the call continues. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 166 | * |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 167 | * @param call The yet-to-be-connected outgoing-call object. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 168 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 169 | void placeOutgoingCall(Call call) { |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 170 | final OutgoingCallEntry callEntry = new OutgoingCallEntry(call); |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 171 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 172 | // Lookup call services |
| 173 | mCallServiceRepository.lookupServices(new LookupCallback<CallServiceWrapper>() { |
| 174 | @Override |
| 175 | public void onComplete(Collection<CallServiceWrapper> services) { |
| 176 | callEntry.setCallServices(services); |
| 177 | } |
| 178 | }); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 179 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 180 | // Lookup selectors |
| 181 | mSelectorRepository.lookupServices(new LookupCallback<CallServiceSelectorWrapper>() { |
| 182 | @Override |
| 183 | public void onComplete(Collection<CallServiceSelectorWrapper> selectors) { |
| 184 | callEntry.setSelectors(selectors); |
| 185 | } |
| 186 | }); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 187 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 188 | Message msg = mHandler.obtainMessage(MSG_EXPIRE_STALE_CALL, callEntry); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 189 | mHandler.sendMessageDelayed(msg, Timeouts.getNewOutgoingCallMillis()); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 190 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 191 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 192 | /** |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 193 | * Retrieves details about the incoming call through the incoming call manager. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 194 | * |
| 195 | * @param call The call object. |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 196 | * @param descriptor The relevant call-service descriptor. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 197 | * @param extras The optional extras passed via |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 198 | * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS} |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 199 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 200 | void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 201 | Log.d(this, "retrieveIncomingCall"); |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 202 | CallServiceWrapper callService = mCallServiceRepository.getService(descriptor); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 203 | call.setCallService(callService); |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 204 | mIncomingCallsManager.retrieveIncomingCall(call, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
Ihab Awad | 4e0f192 | 2014-04-14 17:35:38 -0700 | [diff] [blame] | 208 | * Ensures any state regarding a call is cleaned up. |
| 209 | * |
| 210 | * @param call The call. |
| 211 | */ |
| 212 | void abortCall(Call call) { |
| 213 | Log.d(this, "abortCall"); |
| 214 | mOutgoingCallsManager.abort(call); |
| 215 | } |
| 216 | |
| 217 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 218 | * Attempts to place the specified call. |
| 219 | * |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 220 | * @param callEntry The call entry to place. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 221 | */ |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 222 | private void processNewOutgoingCall(OutgoingCallEntry callEntry) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 223 | Collection<CallServiceSelectorWrapper> selectors; |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 224 | Call call = callEntry.call; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 225 | |
| 226 | // Use the call's selector if it's already tied to one. This is the case for handoff calls. |
| 227 | if (call.getCallServiceSelector() != null) { |
| 228 | selectors = ImmutableList.of(call.getCallServiceSelector()); |
| 229 | } else { |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 230 | selectors = callEntry.getSelectors(); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 231 | } |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 232 | |
| 233 | boolean useEmergencySelector = |
| 234 | EmergencyCallServiceSelector.shouldUseSelector(call.getHandle()); |
| 235 | Log.d(this, "processNewOutgoingCall, isEmergency=%b", useEmergencySelector); |
| 236 | |
| 237 | if (useEmergencySelector) { |
| 238 | // This is potentially an emergency call so add the emergency selector before the |
| 239 | // other selectors. |
| 240 | ImmutableList.Builder<CallServiceSelectorWrapper> selectorsBuilder = |
| 241 | ImmutableList.builder(); |
| 242 | |
| 243 | ComponentName componentName = new ComponentName( |
| 244 | TelecommApp.getInstance(), EmergencyCallServiceSelector.class); |
| 245 | CallServiceSelectorWrapper emergencySelector = |
| 246 | new CallServiceSelectorWrapper( |
| 247 | componentName.flattenToShortString(), |
| 248 | componentName, |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 249 | CallsManager.getInstance(), |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 250 | mOutgoingCallsManager); |
| 251 | |
| 252 | selectorsBuilder.add(emergencySelector); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 253 | selectorsBuilder.addAll(selectors); |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 254 | selectors = selectorsBuilder.build(); |
| 255 | } |
| 256 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 257 | mOutgoingCallsManager.placeCall(call, callEntry.getCallServices(), selectors); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 258 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 259 | } |