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