Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 21 | import android.telecomm.CallAudioState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 22 | import android.telecomm.CallInfo; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 23 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 24 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 25 | import android.telecomm.GatewayInfo; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 26 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 27 | import com.google.common.base.Preconditions; |
| 28 | import com.google.common.base.Strings; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableCollection; |
| 30 | import com.google.common.collect.ImmutableList; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 31 | import com.google.common.collect.Lists; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 32 | import com.google.common.collect.Maps; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 33 | import com.google.common.collect.Sets; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 34 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | import java.util.List; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 36 | import java.util.Map; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 37 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 38 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Singleton. |
| 41 | * |
| 42 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 43 | * access from other packages specifically refraining from passing the CallsManager instance |
| 44 | * beyond the com.android.telecomm package boundary. |
| 45 | */ |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 46 | public final class CallsManager { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 47 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 48 | // TODO(santoscordon): Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 49 | interface CallsManagerListener { |
| 50 | void onCallAdded(Call call); |
| 51 | void onCallRemoved(Call call); |
| 52 | void onCallStateChanged(Call call, CallState oldState, CallState newState); |
| 53 | void onIncomingCallAnswered(Call call); |
| 54 | void onIncomingCallRejected(Call call); |
| 55 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 56 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 59 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 60 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 61 | private final Switchboard mSwitchboard; |
| 62 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 63 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 64 | * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming |
| 65 | * and outgoing calls are added to the map and removed when the calls move to the disconnected |
| 66 | * state. |
| 67 | * TODO(santoscordon): Add new CallId class and use it in place of String. |
| 68 | */ |
| 69 | private final Map<String, Call> mCalls = Maps.newHashMap(); |
| 70 | |
| 71 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 72 | * The call the user is currently interacting with. This is the call that should have audio |
| 73 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 74 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 75 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 76 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 77 | private final CallAudioManager mCallAudioManager; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 78 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 79 | private final Set<CallsManagerListener> mListeners = Sets.newHashSet(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 80 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 81 | private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 82 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 83 | private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 84 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 85 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 86 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 87 | */ |
| 88 | private CallsManager() { |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 89 | mSwitchboard = new Switchboard(this); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 90 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 91 | mCallAudioManager = new CallAudioManager(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 92 | |
| 93 | mListeners.add(new CallLogManager(TelecommApp.getInstance())); |
| 94 | mListeners.add(new PhoneStateBroadcaster()); |
| 95 | mListeners.add(new InCallController()); |
| 96 | mListeners.add(new Ringer(mCallAudioManager)); |
| 97 | mListeners.add(new RingbackPlayer(this, new InCallTonePlayer.Factory(mCallAudioManager))); |
| 98 | mListeners.add(mCallAudioManager); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 101 | static CallsManager getInstance() { |
| 102 | return INSTANCE; |
| 103 | } |
| 104 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 105 | ImmutableCollection<Call> getCalls() { |
| 106 | return ImmutableList.copyOf(mCalls.values()); |
| 107 | } |
| 108 | |
| 109 | Call getForegroundCall() { |
| 110 | return mForegroundCall; |
| 111 | } |
| 112 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 113 | boolean hasEmergencyCall() { |
| 114 | for (Call call : mCalls.values()) { |
| 115 | if (call.isEmergencyCall()) { |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | CallAudioState getAudioState() { |
| 123 | return mCallAudioManager.getAudioState(); |
| 124 | } |
| 125 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 126 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 127 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 128 | * specified call; using the specified call service descriptor. Upon success, execution returns |
| 129 | * to {@link #handleSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 130 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 131 | * @param descriptor The descriptor of the call service to use for this incoming call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 132 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 133 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 134 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 135 | Log.d(this, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 136 | // Create a call with no handle. Eventually, switchboard will update the call with |
| 137 | // additional information from the call service, but for now we just need one to pass around |
| 138 | // with a unique call ID. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 139 | Call call = new Call(true /* isIncoming */); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 140 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 141 | mSwitchboard.retrieveIncomingCall(call, descriptor, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 145 | * Validates the specified call and, upon no objection to connect it, adds the new call to the |
| 146 | * list of live calls. Also notifies the in-call app so the user can answer or reject the call. |
| 147 | * |
| 148 | * @param call The new incoming call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 149 | * @param callInfo The details of the call. |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 150 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 151 | void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 152 | Log.d(this, "handleSuccessfulIncomingCall"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 153 | Preconditions.checkState(callInfo.getState() == CallState.RINGING); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 154 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 155 | Uri handle = call.getHandle(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 156 | ContactInfo contactInfo = call.getContactInfo(); |
| 157 | for (IncomingCallValidator validator : mIncomingCallValidators) { |
| 158 | if (!validator.isValid(handle, contactInfo)) { |
| 159 | // TODO(gilad): Consider displaying an error message. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 160 | Log.i(this, "Dropping restricted incoming call"); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // No objection to accept the incoming call, proceed with potentially connecting it (based |
| 166 | // on the user's action, or lack thereof). |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 167 | call.setHandle(callInfo.getHandle()); |
| 168 | setCallState(call, callInfo.getState()); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 169 | addCall(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 170 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 171 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Called when an incoming call was not connected. |
| 174 | * |
| 175 | * @param call The incoming call. |
| 176 | */ |
| 177 | void handleUnsuccessfulIncomingCall(Call call) { |
| 178 | setCallState(call, CallState.DISCONNECTED); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 182 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 183 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 184 | * @param handle Handle to connect the call with. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 185 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 186 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
| 187 | * actual dialed handle via a gateway provider. May be null. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 188 | */ |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 189 | void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) { |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 190 | for (OutgoingCallValidator validator : mOutgoingCallValidators) { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 191 | if (!validator.isValid(handle, contactInfo)) { |
| 192 | // TODO(gilad): Display an error message. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 193 | Log.i(this, "Dropping restricted outgoing call."); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 194 | return; |
| 195 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 198 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle(); |
| 199 | |
| 200 | if (gatewayInfo == null) { |
| 201 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle)); |
| 202 | } else { |
| 203 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 204 | Log.pii(uriHandle), Log.pii(handle)); |
| 205 | } |
| 206 | Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 207 | setCallState(call, CallState.DIALING); |
| 208 | addCall(call); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 209 | mSwitchboard.placeOutgoingCall(call); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 210 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 211 | |
| 212 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 213 | * Called when a call service acknowledges that it can place a call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 214 | * |
| 215 | * @param call The new outgoing call. |
| 216 | */ |
| 217 | void handleSuccessfulOutgoingCall(Call call) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 218 | Log.v(this, "handleSuccessfulOutgoingCall, %s", call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 222 | * Called when an outgoing call was not placed. |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 223 | * |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 224 | * @param call The outgoing call. |
| 225 | * @param isAborted True if the call was unsuccessful because it was aborted. |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 226 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 227 | void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) { |
| 228 | Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted); |
| 229 | if (isAborted) { |
| 230 | call.abort(); |
| 231 | setCallState(call, CallState.ABORTED); |
| 232 | } else { |
| 233 | setCallState(call, CallState.DISCONNECTED); |
| 234 | } |
| 235 | removeCall(call); |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 239 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 240 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 241 | * the user opting to answer said call. |
| 242 | * |
| 243 | * @param callId The ID of the call. |
| 244 | */ |
| 245 | void answerCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 246 | Call call = mCalls.get(callId); |
| 247 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 248 | Log.i(this, "Request to answer a non-existent call %s", callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 249 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 250 | for (CallsManagerListener listener : mListeners) { |
| 251 | listener.onIncomingCallAnswered(call); |
| 252 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 253 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 254 | // We do not update the UI until we get confirmation of the answer() through |
| 255 | // {@link #markCallAsActive}. However, if we ever change that to look more responsive, |
| 256 | // then we need to make sure we add a timeout for the answer() in case the call never |
| 257 | // comes out of RINGING. |
| 258 | call.answer(); |
| 259 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 264 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 265 | * the user opting to reject said call. |
| 266 | * |
| 267 | * @param callId The ID of the call. |
| 268 | */ |
| 269 | void rejectCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 270 | Call call = mCalls.get(callId); |
| 271 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 272 | Log.i(this, "Request to reject a non-existent call %s", callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 273 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 274 | for (CallsManagerListener listener : mListeners) { |
| 275 | listener.onIncomingCallRejected(call); |
| 276 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 277 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 278 | call.reject(); |
| 279 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 284 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 285 | * the user hitting the end-call button. |
| 286 | * |
| 287 | * @param callId The ID of the call. |
| 288 | */ |
| 289 | void disconnectCall(String callId) { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 290 | Call call = mCalls.get(callId); |
| 291 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 292 | Log.w(this, "Unknown call (%s) asked to disconnect", callId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 293 | } else { |
| 294 | call.disconnect(); |
| 295 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 296 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 297 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the |
| 300 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 301 | * the user hitting the hold button during an active call. |
| 302 | * |
| 303 | * @param callId The ID of the call. |
| 304 | */ |
| 305 | void holdCall(String callId) { |
| 306 | Call call = mCalls.get(callId); |
| 307 | if (call == null) { |
| 308 | Log.w(this, "Unknown call (%s) asked to be put on hold", callId); |
| 309 | } else { |
| 310 | Log.d(this, "Putting call on hold: (%s)", callId); |
| 311 | call.hold(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Instructs Telecomm to release the specified call from hold. Intended to be invoked by |
| 317 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 318 | * by the user hitting the hold button during a held call. |
| 319 | * |
| 320 | * @param callId The ID of the call |
| 321 | */ |
| 322 | void unholdCall(String callId) { |
| 323 | Call call = mCalls.get(callId); |
| 324 | if (call == null) { |
| 325 | Log.w(this, "Unknown call (%s) asked to be removed from hold", callId); |
| 326 | } else { |
| 327 | Log.d(this, "Removing call from hold: (%s)", callId); |
| 328 | call.unhold(); |
| 329 | } |
| 330 | } |
| 331 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 332 | /** Called by the in-call UI to change the mute state. */ |
| 333 | void mute(boolean shouldMute) { |
| 334 | mCallAudioManager.mute(shouldMute); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 339 | * speaker phone. |
| 340 | */ |
| 341 | void setAudioRoute(int route) { |
| 342 | mCallAudioManager.setAudioRoute(route); |
| 343 | } |
| 344 | |
| 345 | /** Called when the audio state changes. */ |
| 346 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
| 347 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 348 | for (CallsManagerListener listener : mListeners) { |
| 349 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 350 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 353 | void markCallAsRinging(String callId) { |
| 354 | setCallState(callId, CallState.RINGING); |
| 355 | } |
| 356 | |
| 357 | void markCallAsDialing(String callId) { |
| 358 | setCallState(callId, CallState.DIALING); |
| 359 | } |
| 360 | |
| 361 | void markCallAsActive(String callId) { |
| 362 | setCallState(callId, CallState.ACTIVE); |
| 363 | } |
| 364 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 365 | void markCallAsOnHold(String callId) { |
| 366 | setCallState(callId, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 369 | /** |
| 370 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 371 | * live call, then also disconnect from the in-call controller. |
| 372 | * |
| 373 | * @param callId The ID of the call. |
| 374 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 375 | void markCallAsDisconnected(String callId) { |
| 376 | setCallState(callId, CallState.DISCONNECTED); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 377 | removeCall(mCalls.remove(callId)); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 381 | * Cleans up any calls currently associated with the specified call service when the |
| 382 | * call-service binder disconnects unexpectedly. |
| 383 | * |
| 384 | * @param callService The call service that disconnected. |
| 385 | */ |
| 386 | void handleCallServiceDeath(CallServiceWrapper callService) { |
| 387 | Preconditions.checkNotNull(callService); |
| 388 | for (Call call : ImmutableList.copyOf(mCalls.values())) { |
| 389 | if (call.getCallService() == callService) { |
| 390 | markCallAsDisconnected(call.getId()); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 396 | * Adds the specified call to the main list of live calls. |
| 397 | * |
| 398 | * @param call The call to add. |
| 399 | */ |
| 400 | private void addCall(Call call) { |
| 401 | mCalls.put(call.getId(), call); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 402 | for (CallsManagerListener listener : mListeners) { |
| 403 | listener.onCallAdded(call); |
| 404 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 405 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 408 | private void removeCall(Call call) { |
| 409 | call.clearCallService(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 410 | for (CallsManagerListener listener : mListeners) { |
| 411 | listener.onCallRemoved(call); |
| 412 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 413 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 417 | * Sets the specified state on the specified call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 418 | * |
| 419 | * @param callId The ID of the call to update. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 420 | * @param newState The new state of the call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 421 | */ |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 422 | private void setCallState(String callId, CallState newState) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 423 | Preconditions.checkState(!Strings.isNullOrEmpty(callId)); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 424 | Preconditions.checkNotNull(newState); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 425 | |
| 426 | Call call = mCalls.get(callId); |
| 427 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 428 | Log.w(this, "Call %s was not found while attempting to update the state to %s.", |
| 429 | callId, newState ); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 430 | } else { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 431 | setCallState(call, newState); |
| 432 | } |
| 433 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 434 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 435 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 436 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 437 | * |
| 438 | * @param call The call. |
| 439 | * @param newState The new state of the call. |
| 440 | */ |
| 441 | private void setCallState(Call call, CallState newState) { |
| 442 | CallState oldState = call.getState(); |
| 443 | if (newState != oldState) { |
| 444 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 445 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 446 | // sense (e.g., ACTIVE -> RINGING). |
| 447 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 448 | // into a well-defined state machine. |
| 449 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 450 | // unexpected transition occurs. |
| 451 | call.setState(newState); |
| 452 | |
| 453 | // Only broadcast state change for calls that are being tracked. |
| 454 | if (mCalls.containsKey(call.getId())) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 455 | for (CallsManagerListener listener : mListeners) { |
| 456 | listener.onCallStateChanged(call, oldState, newState); |
| 457 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 458 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 464 | * Checks which call should be visible to the user and have audio focus. |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 465 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 466 | private void updateForegroundCall() { |
| 467 | Call newForegroundCall = null; |
| 468 | for (Call call : mCalls.values()) { |
| 469 | // Incoming ringing calls have priority. |
| 470 | if (call.getState() == CallState.RINGING) { |
| 471 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 472 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 473 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 474 | if (call.isAlive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 475 | newForegroundCall = call; |
| 476 | // Don't break in case there's a ringing call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 479 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 480 | if (newForegroundCall != mForegroundCall) { |
| 481 | Call oldForegroundCall = mForegroundCall; |
| 482 | mForegroundCall = newForegroundCall; |
| 483 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame^] | 484 | for (CallsManagerListener listener : mListeners) { |
| 485 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 486 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 489 | } |