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 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 21 | import android.telecomm.CallAudioState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 22 | import android.telecomm.CallInfo; |
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; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 25 | import android.telecomm.GatewayInfo; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 26 | import android.telephony.DisconnectCause; |
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; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableCollection; |
| 31 | import com.google.common.collect.ImmutableList; |
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; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 34 | import com.google.common.collect.Sets; |
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; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 38 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 39 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Singleton. |
| 42 | * |
| 43 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 44 | * access from other packages specifically refraining from passing the CallsManager instance |
| 45 | * beyond the com.android.telecomm package boundary. |
| 46 | */ |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 47 | public final class CallsManager { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 48 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 49 | // TODO(santoscordon): Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 50 | interface CallsManagerListener { |
| 51 | void onCallAdded(Call call); |
| 52 | void onCallRemoved(Call call); |
| 53 | void onCallStateChanged(Call call, CallState oldState, CallState newState); |
| 54 | void onIncomingCallAnswered(Call call); |
| 55 | void onIncomingCallRejected(Call call); |
| 56 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 57 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 60 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 61 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 62 | private final Switchboard mSwitchboard; |
| 63 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 64 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 65 | * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming |
| 66 | * and outgoing calls are added to the map and removed when the calls move to the disconnected |
| 67 | * state. |
| 68 | * TODO(santoscordon): Add new CallId class and use it in place of String. |
| 69 | */ |
| 70 | private final Map<String, Call> mCalls = Maps.newHashMap(); |
| 71 | |
| 72 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 73 | * The call the user is currently interacting with. This is the call that should have audio |
| 74 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 75 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 76 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 77 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 78 | private final CallAudioManager mCallAudioManager; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 79 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 80 | private final Set<CallsManagerListener> mListeners = Sets.newHashSet(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 81 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 82 | private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 83 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 84 | private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 85 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 86 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 87 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 88 | */ |
| 89 | private CallsManager() { |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 90 | mSwitchboard = new Switchboard(this); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 91 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 92 | mCallAudioManager = new CallAudioManager(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 93 | |
| 94 | mListeners.add(new CallLogManager(TelecommApp.getInstance())); |
| 95 | mListeners.add(new PhoneStateBroadcaster()); |
| 96 | mListeners.add(new InCallController()); |
| 97 | mListeners.add(new Ringer(mCallAudioManager)); |
| 98 | mListeners.add(new RingbackPlayer(this, new InCallTonePlayer.Factory(mCallAudioManager))); |
| 99 | mListeners.add(mCallAudioManager); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 102 | static CallsManager getInstance() { |
| 103 | return INSTANCE; |
| 104 | } |
| 105 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 106 | ImmutableCollection<Call> getCalls() { |
| 107 | return ImmutableList.copyOf(mCalls.values()); |
| 108 | } |
| 109 | |
| 110 | Call getForegroundCall() { |
| 111 | return mForegroundCall; |
| 112 | } |
| 113 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 114 | boolean hasEmergencyCall() { |
| 115 | for (Call call : mCalls.values()) { |
| 116 | if (call.isEmergencyCall()) { |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | CallAudioState getAudioState() { |
| 124 | return mCallAudioManager.getAudioState(); |
| 125 | } |
| 126 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 127 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 128 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 129 | * specified call; using the specified call service descriptor. Upon success, execution returns |
| 130 | * to {@link #handleSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 131 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 132 | * @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] | 133 | * @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] | 134 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 135 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 136 | Log.d(this, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 137 | // Create a call with no handle. Eventually, switchboard will update the call with |
| 138 | // additional information from the call service, but for now we just need one to pass around |
| 139 | // with a unique call ID. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 140 | Call call = new Call(true /* isIncoming */); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 141 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 142 | mSwitchboard.retrieveIncomingCall(call, descriptor, extras); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 146 | * Validates the specified call and, upon no objection to connect it, adds the new call to the |
| 147 | * list of live calls. Also notifies the in-call app so the user can answer or reject the call. |
| 148 | * |
| 149 | * @param call The new incoming call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 150 | * @param callInfo The details of the call. |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 151 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 152 | void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 153 | Log.d(this, "handleSuccessfulIncomingCall"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 154 | Preconditions.checkState(callInfo.getState() == CallState.RINGING); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 155 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 156 | Uri handle = call.getHandle(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 157 | ContactInfo contactInfo = call.getContactInfo(); |
| 158 | for (IncomingCallValidator validator : mIncomingCallValidators) { |
| 159 | if (!validator.isValid(handle, contactInfo)) { |
| 160 | // TODO(gilad): Consider displaying an error message. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 161 | Log.i(this, "Dropping restricted incoming call"); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // No objection to accept the incoming call, proceed with potentially connecting it (based |
| 167 | // on the user's action, or lack thereof). |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 168 | call.setHandle(callInfo.getHandle()); |
| 169 | setCallState(call, callInfo.getState()); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 170 | addCall(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 171 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 172 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Called when an incoming call was not connected. |
| 175 | * |
| 176 | * @param call The incoming call. |
| 177 | */ |
| 178 | void handleUnsuccessfulIncomingCall(Call call) { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 179 | // Incoming calls are not added unless they are successful. We set the state and disconnect |
| 180 | // cause just as a matter of good bookkeeping. We do not use the specific methods for |
| 181 | // setting those values so that we do not trigger CallsManagerListener events. |
| 182 | // TODO: Needs more specific disconnect error for this case. |
| 183 | call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null); |
| 184 | call.setState(CallState.DISCONNECTED); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 188 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 189 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 190 | * @param handle Handle to connect the call with. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 191 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 192 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
| 193 | * actual dialed handle via a gateway provider. May be null. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 194 | */ |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 195 | void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) { |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 196 | for (OutgoingCallValidator validator : mOutgoingCallValidators) { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 197 | if (!validator.isValid(handle, contactInfo)) { |
| 198 | // TODO(gilad): Display an error message. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 199 | Log.i(this, "Dropping restricted outgoing call."); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 204 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle(); |
| 205 | |
| 206 | if (gatewayInfo == null) { |
| 207 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle)); |
| 208 | } else { |
| 209 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 210 | Log.pii(uriHandle), Log.pii(handle)); |
| 211 | } |
| 212 | Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 213 | setCallState(call, CallState.DIALING); |
| 214 | addCall(call); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 215 | mSwitchboard.placeOutgoingCall(call); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 216 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 217 | |
| 218 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 219 | * Called when a call service acknowledges that it can place a call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 220 | * |
| 221 | * @param call The new outgoing call. |
| 222 | */ |
| 223 | void handleSuccessfulOutgoingCall(Call call) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 224 | Log.v(this, "handleSuccessfulOutgoingCall, %s", call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 228 | * Called when an outgoing call was not placed. |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 229 | * |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 230 | * @param call The outgoing call. |
| 231 | * @param isAborted True if the call was unsuccessful because it was aborted. |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 232 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 233 | void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) { |
| 234 | Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted); |
| 235 | if (isAborted) { |
| 236 | call.abort(); |
| 237 | setCallState(call, CallState.ABORTED); |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 238 | removeCall(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 239 | } else { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 240 | // TODO: Replace disconnect cause with more specific disconnect causes. |
| 241 | markCallAsDisconnected(call.getId(), DisconnectCause.ERROR_UNSPECIFIED, null); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 242 | } |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 246 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 247 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 248 | * the user opting to answer said call. |
| 249 | * |
| 250 | * @param callId The ID of the call. |
| 251 | */ |
| 252 | void answerCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 253 | Call call = mCalls.get(callId); |
| 254 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 255 | Log.i(this, "Request to answer a non-existent call %s", callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 256 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 257 | for (CallsManagerListener listener : mListeners) { |
| 258 | listener.onIncomingCallAnswered(call); |
| 259 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 260 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 261 | // We do not update the UI until we get confirmation of the answer() through |
| 262 | // {@link #markCallAsActive}. However, if we ever change that to look more responsive, |
| 263 | // then we need to make sure we add a timeout for the answer() in case the call never |
| 264 | // comes out of RINGING. |
| 265 | call.answer(); |
| 266 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 271 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 272 | * the user opting to reject said call. |
| 273 | * |
| 274 | * @param callId The ID of the call. |
| 275 | */ |
| 276 | void rejectCall(String callId) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 277 | Call call = mCalls.get(callId); |
| 278 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 279 | Log.i(this, "Request to reject a non-existent call %s", callId); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 280 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 281 | for (CallsManagerListener listener : mListeners) { |
| 282 | listener.onIncomingCallRejected(call); |
| 283 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 284 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 285 | call.reject(); |
| 286 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame^] | 290 | * Instructs Telecomm to play the specified DTMF tone within the specified call. |
| 291 | * |
| 292 | * @param callId The ID of the call. |
| 293 | * @param digit The DTMF digit to play. |
| 294 | */ |
| 295 | void playDtmfTone(String callId, char digit) { |
| 296 | Call call = mCalls.get(callId); |
| 297 | if (call == null) { |
| 298 | Log.i(this, "Request to play DTMF in a non-existent call %s", callId); |
| 299 | } else { |
| 300 | call.playDtmfTone(digit); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Instructs Telecomm to stop the currently playing DTMF tone, if any. |
| 306 | * |
| 307 | * @param callId The ID of the call. |
| 308 | */ |
| 309 | void stopDtmfTone(String callId) { |
| 310 | Call call = mCalls.get(callId); |
| 311 | if (call == null) { |
| 312 | Log.i(this, "Request to stop DTMF in a non-existent call %s", callId); |
| 313 | } else { |
| 314 | call.stopDtmfTone(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Instructs Telecomm to continue the current post-dial DTMF string, if any. |
| 320 | * |
| 321 | * @param callId The ID of the call. |
| 322 | */ |
| 323 | void postDialContinue(String callId) { |
| 324 | Call call = mCalls.get(callId); |
| 325 | if (call == null) { |
| 326 | Log.i(this, "Request to continue post-dial string in a non-existent call %s", callId); |
| 327 | } else { |
| 328 | // TODO(ihab): Implement this from this level on downwards |
| 329 | // call.postDialContinue(); |
| 330 | // Must play tones locally -- see DTMFTonePlayer.java in Telephony |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 335 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 336 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 337 | * the user hitting the end-call button. |
| 338 | * |
| 339 | * @param callId The ID of the call. |
| 340 | */ |
| 341 | void disconnectCall(String callId) { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 342 | Call call = mCalls.get(callId); |
| 343 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 344 | Log.w(this, "Unknown call (%s) asked to disconnect", callId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 345 | } else { |
| 346 | call.disconnect(); |
| 347 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 348 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 349 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the |
| 352 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 353 | * the user hitting the hold button during an active call. |
| 354 | * |
| 355 | * @param callId The ID of the call. |
| 356 | */ |
| 357 | void holdCall(String callId) { |
| 358 | Call call = mCalls.get(callId); |
| 359 | if (call == null) { |
| 360 | Log.w(this, "Unknown call (%s) asked to be put on hold", callId); |
| 361 | } else { |
| 362 | Log.d(this, "Putting call on hold: (%s)", callId); |
| 363 | call.hold(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Instructs Telecomm to release the specified call from hold. Intended to be invoked by |
| 369 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 370 | * by the user hitting the hold button during a held call. |
| 371 | * |
| 372 | * @param callId The ID of the call |
| 373 | */ |
| 374 | void unholdCall(String callId) { |
| 375 | Call call = mCalls.get(callId); |
| 376 | if (call == null) { |
| 377 | Log.w(this, "Unknown call (%s) asked to be removed from hold", callId); |
| 378 | } else { |
| 379 | Log.d(this, "Removing call from hold: (%s)", callId); |
| 380 | call.unhold(); |
| 381 | } |
| 382 | } |
| 383 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 384 | /** Called by the in-call UI to change the mute state. */ |
| 385 | void mute(boolean shouldMute) { |
| 386 | mCallAudioManager.mute(shouldMute); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 391 | * speaker phone. |
| 392 | */ |
| 393 | void setAudioRoute(int route) { |
| 394 | mCallAudioManager.setAudioRoute(route); |
| 395 | } |
| 396 | |
| 397 | /** Called when the audio state changes. */ |
| 398 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
| 399 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 400 | for (CallsManagerListener listener : mListeners) { |
| 401 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 402 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 405 | void markCallAsRinging(String callId) { |
| 406 | setCallState(callId, CallState.RINGING); |
| 407 | } |
| 408 | |
| 409 | void markCallAsDialing(String callId) { |
| 410 | setCallState(callId, CallState.DIALING); |
| 411 | } |
| 412 | |
| 413 | void markCallAsActive(String callId) { |
| 414 | setCallState(callId, CallState.ACTIVE); |
| 415 | } |
| 416 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 417 | void markCallAsOnHold(String callId) { |
| 418 | setCallState(callId, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 421 | /** |
| 422 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 423 | * live call, then also disconnect from the in-call controller. |
| 424 | * |
| 425 | * @param callId The ID of the call. |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 426 | * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}. |
| 427 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 428 | */ |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 429 | void markCallAsDisconnected(String callId, int disconnectCause, String disconnectMessage) { |
| 430 | Call call = mCalls.get(callId); |
| 431 | if (call != null) { |
| 432 | call.setDisconnectCause(disconnectCause, disconnectMessage); |
| 433 | setCallState(callId, CallState.DISCONNECTED); |
| 434 | removeCall(call); |
| 435 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 439 | * Cleans up any calls currently associated with the specified call service when the |
| 440 | * call-service binder disconnects unexpectedly. |
| 441 | * |
| 442 | * @param callService The call service that disconnected. |
| 443 | */ |
| 444 | void handleCallServiceDeath(CallServiceWrapper callService) { |
| 445 | Preconditions.checkNotNull(callService); |
| 446 | for (Call call : ImmutableList.copyOf(mCalls.values())) { |
| 447 | if (call.getCallService() == callService) { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 448 | markCallAsDisconnected(call.getId(), DisconnectCause.ERROR_UNSPECIFIED, null); |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 454 | * Adds the specified call to the main list of live calls. |
| 455 | * |
| 456 | * @param call The call to add. |
| 457 | */ |
| 458 | private void addCall(Call call) { |
| 459 | mCalls.put(call.getId(), call); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 460 | for (CallsManagerListener listener : mListeners) { |
| 461 | listener.onCallAdded(call); |
| 462 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 463 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 466 | private void removeCall(Call call) { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 467 | mCalls.remove(call.getId()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 468 | call.clearCallService(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 469 | for (CallsManagerListener listener : mListeners) { |
| 470 | listener.onCallRemoved(call); |
| 471 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 472 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 476 | * Sets the specified state on the specified call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 477 | * |
| 478 | * @param callId The ID of the call to update. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 479 | * @param newState The new state of the call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 480 | */ |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 481 | private void setCallState(String callId, CallState newState) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 482 | Preconditions.checkState(!Strings.isNullOrEmpty(callId)); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 483 | Preconditions.checkNotNull(newState); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 484 | |
| 485 | Call call = mCalls.get(callId); |
| 486 | if (call == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 487 | Log.w(this, "Call %s was not found while attempting to update the state to %s.", |
| 488 | callId, newState ); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 489 | } else { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 490 | setCallState(call, newState); |
| 491 | } |
| 492 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 493 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 494 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 495 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 496 | * |
| 497 | * @param call The call. |
| 498 | * @param newState The new state of the call. |
| 499 | */ |
| 500 | private void setCallState(Call call, CallState newState) { |
| 501 | CallState oldState = call.getState(); |
| 502 | if (newState != oldState) { |
| 503 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 504 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 505 | // sense (e.g., ACTIVE -> RINGING). |
| 506 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 507 | // into a well-defined state machine. |
| 508 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 509 | // unexpected transition occurs. |
| 510 | call.setState(newState); |
| 511 | |
| 512 | // Only broadcast state change for calls that are being tracked. |
| 513 | if (mCalls.containsKey(call.getId())) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 514 | for (CallsManagerListener listener : mListeners) { |
| 515 | listener.onCallStateChanged(call, oldState, newState); |
| 516 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 517 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 523 | * Checks which call should be visible to the user and have audio focus. |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 524 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 525 | private void updateForegroundCall() { |
| 526 | Call newForegroundCall = null; |
| 527 | for (Call call : mCalls.values()) { |
| 528 | // Incoming ringing calls have priority. |
| 529 | if (call.getState() == CallState.RINGING) { |
| 530 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 531 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 532 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 533 | if (call.isAlive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 534 | newForegroundCall = call; |
| 535 | // Don't break in case there's a ringing call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 538 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 539 | if (newForegroundCall != mForegroundCall) { |
| 540 | Call oldForegroundCall = mForegroundCall; |
| 541 | mForegroundCall = newForegroundCall; |
| 542 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 543 | for (CallsManagerListener listener : mListeners) { |
| 544 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 545 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 548 | } |