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