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