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