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 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 20 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 21 | import android.telecomm.CallState; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 23 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 24 | import com.google.common.base.Preconditions; |
| 25 | import com.google.common.base.Strings; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 26 | import com.google.common.collect.Lists; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 27 | import com.google.common.collect.Maps; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 28 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 29 | import java.util.List; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 30 | import java.util.Map; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 31 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Singleton. |
| 34 | * |
| 35 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 36 | * access from other packages specifically refraining from passing the CallsManager instance |
| 37 | * beyond the com.android.telecomm package boundary. |
| 38 | */ |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 39 | public final class CallsManager { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 40 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 41 | private static final String TAG = CallsManager.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 42 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 43 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 44 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 45 | private final Switchboard mSwitchboard; |
| 46 | |
| 47 | /** Used to control the in-call app. */ |
| 48 | private final InCallController mInCallController; |
| 49 | |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 50 | private final Ringer mRinger; |
| 51 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 52 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 53 | * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming |
| 54 | * and outgoing calls are added to the map and removed when the calls move to the disconnected |
| 55 | * state. |
| 56 | * TODO(santoscordon): Add new CallId class and use it in place of String. |
| 57 | */ |
| 58 | private final Map<String, Call> mCalls = Maps.newHashMap(); |
| 59 | |
| 60 | /** |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 61 | * Used to keep ordering of unanswered incoming calls. The existence of multiple call services |
| 62 | * means that there can easily exist multiple incoming calls and explicit ordering is useful for |
| 63 | * maintaining the proper state of the ringer. |
| 64 | * TODO(santoscordon): May want to add comments about ITelephony.answerCall() method since |
| 65 | * ordering may also apply to that case. |
| 66 | */ |
| 67 | private final List<Call> mUnansweredIncomingCalls = Lists.newLinkedList(); |
| 68 | |
| 69 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 70 | * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set |
| 71 | * of CallsManager APIs that need to be exposed to the dialer (or any application firing call |
| 72 | * intents) may be empty. |
| 73 | */ |
| 74 | private DialerAdapter mDialerAdapter; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 75 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 76 | private InCallAdapter mInCallAdapter; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 77 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 78 | private CallLogManager mCallLogManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 79 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 80 | private VoicemailManager mVoicemailManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 81 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame^] | 82 | private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 83 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame^] | 84 | private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 85 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 86 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 87 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 88 | */ |
| 89 | private CallsManager() { |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 90 | mSwitchboard = new Switchboard(this); |
| 91 | mInCallController = new InCallController(this); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 92 | mRinger = new Ringer(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 95 | static CallsManager getInstance() { |
| 96 | return INSTANCE; |
| 97 | } |
| 98 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 99 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 100 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 101 | * specified call; using the specified call service descriptor. Upon success, execution returns |
| 102 | * to {@link #handleSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 103 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 104 | * @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] | 105 | * @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] | 106 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 107 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 108 | Log.d(TAG, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 109 | // Create a call with no handle. Eventually, switchboard will update the call with |
| 110 | // additional information from the call service, but for now we just need one to pass around |
| 111 | // with a unique call ID. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 112 | Call call = new Call(); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 113 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 114 | mSwitchboard.retrieveIncomingCall(call, descriptor, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 118 | * Validates the specified call and, upon no objection to connect it, adds the new call to the |
| 119 | * list of live calls. Also notifies the in-call app so the user can answer or reject the call. |
| 120 | * |
| 121 | * @param call The new incoming call. |
| 122 | */ |
| 123 | void handleSuccessfulIncomingCall(Call call) { |
| 124 | Log.d(TAG, "handleSuccessfulIncomingCall"); |
| 125 | Preconditions.checkState(call.getState() == CallState.RINGING); |
| 126 | |
| 127 | String handle = call.getHandle(); |
| 128 | ContactInfo contactInfo = call.getContactInfo(); |
| 129 | for (IncomingCallValidator validator : mIncomingCallValidators) { |
| 130 | if (!validator.isValid(handle, contactInfo)) { |
| 131 | // TODO(gilad): Consider displaying an error message. |
| 132 | Log.i(TAG, "Dropping restricted incoming call"); |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // No objection to accept the incoming call, proceed with potentially connecting it (based |
| 138 | // on the user's action, or lack thereof). |
| 139 | addCall(call); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 140 | |
| 141 | mUnansweredIncomingCalls.add(call); |
| 142 | if (mUnansweredIncomingCalls.size() == 1) { |
| 143 | // Start the ringer if we are the top-most incoming call (the only one in this case). |
| 144 | mRinger.startRinging(); |
| 145 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 149 | * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint, |
| 150 | * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED, |
| 151 | * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes |
| 152 | * this method. |
| 153 | * |
| 154 | * @param handle The handle to dial. |
| 155 | * @param contactInfo Information about the entity being called. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 156 | */ |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 157 | void processOutgoingCallIntent(String handle, ContactInfo contactInfo) { |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 158 | for (OutgoingCallValidator validator : mOutgoingCallValidators) { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 159 | if (!validator.isValid(handle, contactInfo)) { |
| 160 | // TODO(gilad): Display an error message. |
| 161 | Log.i(TAG, "Dropping restricted outgoing call."); |
| 162 | return; |
| 163 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // No objection to issue the call, proceed with trying to put it through. |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 167 | Call call = new Call(handle, contactInfo); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 168 | mSwitchboard.placeOutgoingCall(call); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 169 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 170 | |
| 171 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 172 | * Adds a new outgoing call to the list of live calls and notifies the in-call app. |
| 173 | * |
| 174 | * @param call The new outgoing call. |
| 175 | */ |
| 176 | void handleSuccessfulOutgoingCall(Call call) { |
| 177 | // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the |
| 178 | // placed call from the call service so there is no need to set it here. Instead, check that |
| 179 | // the state is appropriate. |
| 180 | Preconditions.checkState(call.getState() == CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 181 | addCall(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 185 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 186 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 187 | * the user opting to answer said call. |
| 188 | * |
| 189 | * @param callId The ID of the call. |
| 190 | */ |
| 191 | void answerCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 192 | Call call = mCalls.get(callId); |
| 193 | if (call == null) { |
| 194 | Log.i(TAG, "Request to answer a non-existent call " + callId); |
| 195 | } else { |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 196 | stopRinging(call); |
| 197 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 198 | // We do not update the UI until we get confirmation of the answer() through |
| 199 | // {@link #markCallAsActive}. However, if we ever change that to look more responsive, |
| 200 | // then we need to make sure we add a timeout for the answer() in case the call never |
| 201 | // comes out of RINGING. |
| 202 | call.answer(); |
| 203 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 208 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 209 | * the user opting to reject said call. |
| 210 | * |
| 211 | * @param callId The ID of the call. |
| 212 | */ |
| 213 | void rejectCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 214 | Call call = mCalls.get(callId); |
| 215 | if (call == null) { |
| 216 | Log.i(TAG, "Request to reject a non-existent call " + callId); |
| 217 | } else { |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 218 | stopRinging(call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 219 | call.reject(); |
| 220 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 225 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 226 | * the user hitting the end-call button. |
| 227 | * |
| 228 | * @param callId The ID of the call. |
| 229 | */ |
| 230 | void disconnectCall(String callId) { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 231 | Call call = mCalls.get(callId); |
| 232 | if (call == null) { |
| 233 | Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect"); |
| 234 | } else { |
| 235 | call.disconnect(); |
| 236 | } |
| 237 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 238 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 239 | |
| 240 | void markCallAsRinging(String callId) { |
| 241 | setCallState(callId, CallState.RINGING); |
| 242 | } |
| 243 | |
| 244 | void markCallAsDialing(String callId) { |
| 245 | setCallState(callId, CallState.DIALING); |
| 246 | } |
| 247 | |
| 248 | void markCallAsActive(String callId) { |
| 249 | setCallState(callId, CallState.ACTIVE); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 250 | removeFromUnansweredCalls(callId); |
Sailesh Nepal | fc511ca | 2014-02-20 18:48:53 -0800 | [diff] [blame] | 251 | mInCallController.markCallAsActive(callId); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 254 | /** |
| 255 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 256 | * live call, then also disconnect from the in-call controller. |
| 257 | * |
| 258 | * @param callId The ID of the call. |
| 259 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 260 | void markCallAsDisconnected(String callId) { |
| 261 | setCallState(callId, CallState.DISCONNECTED); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 262 | removeFromUnansweredCalls(callId); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 263 | |
| 264 | Call call = mCalls.remove(callId); |
| 265 | // At this point the call service has confirmed that the call is disconnected to it is |
| 266 | // safe to disassociate the call from its call service. |
| 267 | call.clearCallService(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 268 | |
| 269 | // Notify the in-call UI |
| 270 | mInCallController.markCallAsDisconnected(callId); |
| 271 | if (mCalls.isEmpty()) { |
| 272 | mInCallController.unbind(); |
| 273 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 277 | * Sends all the live calls to the in-call app if any exist. If there are no live calls, then |
| 278 | * tells the in-call controller to unbind since it is not needed. |
| 279 | */ |
| 280 | void updateInCall() { |
| 281 | if (mCalls.isEmpty()) { |
| 282 | mInCallController.unbind(); |
| 283 | } else { |
| 284 | for (Call call : mCalls.values()) { |
| 285 | addInCallEntry(call); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Adds the specified call to the main list of live calls. |
| 292 | * |
| 293 | * @param call The call to add. |
| 294 | */ |
| 295 | private void addCall(Call call) { |
| 296 | mCalls.put(call.getId(), call); |
| 297 | addInCallEntry(call); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Notifies the in-call app of the specified (new) call. |
| 302 | */ |
| 303 | private void addInCallEntry(Call call) { |
| 304 | mInCallController.addCall(call.toCallInfo()); |
| 305 | } |
| 306 | |
| 307 | /** |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 308 | * Sets the specified state on the specified call. Updates the ringer if the call is exiting |
| 309 | * the RINGING state. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 310 | * |
| 311 | * @param callId The ID of the call to update. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 312 | * @param newState The new state of the call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 313 | */ |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 314 | private void setCallState(String callId, CallState newState) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 315 | Preconditions.checkState(!Strings.isNullOrEmpty(callId)); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 316 | Preconditions.checkNotNull(newState); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 317 | |
| 318 | Call call = mCalls.get(callId); |
| 319 | if (call == null) { |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 320 | Log.e(TAG, "Call " + callId + " was not found while attempting to update the state " + |
| 321 | "to " + newState + "."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 322 | } else { |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 323 | if (newState != call.getState()) { |
| 324 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 325 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 326 | // sense (e.g., ACTIVE -> RINGING). |
| 327 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 328 | // into a well-defined state machine. |
| 329 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 330 | // unexpected transition occurs. |
| 331 | call.setState(newState); |
| 332 | // TODO(santoscordon): Notify the in-call app whenever a call changes state. |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Removes the specified call from the list of unanswered incoming calls and updates the ringer |
| 339 | * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that |
| 340 | * is not present in the list of incoming calls. |
| 341 | * |
| 342 | * @param callId The ID of the call. |
| 343 | */ |
| 344 | private void removeFromUnansweredCalls(String callId) { |
| 345 | Call call = mCalls.get(callId); |
| 346 | if (call != null && mUnansweredIncomingCalls.remove(call)) { |
| 347 | if (mUnansweredIncomingCalls.isEmpty()) { |
| 348 | mRinger.stopRinging(); |
| 349 | } else { |
| 350 | mRinger.startRinging(); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Stops playing the ringer if the specified call is the top-most incoming call. This exists |
| 357 | * separately from {@link #removeIncomingCall} for cases where we would like to stop playing the |
| 358 | * ringer for a call, but that call may still exist in {@link #mUnansweredIncomingCalls} - See |
| 359 | * {@link #rejectCall}, {@link #answerCall}. |
| 360 | * |
| 361 | * @param call The call for which we should stop ringing. |
| 362 | */ |
| 363 | private void stopRinging(Call call) { |
| 364 | // Only stop the ringer if this call is the top-most incoming call. |
| 365 | if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) { |
| 366 | mRinger.stopRinging(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 369 | } |