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