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