blob: 79f085ca851008628e5ad0e04bf67fcea7080806 [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 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
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Santos Cordon5c6fa952014-07-20 17:47:12 -070019import android.annotation.SdkConstant;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070020import android.app.Service;
Santos Cordon52d8a152014-06-17 19:08:45 -070021import android.content.ComponentName;
Santos Cordon5c6fa952014-07-20 17:47:12 -070022import android.content.Intent;
Ihab Awad542e0ea2014-05-16 10:22:16 -070023import android.net.Uri;
Santos Cordon6b7f9552015-05-27 17:21:45 -070024import android.os.Bundle;
Santos Cordon52d8a152014-06-17 19:08:45 -070025import android.os.Handler;
26import android.os.IBinder;
27import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070028import android.os.Message;
Brad Ebingerb32d4f82016-10-24 16:40:49 -070029import android.telecom.Logging.Session;
Andrew Lee14185762014-07-25 09:41:56 -070030
Sailesh Nepal2a46b902014-07-04 17:21:07 -070031import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070032import com.android.internal.telecom.IConnectionService;
33import com.android.internal.telecom.IConnectionServiceAdapter;
34import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070035
Ihab Awad5d0410f2014-07-30 10:07:40 -070036import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070037import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070038import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070039import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070040import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070041import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070042import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070043
44/**
Tyler Gunnf5035432017-01-09 09:43:12 -080045 * An abstract service that should be implemented by any apps which either:
46 * <ol>
47 * <li>Can make phone calls (VoIP or otherwise) and want those calls to be integrated into the
48 * built-in phone app. Referred to as a <b>system managed</b> {@link ConnectionService}.</li>
49 * <li>Are a standalone calling app and don't want their calls to be integrated into the
50 * built-in phone app. Referred to as a <b>self managed</b> {@link ConnectionService}.</li>
51 * </ol>
52 * Once implemented, the {@link ConnectionService} needs to take the following steps so that Telecom
53 * will bind to it:
Santos Cordona663f862014-10-29 13:49:58 -070054 * <p>
55 * 1. <i>Registration in AndroidManifest.xml</i>
56 * <br/>
57 * <pre>
58 * &lt;service android:name="com.example.package.MyConnectionService"
59 * android:label="@string/some_label_for_my_connection_service"
Yorke Lee249c12e2015-05-13 15:59:29 -070060 * android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"&gt;
Santos Cordona663f862014-10-29 13:49:58 -070061 * &lt;intent-filter&gt;
62 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
63 * &lt;/intent-filter&gt;
64 * &lt;/service&gt;
65 * </pre>
66 * <p>
67 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
68 * <br/>
69 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
70 * <p>
Tyler Gunnf5035432017-01-09 09:43:12 -080071 * System managed {@link ConnectionService}s must be enabled by the user in the phone app settings
72 * before Telecom will bind to them. Self-manged {@link ConnectionService}s must be granted the
73 * appropriate permission before Telecom will bind to them.
74 * <p>
75 * Once registered and enabled by the user in the phone app settings or granted permission, telecom
76 * will bind to a {@link ConnectionService} implementation when it wants that
77 * {@link ConnectionService} to place a call or the service has indicated that is has an incoming
78 * call through {@link TelecomManager#addNewIncomingCall}. The {@link ConnectionService} can then
79 * expect a call to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection}
80 * wherein it should provide a new instance of a {@link Connection} object. It is through this
81 * {@link Connection} object that telecom receives state updates and the {@link ConnectionService}
Santos Cordona663f862014-10-29 13:49:58 -070082 * receives call-commands such as answer, reject, hold and disconnect.
83 * <p>
Tyler Gunnf5035432017-01-09 09:43:12 -080084 * When there are no more live calls, telecom will unbind from the {@link ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070085 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070086public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070087 /**
88 * The {@link Intent} that must be declared as handled by the service.
89 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070090 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070091 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070092
Ihab Awad542e0ea2014-05-16 10:22:16 -070093 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070094 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070095
Brad Ebingerb32d4f82016-10-24 16:40:49 -070096 // Session Definitions
97 private static final String SESSION_HANDLER = "H.";
98 private static final String SESSION_ADD_CS_ADAPTER = "CS.aCSA";
99 private static final String SESSION_REMOVE_CS_ADAPTER = "CS.rCSA";
100 private static final String SESSION_CREATE_CONN = "CS.crCo";
Tyler Gunn44e01912017-01-31 10:49:05 -0800101 private static final String SESSION_CREATE_CONN_FAILED = "CS.crCoF";
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700102 private static final String SESSION_ABORT = "CS.ab";
103 private static final String SESSION_ANSWER = "CS.an";
104 private static final String SESSION_ANSWER_VIDEO = "CS.anV";
105 private static final String SESSION_REJECT = "CS.r";
106 private static final String SESSION_REJECT_MESSAGE = "CS.rWM";
107 private static final String SESSION_SILENCE = "CS.s";
108 private static final String SESSION_DISCONNECT = "CS.d";
109 private static final String SESSION_HOLD = "CS.h";
110 private static final String SESSION_UNHOLD = "CS.u";
111 private static final String SESSION_CALL_AUDIO_SC = "CS.cASC";
112 private static final String SESSION_PLAY_DTMF = "CS.pDT";
113 private static final String SESSION_STOP_DTMF = "CS.sDT";
114 private static final String SESSION_CONFERENCE = "CS.c";
115 private static final String SESSION_SPLIT_CONFERENCE = "CS.sFC";
116 private static final String SESSION_MERGE_CONFERENCE = "CS.mC";
117 private static final String SESSION_SWAP_CONFERENCE = "CS.sC";
118 private static final String SESSION_POST_DIAL_CONT = "CS.oPDC";
119 private static final String SESSION_PULL_EXTERNAL_CALL = "CS.pEC";
120 private static final String SESSION_SEND_CALL_EVENT = "CS.sCE";
121 private static final String SESSION_EXTRAS_CHANGED = "CS.oEC";
122
Ihab Awad8aecfed2014-08-08 17:06:11 -0700123 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700124 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700125 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700126 private static final int MSG_ANSWER = 4;
127 private static final int MSG_REJECT = 5;
128 private static final int MSG_DISCONNECT = 6;
129 private static final int MSG_HOLD = 7;
130 private static final int MSG_UNHOLD = 8;
Yorke Lee4af59352015-05-13 14:14:54 -0700131 private static final int MSG_ON_CALL_AUDIO_STATE_CHANGED = 9;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700132 private static final int MSG_PLAY_DTMF_TONE = 10;
133 private static final int MSG_STOP_DTMF_TONE = 11;
134 private static final int MSG_CONFERENCE = 12;
135 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700136 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700137 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700138 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700139 private static final int MSG_MERGE_CONFERENCE = 18;
140 private static final int MSG_SWAP_CONFERENCE = 19;
Bryce Lee81901682015-08-28 16:38:02 -0700141 private static final int MSG_REJECT_WITH_MESSAGE = 20;
Bryce Leecac50772015-11-17 15:13:29 -0800142 private static final int MSG_SILENCE = 21;
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700143 private static final int MSG_PULL_EXTERNAL_CALL = 22;
144 private static final int MSG_SEND_CALL_EVENT = 23;
Tyler Gunndee56a82016-03-23 16:06:34 -0700145 private static final int MSG_ON_EXTRAS_CHANGED = 24;
Tyler Gunn44e01912017-01-31 10:49:05 -0800146 private static final int MSG_CREATE_CONNECTION_FAILED = 25;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700147
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700148 private static Connection sNullConnection;
149
mike dooley95e80702014-09-18 14:07:52 -0700150 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
151 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
152 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
153 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700154 private final RemoteConnectionManager mRemoteConnectionManager =
155 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700156 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700157 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700158
Santos Cordon823fd3c2014-08-07 18:35:18 -0700159 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700160 private Conference sNullConference;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700161 private Object mIdSyncRoot = new Object();
162 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700163
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700164 private final IBinder mBinder = new IConnectionService.Stub() {
165 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700166 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter,
167 Session.Info sessionInfo) {
168 Log.startSession(sessionInfo, SESSION_ADD_CS_ADAPTER);
169 try {
170 SomeArgs args = SomeArgs.obtain();
171 args.arg1 = adapter;
172 args.arg2 = Log.createSubsession();
173 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
174 } finally {
175 Log.endSession();
176 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700177 }
178
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700179 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter,
180 Session.Info sessionInfo) {
181 Log.startSession(sessionInfo, SESSION_REMOVE_CS_ADAPTER);
182 try {
183 SomeArgs args = SomeArgs.obtain();
184 args.arg1 = adapter;
185 args.arg2 = Log.createSubsession();
186 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
187 } finally {
188 Log.endSession();
189 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700190 }
191
192 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700193 public void createConnection(
194 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700195 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700196 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700197 boolean isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700198 boolean isUnknown,
199 Session.Info sessionInfo) {
200 Log.startSession(sessionInfo, SESSION_CREATE_CONN);
201 try {
202 SomeArgs args = SomeArgs.obtain();
203 args.arg1 = connectionManagerPhoneAccount;
204 args.arg2 = id;
205 args.arg3 = request;
206 args.arg4 = Log.createSubsession();
207 args.argi1 = isIncoming ? 1 : 0;
208 args.argi2 = isUnknown ? 1 : 0;
209 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
210 } finally {
211 Log.endSession();
212 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700213 }
214
215 @Override
Tyler Gunn44e01912017-01-31 10:49:05 -0800216 public void createConnectionFailed(
Tyler Gunn159f35c2017-03-02 09:28:37 -0800217 PhoneAccountHandle connectionManagerPhoneAccount,
Tyler Gunn44e01912017-01-31 10:49:05 -0800218 String callId,
219 ConnectionRequest request,
220 boolean isIncoming,
221 Session.Info sessionInfo) {
222 Log.startSession(sessionInfo, SESSION_CREATE_CONN_FAILED);
223 try {
224 SomeArgs args = SomeArgs.obtain();
225 args.arg1 = callId;
226 args.arg2 = request;
227 args.arg3 = Log.createSubsession();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800228 args.arg4 = connectionManagerPhoneAccount;
Tyler Gunn44e01912017-01-31 10:49:05 -0800229 args.argi1 = isIncoming ? 1 : 0;
230 mHandler.obtainMessage(MSG_CREATE_CONNECTION_FAILED, args).sendToTarget();
231 } finally {
232 Log.endSession();
233 }
234 }
235
236 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700237 public void abort(String callId, Session.Info sessionInfo) {
238 Log.startSession(sessionInfo, SESSION_ABORT);
239 try {
240 SomeArgs args = SomeArgs.obtain();
241 args.arg1 = callId;
242 args.arg2 = Log.createSubsession();
243 mHandler.obtainMessage(MSG_ABORT, args).sendToTarget();
244 } finally {
245 Log.endSession();
246 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700247 }
248
249 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700250 public void answerVideo(String callId, int videoState, Session.Info sessionInfo) {
251 Log.startSession(sessionInfo, SESSION_ANSWER_VIDEO);
252 try {
253 SomeArgs args = SomeArgs.obtain();
254 args.arg1 = callId;
255 args.arg2 = Log.createSubsession();
256 args.argi1 = videoState;
257 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
258 } finally {
259 Log.endSession();
260 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700261 }
262
263 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700264 public void answer(String callId, Session.Info sessionInfo) {
265 Log.startSession(sessionInfo, SESSION_ANSWER);
266 try {
267 SomeArgs args = SomeArgs.obtain();
268 args.arg1 = callId;
269 args.arg2 = Log.createSubsession();
270 mHandler.obtainMessage(MSG_ANSWER, args).sendToTarget();
271 } finally {
272 Log.endSession();
273 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700274 }
275
276 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700277 public void reject(String callId, Session.Info sessionInfo) {
278 Log.startSession(sessionInfo, SESSION_REJECT);
279 try {
280 SomeArgs args = SomeArgs.obtain();
281 args.arg1 = callId;
282 args.arg2 = Log.createSubsession();
283 mHandler.obtainMessage(MSG_REJECT, args).sendToTarget();
284 } finally {
285 Log.endSession();
286 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700287 }
288
289 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700290 public void rejectWithMessage(String callId, String message, Session.Info sessionInfo) {
291 Log.startSession(sessionInfo, SESSION_REJECT_MESSAGE);
292 try {
293 SomeArgs args = SomeArgs.obtain();
294 args.arg1 = callId;
295 args.arg2 = message;
296 args.arg3 = Log.createSubsession();
297 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
298 } finally {
299 Log.endSession();
300 }
Bryce Lee81901682015-08-28 16:38:02 -0700301 }
302
303 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700304 public void silence(String callId, Session.Info sessionInfo) {
305 Log.startSession(sessionInfo, SESSION_SILENCE);
306 try {
307 SomeArgs args = SomeArgs.obtain();
308 args.arg1 = callId;
309 args.arg2 = Log.createSubsession();
310 mHandler.obtainMessage(MSG_SILENCE, args).sendToTarget();
311 } finally {
312 Log.endSession();
313 }
Bryce Leecac50772015-11-17 15:13:29 -0800314 }
315
316 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700317 public void disconnect(String callId, Session.Info sessionInfo) {
318 Log.startSession(sessionInfo, SESSION_DISCONNECT);
319 try {
320 SomeArgs args = SomeArgs.obtain();
321 args.arg1 = callId;
322 args.arg2 = Log.createSubsession();
323 mHandler.obtainMessage(MSG_DISCONNECT, args).sendToTarget();
324 } finally {
325 Log.endSession();
326 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700327 }
328
329 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700330 public void hold(String callId, Session.Info sessionInfo) {
331 Log.startSession(sessionInfo, SESSION_HOLD);
332 try {
333 SomeArgs args = SomeArgs.obtain();
334 args.arg1 = callId;
335 args.arg2 = Log.createSubsession();
336 mHandler.obtainMessage(MSG_HOLD, args).sendToTarget();
337 } finally {
338 Log.endSession();
339 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700340 }
341
342 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700343 public void unhold(String callId, Session.Info sessionInfo) {
344 Log.startSession(sessionInfo, SESSION_UNHOLD);
345 try {
346 SomeArgs args = SomeArgs.obtain();
347 args.arg1 = callId;
348 args.arg2 = Log.createSubsession();
349 mHandler.obtainMessage(MSG_UNHOLD, args).sendToTarget();
350 } finally {
351 Log.endSession();
352 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700353 }
354
355 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700356 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState,
357 Session.Info sessionInfo) {
358 Log.startSession(sessionInfo, SESSION_CALL_AUDIO_SC);
359 try {
360 SomeArgs args = SomeArgs.obtain();
361 args.arg1 = callId;
362 args.arg2 = callAudioState;
363 args.arg3 = Log.createSubsession();
364 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
365 } finally {
366 Log.endSession();
367 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700368 }
369
370 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700371 public void playDtmfTone(String callId, char digit, Session.Info sessionInfo) {
372 Log.startSession(sessionInfo, SESSION_PLAY_DTMF);
373 try {
374 SomeArgs args = SomeArgs.obtain();
375 args.arg1 = digit;
376 args.arg2 = callId;
377 args.arg3 = Log.createSubsession();
378 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, args).sendToTarget();
379 } finally {
380 Log.endSession();
381 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700382 }
383
384 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700385 public void stopDtmfTone(String callId, Session.Info sessionInfo) {
386 Log.startSession(sessionInfo, SESSION_STOP_DTMF);
387 try {
388 SomeArgs args = SomeArgs.obtain();
389 args.arg1 = callId;
390 args.arg2 = Log.createSubsession();
391 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, args).sendToTarget();
392 } finally {
393 Log.endSession();
394 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700395 }
396
397 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700398 public void conference(String callId1, String callId2, Session.Info sessionInfo) {
399 Log.startSession(sessionInfo, SESSION_CONFERENCE);
400 try {
401 SomeArgs args = SomeArgs.obtain();
402 args.arg1 = callId1;
403 args.arg2 = callId2;
404 args.arg3 = Log.createSubsession();
405 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
406 } finally {
407 Log.endSession();
408 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700409 }
410
411 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700412 public void splitFromConference(String callId, Session.Info sessionInfo) {
413 Log.startSession(sessionInfo, SESSION_SPLIT_CONFERENCE);
414 try {
415 SomeArgs args = SomeArgs.obtain();
416 args.arg1 = callId;
417 args.arg2 = Log.createSubsession();
418 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, args).sendToTarget();
419 } finally {
420 Log.endSession();
421 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700422 }
423
424 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700425 public void mergeConference(String callId, Session.Info sessionInfo) {
426 Log.startSession(sessionInfo, SESSION_MERGE_CONFERENCE);
427 try {
428 SomeArgs args = SomeArgs.obtain();
429 args.arg1 = callId;
430 args.arg2 = Log.createSubsession();
431 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, args).sendToTarget();
432 } finally {
433 Log.endSession();
434 }
Santos Cordona4868042014-09-04 17:39:22 -0700435 }
436
437 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700438 public void swapConference(String callId, Session.Info sessionInfo) {
439 Log.startSession(sessionInfo, SESSION_SWAP_CONFERENCE);
440 try {
441 SomeArgs args = SomeArgs.obtain();
442 args.arg1 = callId;
443 args.arg2 = Log.createSubsession();
444 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, args).sendToTarget();
445 } finally {
446 Log.endSession();
447 }
Santos Cordona4868042014-09-04 17:39:22 -0700448 }
449
450 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700451 public void onPostDialContinue(String callId, boolean proceed, Session.Info sessionInfo) {
452 Log.startSession(sessionInfo, SESSION_POST_DIAL_CONT);
453 try {
454 SomeArgs args = SomeArgs.obtain();
455 args.arg1 = callId;
456 args.arg2 = Log.createSubsession();
457 args.argi1 = proceed ? 1 : 0;
458 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
459 } finally {
460 Log.endSession();
461 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700462 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700463
464 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700465 public void pullExternalCall(String callId, Session.Info sessionInfo) {
466 Log.startSession(sessionInfo, SESSION_PULL_EXTERNAL_CALL);
467 try {
468 SomeArgs args = SomeArgs.obtain();
469 args.arg1 = callId;
470 args.arg2 = Log.createSubsession();
471 mHandler.obtainMessage(MSG_PULL_EXTERNAL_CALL, args).sendToTarget();
472 } finally {
473 Log.endSession();
474 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700475 }
476
477 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700478 public void sendCallEvent(String callId, String event, Bundle extras,
479 Session.Info sessionInfo) {
480 Log.startSession(sessionInfo, SESSION_SEND_CALL_EVENT);
481 try {
482 SomeArgs args = SomeArgs.obtain();
483 args.arg1 = callId;
484 args.arg2 = event;
485 args.arg3 = extras;
486 args.arg4 = Log.createSubsession();
487 mHandler.obtainMessage(MSG_SEND_CALL_EVENT, args).sendToTarget();
488 } finally {
489 Log.endSession();
490 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700491 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700492
493 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700494 public void onExtrasChanged(String callId, Bundle extras, Session.Info sessionInfo) {
495 Log.startSession(sessionInfo, SESSION_EXTRAS_CHANGED);
496 try {
497 SomeArgs args = SomeArgs.obtain();
498 args.arg1 = callId;
499 args.arg2 = extras;
500 args.arg3 = Log.createSubsession();
501 mHandler.obtainMessage(MSG_ON_EXTRAS_CHANGED, args).sendToTarget();
502 } finally {
503 Log.endSession();
504 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700505 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700506 };
507
508 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
509 @Override
510 public void handleMessage(Message msg) {
511 switch (msg.what) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700512 case MSG_ADD_CONNECTION_SERVICE_ADAPTER: {
513 SomeArgs args = (SomeArgs) msg.obj;
514 try {
515 IConnectionServiceAdapter adapter = (IConnectionServiceAdapter) args.arg1;
516 Log.continueSession((Session) args.arg2,
517 SESSION_HANDLER + SESSION_ADD_CS_ADAPTER);
518 mAdapter.addAdapter(adapter);
519 onAdapterAttached();
520 } finally {
521 args.recycle();
522 Log.endSession();
523 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700524 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700525 }
526 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER: {
527 SomeArgs args = (SomeArgs) msg.obj;
528 try {
529 Log.continueSession((Session) args.arg2,
530 SESSION_HANDLER + SESSION_REMOVE_CS_ADAPTER);
531 mAdapter.removeAdapter((IConnectionServiceAdapter) args.arg1);
532 } finally {
533 args.recycle();
534 Log.endSession();
535 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700536 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700537 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700538 case MSG_CREATE_CONNECTION: {
539 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700540 Log.continueSession((Session) args.arg4, SESSION_HANDLER + SESSION_CREATE_CONN);
Ihab Awadf8b69882014-07-25 15:14:01 -0700541 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700542 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700543 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700544 final String id = (String) args.arg2;
545 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700546 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700547 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700548 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700549 Log.d(this, "Enqueueing pre-init request %s", id);
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700550 mPreInitializationConnectionRequests.add(
551 new android.telecom.Logging.Runnable(
552 SESSION_HANDLER + SESSION_CREATE_CONN + ".pICR",
553 null /*lock*/) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700554 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700555 public void loggedRun() {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700556 createConnection(
557 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700558 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700559 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700560 isIncoming,
561 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700562 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700563 }.prepare());
Ihab Awad5d0410f2014-07-30 10:07:40 -0700564 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700565 createConnection(
566 connectionManagerPhoneAccount,
567 id,
568 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700569 isIncoming,
570 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700571 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700572 } finally {
573 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700574 Log.endSession();
Ihab Awadf8b69882014-07-25 15:14:01 -0700575 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700576 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700577 }
Tyler Gunn44e01912017-01-31 10:49:05 -0800578 case MSG_CREATE_CONNECTION_FAILED: {
579 SomeArgs args = (SomeArgs) msg.obj;
580 Log.continueSession((Session) args.arg3, SESSION_HANDLER +
581 SESSION_CREATE_CONN_FAILED);
582 try {
583 final String id = (String) args.arg1;
584 final ConnectionRequest request = (ConnectionRequest) args.arg2;
585 final boolean isIncoming = args.argi1 == 1;
Tyler Gunn159f35c2017-03-02 09:28:37 -0800586 final PhoneAccountHandle connectionMgrPhoneAccount =
587 (PhoneAccountHandle) args.arg4;
Tyler Gunn44e01912017-01-31 10:49:05 -0800588 if (!mAreAccountsInitialized) {
589 Log.d(this, "Enqueueing pre-init request %s", id);
590 mPreInitializationConnectionRequests.add(
591 new android.telecom.Logging.Runnable(
592 SESSION_HANDLER + SESSION_CREATE_CONN_FAILED + ".pICR",
593 null /*lock*/) {
594 @Override
595 public void loggedRun() {
Tyler Gunn159f35c2017-03-02 09:28:37 -0800596 createConnectionFailed(connectionMgrPhoneAccount, id,
597 request, isIncoming);
Tyler Gunn44e01912017-01-31 10:49:05 -0800598 }
599 }.prepare());
600 } else {
601 Log.i(this, "createConnectionFailed %s", id);
Tyler Gunn159f35c2017-03-02 09:28:37 -0800602 createConnectionFailed(connectionMgrPhoneAccount, id, request,
603 isIncoming);
Tyler Gunn44e01912017-01-31 10:49:05 -0800604 }
605 } finally {
606 args.recycle();
607 Log.endSession();
608 }
609 break;
610 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700611 case MSG_ABORT: {
612 SomeArgs args = (SomeArgs) msg.obj;
613 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ABORT);
614 try {
615 abort((String) args.arg1);
616 } finally {
617 args.recycle();
618 Log.endSession();
619 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700620 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700621 }
622 case MSG_ANSWER: {
623 SomeArgs args = (SomeArgs) msg.obj;
624 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ANSWER);
625 try {
626 answer((String) args.arg1);
627 } finally {
628 args.recycle();
629 Log.endSession();
630 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700631 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700632 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700633 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700634 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700635 Log.continueSession((Session) args.arg2,
636 SESSION_HANDLER + SESSION_ANSWER_VIDEO);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700637 try {
638 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700639 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700640 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700641 } finally {
642 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700643 Log.endSession();
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700644 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700645 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700646 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700647 case MSG_REJECT: {
648 SomeArgs args = (SomeArgs) msg.obj;
649 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
650 try {
651 reject((String) args.arg1);
652 } finally {
653 args.recycle();
654 Log.endSession();
655 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700656 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700657 }
Bryce Lee81901682015-08-28 16:38:02 -0700658 case MSG_REJECT_WITH_MESSAGE: {
659 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700660 Log.continueSession((Session) args.arg3,
661 SESSION_HANDLER + SESSION_REJECT_MESSAGE);
Bryce Lee81901682015-08-28 16:38:02 -0700662 try {
663 reject((String) args.arg1, (String) args.arg2);
664 } finally {
665 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700666 Log.endSession();
Bryce Lee81901682015-08-28 16:38:02 -0700667 }
668 break;
669 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700670 case MSG_DISCONNECT: {
671 SomeArgs args = (SomeArgs) msg.obj;
672 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_DISCONNECT);
673 try {
674 disconnect((String) args.arg1);
675 } finally {
676 args.recycle();
677 Log.endSession();
678 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700679 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700680 }
681 case MSG_SILENCE: {
682 SomeArgs args = (SomeArgs) msg.obj;
683 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_SILENCE);
684 try {
685 silence((String) args.arg1);
686 } finally {
687 args.recycle();
688 Log.endSession();
689 }
Bryce Leecac50772015-11-17 15:13:29 -0800690 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700691 }
692 case MSG_HOLD: {
693 SomeArgs args = (SomeArgs) msg.obj;
694 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
695 try {
696 hold((String) args.arg1);
697 } finally {
698 args.recycle();
699 Log.endSession();
700 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700701 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700702 }
703 case MSG_UNHOLD: {
704 SomeArgs args = (SomeArgs) msg.obj;
705 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_UNHOLD);
706 try {
707 unhold((String) args.arg1);
708 } finally {
709 args.recycle();
710 Log.endSession();
711 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700712 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700713 }
Yorke Lee4af59352015-05-13 14:14:54 -0700714 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700715 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700716 Log.continueSession((Session) args.arg3,
717 SESSION_HANDLER + SESSION_CALL_AUDIO_SC);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700718 try {
719 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700720 CallAudioState audioState = (CallAudioState) args.arg2;
721 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700722 } finally {
723 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700724 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700725 }
726 break;
727 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700728 case MSG_PLAY_DTMF_TONE: {
729 SomeArgs args = (SomeArgs) msg.obj;
730 try {
731 Log.continueSession((Session) args.arg3,
732 SESSION_HANDLER + SESSION_PLAY_DTMF);
733 playDtmfTone((String) args.arg2, (char) args.arg1);
734 } finally {
735 args.recycle();
736 Log.endSession();
737 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700738 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700739 }
740 case MSG_STOP_DTMF_TONE: {
741 SomeArgs args = (SomeArgs) msg.obj;
742 try {
743 Log.continueSession((Session) args.arg2,
744 SESSION_HANDLER + SESSION_STOP_DTMF);
745 stopDtmfTone((String) args.arg1);
746 } finally {
747 args.recycle();
748 Log.endSession();
749 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700750 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700751 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700752 case MSG_CONFERENCE: {
753 SomeArgs args = (SomeArgs) msg.obj;
754 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700755 Log.continueSession((Session) args.arg3,
756 SESSION_HANDLER + SESSION_CONFERENCE);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700757 String callId1 = (String) args.arg1;
758 String callId2 = (String) args.arg2;
759 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700760 } finally {
761 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700762 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700763 }
764 break;
765 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700766 case MSG_SPLIT_FROM_CONFERENCE: {
767 SomeArgs args = (SomeArgs) msg.obj;
768 try {
769 Log.continueSession((Session) args.arg2,
770 SESSION_HANDLER + SESSION_SPLIT_CONFERENCE);
771 splitFromConference((String) args.arg1);
772 } finally {
773 args.recycle();
774 Log.endSession();
775 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700776 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700777 }
778 case MSG_MERGE_CONFERENCE: {
779 SomeArgs args = (SomeArgs) msg.obj;
780 try {
781 Log.continueSession((Session) args.arg2,
782 SESSION_HANDLER + SESSION_MERGE_CONFERENCE);
783 mergeConference((String) args.arg1);
784 } finally {
785 args.recycle();
786 Log.endSession();
787 }
Santos Cordona4868042014-09-04 17:39:22 -0700788 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700789 }
790 case MSG_SWAP_CONFERENCE: {
791 SomeArgs args = (SomeArgs) msg.obj;
792 try {
793 Log.continueSession((Session) args.arg2,
794 SESSION_HANDLER + SESSION_SWAP_CONFERENCE);
795 swapConference((String) args.arg1);
796 } finally {
797 args.recycle();
798 Log.endSession();
799 }
Santos Cordona4868042014-09-04 17:39:22 -0700800 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700801 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700802 case MSG_ON_POST_DIAL_CONTINUE: {
803 SomeArgs args = (SomeArgs) msg.obj;
804 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700805 Log.continueSession((Session) args.arg2,
806 SESSION_HANDLER + SESSION_POST_DIAL_CONT);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700807 String callId = (String) args.arg1;
808 boolean proceed = (args.argi1 == 1);
809 onPostDialContinue(callId, proceed);
810 } finally {
811 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700812 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700813 }
814 break;
815 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700816 case MSG_PULL_EXTERNAL_CALL: {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700817 SomeArgs args = (SomeArgs) msg.obj;
818 try {
819 Log.continueSession((Session) args.arg2,
820 SESSION_HANDLER + SESSION_PULL_EXTERNAL_CALL);
821 pullExternalCall((String) args.arg1);
822 } finally {
823 args.recycle();
824 Log.endSession();
825 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700826 break;
827 }
828 case MSG_SEND_CALL_EVENT: {
829 SomeArgs args = (SomeArgs) msg.obj;
830 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700831 Log.continueSession((Session) args.arg4,
832 SESSION_HANDLER + SESSION_SEND_CALL_EVENT);
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700833 String callId = (String) args.arg1;
834 String event = (String) args.arg2;
835 Bundle extras = (Bundle) args.arg3;
836 sendCallEvent(callId, event, extras);
837 } finally {
838 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700839 Log.endSession();
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700840 }
841 break;
842 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700843 case MSG_ON_EXTRAS_CHANGED: {
844 SomeArgs args = (SomeArgs) msg.obj;
845 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700846 Log.continueSession((Session) args.arg3,
847 SESSION_HANDLER + SESSION_EXTRAS_CHANGED);
Tyler Gunndee56a82016-03-23 16:06:34 -0700848 String callId = (String) args.arg1;
849 Bundle extras = (Bundle) args.arg2;
850 handleExtrasChanged(callId, extras);
851 } finally {
852 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700853 Log.endSession();
Tyler Gunndee56a82016-03-23 16:06:34 -0700854 }
855 break;
856 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700857 default:
858 break;
859 }
860 }
861 };
862
Santos Cordon823fd3c2014-08-07 18:35:18 -0700863 private final Conference.Listener mConferenceListener = new Conference.Listener() {
864 @Override
865 public void onStateChanged(Conference conference, int oldState, int newState) {
866 String id = mIdByConference.get(conference);
867 switch (newState) {
868 case Connection.STATE_ACTIVE:
869 mAdapter.setActive(id);
870 break;
871 case Connection.STATE_HOLDING:
872 mAdapter.setOnHold(id);
873 break;
874 case Connection.STATE_DISCONNECTED:
875 // handled by onDisconnected
876 break;
877 }
878 }
879
880 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700881 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700882 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700883 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700884 }
885
886 @Override
887 public void onConnectionAdded(Conference conference, Connection connection) {
888 }
889
890 @Override
891 public void onConnectionRemoved(Conference conference, Connection connection) {
892 }
893
894 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700895 public void onConferenceableConnectionsChanged(
896 Conference conference, List<Connection> conferenceableConnections) {
897 mAdapter.setConferenceableConnections(
898 mIdByConference.get(conference),
899 createConnectionIdList(conferenceableConnections));
900 }
901
902 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700903 public void onDestroyed(Conference conference) {
904 removeConference(conference);
905 }
906
907 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800908 public void onConnectionCapabilitiesChanged(
909 Conference conference,
910 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700911 String id = mIdByConference.get(conference);
912 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800913 Connection.capabilitiesToString(connectionCapabilities));
914 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700915 }
Rekha Kumar07366812015-03-24 16:42:31 -0700916
917 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700918 public void onConnectionPropertiesChanged(
919 Conference conference,
920 int connectionProperties) {
921 String id = mIdByConference.get(conference);
922 Log.d(this, "call capabilities: conference: %s",
923 Connection.propertiesToString(connectionProperties));
924 mAdapter.setConnectionProperties(id, connectionProperties);
925 }
926
927 @Override
Rekha Kumar07366812015-03-24 16:42:31 -0700928 public void onVideoStateChanged(Conference c, int videoState) {
929 String id = mIdByConference.get(c);
930 Log.d(this, "onVideoStateChanged set video state %d", videoState);
931 mAdapter.setVideoState(id, videoState);
932 }
933
934 @Override
935 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
936 String id = mIdByConference.get(c);
937 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
938 videoProvider);
939 mAdapter.setVideoProvider(id, videoProvider);
940 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700941
942 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700943 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
944 String id = mIdByConference.get(conference);
Tyler Gunndee56a82016-03-23 16:06:34 -0700945 if (id != null) {
946 mAdapter.setStatusHints(id, statusHints);
947 }
Andrew Leeedc625f2015-04-14 13:38:12 -0700948 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700949
950 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700951 public void onExtrasChanged(Conference c, Bundle extras) {
952 String id = mIdByConference.get(c);
953 if (id != null) {
954 mAdapter.putExtras(id, extras);
955 }
956 }
957
958 @Override
959 public void onExtrasRemoved(Conference c, List<String> keys) {
960 String id = mIdByConference.get(c);
961 if (id != null) {
962 mAdapter.removeExtras(id, keys);
963 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700964 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700965 };
966
Ihab Awad542e0ea2014-05-16 10:22:16 -0700967 private final Connection.Listener mConnectionListener = new Connection.Listener() {
968 @Override
969 public void onStateChanged(Connection c, int state) {
970 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700971 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700972 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700973 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700974 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700975 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700976 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700977 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700978 break;
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700979 case Connection.STATE_PULLING_CALL:
980 mAdapter.setPulling(id);
981 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700982 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700983 // Handled in onDisconnected()
984 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700985 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700986 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700987 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700988 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700989 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700990 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700991 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700992 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700993 break;
994 }
995 }
996
997 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700998 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700999 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -07001000 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001001 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001002 }
1003
1004 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -07001005 public void onVideoStateChanged(Connection c, int videoState) {
1006 String id = mIdByConnection.get(c);
1007 Log.d(this, "Adapter set video state %d", videoState);
1008 mAdapter.setVideoState(id, videoState);
1009 }
1010
1011 @Override
Andrew Lee100e2932014-09-08 15:34:24 -07001012 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -07001013 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001014 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -07001015 }
1016
1017 @Override
1018 public void onCallerDisplayNameChanged(
1019 Connection c, String callerDisplayName, int presentation) {
1020 String id = mIdByConnection.get(c);
1021 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001022 }
1023
1024 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -07001025 public void onDestroyed(Connection c) {
1026 removeConnection(c);
1027 }
Ihab Awadf8358972014-05-28 16:46:42 -07001028
1029 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001030 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -07001031 String id = mIdByConnection.get(c);
1032 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001033 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -07001034 }
1035
1036 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001037 public void onPostDialChar(Connection c, char nextChar) {
1038 String id = mIdByConnection.get(c);
1039 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
1040 mAdapter.onPostDialChar(id, nextChar);
1041 }
1042
1043 @Override
Andrew Lee100e2932014-09-08 15:34:24 -07001044 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -07001045 String id = mIdByConnection.get(c);
1046 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -07001047 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -07001048 }
Santos Cordonb6939982014-06-04 20:20:58 -07001049
1050 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001051 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -07001052 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001053 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001054 Connection.capabilitiesToString(capabilities));
1055 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -07001056 }
1057
Santos Cordonb6939982014-06-04 20:20:58 -07001058 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -07001059 public void onConnectionPropertiesChanged(Connection c, int properties) {
1060 String id = mIdByConnection.get(c);
1061 Log.d(this, "properties: parcelableconnection: %s",
1062 Connection.propertiesToString(properties));
1063 mAdapter.setConnectionProperties(id, properties);
1064 }
1065
1066 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001067 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001068 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -07001069 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
1070 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001071 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001072 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001073
1074 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001075 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001076 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001077 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001078 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001079
1080 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001081 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001082 String id = mIdByConnection.get(c);
1083 mAdapter.setStatusHints(id, statusHints);
1084 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -07001085
1086 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001087 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001088 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -07001089 mAdapter.setConferenceableConnections(
1090 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001091 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001092 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001093
1094 @Override
1095 public void onConferenceChanged(Connection connection, Conference conference) {
1096 String id = mIdByConnection.get(connection);
1097 if (id != null) {
1098 String conferenceId = null;
1099 if (conference != null) {
1100 conferenceId = mIdByConference.get(conference);
1101 }
1102 mAdapter.setIsConferenced(id, conferenceId);
1103 }
1104 }
Anthony Lee17455a32015-04-24 15:25:29 -07001105
1106 @Override
1107 public void onConferenceMergeFailed(Connection connection) {
1108 String id = mIdByConnection.get(connection);
1109 if (id != null) {
1110 mAdapter.onConferenceMergeFailed(id);
1111 }
1112 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001113
1114 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001115 public void onExtrasChanged(Connection c, Bundle extras) {
1116 String id = mIdByConnection.get(c);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001117 if (id != null) {
Tyler Gunndee56a82016-03-23 16:06:34 -07001118 mAdapter.putExtras(id, extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001119 }
1120 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001121
Tyler Gunnf5035432017-01-09 09:43:12 -08001122 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001123 public void onExtrasRemoved(Connection c, List<String> keys) {
1124 String id = mIdByConnection.get(c);
1125 if (id != null) {
1126 mAdapter.removeExtras(id, keys);
1127 }
1128 }
1129
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001130 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001131 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001132 String id = mIdByConnection.get(connection);
1133 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001134 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001135 }
1136 }
Tyler Gunnf5035432017-01-09 09:43:12 -08001137
1138 @Override
1139 public void onAudioRouteChanged(Connection c, int audioRoute) {
1140 String id = mIdByConnection.get(c);
1141 if (id != null) {
1142 mAdapter.setAudioRoute(id, audioRoute);
1143 }
1144 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001145 };
1146
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001147 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -07001148 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001149 public final IBinder onBind(Intent intent) {
1150 return mBinder;
1151 }
1152
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001153 /** {@inheritDoc} */
1154 @Override
1155 public boolean onUnbind(Intent intent) {
1156 endAllConnections();
1157 return super.onUnbind(intent);
1158 }
1159
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001160 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001161 * This can be used by telecom to either create a new outgoing call or attach to an existing
1162 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001163 * createConnection util a connection service cancels the process or completes it successfully.
1164 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001165 private void createConnection(
1166 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001167 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -07001168 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -07001169 boolean isIncoming,
1170 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001171 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001172 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
1173 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -07001174 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001175
Yorke Leec3cf9822014-10-02 09:38:39 -07001176 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
1177 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -07001178 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001179 Log.d(this, "createConnection, connection: %s", connection);
1180 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001181 connection = Connection.createFailedConnection(
1182 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001183 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001184
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001185 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001186 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001187 addConnection(callId, connection);
1188 }
1189
Andrew Lee100e2932014-09-08 15:34:24 -07001190 Uri address = connection.getAddress();
1191 String number = address == null ? "null" : address.getSchemeSpecificPart();
Tyler Gunn720c6642016-03-22 09:02:47 -07001192 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s, properties: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001193 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001194 Connection.stateToString(connection.getState()),
Tyler Gunn720c6642016-03-22 09:02:47 -07001195 Connection.capabilitiesToString(connection.getConnectionCapabilities()),
1196 Connection.propertiesToString(connection.getConnectionProperties()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001197
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001198 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -07001199 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001200 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -07001201 request,
1202 new ParcelableConnection(
1203 request.getAccountHandle(),
1204 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001205 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001206 connection.getConnectionProperties(),
Christine Hallstrom2830ce92016-11-30 16:06:42 -08001207 connection.getSupportedAudioRoutes(),
Andrew Lee100e2932014-09-08 15:34:24 -07001208 connection.getAddress(),
1209 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -07001210 connection.getCallerDisplayName(),
1211 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001212 connection.getVideoProvider() == null ?
1213 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001214 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -07001215 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001216 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001217 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -07001218 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001219 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001220 createIdList(connection.getConferenceables()),
1221 connection.getExtras()));
Tyler Gunnf5035432017-01-09 09:43:12 -08001222
1223 if (isIncoming && request.shouldShowIncomingCallUi() &&
1224 (connection.getConnectionProperties() & Connection.PROPERTY_SELF_MANAGED) ==
1225 Connection.PROPERTY_SELF_MANAGED) {
1226 // Tell ConnectionService to show its incoming call UX.
1227 connection.onShowIncomingCallUi();
1228 }
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001229 if (isUnknown) {
1230 triggerConferenceRecalculate();
1231 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001232 }
1233
Tyler Gunn159f35c2017-03-02 09:28:37 -08001234 private void createConnectionFailed(final PhoneAccountHandle callManagerAccount,
1235 final String callId, final ConnectionRequest request,
1236 boolean isIncoming) {
Tyler Gunn44e01912017-01-31 10:49:05 -08001237
1238 Log.i(this, "createConnectionFailed %s", callId);
1239 if (isIncoming) {
Tyler Gunn159f35c2017-03-02 09:28:37 -08001240 onCreateIncomingConnectionFailed(callManagerAccount, request);
Tyler Gunn44e01912017-01-31 10:49:05 -08001241 } else {
Tyler Gunn159f35c2017-03-02 09:28:37 -08001242 onCreateOutgoingConnectionFailed(callManagerAccount, request);
Tyler Gunn44e01912017-01-31 10:49:05 -08001243 }
1244 }
1245
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001246 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001247 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001248 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001249 }
1250
Tyler Gunnbe74de02014-08-29 14:51:48 -07001251 private void answerVideo(String callId, int videoState) {
1252 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001253 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001254 }
1255
Tyler Gunnbe74de02014-08-29 14:51:48 -07001256 private void answer(String callId) {
1257 Log.d(this, "answer %s", callId);
1258 findConnectionForAction(callId, "answer").onAnswer();
1259 }
1260
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001261 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001262 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001263 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001264 }
1265
Bryce Lee81901682015-08-28 16:38:02 -07001266 private void reject(String callId, String rejectWithMessage) {
1267 Log.d(this, "reject %s with message", callId);
1268 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
1269 }
1270
Bryce Leecac50772015-11-17 15:13:29 -08001271 private void silence(String callId) {
1272 Log.d(this, "silence %s", callId);
1273 findConnectionForAction(callId, "silence").onSilence();
1274 }
1275
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001276 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001277 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001278 if (mConnectionById.containsKey(callId)) {
1279 findConnectionForAction(callId, "disconnect").onDisconnect();
1280 } else {
1281 findConferenceForAction(callId, "disconnect").onDisconnect();
1282 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001283 }
1284
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001285 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001286 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001287 if (mConnectionById.containsKey(callId)) {
1288 findConnectionForAction(callId, "hold").onHold();
1289 } else {
1290 findConferenceForAction(callId, "hold").onHold();
1291 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001292 }
1293
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001294 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001295 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001296 if (mConnectionById.containsKey(callId)) {
1297 findConnectionForAction(callId, "unhold").onUnhold();
1298 } else {
1299 findConferenceForAction(callId, "unhold").onUnhold();
1300 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001301 }
1302
Yorke Lee4af59352015-05-13 14:14:54 -07001303 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
1304 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001305 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -07001306 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1307 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001308 } else {
Yorke Lee4af59352015-05-13 14:14:54 -07001309 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1310 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001311 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001312 }
1313
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001314 private void playDtmfTone(String callId, char digit) {
1315 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001316 if (mConnectionById.containsKey(callId)) {
1317 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1318 } else {
1319 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1320 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001321 }
1322
1323 private void stopDtmfTone(String callId) {
1324 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001325 if (mConnectionById.containsKey(callId)) {
1326 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
1327 } else {
1328 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
1329 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001330 }
1331
Santos Cordon823fd3c2014-08-07 18:35:18 -07001332 private void conference(String callId1, String callId2) {
1333 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -07001334
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001335 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001336 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001337 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001338 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001339 conference2 = findConferenceForAction(callId2, "conference");
1340 if (conference2 == getNullConference()) {
1341 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
1342 callId2);
1343 return;
1344 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001345 }
Santos Cordonb6939982014-06-04 20:20:58 -07001346
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001347 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -07001348 Connection connection1 = findConnectionForAction(callId1, "conference");
1349 if (connection1 == getNullConnection()) {
1350 Conference conference1 = findConferenceForAction(callId1, "addConnection");
1351 if (conference1 == getNullConference()) {
1352 Log.w(this,
1353 "Connection1 or Conference1 missing in conference request %s.",
1354 callId1);
1355 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001356 // Call 1 is a conference.
1357 if (connection2 != getNullConnection()) {
1358 // Call 2 is a connection so merge via call 1 (conference).
1359 conference1.onMerge(connection2);
1360 } else {
1361 // Call 2 is ALSO a conference; this should never happen.
1362 Log.wtf(this, "There can only be one conference and an attempt was made to " +
1363 "merge two conferences.");
1364 return;
1365 }
Ihab Awad50e35062014-09-30 09:17:03 -07001366 }
1367 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001368 // Call 1 is a connection.
1369 if (conference2 != getNullConference()) {
1370 // Call 2 is a conference, so merge via call 2.
1371 conference2.onMerge(connection1);
1372 } else {
1373 // Call 2 is a connection, so merge together.
1374 onConference(connection1, connection2);
1375 }
Ihab Awad50e35062014-09-30 09:17:03 -07001376 }
Santos Cordon980acb92014-05-31 10:31:19 -07001377 }
1378
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001379 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -07001380 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -07001381
1382 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001383 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -07001384 Log.w(this, "Connection missing in conference request %s.", callId);
1385 return;
1386 }
1387
Santos Cordon0159ac02014-08-21 14:28:11 -07001388 Conference conference = connection.getConference();
1389 if (conference != null) {
1390 conference.onSeparate(connection);
1391 }
Santos Cordon980acb92014-05-31 10:31:19 -07001392 }
1393
Santos Cordona4868042014-09-04 17:39:22 -07001394 private void mergeConference(String callId) {
1395 Log.d(this, "mergeConference(%s)", callId);
1396 Conference conference = findConferenceForAction(callId, "mergeConference");
1397 if (conference != null) {
1398 conference.onMerge();
1399 }
1400 }
1401
1402 private void swapConference(String callId) {
1403 Log.d(this, "swapConference(%s)", callId);
1404 Conference conference = findConferenceForAction(callId, "swapConference");
1405 if (conference != null) {
1406 conference.onSwap();
1407 }
1408 }
1409
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001410 /**
1411 * Notifies a {@link Connection} of a request to pull an external call.
1412 *
1413 * See {@link Call#pullExternalCall()}.
1414 *
1415 * @param callId The ID of the call to pull.
1416 */
1417 private void pullExternalCall(String callId) {
1418 Log.d(this, "pullExternalCall(%s)", callId);
1419 Connection connection = findConnectionForAction(callId, "pullExternalCall");
1420 if (connection != null) {
1421 connection.onPullExternalCall();
1422 }
1423 }
1424
1425 /**
1426 * Notifies a {@link Connection} of a call event.
1427 *
1428 * See {@link Call#sendCallEvent(String, Bundle)}.
1429 *
1430 * @param callId The ID of the call receiving the event.
1431 * @param event The event.
1432 * @param extras Extras associated with the event.
1433 */
1434 private void sendCallEvent(String callId, String event, Bundle extras) {
1435 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
1436 Connection connection = findConnectionForAction(callId, "sendCallEvent");
1437 if (connection != null) {
1438 connection.onCallEvent(event, extras);
1439 }
1440
1441 }
1442
Tyler Gunndee56a82016-03-23 16:06:34 -07001443 /**
1444 * Notifies a {@link Connection} or {@link Conference} of a change to the extras from Telecom.
1445 * <p>
1446 * These extra changes can originate from Telecom itself, or from an {@link InCallService} via
1447 * the {@link android.telecom.Call#putExtra(String, boolean)},
1448 * {@link android.telecom.Call#putExtra(String, int)},
1449 * {@link android.telecom.Call#putExtra(String, String)},
1450 * {@link Call#removeExtras(List)}.
1451 *
1452 * @param callId The ID of the call receiving the event.
1453 * @param extras The new extras bundle.
1454 */
1455 private void handleExtrasChanged(String callId, Bundle extras) {
1456 Log.d(this, "handleExtrasChanged(%s, %s)", callId, extras);
1457 if (mConnectionById.containsKey(callId)) {
1458 findConnectionForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1459 } else if (mConferenceById.containsKey(callId)) {
1460 findConferenceForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1461 }
1462 }
1463
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001464 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001465 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001466 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001467 }
1468
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001469 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001470 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001471 // No need to query again if we already did it.
1472 return;
1473 }
1474
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001475 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -07001476 @Override
1477 public void onResult(
1478 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001479 final List<IBinder> services) {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001480 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oR", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001481 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001482 public void loggedRun() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001483 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001484 mRemoteConnectionManager.addConnectionService(
1485 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001486 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -07001487 }
Ihab Awad5d0410f2014-07-30 10:07:40 -07001488 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001489 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -07001490 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001491 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001492 }
1493
1494 @Override
1495 public void onError() {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001496 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oE", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001497 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001498 public void loggedRun() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001499 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -07001500 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001501 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001502 }
1503 });
1504 }
1505
Ihab Awadf8b69882014-07-25 15:14:01 -07001506 /**
1507 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001508 * incoming request. This is used by {@code ConnectionService}s that are registered with
1509 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
1510 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001511 *
1512 * @param connectionManagerPhoneAccount See description at
1513 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1514 * @param request Details about the incoming call.
1515 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1516 * not handle the call.
1517 */
1518 public final RemoteConnection createRemoteIncomingConnection(
1519 PhoneAccountHandle connectionManagerPhoneAccount,
1520 ConnectionRequest request) {
1521 return mRemoteConnectionManager.createRemoteConnection(
1522 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -07001523 }
1524
1525 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001526 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001527 * outgoing request. This is used by {@code ConnectionService}s that are registered with
1528 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
1529 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001530 *
1531 * @param connectionManagerPhoneAccount See description at
1532 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Cuihtlauac ALVARADO0b3b2a52016-09-13 14:49:41 +02001533 * @param request Details about the outgoing call.
Ihab Awadf8b69882014-07-25 15:14:01 -07001534 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1535 * not handle the call.
1536 */
1537 public final RemoteConnection createRemoteOutgoingConnection(
1538 PhoneAccountHandle connectionManagerPhoneAccount,
1539 ConnectionRequest request) {
1540 return mRemoteConnectionManager.createRemoteConnection(
1541 connectionManagerPhoneAccount, request, false);
1542 }
1543
1544 /**
Santos Cordona663f862014-10-29 13:49:58 -07001545 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1546 * {@link RemoteConnection}s should be merged into a conference call.
1547 * <p>
1548 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1549 * be invoked.
1550 *
1551 * @param remoteConnection1 The first of the remote connections to conference.
1552 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001553 */
1554 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001555 RemoteConnection remoteConnection1,
1556 RemoteConnection remoteConnection2) {
1557 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001558 }
1559
1560 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001561 * Adds a new conference call. When a conference call is created either as a result of an
1562 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1563 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1564 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1565 *
1566 * @param conference The new conference object.
1567 */
1568 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001569 Log.d(this, "addConference: conference=%s", conference);
1570
Santos Cordon823fd3c2014-08-07 18:35:18 -07001571 String id = addConferenceInternal(conference);
1572 if (id != null) {
1573 List<String> connectionIds = new ArrayList<>(2);
1574 for (Connection connection : conference.getConnections()) {
1575 if (mIdByConnection.containsKey(connection)) {
1576 connectionIds.add(mIdByConnection.get(connection));
1577 }
1578 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001579 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001580 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001581 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001582 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001583 conference.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001584 conference.getConnectionProperties(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001585 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001586 conference.getVideoProvider() == null ?
1587 null : conference.getVideoProvider().getInterface(),
1588 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001589 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001590 conference.getStatusHints(),
1591 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001592
Santos Cordon823fd3c2014-08-07 18:35:18 -07001593 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001594 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1595 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001596
1597 // Go through any child calls and set the parent.
1598 for (Connection connection : conference.getConnections()) {
1599 String connectionId = mIdByConnection.get(connection);
1600 if (connectionId != null) {
1601 mAdapter.setIsConferenced(connectionId, id);
1602 }
1603 }
1604 }
1605 }
1606
1607 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001608 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1609 * connection.
1610 *
1611 * @param phoneAccountHandle The phone account handle for the connection.
1612 * @param connection The connection to add.
1613 */
1614 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1615 Connection connection) {
1616
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001617 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001618 if (id != null) {
1619 List<String> emptyList = new ArrayList<>(0);
1620
1621 ParcelableConnection parcelableConnection = new ParcelableConnection(
1622 phoneAccountHandle,
1623 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001624 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001625 connection.getConnectionProperties(),
Christine Hallstrom2830ce92016-11-30 16:06:42 -08001626 connection.getSupportedAudioRoutes(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001627 connection.getAddress(),
1628 connection.getAddressPresentation(),
1629 connection.getCallerDisplayName(),
1630 connection.getCallerDisplayNamePresentation(),
1631 connection.getVideoProvider() == null ?
1632 null : connection.getVideoProvider().getInterface(),
1633 connection.getVideoState(),
1634 connection.isRingbackRequested(),
1635 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001636 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001637 connection.getStatusHints(),
1638 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001639 emptyList,
1640 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001641 mAdapter.addExistingConnection(id, parcelableConnection);
1642 }
1643 }
1644
1645 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001646 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1647 * has taken responsibility.
1648 *
1649 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001650 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001651 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001652 return mConnectionById.values();
1653 }
1654
1655 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001656 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1657 * has taken responsibility.
1658 *
1659 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1660 */
1661 public final Collection<Conference> getAllConferences() {
1662 return mConferenceById.values();
1663 }
1664
1665 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001666 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1667 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001668 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001669 * @param connectionManagerPhoneAccount See description at
1670 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1671 * @param request Details about the incoming call.
1672 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1673 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001674 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001675 public Connection onCreateIncomingConnection(
1676 PhoneAccountHandle connectionManagerPhoneAccount,
1677 ConnectionRequest request) {
1678 return null;
1679 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001680
1681 /**
Tyler Gunnf5035432017-01-09 09:43:12 -08001682 * Called by Telecom to inform the {@link ConnectionService} that its request to create a new
1683 * incoming {@link Connection} was denied.
1684 * <p>
1685 * Used when a self-managed {@link ConnectionService} attempts to create a new incoming
1686 * {@link Connection}, but Telecom has determined that the call cannot be allowed at this time.
1687 * The {@link ConnectionService} is responsible for silently rejecting the new incoming
1688 * {@link Connection}.
1689 * <p>
1690 * See {@link TelecomManager#isIncomingCallPermitted(PhoneAccountHandle)} for more information.
1691 *
Tyler Gunn159f35c2017-03-02 09:28:37 -08001692 * @param connectionManagerPhoneAccount See description at
1693 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Tyler Gunnf5035432017-01-09 09:43:12 -08001694 * @param request The incoming connection request.
1695 */
Tyler Gunn159f35c2017-03-02 09:28:37 -08001696 public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
1697 ConnectionRequest request) {
Tyler Gunnf5035432017-01-09 09:43:12 -08001698 }
1699
1700 /**
1701 * Called by Telecom to inform the {@link ConnectionService} that its request to create a new
1702 * outgoing {@link Connection} was denied.
1703 * <p>
1704 * Used when a self-managed {@link ConnectionService} attempts to create a new outgoing
1705 * {@link Connection}, but Telecom has determined that the call cannot be placed at this time.
1706 * The {@link ConnectionService} is responisible for informing the user that the
1707 * {@link Connection} cannot be made at this time.
1708 * <p>
1709 * See {@link TelecomManager#isOutgoingCallPermitted(PhoneAccountHandle)} for more information.
1710 *
Tyler Gunn159f35c2017-03-02 09:28:37 -08001711 * @param connectionManagerPhoneAccount See description at
1712 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Tyler Gunnf5035432017-01-09 09:43:12 -08001713 * @param request The outgoing connection request.
1714 */
Tyler Gunn159f35c2017-03-02 09:28:37 -08001715 public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
1716 ConnectionRequest request) {
Tyler Gunnf5035432017-01-09 09:43:12 -08001717 }
1718
1719 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001720 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1721 * Connection is part of a conference controller but is not yet added to Connection
1722 * Service and hence cannot be added to the conference call.
1723 *
1724 * @hide
1725 */
1726 public void triggerConferenceRecalculate() {
1727 }
1728
1729 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001730 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1731 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001732 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001733 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1734 * this call.
1735 * <p>
1736 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1737 * has registered one or more {@code PhoneAccount}s having
1738 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1739 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1740 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1741 * making the connection.
1742 * <p>
1743 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1744 * being asked to make a direct connection. The
1745 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1746 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1747 * making the connection.
1748 * @param request Details about the outgoing call.
1749 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001750 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001751 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001752 public Connection onCreateOutgoingConnection(
1753 PhoneAccountHandle connectionManagerPhoneAccount,
1754 ConnectionRequest request) {
1755 return null;
1756 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001757
1758 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001759 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1760 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1761 * call created using
1762 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1763 *
Yorke Lee770ed6e2014-10-06 18:58:52 -07001764 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001765 */
1766 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1767 ConnectionRequest request) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001768 return null;
Yorke Leec3cf9822014-10-02 09:38:39 -07001769 }
1770
1771 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001772 * Conference two specified connections. Invoked when the user has made a request to merge the
1773 * specified connections into a conference call. In response, the connection service should
1774 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001775 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001776 * @param connection1 A connection to merge into a conference call.
1777 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001778 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001779 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001780
Santos Cordona663f862014-10-29 13:49:58 -07001781 /**
1782 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1783 * When this method is invoked, this {@link ConnectionService} should create its own
1784 * representation of the conference call and send it to telecom using {@link #addConference}.
1785 * <p>
1786 * This is only relevant to {@link ConnectionService}s which are registered with
1787 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1788 *
1789 * @param conference The remote conference call.
1790 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001791 public void onRemoteConferenceAdded(RemoteConference conference) {}
1792
Santos Cordon823fd3c2014-08-07 18:35:18 -07001793 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001794 * Called when an existing connection is added remotely.
1795 * @param connection The existing connection which was added.
1796 */
1797 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1798
1799 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001800 * @hide
1801 */
1802 public boolean containsConference(Conference conference) {
1803 return mIdByConference.containsKey(conference);
1804 }
1805
Ihab Awadb8e85c72014-08-23 20:34:57 -07001806 /** {@hide} */
1807 void addRemoteConference(RemoteConference remoteConference) {
1808 onRemoteConferenceAdded(remoteConference);
1809 }
1810
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001811 /** {@hide} */
1812 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1813 onRemoteExistingConnectionAdded(remoteConnection);
1814 }
1815
Ihab Awad5d0410f2014-07-30 10:07:40 -07001816 private void onAccountsInitialized() {
1817 mAreAccountsInitialized = true;
1818 for (Runnable r : mPreInitializationConnectionRequests) {
1819 r.run();
1820 }
1821 mPreInitializationConnectionRequests.clear();
1822 }
1823
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001824 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001825 * Adds an existing connection to the list of connections, identified by a new call ID unique
1826 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001827 *
1828 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001829 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001830 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001831 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1832 String id;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07001833
1834 if (connection.getExtras() != null && connection.getExtras()
1835 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1836 id = connection.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1837 Log.d(this, "addExistingConnectionInternal - conn %s reusing original id %s",
1838 connection.getTelecomCallId(), id);
1839 } else if (handle == null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001840 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1841 // so just use a random UUID.
1842 id = UUID.randomUUID().toString();
1843 } else {
1844 // Phone account handle was provided, so use the ConnectionService class name as a
1845 // prefix for a unique incremental call ID.
1846 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1847 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001848 addConnection(id, connection);
1849 return id;
1850 }
1851
Ihab Awad542e0ea2014-05-16 10:22:16 -07001852 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001853 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001854 mConnectionById.put(callId, connection);
1855 mIdByConnection.put(connection, callId);
1856 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001857 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001858 }
1859
Anthony Lee30e65842014-11-06 16:30:53 -08001860 /** {@hide} */
1861 protected void removeConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001862 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001863 connection.removeConnectionListener(mConnectionListener);
Chenjie Luoe370b532016-05-12 16:59:43 -07001864 String id = mIdByConnection.get(connection);
1865 if (id != null) {
1866 mConnectionById.remove(id);
1867 mIdByConnection.remove(connection);
1868 mAdapter.removeCall(id);
1869 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001870 }
1871
Santos Cordon823fd3c2014-08-07 18:35:18 -07001872 private String addConferenceInternal(Conference conference) {
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07001873 String originalId = null;
1874 if (conference.getExtras() != null && conference.getExtras()
1875 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1876 originalId = conference.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1877 Log.d(this, "addConferenceInternal: conf %s reusing original id %s",
1878 conference.getTelecomCallId(),
1879 originalId);
1880 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001881 if (mIdByConference.containsKey(conference)) {
1882 Log.w(this, "Re-adding an existing conference: %s.", conference);
1883 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001884 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1885 // cannot determine a ConnectionService class name to associate with the ID, so use
1886 // a unique UUID (for now).
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07001887 String id = originalId == null ? UUID.randomUUID().toString() : originalId;
Santos Cordon823fd3c2014-08-07 18:35:18 -07001888 mConferenceById.put(id, conference);
1889 mIdByConference.put(conference, id);
1890 conference.addListener(mConferenceListener);
1891 return id;
1892 }
1893
1894 return null;
1895 }
1896
1897 private void removeConference(Conference conference) {
1898 if (mIdByConference.containsKey(conference)) {
1899 conference.removeListener(mConferenceListener);
1900
1901 String id = mIdByConference.get(conference);
1902 mConferenceById.remove(id);
1903 mIdByConference.remove(conference);
1904 mAdapter.removeCall(id);
1905 }
1906 }
1907
Ihab Awad542e0ea2014-05-16 10:22:16 -07001908 private Connection findConnectionForAction(String callId, String action) {
1909 if (mConnectionById.containsKey(callId)) {
1910 return mConnectionById.get(callId);
1911 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001912 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001913 return getNullConnection();
1914 }
1915
1916 static synchronized Connection getNullConnection() {
1917 if (sNullConnection == null) {
1918 sNullConnection = new Connection() {};
1919 }
1920 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001921 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001922
1923 private Conference findConferenceForAction(String conferenceId, String action) {
1924 if (mConferenceById.containsKey(conferenceId)) {
1925 return mConferenceById.get(conferenceId);
1926 }
1927 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1928 return getNullConference();
1929 }
1930
Ihab Awadb8e85c72014-08-23 20:34:57 -07001931 private List<String> createConnectionIdList(List<Connection> connections) {
1932 List<String> ids = new ArrayList<>();
1933 for (Connection c : connections) {
1934 if (mIdByConnection.containsKey(c)) {
1935 ids.add(mIdByConnection.get(c));
1936 }
1937 }
1938 Collections.sort(ids);
1939 return ids;
1940 }
1941
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001942 /**
1943 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001944 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001945 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001946 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001947 * @return List of string conference and call Ids.
1948 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001949 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001950 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001951 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001952 // Only allow Connection and Conference conferenceables.
1953 if (c instanceof Connection) {
1954 Connection connection = (Connection) c;
1955 if (mIdByConnection.containsKey(connection)) {
1956 ids.add(mIdByConnection.get(connection));
1957 }
1958 } else if (c instanceof Conference) {
1959 Conference conference = (Conference) c;
1960 if (mIdByConference.containsKey(conference)) {
1961 ids.add(mIdByConference.get(conference));
1962 }
1963 }
1964 }
1965 Collections.sort(ids);
1966 return ids;
1967 }
1968
Santos Cordon0159ac02014-08-21 14:28:11 -07001969 private Conference getNullConference() {
1970 if (sNullConference == null) {
1971 sNullConference = new Conference(null) {};
1972 }
1973 return sNullConference;
1974 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001975
1976 private void endAllConnections() {
1977 // Unbound from telecomm. We should end all connections and conferences.
1978 for (Connection connection : mIdByConnection.keySet()) {
1979 // only operate on top-level calls. Conference calls will be removed on their own.
1980 if (connection.getConference() == null) {
1981 connection.onDisconnect();
1982 }
1983 }
1984 for (Conference conference : mIdByConference.keySet()) {
1985 conference.onDisconnect();
1986 }
1987 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001988
1989 /**
1990 * Retrieves the next call ID as maintainted by the connection service.
1991 *
1992 * @return The call ID.
1993 */
1994 private int getNextCallId() {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001995 synchronized (mIdSyncRoot) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001996 return ++mId;
1997 }
1998 }
Santos Cordon980acb92014-05-31 10:31:19 -07001999}