blob: 4fd602f7a856b01f147848110ee02c2b6b091a3e [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;
Sanket Padawe02d8c272017-12-20 10:13:05 -080024import android.os.Build;
Santos Cordon6b7f9552015-05-27 17:21:45 -070025import android.os.Bundle;
Santos Cordon52d8a152014-06-17 19:08:45 -070026import android.os.Handler;
27import android.os.IBinder;
28import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070029import android.os.Message;
Hall Liub64ac4c2017-02-06 10:49:48 -080030import android.os.ParcelFileDescriptor;
31import android.os.RemoteException;
Brad Ebingerb32d4f82016-10-24 16:40:49 -070032import android.telecom.Logging.Session;
Andrew Lee14185762014-07-25 09:41:56 -070033
Sailesh Nepal2a46b902014-07-04 17:21:07 -070034import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070035import com.android.internal.telecom.IConnectionService;
36import com.android.internal.telecom.IConnectionServiceAdapter;
37import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070038
Ihab Awad5d0410f2014-07-30 10:07:40 -070039import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070040import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070041import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070042import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070043import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070044import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070045import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070046
47/**
Tyler Gunnf5035432017-01-09 09:43:12 -080048 * An abstract service that should be implemented by any apps which either:
49 * <ol>
50 * <li>Can make phone calls (VoIP or otherwise) and want those calls to be integrated into the
51 * built-in phone app. Referred to as a <b>system managed</b> {@link ConnectionService}.</li>
52 * <li>Are a standalone calling app and don't want their calls to be integrated into the
53 * built-in phone app. Referred to as a <b>self managed</b> {@link ConnectionService}.</li>
54 * </ol>
55 * Once implemented, the {@link ConnectionService} needs to take the following steps so that Telecom
56 * will bind to it:
Santos Cordona663f862014-10-29 13:49:58 -070057 * <p>
58 * 1. <i>Registration in AndroidManifest.xml</i>
59 * <br/>
60 * <pre>
61 * &lt;service android:name="com.example.package.MyConnectionService"
62 * android:label="@string/some_label_for_my_connection_service"
Yorke Lee249c12e2015-05-13 15:59:29 -070063 * android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"&gt;
Santos Cordona663f862014-10-29 13:49:58 -070064 * &lt;intent-filter&gt;
65 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
66 * &lt;/intent-filter&gt;
67 * &lt;/service&gt;
68 * </pre>
69 * <p>
70 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
71 * <br/>
72 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
73 * <p>
Tyler Gunnf5035432017-01-09 09:43:12 -080074 * System managed {@link ConnectionService}s must be enabled by the user in the phone app settings
75 * before Telecom will bind to them. Self-manged {@link ConnectionService}s must be granted the
76 * appropriate permission before Telecom will bind to them.
77 * <p>
78 * Once registered and enabled by the user in the phone app settings or granted permission, telecom
79 * will bind to a {@link ConnectionService} implementation when it wants that
80 * {@link ConnectionService} to place a call or the service has indicated that is has an incoming
81 * call through {@link TelecomManager#addNewIncomingCall}. The {@link ConnectionService} can then
82 * expect a call to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection}
83 * wherein it should provide a new instance of a {@link Connection} object. It is through this
84 * {@link Connection} object that telecom receives state updates and the {@link ConnectionService}
Santos Cordona663f862014-10-29 13:49:58 -070085 * receives call-commands such as answer, reject, hold and disconnect.
86 * <p>
Tyler Gunnf5035432017-01-09 09:43:12 -080087 * When there are no more live calls, telecom will unbind from the {@link ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070088 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070089public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070090 /**
91 * The {@link Intent} that must be declared as handled by the service.
92 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070093 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070094 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070095
Tyler Gunn8bf76572017-04-06 15:30:08 -070096 /**
97 * Boolean extra used by Telecom to inform a {@link ConnectionService} that the purpose of it
98 * being asked to create a new outgoing {@link Connection} is to perform a handover of an
99 * ongoing call on the device from another {@link PhoneAccount}/{@link ConnectionService}. Will
100 * be specified in the {@link ConnectionRequest#getExtras()} passed by Telecom when
101 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)} is called.
102 * <p>
Tyler Gunn727c6bd2017-04-11 09:51:40 -0700103 * When your {@link ConnectionService} receives this extra, it should communicate the fact that
104 * this is a handover to the other device's matching {@link ConnectionService}. That
Tyler Gunn8bf76572017-04-06 15:30:08 -0700105 * {@link ConnectionService} will continue the handover using
106 * {@link TelecomManager#addNewIncomingCall(PhoneAccountHandle, Bundle)}, specifying
Tyler Gunn727c6bd2017-04-11 09:51:40 -0700107 * {@link TelecomManager#EXTRA_IS_HANDOVER}. Telecom will match the phone numbers of the
108 * handover call on the other device with ongoing calls for {@link ConnectionService}s which
109 * support {@link PhoneAccount#EXTRA_SUPPORTS_HANDOVER_FROM}.
Tyler Gunn8bf76572017-04-06 15:30:08 -0700110 * @hide
111 */
112 public static final String EXTRA_IS_HANDOVER = TelecomManager.EXTRA_IS_HANDOVER;
113
Ihab Awad542e0ea2014-05-16 10:22:16 -0700114 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -0700115 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700116
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700117 // Session Definitions
118 private static final String SESSION_HANDLER = "H.";
119 private static final String SESSION_ADD_CS_ADAPTER = "CS.aCSA";
120 private static final String SESSION_REMOVE_CS_ADAPTER = "CS.rCSA";
121 private static final String SESSION_CREATE_CONN = "CS.crCo";
Tyler Gunn041a1fe2017-05-12 10:04:49 -0700122 private static final String SESSION_CREATE_CONN_COMPLETE = "CS.crCoC";
Tyler Gunn44e01912017-01-31 10:49:05 -0800123 private static final String SESSION_CREATE_CONN_FAILED = "CS.crCoF";
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700124 private static final String SESSION_ABORT = "CS.ab";
125 private static final String SESSION_ANSWER = "CS.an";
126 private static final String SESSION_ANSWER_VIDEO = "CS.anV";
127 private static final String SESSION_REJECT = "CS.r";
128 private static final String SESSION_REJECT_MESSAGE = "CS.rWM";
129 private static final String SESSION_SILENCE = "CS.s";
130 private static final String SESSION_DISCONNECT = "CS.d";
131 private static final String SESSION_HOLD = "CS.h";
132 private static final String SESSION_UNHOLD = "CS.u";
133 private static final String SESSION_CALL_AUDIO_SC = "CS.cASC";
134 private static final String SESSION_PLAY_DTMF = "CS.pDT";
135 private static final String SESSION_STOP_DTMF = "CS.sDT";
136 private static final String SESSION_CONFERENCE = "CS.c";
137 private static final String SESSION_SPLIT_CONFERENCE = "CS.sFC";
138 private static final String SESSION_MERGE_CONFERENCE = "CS.mC";
139 private static final String SESSION_SWAP_CONFERENCE = "CS.sC";
140 private static final String SESSION_POST_DIAL_CONT = "CS.oPDC";
141 private static final String SESSION_PULL_EXTERNAL_CALL = "CS.pEC";
142 private static final String SESSION_SEND_CALL_EVENT = "CS.sCE";
143 private static final String SESSION_EXTRAS_CHANGED = "CS.oEC";
Hall Liub64ac4c2017-02-06 10:49:48 -0800144 private static final String SESSION_START_RTT = "CS.+RTT";
145 private static final String SESSION_STOP_RTT = "CS.-RTT";
146 private static final String SESSION_RTT_UPGRADE_RESPONSE = "CS.rTRUR";
Sanket Padawe02d8c272017-12-20 10:13:05 -0800147 private static final String SESSION_HANDOVER_FAILED = "CS.haF";
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700148
Ihab Awad8aecfed2014-08-08 17:06:11 -0700149 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700150 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700151 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700152 private static final int MSG_ANSWER = 4;
153 private static final int MSG_REJECT = 5;
154 private static final int MSG_DISCONNECT = 6;
155 private static final int MSG_HOLD = 7;
156 private static final int MSG_UNHOLD = 8;
Yorke Lee4af59352015-05-13 14:14:54 -0700157 private static final int MSG_ON_CALL_AUDIO_STATE_CHANGED = 9;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700158 private static final int MSG_PLAY_DTMF_TONE = 10;
159 private static final int MSG_STOP_DTMF_TONE = 11;
160 private static final int MSG_CONFERENCE = 12;
161 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700162 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700163 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700164 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700165 private static final int MSG_MERGE_CONFERENCE = 18;
166 private static final int MSG_SWAP_CONFERENCE = 19;
Bryce Lee81901682015-08-28 16:38:02 -0700167 private static final int MSG_REJECT_WITH_MESSAGE = 20;
Bryce Leecac50772015-11-17 15:13:29 -0800168 private static final int MSG_SILENCE = 21;
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700169 private static final int MSG_PULL_EXTERNAL_CALL = 22;
170 private static final int MSG_SEND_CALL_EVENT = 23;
Tyler Gunndee56a82016-03-23 16:06:34 -0700171 private static final int MSG_ON_EXTRAS_CHANGED = 24;
Tyler Gunn44e01912017-01-31 10:49:05 -0800172 private static final int MSG_CREATE_CONNECTION_FAILED = 25;
Hall Liub64ac4c2017-02-06 10:49:48 -0800173 private static final int MSG_ON_START_RTT = 26;
174 private static final int MSG_ON_STOP_RTT = 27;
175 private static final int MSG_RTT_UPGRADE_RESPONSE = 28;
Tyler Gunn041a1fe2017-05-12 10:04:49 -0700176 private static final int MSG_CREATE_CONNECTION_COMPLETE = 29;
Sanket Padawe02d8c272017-12-20 10:13:05 -0800177 private static final int MSG_HANDOVER_FAILED = 32;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700178
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700179 private static Connection sNullConnection;
180
mike dooley95e80702014-09-18 14:07:52 -0700181 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
182 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
183 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
184 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700185 private final RemoteConnectionManager mRemoteConnectionManager =
186 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700187 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700188 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700189
Santos Cordon823fd3c2014-08-07 18:35:18 -0700190 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700191 private Conference sNullConference;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700192 private Object mIdSyncRoot = new Object();
193 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700194
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700195 private final IBinder mBinder = new IConnectionService.Stub() {
196 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700197 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter,
198 Session.Info sessionInfo) {
199 Log.startSession(sessionInfo, SESSION_ADD_CS_ADAPTER);
200 try {
201 SomeArgs args = SomeArgs.obtain();
202 args.arg1 = adapter;
203 args.arg2 = Log.createSubsession();
204 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
205 } finally {
206 Log.endSession();
207 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700208 }
209
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700210 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter,
211 Session.Info sessionInfo) {
212 Log.startSession(sessionInfo, SESSION_REMOVE_CS_ADAPTER);
213 try {
214 SomeArgs args = SomeArgs.obtain();
215 args.arg1 = adapter;
216 args.arg2 = Log.createSubsession();
217 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
218 } finally {
219 Log.endSession();
220 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700221 }
222
223 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700224 public void createConnection(
225 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700226 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700227 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700228 boolean isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700229 boolean isUnknown,
230 Session.Info sessionInfo) {
231 Log.startSession(sessionInfo, SESSION_CREATE_CONN);
232 try {
233 SomeArgs args = SomeArgs.obtain();
234 args.arg1 = connectionManagerPhoneAccount;
235 args.arg2 = id;
236 args.arg3 = request;
237 args.arg4 = Log.createSubsession();
238 args.argi1 = isIncoming ? 1 : 0;
239 args.argi2 = isUnknown ? 1 : 0;
240 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
241 } finally {
242 Log.endSession();
243 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700244 }
245
246 @Override
Tyler Gunn041a1fe2017-05-12 10:04:49 -0700247 public void createConnectionComplete(String id, Session.Info sessionInfo) {
248 Log.startSession(sessionInfo, SESSION_CREATE_CONN_COMPLETE);
249 try {
250 SomeArgs args = SomeArgs.obtain();
251 args.arg1 = id;
252 args.arg2 = Log.createSubsession();
253 mHandler.obtainMessage(MSG_CREATE_CONNECTION_COMPLETE, args).sendToTarget();
254 } finally {
255 Log.endSession();
256 }
257 }
258
259 @Override
Tyler Gunn44e01912017-01-31 10:49:05 -0800260 public void createConnectionFailed(
Tyler Gunn159f35c2017-03-02 09:28:37 -0800261 PhoneAccountHandle connectionManagerPhoneAccount,
Tyler Gunn44e01912017-01-31 10:49:05 -0800262 String callId,
263 ConnectionRequest request,
264 boolean isIncoming,
265 Session.Info sessionInfo) {
266 Log.startSession(sessionInfo, SESSION_CREATE_CONN_FAILED);
267 try {
268 SomeArgs args = SomeArgs.obtain();
269 args.arg1 = callId;
270 args.arg2 = request;
271 args.arg3 = Log.createSubsession();
Tyler Gunn159f35c2017-03-02 09:28:37 -0800272 args.arg4 = connectionManagerPhoneAccount;
Tyler Gunn44e01912017-01-31 10:49:05 -0800273 args.argi1 = isIncoming ? 1 : 0;
274 mHandler.obtainMessage(MSG_CREATE_CONNECTION_FAILED, args).sendToTarget();
275 } finally {
276 Log.endSession();
277 }
278 }
279
280 @Override
Sanket Padawe02d8c272017-12-20 10:13:05 -0800281 public void handoverFailed(String callId, ConnectionRequest request, int reason,
282 Session.Info sessionInfo) {
283 Log.startSession(sessionInfo, SESSION_HANDOVER_FAILED);
284 try {
285 SomeArgs args = SomeArgs.obtain();
286 args.arg1 = callId;
287 args.arg2 = request;
288 args.arg3 = Log.createSubsession();
289 args.arg4 = reason;
290 mHandler.obtainMessage(MSG_HANDOVER_FAILED, args).sendToTarget();
291 } finally {
292 Log.endSession();
293 }
294 }
295
296 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700297 public void abort(String callId, Session.Info sessionInfo) {
298 Log.startSession(sessionInfo, SESSION_ABORT);
299 try {
300 SomeArgs args = SomeArgs.obtain();
301 args.arg1 = callId;
302 args.arg2 = Log.createSubsession();
303 mHandler.obtainMessage(MSG_ABORT, args).sendToTarget();
304 } finally {
305 Log.endSession();
306 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700307 }
308
309 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700310 public void answerVideo(String callId, int videoState, Session.Info sessionInfo) {
311 Log.startSession(sessionInfo, SESSION_ANSWER_VIDEO);
312 try {
313 SomeArgs args = SomeArgs.obtain();
314 args.arg1 = callId;
315 args.arg2 = Log.createSubsession();
316 args.argi1 = videoState;
317 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
318 } finally {
319 Log.endSession();
320 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700321 }
322
323 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700324 public void answer(String callId, Session.Info sessionInfo) {
325 Log.startSession(sessionInfo, SESSION_ANSWER);
326 try {
327 SomeArgs args = SomeArgs.obtain();
328 args.arg1 = callId;
329 args.arg2 = Log.createSubsession();
330 mHandler.obtainMessage(MSG_ANSWER, args).sendToTarget();
331 } finally {
332 Log.endSession();
333 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700334 }
335
336 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700337 public void reject(String callId, Session.Info sessionInfo) {
338 Log.startSession(sessionInfo, SESSION_REJECT);
339 try {
340 SomeArgs args = SomeArgs.obtain();
341 args.arg1 = callId;
342 args.arg2 = Log.createSubsession();
343 mHandler.obtainMessage(MSG_REJECT, args).sendToTarget();
344 } finally {
345 Log.endSession();
346 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700347 }
348
349 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700350 public void rejectWithMessage(String callId, String message, Session.Info sessionInfo) {
351 Log.startSession(sessionInfo, SESSION_REJECT_MESSAGE);
352 try {
353 SomeArgs args = SomeArgs.obtain();
354 args.arg1 = callId;
355 args.arg2 = message;
356 args.arg3 = Log.createSubsession();
357 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
358 } finally {
359 Log.endSession();
360 }
Bryce Lee81901682015-08-28 16:38:02 -0700361 }
362
363 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700364 public void silence(String callId, Session.Info sessionInfo) {
365 Log.startSession(sessionInfo, SESSION_SILENCE);
366 try {
367 SomeArgs args = SomeArgs.obtain();
368 args.arg1 = callId;
369 args.arg2 = Log.createSubsession();
370 mHandler.obtainMessage(MSG_SILENCE, args).sendToTarget();
371 } finally {
372 Log.endSession();
373 }
Bryce Leecac50772015-11-17 15:13:29 -0800374 }
375
376 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700377 public void disconnect(String callId, Session.Info sessionInfo) {
378 Log.startSession(sessionInfo, SESSION_DISCONNECT);
379 try {
380 SomeArgs args = SomeArgs.obtain();
381 args.arg1 = callId;
382 args.arg2 = Log.createSubsession();
383 mHandler.obtainMessage(MSG_DISCONNECT, args).sendToTarget();
384 } finally {
385 Log.endSession();
386 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700387 }
388
389 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700390 public void hold(String callId, Session.Info sessionInfo) {
391 Log.startSession(sessionInfo, SESSION_HOLD);
392 try {
393 SomeArgs args = SomeArgs.obtain();
394 args.arg1 = callId;
395 args.arg2 = Log.createSubsession();
396 mHandler.obtainMessage(MSG_HOLD, args).sendToTarget();
397 } finally {
398 Log.endSession();
399 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700400 }
401
402 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700403 public void unhold(String callId, Session.Info sessionInfo) {
404 Log.startSession(sessionInfo, SESSION_UNHOLD);
405 try {
406 SomeArgs args = SomeArgs.obtain();
407 args.arg1 = callId;
408 args.arg2 = Log.createSubsession();
409 mHandler.obtainMessage(MSG_UNHOLD, args).sendToTarget();
410 } finally {
411 Log.endSession();
412 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700413 }
414
415 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700416 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState,
417 Session.Info sessionInfo) {
418 Log.startSession(sessionInfo, SESSION_CALL_AUDIO_SC);
419 try {
420 SomeArgs args = SomeArgs.obtain();
421 args.arg1 = callId;
422 args.arg2 = callAudioState;
423 args.arg3 = Log.createSubsession();
424 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
425 } finally {
426 Log.endSession();
427 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700428 }
429
430 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700431 public void playDtmfTone(String callId, char digit, Session.Info sessionInfo) {
432 Log.startSession(sessionInfo, SESSION_PLAY_DTMF);
433 try {
434 SomeArgs args = SomeArgs.obtain();
435 args.arg1 = digit;
436 args.arg2 = callId;
437 args.arg3 = Log.createSubsession();
438 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, args).sendToTarget();
439 } finally {
440 Log.endSession();
441 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700442 }
443
444 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700445 public void stopDtmfTone(String callId, Session.Info sessionInfo) {
446 Log.startSession(sessionInfo, SESSION_STOP_DTMF);
447 try {
448 SomeArgs args = SomeArgs.obtain();
449 args.arg1 = callId;
450 args.arg2 = Log.createSubsession();
451 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, args).sendToTarget();
452 } finally {
453 Log.endSession();
454 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700455 }
456
457 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700458 public void conference(String callId1, String callId2, Session.Info sessionInfo) {
459 Log.startSession(sessionInfo, SESSION_CONFERENCE);
460 try {
461 SomeArgs args = SomeArgs.obtain();
462 args.arg1 = callId1;
463 args.arg2 = callId2;
464 args.arg3 = Log.createSubsession();
465 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
466 } finally {
467 Log.endSession();
468 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700469 }
470
471 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700472 public void splitFromConference(String callId, Session.Info sessionInfo) {
473 Log.startSession(sessionInfo, SESSION_SPLIT_CONFERENCE);
474 try {
475 SomeArgs args = SomeArgs.obtain();
476 args.arg1 = callId;
477 args.arg2 = Log.createSubsession();
478 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, args).sendToTarget();
479 } finally {
480 Log.endSession();
481 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700482 }
483
484 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700485 public void mergeConference(String callId, Session.Info sessionInfo) {
486 Log.startSession(sessionInfo, SESSION_MERGE_CONFERENCE);
487 try {
488 SomeArgs args = SomeArgs.obtain();
489 args.arg1 = callId;
490 args.arg2 = Log.createSubsession();
491 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, args).sendToTarget();
492 } finally {
493 Log.endSession();
494 }
Santos Cordona4868042014-09-04 17:39:22 -0700495 }
496
497 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700498 public void swapConference(String callId, Session.Info sessionInfo) {
499 Log.startSession(sessionInfo, SESSION_SWAP_CONFERENCE);
500 try {
501 SomeArgs args = SomeArgs.obtain();
502 args.arg1 = callId;
503 args.arg2 = Log.createSubsession();
504 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, args).sendToTarget();
505 } finally {
506 Log.endSession();
507 }
Santos Cordona4868042014-09-04 17:39:22 -0700508 }
509
510 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700511 public void onPostDialContinue(String callId, boolean proceed, Session.Info sessionInfo) {
512 Log.startSession(sessionInfo, SESSION_POST_DIAL_CONT);
513 try {
514 SomeArgs args = SomeArgs.obtain();
515 args.arg1 = callId;
516 args.arg2 = Log.createSubsession();
517 args.argi1 = proceed ? 1 : 0;
518 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
519 } finally {
520 Log.endSession();
521 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700522 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700523
524 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700525 public void pullExternalCall(String callId, Session.Info sessionInfo) {
526 Log.startSession(sessionInfo, SESSION_PULL_EXTERNAL_CALL);
527 try {
528 SomeArgs args = SomeArgs.obtain();
529 args.arg1 = callId;
530 args.arg2 = Log.createSubsession();
531 mHandler.obtainMessage(MSG_PULL_EXTERNAL_CALL, args).sendToTarget();
532 } finally {
533 Log.endSession();
534 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700535 }
536
537 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700538 public void sendCallEvent(String callId, String event, Bundle extras,
539 Session.Info sessionInfo) {
540 Log.startSession(sessionInfo, SESSION_SEND_CALL_EVENT);
541 try {
542 SomeArgs args = SomeArgs.obtain();
543 args.arg1 = callId;
544 args.arg2 = event;
545 args.arg3 = extras;
546 args.arg4 = Log.createSubsession();
547 mHandler.obtainMessage(MSG_SEND_CALL_EVENT, args).sendToTarget();
548 } finally {
549 Log.endSession();
550 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700551 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700552
553 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700554 public void onExtrasChanged(String callId, Bundle extras, Session.Info sessionInfo) {
555 Log.startSession(sessionInfo, SESSION_EXTRAS_CHANGED);
556 try {
557 SomeArgs args = SomeArgs.obtain();
558 args.arg1 = callId;
559 args.arg2 = extras;
560 args.arg3 = Log.createSubsession();
561 mHandler.obtainMessage(MSG_ON_EXTRAS_CHANGED, args).sendToTarget();
562 } finally {
563 Log.endSession();
564 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700565 }
Hall Liub64ac4c2017-02-06 10:49:48 -0800566
567 @Override
568 public void startRtt(String callId, ParcelFileDescriptor fromInCall,
569 ParcelFileDescriptor toInCall, Session.Info sessionInfo) throws RemoteException {
570 Log.startSession(sessionInfo, SESSION_START_RTT);
571 try {
572 SomeArgs args = SomeArgs.obtain();
573 args.arg1 = callId;
574 args.arg2 = new Connection.RttTextStream(toInCall, fromInCall);
575 args.arg3 = Log.createSubsession();
576 mHandler.obtainMessage(MSG_ON_START_RTT, args).sendToTarget();
577 } finally {
578 Log.endSession();
579 }
580 }
581
582 @Override
583 public void stopRtt(String callId, Session.Info sessionInfo) throws RemoteException {
584 Log.startSession(sessionInfo, SESSION_STOP_RTT);
585 try {
586 SomeArgs args = SomeArgs.obtain();
587 args.arg1 = callId;
588 args.arg2 = Log.createSubsession();
589 mHandler.obtainMessage(MSG_ON_STOP_RTT, args).sendToTarget();
590 } finally {
591 Log.endSession();
592 }
593 }
594
595 @Override
596 public void respondToRttUpgradeRequest(String callId, ParcelFileDescriptor fromInCall,
597 ParcelFileDescriptor toInCall, Session.Info sessionInfo) throws RemoteException {
598 Log.startSession(sessionInfo, SESSION_RTT_UPGRADE_RESPONSE);
599 try {
600 SomeArgs args = SomeArgs.obtain();
601 args.arg1 = callId;
602 if (toInCall == null || fromInCall == null) {
603 args.arg2 = null;
604 } else {
605 args.arg2 = new Connection.RttTextStream(toInCall, fromInCall);
606 }
607 args.arg3 = Log.createSubsession();
608 mHandler.obtainMessage(MSG_RTT_UPGRADE_RESPONSE, args).sendToTarget();
609 } finally {
610 Log.endSession();
611 }
612 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700613 };
614
615 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
616 @Override
617 public void handleMessage(Message msg) {
618 switch (msg.what) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700619 case MSG_ADD_CONNECTION_SERVICE_ADAPTER: {
620 SomeArgs args = (SomeArgs) msg.obj;
621 try {
622 IConnectionServiceAdapter adapter = (IConnectionServiceAdapter) args.arg1;
623 Log.continueSession((Session) args.arg2,
624 SESSION_HANDLER + SESSION_ADD_CS_ADAPTER);
625 mAdapter.addAdapter(adapter);
626 onAdapterAttached();
627 } finally {
628 args.recycle();
629 Log.endSession();
630 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700631 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700632 }
633 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER: {
634 SomeArgs args = (SomeArgs) msg.obj;
635 try {
636 Log.continueSession((Session) args.arg2,
637 SESSION_HANDLER + SESSION_REMOVE_CS_ADAPTER);
638 mAdapter.removeAdapter((IConnectionServiceAdapter) args.arg1);
639 } finally {
640 args.recycle();
641 Log.endSession();
642 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700643 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700644 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700645 case MSG_CREATE_CONNECTION: {
646 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700647 Log.continueSession((Session) args.arg4, SESSION_HANDLER + SESSION_CREATE_CONN);
Ihab Awadf8b69882014-07-25 15:14:01 -0700648 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700649 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700650 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700651 final String id = (String) args.arg2;
652 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700653 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700654 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700655 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700656 Log.d(this, "Enqueueing pre-init request %s", id);
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700657 mPreInitializationConnectionRequests.add(
658 new android.telecom.Logging.Runnable(
659 SESSION_HANDLER + SESSION_CREATE_CONN + ".pICR",
660 null /*lock*/) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700661 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700662 public void loggedRun() {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700663 createConnection(
664 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700665 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700666 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700667 isIncoming,
668 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700669 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700670 }.prepare());
Ihab Awad5d0410f2014-07-30 10:07:40 -0700671 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700672 createConnection(
673 connectionManagerPhoneAccount,
674 id,
675 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700676 isIncoming,
677 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700678 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700679 } finally {
680 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700681 Log.endSession();
Ihab Awadf8b69882014-07-25 15:14:01 -0700682 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700683 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700684 }
Tyler Gunn041a1fe2017-05-12 10:04:49 -0700685 case MSG_CREATE_CONNECTION_COMPLETE: {
686 SomeArgs args = (SomeArgs) msg.obj;
687 Log.continueSession((Session) args.arg2,
688 SESSION_HANDLER + SESSION_CREATE_CONN_COMPLETE);
689 try {
690 final String id = (String) args.arg1;
691 if (!mAreAccountsInitialized) {
692 Log.d(this, "Enqueueing pre-init request %s", id);
693 mPreInitializationConnectionRequests.add(
694 new android.telecom.Logging.Runnable(
695 SESSION_HANDLER + SESSION_CREATE_CONN_COMPLETE
696 + ".pICR",
697 null /*lock*/) {
698 @Override
699 public void loggedRun() {
700 notifyCreateConnectionComplete(id);
701 }
702 }.prepare());
703 } else {
704 notifyCreateConnectionComplete(id);
705 }
706 } finally {
707 args.recycle();
708 Log.endSession();
709 }
710 break;
711 }
Tyler Gunn44e01912017-01-31 10:49:05 -0800712 case MSG_CREATE_CONNECTION_FAILED: {
713 SomeArgs args = (SomeArgs) msg.obj;
714 Log.continueSession((Session) args.arg3, SESSION_HANDLER +
715 SESSION_CREATE_CONN_FAILED);
716 try {
717 final String id = (String) args.arg1;
718 final ConnectionRequest request = (ConnectionRequest) args.arg2;
719 final boolean isIncoming = args.argi1 == 1;
Tyler Gunn159f35c2017-03-02 09:28:37 -0800720 final PhoneAccountHandle connectionMgrPhoneAccount =
721 (PhoneAccountHandle) args.arg4;
Tyler Gunn44e01912017-01-31 10:49:05 -0800722 if (!mAreAccountsInitialized) {
723 Log.d(this, "Enqueueing pre-init request %s", id);
724 mPreInitializationConnectionRequests.add(
725 new android.telecom.Logging.Runnable(
726 SESSION_HANDLER + SESSION_CREATE_CONN_FAILED + ".pICR",
727 null /*lock*/) {
728 @Override
729 public void loggedRun() {
Tyler Gunn159f35c2017-03-02 09:28:37 -0800730 createConnectionFailed(connectionMgrPhoneAccount, id,
731 request, isIncoming);
Tyler Gunn44e01912017-01-31 10:49:05 -0800732 }
733 }.prepare());
734 } else {
735 Log.i(this, "createConnectionFailed %s", id);
Tyler Gunn159f35c2017-03-02 09:28:37 -0800736 createConnectionFailed(connectionMgrPhoneAccount, id, request,
737 isIncoming);
Tyler Gunn44e01912017-01-31 10:49:05 -0800738 }
739 } finally {
740 args.recycle();
741 Log.endSession();
742 }
743 break;
744 }
Sanket Padawe02d8c272017-12-20 10:13:05 -0800745 case MSG_HANDOVER_FAILED: {
746 SomeArgs args = (SomeArgs) msg.obj;
747 Log.continueSession((Session) args.arg3, SESSION_HANDLER +
748 SESSION_HANDOVER_FAILED);
749 try {
750 final String id = (String) args.arg1;
751 final ConnectionRequest request = (ConnectionRequest) args.arg2;
752 final int reason = (int) args.arg4;
753 if (!mAreAccountsInitialized) {
754 Log.d(this, "Enqueueing pre-init request %s", id);
755 mPreInitializationConnectionRequests.add(
756 new android.telecom.Logging.Runnable(
757 SESSION_HANDLER
758 + SESSION_HANDOVER_FAILED + ".pICR",
759 null /*lock*/) {
760 @Override
761 public void loggedRun() {
762 handoverFailed(id, request, reason);
763 }
764 }.prepare());
765 } else {
766 Log.i(this, "createConnectionFailed %s", id);
767 handoverFailed(id, request, reason);
768 }
769 } finally {
770 args.recycle();
771 Log.endSession();
772 }
773 break;
774 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700775 case MSG_ABORT: {
776 SomeArgs args = (SomeArgs) msg.obj;
777 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ABORT);
778 try {
779 abort((String) args.arg1);
780 } finally {
781 args.recycle();
782 Log.endSession();
783 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700784 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700785 }
786 case MSG_ANSWER: {
787 SomeArgs args = (SomeArgs) msg.obj;
788 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ANSWER);
789 try {
790 answer((String) args.arg1);
791 } finally {
792 args.recycle();
793 Log.endSession();
794 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700795 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700796 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700797 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700798 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700799 Log.continueSession((Session) args.arg2,
800 SESSION_HANDLER + SESSION_ANSWER_VIDEO);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700801 try {
802 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700803 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700804 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700805 } finally {
806 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700807 Log.endSession();
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700808 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700809 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700810 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700811 case MSG_REJECT: {
812 SomeArgs args = (SomeArgs) msg.obj;
813 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
814 try {
815 reject((String) args.arg1);
816 } finally {
817 args.recycle();
818 Log.endSession();
819 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700820 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700821 }
Bryce Lee81901682015-08-28 16:38:02 -0700822 case MSG_REJECT_WITH_MESSAGE: {
823 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700824 Log.continueSession((Session) args.arg3,
825 SESSION_HANDLER + SESSION_REJECT_MESSAGE);
Bryce Lee81901682015-08-28 16:38:02 -0700826 try {
827 reject((String) args.arg1, (String) args.arg2);
828 } finally {
829 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700830 Log.endSession();
Bryce Lee81901682015-08-28 16:38:02 -0700831 }
832 break;
833 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700834 case MSG_DISCONNECT: {
835 SomeArgs args = (SomeArgs) msg.obj;
836 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_DISCONNECT);
837 try {
838 disconnect((String) args.arg1);
839 } finally {
840 args.recycle();
841 Log.endSession();
842 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700843 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700844 }
845 case MSG_SILENCE: {
846 SomeArgs args = (SomeArgs) msg.obj;
847 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_SILENCE);
848 try {
849 silence((String) args.arg1);
850 } finally {
851 args.recycle();
852 Log.endSession();
853 }
Bryce Leecac50772015-11-17 15:13:29 -0800854 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700855 }
856 case MSG_HOLD: {
857 SomeArgs args = (SomeArgs) msg.obj;
858 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
859 try {
860 hold((String) args.arg1);
861 } finally {
862 args.recycle();
863 Log.endSession();
864 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700865 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700866 }
867 case MSG_UNHOLD: {
868 SomeArgs args = (SomeArgs) msg.obj;
869 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_UNHOLD);
870 try {
871 unhold((String) args.arg1);
872 } finally {
873 args.recycle();
874 Log.endSession();
875 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700876 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700877 }
Yorke Lee4af59352015-05-13 14:14:54 -0700878 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700879 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700880 Log.continueSession((Session) args.arg3,
881 SESSION_HANDLER + SESSION_CALL_AUDIO_SC);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700882 try {
883 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700884 CallAudioState audioState = (CallAudioState) args.arg2;
885 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700886 } finally {
887 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700888 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700889 }
890 break;
891 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700892 case MSG_PLAY_DTMF_TONE: {
893 SomeArgs args = (SomeArgs) msg.obj;
894 try {
895 Log.continueSession((Session) args.arg3,
896 SESSION_HANDLER + SESSION_PLAY_DTMF);
897 playDtmfTone((String) args.arg2, (char) args.arg1);
898 } finally {
899 args.recycle();
900 Log.endSession();
901 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700902 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700903 }
904 case MSG_STOP_DTMF_TONE: {
905 SomeArgs args = (SomeArgs) msg.obj;
906 try {
907 Log.continueSession((Session) args.arg2,
908 SESSION_HANDLER + SESSION_STOP_DTMF);
909 stopDtmfTone((String) args.arg1);
910 } finally {
911 args.recycle();
912 Log.endSession();
913 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700914 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700915 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700916 case MSG_CONFERENCE: {
917 SomeArgs args = (SomeArgs) msg.obj;
918 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700919 Log.continueSession((Session) args.arg3,
920 SESSION_HANDLER + SESSION_CONFERENCE);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700921 String callId1 = (String) args.arg1;
922 String callId2 = (String) args.arg2;
923 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700924 } finally {
925 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700926 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700927 }
928 break;
929 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700930 case MSG_SPLIT_FROM_CONFERENCE: {
931 SomeArgs args = (SomeArgs) msg.obj;
932 try {
933 Log.continueSession((Session) args.arg2,
934 SESSION_HANDLER + SESSION_SPLIT_CONFERENCE);
935 splitFromConference((String) args.arg1);
936 } finally {
937 args.recycle();
938 Log.endSession();
939 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700940 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700941 }
942 case MSG_MERGE_CONFERENCE: {
943 SomeArgs args = (SomeArgs) msg.obj;
944 try {
945 Log.continueSession((Session) args.arg2,
946 SESSION_HANDLER + SESSION_MERGE_CONFERENCE);
947 mergeConference((String) args.arg1);
948 } finally {
949 args.recycle();
950 Log.endSession();
951 }
Santos Cordona4868042014-09-04 17:39:22 -0700952 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700953 }
954 case MSG_SWAP_CONFERENCE: {
955 SomeArgs args = (SomeArgs) msg.obj;
956 try {
957 Log.continueSession((Session) args.arg2,
958 SESSION_HANDLER + SESSION_SWAP_CONFERENCE);
959 swapConference((String) args.arg1);
960 } finally {
961 args.recycle();
962 Log.endSession();
963 }
Santos Cordona4868042014-09-04 17:39:22 -0700964 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700965 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700966 case MSG_ON_POST_DIAL_CONTINUE: {
967 SomeArgs args = (SomeArgs) msg.obj;
968 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700969 Log.continueSession((Session) args.arg2,
970 SESSION_HANDLER + SESSION_POST_DIAL_CONT);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700971 String callId = (String) args.arg1;
972 boolean proceed = (args.argi1 == 1);
973 onPostDialContinue(callId, proceed);
974 } finally {
975 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700976 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700977 }
978 break;
979 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700980 case MSG_PULL_EXTERNAL_CALL: {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700981 SomeArgs args = (SomeArgs) msg.obj;
982 try {
983 Log.continueSession((Session) args.arg2,
984 SESSION_HANDLER + SESSION_PULL_EXTERNAL_CALL);
985 pullExternalCall((String) args.arg1);
986 } finally {
987 args.recycle();
988 Log.endSession();
989 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700990 break;
991 }
992 case MSG_SEND_CALL_EVENT: {
993 SomeArgs args = (SomeArgs) msg.obj;
994 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700995 Log.continueSession((Session) args.arg4,
996 SESSION_HANDLER + SESSION_SEND_CALL_EVENT);
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700997 String callId = (String) args.arg1;
998 String event = (String) args.arg2;
999 Bundle extras = (Bundle) args.arg3;
1000 sendCallEvent(callId, event, extras);
1001 } finally {
1002 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001003 Log.endSession();
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001004 }
1005 break;
1006 }
Tyler Gunndee56a82016-03-23 16:06:34 -07001007 case MSG_ON_EXTRAS_CHANGED: {
1008 SomeArgs args = (SomeArgs) msg.obj;
1009 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001010 Log.continueSession((Session) args.arg3,
1011 SESSION_HANDLER + SESSION_EXTRAS_CHANGED);
Tyler Gunndee56a82016-03-23 16:06:34 -07001012 String callId = (String) args.arg1;
1013 Bundle extras = (Bundle) args.arg2;
1014 handleExtrasChanged(callId, extras);
1015 } finally {
1016 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001017 Log.endSession();
Tyler Gunndee56a82016-03-23 16:06:34 -07001018 }
1019 break;
1020 }
Hall Liub64ac4c2017-02-06 10:49:48 -08001021 case MSG_ON_START_RTT: {
1022 SomeArgs args = (SomeArgs) msg.obj;
1023 try {
1024 Log.continueSession((Session) args.arg3,
1025 SESSION_HANDLER + SESSION_START_RTT);
1026 String callId = (String) args.arg1;
1027 Connection.RttTextStream rttTextStream =
1028 (Connection.RttTextStream) args.arg2;
1029 startRtt(callId, rttTextStream);
1030 } finally {
1031 args.recycle();
1032 Log.endSession();
1033 }
1034 break;
1035 }
1036 case MSG_ON_STOP_RTT: {
1037 SomeArgs args = (SomeArgs) msg.obj;
1038 try {
1039 Log.continueSession((Session) args.arg2,
1040 SESSION_HANDLER + SESSION_STOP_RTT);
1041 String callId = (String) args.arg1;
1042 stopRtt(callId);
1043 } finally {
1044 args.recycle();
1045 Log.endSession();
1046 }
1047 break;
1048 }
1049 case MSG_RTT_UPGRADE_RESPONSE: {
1050 SomeArgs args = (SomeArgs) msg.obj;
1051 try {
1052 Log.continueSession((Session) args.arg3,
1053 SESSION_HANDLER + SESSION_RTT_UPGRADE_RESPONSE);
1054 String callId = (String) args.arg1;
1055 Connection.RttTextStream rttTextStream =
1056 (Connection.RttTextStream) args.arg2;
1057 handleRttUpgradeResponse(callId, rttTextStream);
1058 } finally {
1059 args.recycle();
1060 Log.endSession();
1061 }
1062 break;
1063 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001064 default:
1065 break;
1066 }
1067 }
1068 };
1069
Santos Cordon823fd3c2014-08-07 18:35:18 -07001070 private final Conference.Listener mConferenceListener = new Conference.Listener() {
1071 @Override
1072 public void onStateChanged(Conference conference, int oldState, int newState) {
1073 String id = mIdByConference.get(conference);
1074 switch (newState) {
1075 case Connection.STATE_ACTIVE:
1076 mAdapter.setActive(id);
1077 break;
1078 case Connection.STATE_HOLDING:
1079 mAdapter.setOnHold(id);
1080 break;
1081 case Connection.STATE_DISCONNECTED:
1082 // handled by onDisconnected
1083 break;
1084 }
1085 }
1086
1087 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001088 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001089 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001090 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001091 }
1092
1093 @Override
1094 public void onConnectionAdded(Conference conference, Connection connection) {
1095 }
1096
1097 @Override
1098 public void onConnectionRemoved(Conference conference, Connection connection) {
1099 }
1100
1101 @Override
Ihab Awad50e35062014-09-30 09:17:03 -07001102 public void onConferenceableConnectionsChanged(
1103 Conference conference, List<Connection> conferenceableConnections) {
1104 mAdapter.setConferenceableConnections(
1105 mIdByConference.get(conference),
1106 createConnectionIdList(conferenceableConnections));
1107 }
1108
1109 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -07001110 public void onDestroyed(Conference conference) {
1111 removeConference(conference);
1112 }
1113
1114 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001115 public void onConnectionCapabilitiesChanged(
1116 Conference conference,
1117 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001118 String id = mIdByConference.get(conference);
1119 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001120 Connection.capabilitiesToString(connectionCapabilities));
1121 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001122 }
Rekha Kumar07366812015-03-24 16:42:31 -07001123
1124 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -07001125 public void onConnectionPropertiesChanged(
1126 Conference conference,
1127 int connectionProperties) {
1128 String id = mIdByConference.get(conference);
1129 Log.d(this, "call capabilities: conference: %s",
1130 Connection.propertiesToString(connectionProperties));
1131 mAdapter.setConnectionProperties(id, connectionProperties);
1132 }
1133
1134 @Override
Rekha Kumar07366812015-03-24 16:42:31 -07001135 public void onVideoStateChanged(Conference c, int videoState) {
1136 String id = mIdByConference.get(c);
1137 Log.d(this, "onVideoStateChanged set video state %d", videoState);
1138 mAdapter.setVideoState(id, videoState);
1139 }
1140
1141 @Override
1142 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
1143 String id = mIdByConference.get(c);
1144 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
1145 videoProvider);
1146 mAdapter.setVideoProvider(id, videoProvider);
1147 }
Andrew Lee0f51da32015-04-16 13:11:55 -07001148
1149 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -07001150 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
1151 String id = mIdByConference.get(conference);
Tyler Gunndee56a82016-03-23 16:06:34 -07001152 if (id != null) {
1153 mAdapter.setStatusHints(id, statusHints);
1154 }
Andrew Leeedc625f2015-04-14 13:38:12 -07001155 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001156
1157 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001158 public void onExtrasChanged(Conference c, Bundle extras) {
1159 String id = mIdByConference.get(c);
1160 if (id != null) {
1161 mAdapter.putExtras(id, extras);
1162 }
1163 }
1164
1165 @Override
1166 public void onExtrasRemoved(Conference c, List<String> keys) {
1167 String id = mIdByConference.get(c);
1168 if (id != null) {
1169 mAdapter.removeExtras(id, keys);
1170 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001171 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001172 };
1173
Ihab Awad542e0ea2014-05-16 10:22:16 -07001174 private final Connection.Listener mConnectionListener = new Connection.Listener() {
1175 @Override
1176 public void onStateChanged(Connection c, int state) {
1177 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -07001178 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -07001179 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001180 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001181 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001182 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001183 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001184 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001185 break;
Tyler Gunnc96b5e02016-07-07 22:53:57 -07001186 case Connection.STATE_PULLING_CALL:
1187 mAdapter.setPulling(id);
1188 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001189 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -07001190 // Handled in onDisconnected()
1191 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001192 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001193 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001194 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001195 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001196 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -07001197 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001198 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001199 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001200 break;
1201 }
1202 }
1203
1204 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001205 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -07001206 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -07001207 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001208 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001209 }
1210
1211 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -07001212 public void onVideoStateChanged(Connection c, int videoState) {
1213 String id = mIdByConnection.get(c);
1214 Log.d(this, "Adapter set video state %d", videoState);
1215 mAdapter.setVideoState(id, videoState);
1216 }
1217
1218 @Override
Andrew Lee100e2932014-09-08 15:34:24 -07001219 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -07001220 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001221 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -07001222 }
1223
1224 @Override
1225 public void onCallerDisplayNameChanged(
1226 Connection c, String callerDisplayName, int presentation) {
1227 String id = mIdByConnection.get(c);
1228 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001229 }
1230
1231 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -07001232 public void onDestroyed(Connection c) {
1233 removeConnection(c);
1234 }
Ihab Awadf8358972014-05-28 16:46:42 -07001235
1236 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001237 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -07001238 String id = mIdByConnection.get(c);
1239 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001240 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -07001241 }
1242
1243 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001244 public void onPostDialChar(Connection c, char nextChar) {
1245 String id = mIdByConnection.get(c);
1246 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
1247 mAdapter.onPostDialChar(id, nextChar);
1248 }
1249
1250 @Override
Andrew Lee100e2932014-09-08 15:34:24 -07001251 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -07001252 String id = mIdByConnection.get(c);
1253 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -07001254 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -07001255 }
Santos Cordonb6939982014-06-04 20:20:58 -07001256
1257 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001258 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -07001259 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001260 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001261 Connection.capabilitiesToString(capabilities));
1262 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -07001263 }
1264
Santos Cordonb6939982014-06-04 20:20:58 -07001265 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -07001266 public void onConnectionPropertiesChanged(Connection c, int properties) {
1267 String id = mIdByConnection.get(c);
1268 Log.d(this, "properties: parcelableconnection: %s",
1269 Connection.propertiesToString(properties));
1270 mAdapter.setConnectionProperties(id, properties);
1271 }
1272
1273 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001274 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001275 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -07001276 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
1277 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001278 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001279 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001280
1281 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001282 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001283 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001284 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001285 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001286
1287 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001288 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001289 String id = mIdByConnection.get(c);
1290 mAdapter.setStatusHints(id, statusHints);
1291 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -07001292
1293 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001294 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001295 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -07001296 mAdapter.setConferenceableConnections(
1297 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001298 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001299 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001300
1301 @Override
1302 public void onConferenceChanged(Connection connection, Conference conference) {
1303 String id = mIdByConnection.get(connection);
1304 if (id != null) {
1305 String conferenceId = null;
1306 if (conference != null) {
1307 conferenceId = mIdByConference.get(conference);
1308 }
1309 mAdapter.setIsConferenced(id, conferenceId);
1310 }
1311 }
Anthony Lee17455a32015-04-24 15:25:29 -07001312
1313 @Override
1314 public void onConferenceMergeFailed(Connection connection) {
1315 String id = mIdByConnection.get(connection);
1316 if (id != null) {
1317 mAdapter.onConferenceMergeFailed(id);
1318 }
1319 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001320
1321 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001322 public void onExtrasChanged(Connection c, Bundle extras) {
1323 String id = mIdByConnection.get(c);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001324 if (id != null) {
Tyler Gunndee56a82016-03-23 16:06:34 -07001325 mAdapter.putExtras(id, extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001326 }
1327 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001328
Tyler Gunnf5035432017-01-09 09:43:12 -08001329 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001330 public void onExtrasRemoved(Connection c, List<String> keys) {
1331 String id = mIdByConnection.get(c);
1332 if (id != null) {
1333 mAdapter.removeExtras(id, keys);
1334 }
1335 }
1336
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001337 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001338 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001339 String id = mIdByConnection.get(connection);
1340 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001341 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001342 }
1343 }
Tyler Gunnf5035432017-01-09 09:43:12 -08001344
1345 @Override
Hall Liua98f58b52017-11-07 17:59:28 -08001346 public void onAudioRouteChanged(Connection c, int audioRoute, String bluetoothAddress) {
Tyler Gunnf5035432017-01-09 09:43:12 -08001347 String id = mIdByConnection.get(c);
1348 if (id != null) {
Hall Liua98f58b52017-11-07 17:59:28 -08001349 mAdapter.setAudioRoute(id, audioRoute, bluetoothAddress);
Tyler Gunnf5035432017-01-09 09:43:12 -08001350 }
1351 }
Hall Liub64ac4c2017-02-06 10:49:48 -08001352
1353 @Override
1354 public void onRttInitiationSuccess(Connection c) {
1355 String id = mIdByConnection.get(c);
1356 if (id != null) {
1357 mAdapter.onRttInitiationSuccess(id);
1358 }
1359 }
1360
1361 @Override
1362 public void onRttInitiationFailure(Connection c, int reason) {
1363 String id = mIdByConnection.get(c);
1364 if (id != null) {
1365 mAdapter.onRttInitiationFailure(id, reason);
1366 }
1367 }
1368
1369 @Override
1370 public void onRttSessionRemotelyTerminated(Connection c) {
1371 String id = mIdByConnection.get(c);
1372 if (id != null) {
1373 mAdapter.onRttSessionRemotelyTerminated(id);
1374 }
1375 }
1376
1377 @Override
1378 public void onRemoteRttRequest(Connection c) {
1379 String id = mIdByConnection.get(c);
1380 if (id != null) {
1381 mAdapter.onRemoteRttRequest(id);
1382 }
1383 }
Srikanth Chintalafcb15012017-05-04 20:58:34 +05301384
1385 @Override
1386 public void onPhoneAccountChanged(Connection c, PhoneAccountHandle pHandle) {
1387 String id = mIdByConnection.get(c);
1388 if (id != null) {
1389 mAdapter.onPhoneAccountChanged(id, pHandle);
1390 }
1391 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001392 };
1393
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001394 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -07001395 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001396 public final IBinder onBind(Intent intent) {
1397 return mBinder;
1398 }
1399
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001400 /** {@inheritDoc} */
1401 @Override
1402 public boolean onUnbind(Intent intent) {
1403 endAllConnections();
1404 return super.onUnbind(intent);
1405 }
1406
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001407 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001408 * This can be used by telecom to either create a new outgoing call or attach to an existing
1409 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001410 * createConnection util a connection service cancels the process or completes it successfully.
1411 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001412 private void createConnection(
1413 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001414 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -07001415 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -07001416 boolean isIncoming,
1417 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001418 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001419 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
1420 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -07001421 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001422
Sanket Padawe85291f62017-12-01 13:59:27 -08001423 Connection connection = null;
Sanket Padawe02d8c272017-12-20 10:13:05 -08001424 if (getApplicationContext().getApplicationInfo().targetSdkVersion >
1425 Build.VERSION_CODES.O_MR1 && request.getExtras() != null &&
1426 request.getExtras().getBoolean(TelecomManager.EXTRA_IS_HANDOVER,false)) {
Sanket Padawe85291f62017-12-01 13:59:27 -08001427 if (!isIncoming) {
1428 connection = onCreateOutgoingHandoverConnection(callManagerAccount, request);
1429 } else {
Sanket Padawe02d8c272017-12-20 10:13:05 -08001430 connection = onCreateIncomingHandoverConnection(callManagerAccount, request);
Sanket Padawe85291f62017-12-01 13:59:27 -08001431 }
1432 } else {
1433 connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
1434 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
1435 : onCreateOutgoingConnection(callManagerAccount, request);
1436 }
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001437 Log.d(this, "createConnection, connection: %s", connection);
1438 if (connection == null) {
Tyler Gunnfba1a8e2017-12-19 15:23:59 -08001439 Log.i(this, "createConnection, implementation returned null connection.");
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001440 connection = Connection.createFailedConnection(
Tyler Gunnfba1a8e2017-12-19 15:23:59 -08001441 new DisconnectCause(DisconnectCause.ERROR, "IMPL_RETURNED_NULL_CONNECTION"));
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001442 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001443
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001444 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001445 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001446 addConnection(callId, connection);
1447 }
1448
Andrew Lee100e2932014-09-08 15:34:24 -07001449 Uri address = connection.getAddress();
1450 String number = address == null ? "null" : address.getSchemeSpecificPart();
Tyler Gunn720c6642016-03-22 09:02:47 -07001451 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s, properties: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001452 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001453 Connection.stateToString(connection.getState()),
Tyler Gunn720c6642016-03-22 09:02:47 -07001454 Connection.capabilitiesToString(connection.getConnectionCapabilities()),
1455 Connection.propertiesToString(connection.getConnectionProperties()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001456
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001457 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -07001458 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001459 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -07001460 request,
1461 new ParcelableConnection(
1462 request.getAccountHandle(),
1463 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001464 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001465 connection.getConnectionProperties(),
Christine Hallstrom2830ce92016-11-30 16:06:42 -08001466 connection.getSupportedAudioRoutes(),
Andrew Lee100e2932014-09-08 15:34:24 -07001467 connection.getAddress(),
1468 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -07001469 connection.getCallerDisplayName(),
1470 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001471 connection.getVideoProvider() == null ?
1472 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001473 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -07001474 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001475 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001476 connection.getConnectTimeMillis(),
Tyler Gunn3fa819c2017-08-04 09:27:26 -07001477 connection.getConnectElapsedTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -07001478 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001479 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001480 createIdList(connection.getConferenceables()),
1481 connection.getExtras()));
Tyler Gunnf5035432017-01-09 09:43:12 -08001482
1483 if (isIncoming && request.shouldShowIncomingCallUi() &&
1484 (connection.getConnectionProperties() & Connection.PROPERTY_SELF_MANAGED) ==
1485 Connection.PROPERTY_SELF_MANAGED) {
1486 // Tell ConnectionService to show its incoming call UX.
1487 connection.onShowIncomingCallUi();
1488 }
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001489 if (isUnknown) {
1490 triggerConferenceRecalculate();
1491 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001492 }
1493
Tyler Gunn159f35c2017-03-02 09:28:37 -08001494 private void createConnectionFailed(final PhoneAccountHandle callManagerAccount,
1495 final String callId, final ConnectionRequest request,
1496 boolean isIncoming) {
Tyler Gunn44e01912017-01-31 10:49:05 -08001497
1498 Log.i(this, "createConnectionFailed %s", callId);
1499 if (isIncoming) {
Tyler Gunn159f35c2017-03-02 09:28:37 -08001500 onCreateIncomingConnectionFailed(callManagerAccount, request);
Tyler Gunn44e01912017-01-31 10:49:05 -08001501 } else {
Tyler Gunn159f35c2017-03-02 09:28:37 -08001502 onCreateOutgoingConnectionFailed(callManagerAccount, request);
Tyler Gunn44e01912017-01-31 10:49:05 -08001503 }
1504 }
1505
Sanket Padawe02d8c272017-12-20 10:13:05 -08001506 private void handoverFailed(final String callId, final ConnectionRequest request,
1507 int reason) {
1508
1509 Log.i(this, "handoverFailed %s", callId);
1510 onHandoverFailed(request, reason);
1511 }
1512
Tyler Gunn041a1fe2017-05-12 10:04:49 -07001513 /**
1514 * Called by Telecom when the creation of a new Connection has completed and it is now added
1515 * to Telecom.
1516 * @param callId The ID of the connection.
1517 */
1518 private void notifyCreateConnectionComplete(final String callId) {
1519 Log.i(this, "notifyCreateConnectionComplete %s", callId);
Tyler Gunn0a88f2e2017-06-16 20:20:34 -07001520 if (callId == null) {
1521 // This could happen if the connection fails quickly and is removed from the
1522 // ConnectionService before Telecom sends the create connection complete callback.
1523 Log.w(this, "notifyCreateConnectionComplete: callId is null.");
1524 return;
1525 }
Tyler Gunn041a1fe2017-05-12 10:04:49 -07001526 onCreateConnectionComplete(findConnectionForAction(callId,
1527 "notifyCreateConnectionComplete"));
1528 }
1529
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001530 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001531 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001532 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001533 }
1534
Tyler Gunnbe74de02014-08-29 14:51:48 -07001535 private void answerVideo(String callId, int videoState) {
1536 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001537 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001538 }
1539
Tyler Gunnbe74de02014-08-29 14:51:48 -07001540 private void answer(String callId) {
1541 Log.d(this, "answer %s", callId);
1542 findConnectionForAction(callId, "answer").onAnswer();
1543 }
1544
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001545 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001546 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001547 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001548 }
1549
Bryce Lee81901682015-08-28 16:38:02 -07001550 private void reject(String callId, String rejectWithMessage) {
1551 Log.d(this, "reject %s with message", callId);
1552 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
1553 }
1554
Bryce Leecac50772015-11-17 15:13:29 -08001555 private void silence(String callId) {
1556 Log.d(this, "silence %s", callId);
1557 findConnectionForAction(callId, "silence").onSilence();
1558 }
1559
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001560 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001561 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001562 if (mConnectionById.containsKey(callId)) {
1563 findConnectionForAction(callId, "disconnect").onDisconnect();
1564 } else {
1565 findConferenceForAction(callId, "disconnect").onDisconnect();
1566 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001567 }
1568
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001569 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001570 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001571 if (mConnectionById.containsKey(callId)) {
1572 findConnectionForAction(callId, "hold").onHold();
1573 } else {
1574 findConferenceForAction(callId, "hold").onHold();
1575 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001576 }
1577
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001578 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001579 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001580 if (mConnectionById.containsKey(callId)) {
1581 findConnectionForAction(callId, "unhold").onUnhold();
1582 } else {
1583 findConferenceForAction(callId, "unhold").onUnhold();
1584 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001585 }
1586
Yorke Lee4af59352015-05-13 14:14:54 -07001587 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
1588 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001589 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -07001590 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1591 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001592 } else {
Yorke Lee4af59352015-05-13 14:14:54 -07001593 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1594 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001595 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001596 }
1597
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001598 private void playDtmfTone(String callId, char digit) {
1599 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001600 if (mConnectionById.containsKey(callId)) {
1601 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1602 } else {
1603 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1604 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001605 }
1606
1607 private void stopDtmfTone(String callId) {
1608 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001609 if (mConnectionById.containsKey(callId)) {
1610 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
1611 } else {
1612 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
1613 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001614 }
1615
Santos Cordon823fd3c2014-08-07 18:35:18 -07001616 private void conference(String callId1, String callId2) {
1617 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -07001618
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001619 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001620 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001621 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001622 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001623 conference2 = findConferenceForAction(callId2, "conference");
1624 if (conference2 == getNullConference()) {
1625 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
1626 callId2);
1627 return;
1628 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001629 }
Santos Cordonb6939982014-06-04 20:20:58 -07001630
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001631 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -07001632 Connection connection1 = findConnectionForAction(callId1, "conference");
1633 if (connection1 == getNullConnection()) {
1634 Conference conference1 = findConferenceForAction(callId1, "addConnection");
1635 if (conference1 == getNullConference()) {
1636 Log.w(this,
1637 "Connection1 or Conference1 missing in conference request %s.",
1638 callId1);
1639 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001640 // Call 1 is a conference.
1641 if (connection2 != getNullConnection()) {
1642 // Call 2 is a connection so merge via call 1 (conference).
1643 conference1.onMerge(connection2);
1644 } else {
1645 // Call 2 is ALSO a conference; this should never happen.
1646 Log.wtf(this, "There can only be one conference and an attempt was made to " +
1647 "merge two conferences.");
1648 return;
1649 }
Ihab Awad50e35062014-09-30 09:17:03 -07001650 }
1651 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001652 // Call 1 is a connection.
1653 if (conference2 != getNullConference()) {
1654 // Call 2 is a conference, so merge via call 2.
1655 conference2.onMerge(connection1);
1656 } else {
1657 // Call 2 is a connection, so merge together.
1658 onConference(connection1, connection2);
1659 }
Ihab Awad50e35062014-09-30 09:17:03 -07001660 }
Santos Cordon980acb92014-05-31 10:31:19 -07001661 }
1662
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001663 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -07001664 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -07001665
1666 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001667 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -07001668 Log.w(this, "Connection missing in conference request %s.", callId);
1669 return;
1670 }
1671
Santos Cordon0159ac02014-08-21 14:28:11 -07001672 Conference conference = connection.getConference();
1673 if (conference != null) {
1674 conference.onSeparate(connection);
1675 }
Santos Cordon980acb92014-05-31 10:31:19 -07001676 }
1677
Santos Cordona4868042014-09-04 17:39:22 -07001678 private void mergeConference(String callId) {
1679 Log.d(this, "mergeConference(%s)", callId);
1680 Conference conference = findConferenceForAction(callId, "mergeConference");
1681 if (conference != null) {
1682 conference.onMerge();
1683 }
1684 }
1685
1686 private void swapConference(String callId) {
1687 Log.d(this, "swapConference(%s)", callId);
1688 Conference conference = findConferenceForAction(callId, "swapConference");
1689 if (conference != null) {
1690 conference.onSwap();
1691 }
1692 }
1693
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001694 /**
1695 * Notifies a {@link Connection} of a request to pull an external call.
1696 *
1697 * See {@link Call#pullExternalCall()}.
1698 *
1699 * @param callId The ID of the call to pull.
1700 */
1701 private void pullExternalCall(String callId) {
1702 Log.d(this, "pullExternalCall(%s)", callId);
1703 Connection connection = findConnectionForAction(callId, "pullExternalCall");
1704 if (connection != null) {
1705 connection.onPullExternalCall();
1706 }
1707 }
1708
1709 /**
1710 * Notifies a {@link Connection} of a call event.
1711 *
1712 * See {@link Call#sendCallEvent(String, Bundle)}.
1713 *
1714 * @param callId The ID of the call receiving the event.
1715 * @param event The event.
1716 * @param extras Extras associated with the event.
1717 */
1718 private void sendCallEvent(String callId, String event, Bundle extras) {
1719 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
1720 Connection connection = findConnectionForAction(callId, "sendCallEvent");
1721 if (connection != null) {
1722 connection.onCallEvent(event, extras);
1723 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001724 }
1725
Tyler Gunndee56a82016-03-23 16:06:34 -07001726 /**
1727 * Notifies a {@link Connection} or {@link Conference} of a change to the extras from Telecom.
1728 * <p>
1729 * These extra changes can originate from Telecom itself, or from an {@link InCallService} via
1730 * the {@link android.telecom.Call#putExtra(String, boolean)},
1731 * {@link android.telecom.Call#putExtra(String, int)},
1732 * {@link android.telecom.Call#putExtra(String, String)},
1733 * {@link Call#removeExtras(List)}.
1734 *
1735 * @param callId The ID of the call receiving the event.
1736 * @param extras The new extras bundle.
1737 */
1738 private void handleExtrasChanged(String callId, Bundle extras) {
1739 Log.d(this, "handleExtrasChanged(%s, %s)", callId, extras);
1740 if (mConnectionById.containsKey(callId)) {
1741 findConnectionForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1742 } else if (mConferenceById.containsKey(callId)) {
1743 findConferenceForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1744 }
1745 }
1746
Hall Liub64ac4c2017-02-06 10:49:48 -08001747 private void startRtt(String callId, Connection.RttTextStream rttTextStream) {
1748 Log.d(this, "startRtt(%s)", callId);
1749 if (mConnectionById.containsKey(callId)) {
1750 findConnectionForAction(callId, "startRtt").onStartRtt(rttTextStream);
1751 } else if (mConferenceById.containsKey(callId)) {
1752 Log.w(this, "startRtt called on a conference.");
1753 }
1754 }
1755
1756 private void stopRtt(String callId) {
1757 Log.d(this, "stopRtt(%s)", callId);
1758 if (mConnectionById.containsKey(callId)) {
1759 findConnectionForAction(callId, "stopRtt").onStopRtt();
Hall Liuffa4a812017-03-02 16:11:00 -08001760 findConnectionForAction(callId, "stopRtt").unsetRttProperty();
Hall Liub64ac4c2017-02-06 10:49:48 -08001761 } else if (mConferenceById.containsKey(callId)) {
1762 Log.w(this, "stopRtt called on a conference.");
1763 }
1764 }
1765
1766 private void handleRttUpgradeResponse(String callId, Connection.RttTextStream rttTextStream) {
1767 Log.d(this, "handleRttUpgradeResponse(%s, %s)", callId, rttTextStream == null);
1768 if (mConnectionById.containsKey(callId)) {
1769 findConnectionForAction(callId, "handleRttUpgradeResponse")
1770 .handleRttUpgradeResponse(rttTextStream);
1771 } else if (mConferenceById.containsKey(callId)) {
1772 Log.w(this, "handleRttUpgradeResponse called on a conference.");
1773 }
1774 }
1775
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001776 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001777 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001778 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001779 }
1780
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001781 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001782 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001783 // No need to query again if we already did it.
1784 return;
1785 }
1786
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001787 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -07001788 @Override
1789 public void onResult(
1790 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001791 final List<IBinder> services) {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001792 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oR", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001793 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001794 public void loggedRun() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001795 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001796 mRemoteConnectionManager.addConnectionService(
1797 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001798 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -07001799 }
Ihab Awad5d0410f2014-07-30 10:07:40 -07001800 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001801 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -07001802 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001803 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001804 }
1805
1806 @Override
1807 public void onError() {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001808 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oE", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001809 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001810 public void loggedRun() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001811 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -07001812 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001813 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001814 }
1815 });
1816 }
1817
Ihab Awadf8b69882014-07-25 15:14:01 -07001818 /**
1819 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001820 * incoming request. This is used by {@code ConnectionService}s that are registered with
1821 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
1822 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001823 *
1824 * @param connectionManagerPhoneAccount See description at
1825 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1826 * @param request Details about the incoming call.
1827 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1828 * not handle the call.
1829 */
1830 public final RemoteConnection createRemoteIncomingConnection(
1831 PhoneAccountHandle connectionManagerPhoneAccount,
1832 ConnectionRequest request) {
1833 return mRemoteConnectionManager.createRemoteConnection(
1834 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -07001835 }
1836
1837 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001838 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001839 * outgoing request. This is used by {@code ConnectionService}s that are registered with
1840 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
1841 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001842 *
1843 * @param connectionManagerPhoneAccount See description at
1844 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Cuihtlauac ALVARADO0b3b2a52016-09-13 14:49:41 +02001845 * @param request Details about the outgoing call.
Ihab Awadf8b69882014-07-25 15:14:01 -07001846 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1847 * not handle the call.
1848 */
1849 public final RemoteConnection createRemoteOutgoingConnection(
1850 PhoneAccountHandle connectionManagerPhoneAccount,
1851 ConnectionRequest request) {
1852 return mRemoteConnectionManager.createRemoteConnection(
1853 connectionManagerPhoneAccount, request, false);
1854 }
1855
1856 /**
Santos Cordona663f862014-10-29 13:49:58 -07001857 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1858 * {@link RemoteConnection}s should be merged into a conference call.
1859 * <p>
1860 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1861 * be invoked.
1862 *
1863 * @param remoteConnection1 The first of the remote connections to conference.
1864 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001865 */
1866 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001867 RemoteConnection remoteConnection1,
1868 RemoteConnection remoteConnection2) {
1869 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001870 }
1871
1872 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001873 * Adds a new conference call. When a conference call is created either as a result of an
1874 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1875 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1876 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1877 *
1878 * @param conference The new conference object.
1879 */
1880 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001881 Log.d(this, "addConference: conference=%s", conference);
1882
Santos Cordon823fd3c2014-08-07 18:35:18 -07001883 String id = addConferenceInternal(conference);
1884 if (id != null) {
1885 List<String> connectionIds = new ArrayList<>(2);
1886 for (Connection connection : conference.getConnections()) {
1887 if (mIdByConnection.containsKey(connection)) {
1888 connectionIds.add(mIdByConnection.get(connection));
1889 }
1890 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001891 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001892 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001893 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001894 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001895 conference.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001896 conference.getConnectionProperties(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001897 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001898 conference.getVideoProvider() == null ?
1899 null : conference.getVideoProvider().getInterface(),
1900 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001901 conference.getConnectTimeMillis(),
Tyler Gunn3fa819c2017-08-04 09:27:26 -07001902 conference.getConnectElapsedTime(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001903 conference.getStatusHints(),
1904 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001905
Santos Cordon823fd3c2014-08-07 18:35:18 -07001906 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001907 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1908 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001909
1910 // Go through any child calls and set the parent.
1911 for (Connection connection : conference.getConnections()) {
1912 String connectionId = mIdByConnection.get(connection);
1913 if (connectionId != null) {
1914 mAdapter.setIsConferenced(connectionId, id);
1915 }
1916 }
1917 }
1918 }
1919
1920 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001921 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1922 * connection.
1923 *
1924 * @param phoneAccountHandle The phone account handle for the connection.
1925 * @param connection The connection to add.
1926 */
1927 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1928 Connection connection) {
Tyler Gunn78da7812017-05-09 14:34:57 -07001929 addExistingConnection(phoneAccountHandle, connection, null /* conference */);
1930 }
1931
1932 /**
1933 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1934 * connection.
1935 *
1936 * @param phoneAccountHandle The phone account handle for the connection.
1937 * @param connection The connection to add.
1938 * @param conference The parent conference of the new connection.
1939 * @hide
1940 */
1941 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1942 Connection connection, Conference conference) {
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001943
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001944 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001945 if (id != null) {
1946 List<String> emptyList = new ArrayList<>(0);
Tyler Gunn78da7812017-05-09 14:34:57 -07001947 String conferenceId = null;
1948 if (conference != null) {
1949 conferenceId = mIdByConference.get(conference);
1950 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001951
1952 ParcelableConnection parcelableConnection = new ParcelableConnection(
1953 phoneAccountHandle,
1954 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001955 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001956 connection.getConnectionProperties(),
Christine Hallstrom2830ce92016-11-30 16:06:42 -08001957 connection.getSupportedAudioRoutes(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001958 connection.getAddress(),
1959 connection.getAddressPresentation(),
1960 connection.getCallerDisplayName(),
1961 connection.getCallerDisplayNamePresentation(),
1962 connection.getVideoProvider() == null ?
1963 null : connection.getVideoProvider().getInterface(),
1964 connection.getVideoState(),
1965 connection.isRingbackRequested(),
1966 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001967 connection.getConnectTimeMillis(),
Tyler Gunn3fa819c2017-08-04 09:27:26 -07001968 connection.getConnectElapsedTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001969 connection.getStatusHints(),
1970 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001971 emptyList,
Tyler Gunn78da7812017-05-09 14:34:57 -07001972 connection.getExtras(),
1973 conferenceId);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001974 mAdapter.addExistingConnection(id, parcelableConnection);
1975 }
1976 }
1977
1978 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001979 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1980 * has taken responsibility.
1981 *
1982 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001983 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001984 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001985 return mConnectionById.values();
1986 }
1987
1988 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001989 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1990 * has taken responsibility.
1991 *
1992 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1993 */
1994 public final Collection<Conference> getAllConferences() {
1995 return mConferenceById.values();
1996 }
1997
1998 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001999 * Create a {@code Connection} given an incoming request. This is used to attach to existing
2000 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07002001 *
Ihab Awadf8b69882014-07-25 15:14:01 -07002002 * @param connectionManagerPhoneAccount See description at
2003 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
2004 * @param request Details about the incoming call.
2005 * @return The {@code Connection} object to satisfy this call, or {@code null} to
2006 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07002007 */
Ihab Awadf8b69882014-07-25 15:14:01 -07002008 public Connection onCreateIncomingConnection(
2009 PhoneAccountHandle connectionManagerPhoneAccount,
2010 ConnectionRequest request) {
2011 return null;
2012 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07002013
2014 /**
Tyler Gunn041a1fe2017-05-12 10:04:49 -07002015 * Called after the {@link Connection} returned by
2016 * {@link #onCreateIncomingConnection(PhoneAccountHandle, ConnectionRequest)}
2017 * or {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)} has been
2018 * added to the {@link ConnectionService} and sent to Telecom.
2019 *
2020 * @param connection the {@link Connection}.
2021 * @hide
2022 */
2023 public void onCreateConnectionComplete(Connection connection) {
2024 }
2025
2026 /**
Tyler Gunnf5035432017-01-09 09:43:12 -08002027 * Called by Telecom to inform the {@link ConnectionService} that its request to create a new
2028 * incoming {@link Connection} was denied.
2029 * <p>
2030 * Used when a self-managed {@link ConnectionService} attempts to create a new incoming
2031 * {@link Connection}, but Telecom has determined that the call cannot be allowed at this time.
2032 * The {@link ConnectionService} is responsible for silently rejecting the new incoming
2033 * {@link Connection}.
2034 * <p>
2035 * See {@link TelecomManager#isIncomingCallPermitted(PhoneAccountHandle)} for more information.
2036 *
Tyler Gunn159f35c2017-03-02 09:28:37 -08002037 * @param connectionManagerPhoneAccount See description at
2038 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Tyler Gunnf5035432017-01-09 09:43:12 -08002039 * @param request The incoming connection request.
2040 */
Tyler Gunn159f35c2017-03-02 09:28:37 -08002041 public void onCreateIncomingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
2042 ConnectionRequest request) {
Tyler Gunnf5035432017-01-09 09:43:12 -08002043 }
2044
2045 /**
2046 * Called by Telecom to inform the {@link ConnectionService} that its request to create a new
2047 * outgoing {@link Connection} was denied.
2048 * <p>
2049 * Used when a self-managed {@link ConnectionService} attempts to create a new outgoing
2050 * {@link Connection}, but Telecom has determined that the call cannot be placed at this time.
2051 * The {@link ConnectionService} is responisible for informing the user that the
2052 * {@link Connection} cannot be made at this time.
2053 * <p>
2054 * See {@link TelecomManager#isOutgoingCallPermitted(PhoneAccountHandle)} for more information.
2055 *
Tyler Gunn159f35c2017-03-02 09:28:37 -08002056 * @param connectionManagerPhoneAccount See description at
2057 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Tyler Gunnf5035432017-01-09 09:43:12 -08002058 * @param request The outgoing connection request.
2059 */
Tyler Gunn159f35c2017-03-02 09:28:37 -08002060 public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount,
2061 ConnectionRequest request) {
Tyler Gunnf5035432017-01-09 09:43:12 -08002062 }
2063
2064 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08002065 * Trigger recalculate functinality for conference calls. This is used when a Telephony
2066 * Connection is part of a conference controller but is not yet added to Connection
2067 * Service and hence cannot be added to the conference call.
2068 *
2069 * @hide
2070 */
2071 public void triggerConferenceRecalculate() {
2072 }
2073
2074 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07002075 * Create a {@code Connection} given an outgoing request. This is used to initiate new
2076 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07002077 *
Ihab Awadf8b69882014-07-25 15:14:01 -07002078 * @param connectionManagerPhoneAccount The connection manager account to use for managing
2079 * this call.
2080 * <p>
2081 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
2082 * has registered one or more {@code PhoneAccount}s having
2083 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
2084 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
2085 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
2086 * making the connection.
2087 * <p>
2088 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
2089 * being asked to make a direct connection. The
2090 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
2091 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
2092 * making the connection.
2093 * @param request Details about the outgoing call.
2094 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07002095 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07002096 */
Ihab Awadf8b69882014-07-25 15:14:01 -07002097 public Connection onCreateOutgoingConnection(
2098 PhoneAccountHandle connectionManagerPhoneAccount,
2099 ConnectionRequest request) {
2100 return null;
2101 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07002102
2103 /**
Sanket Padawea8eddd42017-11-03 11:07:35 -07002104 * Called by Telecom on the initiating side of the handover to create an instance of a
2105 * handover connection.
2106 * @param fromPhoneAccountHandle {@link PhoneAccountHandle} associated with the
2107 * ConnectionService which needs to handover the call.
2108 * @param request Details about the call which needs to be handover.
2109 * @return Connection object corresponding to the handover call.
2110 */
2111 public Connection onCreateOutgoingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle,
2112 ConnectionRequest request) {
2113 return null;
2114 }
2115
2116 /**
2117 * Called by Telecom on the receiving side of the handover to request the
2118 * {@link ConnectionService} to create an instance of a handover connection.
2119 * @param fromPhoneAccountHandle {@link PhoneAccountHandle} associated with the
2120 * ConnectionService which needs to handover the call.
2121 * @param request Details about the call which needs to be handover.
2122 * @return {@link Connection} object corresponding to the handover call.
2123 */
2124 public Connection onCreateIncomingHandoverConnection(PhoneAccountHandle fromPhoneAccountHandle,
2125 ConnectionRequest request) {
2126 return null;
2127 }
2128
2129 /**
2130 * Called by Telecom in response to a {@code TelecomManager#acceptHandover()}
2131 * invocation which failed.
2132 * @param request Details about the call which needs to be handover.
2133 * @param error Reason for handover failure as defined in
2134 * {@link android.telecom.Call.Callback#HANDOVER_FAILURE_DEST_INVALID_PERM}
2135 */
2136 public void onHandoverFailed(ConnectionRequest request, int error) {
2137 return;
2138 }
2139
2140 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07002141 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
2142 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
2143 * call created using
2144 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
2145 *
Yorke Lee770ed6e2014-10-06 18:58:52 -07002146 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07002147 */
2148 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
2149 ConnectionRequest request) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07002150 return null;
Yorke Leec3cf9822014-10-02 09:38:39 -07002151 }
2152
2153 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07002154 * Conference two specified connections. Invoked when the user has made a request to merge the
2155 * specified connections into a conference call. In response, the connection service should
2156 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07002157 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07002158 * @param connection1 A connection to merge into a conference call.
2159 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07002160 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07002161 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07002162
Santos Cordona663f862014-10-29 13:49:58 -07002163 /**
2164 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
2165 * When this method is invoked, this {@link ConnectionService} should create its own
2166 * representation of the conference call and send it to telecom using {@link #addConference}.
2167 * <p>
2168 * This is only relevant to {@link ConnectionService}s which are registered with
2169 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
2170 *
2171 * @param conference The remote conference call.
2172 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07002173 public void onRemoteConferenceAdded(RemoteConference conference) {}
2174
Santos Cordon823fd3c2014-08-07 18:35:18 -07002175 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07002176 * Called when an existing connection is added remotely.
2177 * @param connection The existing connection which was added.
2178 */
2179 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
2180
2181 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07002182 * @hide
2183 */
2184 public boolean containsConference(Conference conference) {
2185 return mIdByConference.containsKey(conference);
2186 }
2187
Ihab Awadb8e85c72014-08-23 20:34:57 -07002188 /** {@hide} */
2189 void addRemoteConference(RemoteConference remoteConference) {
2190 onRemoteConferenceAdded(remoteConference);
2191 }
2192
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07002193 /** {@hide} */
2194 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
2195 onRemoteExistingConnectionAdded(remoteConnection);
2196 }
2197
Ihab Awad5d0410f2014-07-30 10:07:40 -07002198 private void onAccountsInitialized() {
2199 mAreAccountsInitialized = true;
2200 for (Runnable r : mPreInitializationConnectionRequests) {
2201 r.run();
2202 }
2203 mPreInitializationConnectionRequests.clear();
2204 }
2205
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07002206 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002207 * Adds an existing connection to the list of connections, identified by a new call ID unique
2208 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07002209 *
2210 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002211 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07002212 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002213 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
2214 String id;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07002215
2216 if (connection.getExtras() != null && connection.getExtras()
2217 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
2218 id = connection.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
2219 Log.d(this, "addExistingConnectionInternal - conn %s reusing original id %s",
2220 connection.getTelecomCallId(), id);
2221 } else if (handle == null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002222 // If no phone account handle was provided, we cannot be sure the call ID is unique,
2223 // so just use a random UUID.
2224 id = UUID.randomUUID().toString();
2225 } else {
2226 // Phone account handle was provided, so use the ConnectionService class name as a
2227 // prefix for a unique incremental call ID.
2228 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
2229 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07002230 addConnection(id, connection);
2231 return id;
2232 }
2233
Ihab Awad542e0ea2014-05-16 10:22:16 -07002234 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002235 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07002236 mConnectionById.put(callId, connection);
2237 mIdByConnection.put(connection, callId);
2238 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07002239 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07002240 }
2241
Anthony Lee30e65842014-11-06 16:30:53 -08002242 /** {@hide} */
2243 protected void removeConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07002244 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07002245 connection.removeConnectionListener(mConnectionListener);
Chenjie Luoe370b532016-05-12 16:59:43 -07002246 String id = mIdByConnection.get(connection);
2247 if (id != null) {
2248 mConnectionById.remove(id);
2249 mIdByConnection.remove(connection);
2250 mAdapter.removeCall(id);
2251 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07002252 }
2253
Santos Cordon823fd3c2014-08-07 18:35:18 -07002254 private String addConferenceInternal(Conference conference) {
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07002255 String originalId = null;
2256 if (conference.getExtras() != null && conference.getExtras()
2257 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
2258 originalId = conference.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
2259 Log.d(this, "addConferenceInternal: conf %s reusing original id %s",
2260 conference.getTelecomCallId(),
2261 originalId);
2262 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07002263 if (mIdByConference.containsKey(conference)) {
2264 Log.w(this, "Re-adding an existing conference: %s.", conference);
2265 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002266 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
2267 // cannot determine a ConnectionService class name to associate with the ID, so use
2268 // a unique UUID (for now).
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07002269 String id = originalId == null ? UUID.randomUUID().toString() : originalId;
Santos Cordon823fd3c2014-08-07 18:35:18 -07002270 mConferenceById.put(id, conference);
2271 mIdByConference.put(conference, id);
2272 conference.addListener(mConferenceListener);
2273 return id;
2274 }
2275
2276 return null;
2277 }
2278
2279 private void removeConference(Conference conference) {
2280 if (mIdByConference.containsKey(conference)) {
2281 conference.removeListener(mConferenceListener);
2282
2283 String id = mIdByConference.get(conference);
2284 mConferenceById.remove(id);
2285 mIdByConference.remove(conference);
2286 mAdapter.removeCall(id);
2287 }
2288 }
2289
Ihab Awad542e0ea2014-05-16 10:22:16 -07002290 private Connection findConnectionForAction(String callId, String action) {
Tyler Gunn0a88f2e2017-06-16 20:20:34 -07002291 if (callId != null && mConnectionById.containsKey(callId)) {
Ihab Awad542e0ea2014-05-16 10:22:16 -07002292 return mConnectionById.get(callId);
2293 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07002294 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07002295 return getNullConnection();
2296 }
2297
2298 static synchronized Connection getNullConnection() {
2299 if (sNullConnection == null) {
2300 sNullConnection = new Connection() {};
2301 }
2302 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07002303 }
Santos Cordon0159ac02014-08-21 14:28:11 -07002304
2305 private Conference findConferenceForAction(String conferenceId, String action) {
2306 if (mConferenceById.containsKey(conferenceId)) {
2307 return mConferenceById.get(conferenceId);
2308 }
2309 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
2310 return getNullConference();
2311 }
2312
Ihab Awadb8e85c72014-08-23 20:34:57 -07002313 private List<String> createConnectionIdList(List<Connection> connections) {
2314 List<String> ids = new ArrayList<>();
2315 for (Connection c : connections) {
2316 if (mIdByConnection.containsKey(c)) {
2317 ids.add(mIdByConnection.get(c));
2318 }
2319 }
2320 Collections.sort(ids);
2321 return ids;
2322 }
2323
Tyler Gunn6d76ca02014-11-17 15:49:51 -08002324 /**
2325 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07002326 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08002327 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07002328 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08002329 * @return List of string conference and call Ids.
2330 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07002331 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08002332 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07002333 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08002334 // Only allow Connection and Conference conferenceables.
2335 if (c instanceof Connection) {
2336 Connection connection = (Connection) c;
2337 if (mIdByConnection.containsKey(connection)) {
2338 ids.add(mIdByConnection.get(connection));
2339 }
2340 } else if (c instanceof Conference) {
2341 Conference conference = (Conference) c;
2342 if (mIdByConference.containsKey(conference)) {
2343 ids.add(mIdByConference.get(conference));
2344 }
2345 }
2346 }
2347 Collections.sort(ids);
2348 return ids;
2349 }
2350
Santos Cordon0159ac02014-08-21 14:28:11 -07002351 private Conference getNullConference() {
2352 if (sNullConference == null) {
2353 sNullConference = new Conference(null) {};
2354 }
2355 return sNullConference;
2356 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07002357
2358 private void endAllConnections() {
2359 // Unbound from telecomm. We should end all connections and conferences.
2360 for (Connection connection : mIdByConnection.keySet()) {
2361 // only operate on top-level calls. Conference calls will be removed on their own.
2362 if (connection.getConference() == null) {
2363 connection.onDisconnect();
2364 }
2365 }
2366 for (Conference conference : mIdByConference.keySet()) {
2367 conference.onDisconnect();
2368 }
2369 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002370
2371 /**
2372 * Retrieves the next call ID as maintainted by the connection service.
2373 *
2374 * @return The call ID.
2375 */
2376 private int getNextCallId() {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07002377 synchronized (mIdSyncRoot) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07002378 return ++mId;
2379 }
2380 }
Santos Cordon980acb92014-05-31 10:31:19 -07002381}