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