blob: 6e100298da35c2efc0f3bf6344ac89a5f0d20287 [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Santos Cordon5c6fa952014-07-20 17:47:12 -070019import android.annotation.SdkConstant;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070020import android.app.Service;
Santos Cordon52d8a152014-06-17 19:08:45 -070021import android.content.ComponentName;
Santos Cordon5c6fa952014-07-20 17:47:12 -070022import android.content.Intent;
Ihab Awad542e0ea2014-05-16 10:22:16 -070023import android.net.Uri;
Santos Cordon6b7f9552015-05-27 17:21:45 -070024import android.os.Bundle;
Santos Cordon52d8a152014-06-17 19:08:45 -070025import android.os.Handler;
26import android.os.IBinder;
27import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070028import android.os.Message;
Brad Ebingerb32d4f82016-10-24 16:40:49 -070029import android.telecom.Logging.Session;
Andrew Lee14185762014-07-25 09:41:56 -070030
Sailesh Nepal2a46b902014-07-04 17:21:07 -070031import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070032import com.android.internal.telecom.IConnectionService;
33import com.android.internal.telecom.IConnectionServiceAdapter;
34import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070035
Ihab Awad5d0410f2014-07-30 10:07:40 -070036import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070037import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070038import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070039import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070040import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070041import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070042import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070043
44/**
Tyler Gunnf5035432017-01-09 09:43:12 -080045 * An abstract service that should be implemented by any apps which either:
46 * <ol>
47 * <li>Can make phone calls (VoIP or otherwise) and want those calls to be integrated into the
48 * built-in phone app. Referred to as a <b>system managed</b> {@link ConnectionService}.</li>
49 * <li>Are a standalone calling app and don't want their calls to be integrated into the
50 * built-in phone app. Referred to as a <b>self managed</b> {@link ConnectionService}.</li>
51 * </ol>
52 * Once implemented, the {@link ConnectionService} needs to take the following steps so that Telecom
53 * will bind to it:
Santos Cordona663f862014-10-29 13:49:58 -070054 * <p>
55 * 1. <i>Registration in AndroidManifest.xml</i>
56 * <br/>
57 * <pre>
58 * &lt;service android:name="com.example.package.MyConnectionService"
59 * android:label="@string/some_label_for_my_connection_service"
Yorke Lee249c12e2015-05-13 15:59:29 -070060 * android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"&gt;
Santos Cordona663f862014-10-29 13:49:58 -070061 * &lt;intent-filter&gt;
62 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
63 * &lt;/intent-filter&gt;
64 * &lt;/service&gt;
65 * </pre>
66 * <p>
67 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
68 * <br/>
69 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
70 * <p>
Tyler Gunnf5035432017-01-09 09:43:12 -080071 * System managed {@link ConnectionService}s must be enabled by the user in the phone app settings
72 * before Telecom will bind to them. Self-manged {@link ConnectionService}s must be granted the
73 * appropriate permission before Telecom will bind to them.
74 * <p>
75 * Once registered and enabled by the user in the phone app settings or granted permission, telecom
76 * will bind to a {@link ConnectionService} implementation when it wants that
77 * {@link ConnectionService} to place a call or the service has indicated that is has an incoming
78 * call through {@link TelecomManager#addNewIncomingCall}. The {@link ConnectionService} can then
79 * expect a call to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection}
80 * wherein it should provide a new instance of a {@link Connection} object. It is through this
81 * {@link Connection} object that telecom receives state updates and the {@link ConnectionService}
Santos Cordona663f862014-10-29 13:49:58 -070082 * receives call-commands such as answer, reject, hold and disconnect.
83 * <p>
Tyler Gunnf5035432017-01-09 09:43:12 -080084 * When there are no more live calls, telecom will unbind from the {@link ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070085 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070086public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070087 /**
88 * The {@link Intent} that must be declared as handled by the service.
89 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070090 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070091 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070092
Ihab Awad542e0ea2014-05-16 10:22:16 -070093 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070094 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070095
Brad Ebingerb32d4f82016-10-24 16:40:49 -070096 // Session Definitions
97 private static final String SESSION_HANDLER = "H.";
98 private static final String SESSION_ADD_CS_ADAPTER = "CS.aCSA";
99 private static final String SESSION_REMOVE_CS_ADAPTER = "CS.rCSA";
100 private static final String SESSION_CREATE_CONN = "CS.crCo";
Tyler Gunn44e01912017-01-31 10:49:05 -0800101 private static final String SESSION_CREATE_CONN_FAILED = "CS.crCoF";
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700102 private static final String SESSION_ABORT = "CS.ab";
103 private static final String SESSION_ANSWER = "CS.an";
104 private static final String SESSION_ANSWER_VIDEO = "CS.anV";
105 private static final String SESSION_REJECT = "CS.r";
106 private static final String SESSION_REJECT_MESSAGE = "CS.rWM";
107 private static final String SESSION_SILENCE = "CS.s";
108 private static final String SESSION_DISCONNECT = "CS.d";
109 private static final String SESSION_HOLD = "CS.h";
110 private static final String SESSION_UNHOLD = "CS.u";
111 private static final String SESSION_CALL_AUDIO_SC = "CS.cASC";
112 private static final String SESSION_PLAY_DTMF = "CS.pDT";
113 private static final String SESSION_STOP_DTMF = "CS.sDT";
114 private static final String SESSION_CONFERENCE = "CS.c";
115 private static final String SESSION_SPLIT_CONFERENCE = "CS.sFC";
116 private static final String SESSION_MERGE_CONFERENCE = "CS.mC";
117 private static final String SESSION_SWAP_CONFERENCE = "CS.sC";
118 private static final String SESSION_POST_DIAL_CONT = "CS.oPDC";
119 private static final String SESSION_PULL_EXTERNAL_CALL = "CS.pEC";
120 private static final String SESSION_SEND_CALL_EVENT = "CS.sCE";
121 private static final String SESSION_EXTRAS_CHANGED = "CS.oEC";
122
Ihab Awad8aecfed2014-08-08 17:06:11 -0700123 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700124 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700125 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700126 private static final int MSG_ANSWER = 4;
127 private static final int MSG_REJECT = 5;
128 private static final int MSG_DISCONNECT = 6;
129 private static final int MSG_HOLD = 7;
130 private static final int MSG_UNHOLD = 8;
Yorke Lee4af59352015-05-13 14:14:54 -0700131 private static final int MSG_ON_CALL_AUDIO_STATE_CHANGED = 9;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700132 private static final int MSG_PLAY_DTMF_TONE = 10;
133 private static final int MSG_STOP_DTMF_TONE = 11;
134 private static final int MSG_CONFERENCE = 12;
135 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700136 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700137 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700138 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700139 private static final int MSG_MERGE_CONFERENCE = 18;
140 private static final int MSG_SWAP_CONFERENCE = 19;
Bryce Lee81901682015-08-28 16:38:02 -0700141 private static final int MSG_REJECT_WITH_MESSAGE = 20;
Bryce Leecac50772015-11-17 15:13:29 -0800142 private static final int MSG_SILENCE = 21;
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700143 private static final int MSG_PULL_EXTERNAL_CALL = 22;
144 private static final int MSG_SEND_CALL_EVENT = 23;
Tyler Gunndee56a82016-03-23 16:06:34 -0700145 private static final int MSG_ON_EXTRAS_CHANGED = 24;
Tyler Gunn44e01912017-01-31 10:49:05 -0800146 private static final int MSG_CREATE_CONNECTION_FAILED = 25;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700147
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700148 private static Connection sNullConnection;
149
mike dooley95e80702014-09-18 14:07:52 -0700150 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
151 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
152 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
153 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700154 private final RemoteConnectionManager mRemoteConnectionManager =
155 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700156 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700157 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700158
Santos Cordon823fd3c2014-08-07 18:35:18 -0700159 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700160 private Conference sNullConference;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700161 private Object mIdSyncRoot = new Object();
162 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700163
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700164 private final IBinder mBinder = new IConnectionService.Stub() {
165 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700166 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter,
167 Session.Info sessionInfo) {
168 Log.startSession(sessionInfo, SESSION_ADD_CS_ADAPTER);
169 try {
170 SomeArgs args = SomeArgs.obtain();
171 args.arg1 = adapter;
172 args.arg2 = Log.createSubsession();
173 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
174 } finally {
175 Log.endSession();
176 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700177 }
178
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700179 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter,
180 Session.Info sessionInfo) {
181 Log.startSession(sessionInfo, SESSION_REMOVE_CS_ADAPTER);
182 try {
183 SomeArgs args = SomeArgs.obtain();
184 args.arg1 = adapter;
185 args.arg2 = Log.createSubsession();
186 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
187 } finally {
188 Log.endSession();
189 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700190 }
191
192 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700193 public void createConnection(
194 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700195 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700196 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700197 boolean isIncoming,
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700198 boolean isUnknown,
199 Session.Info sessionInfo) {
200 Log.startSession(sessionInfo, SESSION_CREATE_CONN);
201 try {
202 SomeArgs args = SomeArgs.obtain();
203 args.arg1 = connectionManagerPhoneAccount;
204 args.arg2 = id;
205 args.arg3 = request;
206 args.arg4 = Log.createSubsession();
207 args.argi1 = isIncoming ? 1 : 0;
208 args.argi2 = isUnknown ? 1 : 0;
209 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
210 } finally {
211 Log.endSession();
212 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700213 }
214
215 @Override
Tyler Gunn44e01912017-01-31 10:49:05 -0800216 public void createConnectionFailed(
217 String callId,
218 ConnectionRequest request,
219 boolean isIncoming,
220 Session.Info sessionInfo) {
221 Log.startSession(sessionInfo, SESSION_CREATE_CONN_FAILED);
222 try {
223 SomeArgs args = SomeArgs.obtain();
224 args.arg1 = callId;
225 args.arg2 = request;
226 args.arg3 = Log.createSubsession();
227 args.argi1 = isIncoming ? 1 : 0;
228 mHandler.obtainMessage(MSG_CREATE_CONNECTION_FAILED, args).sendToTarget();
229 } finally {
230 Log.endSession();
231 }
232 }
233
234 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700235 public void abort(String callId, Session.Info sessionInfo) {
236 Log.startSession(sessionInfo, SESSION_ABORT);
237 try {
238 SomeArgs args = SomeArgs.obtain();
239 args.arg1 = callId;
240 args.arg2 = Log.createSubsession();
241 mHandler.obtainMessage(MSG_ABORT, args).sendToTarget();
242 } finally {
243 Log.endSession();
244 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700245 }
246
247 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700248 public void answerVideo(String callId, int videoState, Session.Info sessionInfo) {
249 Log.startSession(sessionInfo, SESSION_ANSWER_VIDEO);
250 try {
251 SomeArgs args = SomeArgs.obtain();
252 args.arg1 = callId;
253 args.arg2 = Log.createSubsession();
254 args.argi1 = videoState;
255 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
256 } finally {
257 Log.endSession();
258 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700259 }
260
261 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700262 public void answer(String callId, Session.Info sessionInfo) {
263 Log.startSession(sessionInfo, SESSION_ANSWER);
264 try {
265 SomeArgs args = SomeArgs.obtain();
266 args.arg1 = callId;
267 args.arg2 = Log.createSubsession();
268 mHandler.obtainMessage(MSG_ANSWER, args).sendToTarget();
269 } finally {
270 Log.endSession();
271 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700272 }
273
274 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700275 public void reject(String callId, Session.Info sessionInfo) {
276 Log.startSession(sessionInfo, SESSION_REJECT);
277 try {
278 SomeArgs args = SomeArgs.obtain();
279 args.arg1 = callId;
280 args.arg2 = Log.createSubsession();
281 mHandler.obtainMessage(MSG_REJECT, args).sendToTarget();
282 } finally {
283 Log.endSession();
284 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700285 }
286
287 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700288 public void rejectWithMessage(String callId, String message, Session.Info sessionInfo) {
289 Log.startSession(sessionInfo, SESSION_REJECT_MESSAGE);
290 try {
291 SomeArgs args = SomeArgs.obtain();
292 args.arg1 = callId;
293 args.arg2 = message;
294 args.arg3 = Log.createSubsession();
295 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
296 } finally {
297 Log.endSession();
298 }
Bryce Lee81901682015-08-28 16:38:02 -0700299 }
300
301 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700302 public void silence(String callId, Session.Info sessionInfo) {
303 Log.startSession(sessionInfo, SESSION_SILENCE);
304 try {
305 SomeArgs args = SomeArgs.obtain();
306 args.arg1 = callId;
307 args.arg2 = Log.createSubsession();
308 mHandler.obtainMessage(MSG_SILENCE, args).sendToTarget();
309 } finally {
310 Log.endSession();
311 }
Bryce Leecac50772015-11-17 15:13:29 -0800312 }
313
314 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700315 public void disconnect(String callId, Session.Info sessionInfo) {
316 Log.startSession(sessionInfo, SESSION_DISCONNECT);
317 try {
318 SomeArgs args = SomeArgs.obtain();
319 args.arg1 = callId;
320 args.arg2 = Log.createSubsession();
321 mHandler.obtainMessage(MSG_DISCONNECT, args).sendToTarget();
322 } finally {
323 Log.endSession();
324 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700325 }
326
327 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700328 public void hold(String callId, Session.Info sessionInfo) {
329 Log.startSession(sessionInfo, SESSION_HOLD);
330 try {
331 SomeArgs args = SomeArgs.obtain();
332 args.arg1 = callId;
333 args.arg2 = Log.createSubsession();
334 mHandler.obtainMessage(MSG_HOLD, args).sendToTarget();
335 } finally {
336 Log.endSession();
337 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700338 }
339
340 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700341 public void unhold(String callId, Session.Info sessionInfo) {
342 Log.startSession(sessionInfo, SESSION_UNHOLD);
343 try {
344 SomeArgs args = SomeArgs.obtain();
345 args.arg1 = callId;
346 args.arg2 = Log.createSubsession();
347 mHandler.obtainMessage(MSG_UNHOLD, args).sendToTarget();
348 } finally {
349 Log.endSession();
350 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700351 }
352
353 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700354 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState,
355 Session.Info sessionInfo) {
356 Log.startSession(sessionInfo, SESSION_CALL_AUDIO_SC);
357 try {
358 SomeArgs args = SomeArgs.obtain();
359 args.arg1 = callId;
360 args.arg2 = callAudioState;
361 args.arg3 = Log.createSubsession();
362 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
363 } finally {
364 Log.endSession();
365 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700366 }
367
368 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700369 public void playDtmfTone(String callId, char digit, Session.Info sessionInfo) {
370 Log.startSession(sessionInfo, SESSION_PLAY_DTMF);
371 try {
372 SomeArgs args = SomeArgs.obtain();
373 args.arg1 = digit;
374 args.arg2 = callId;
375 args.arg3 = Log.createSubsession();
376 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, args).sendToTarget();
377 } finally {
378 Log.endSession();
379 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700380 }
381
382 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700383 public void stopDtmfTone(String callId, Session.Info sessionInfo) {
384 Log.startSession(sessionInfo, SESSION_STOP_DTMF);
385 try {
386 SomeArgs args = SomeArgs.obtain();
387 args.arg1 = callId;
388 args.arg2 = Log.createSubsession();
389 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, args).sendToTarget();
390 } finally {
391 Log.endSession();
392 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700393 }
394
395 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700396 public void conference(String callId1, String callId2, Session.Info sessionInfo) {
397 Log.startSession(sessionInfo, SESSION_CONFERENCE);
398 try {
399 SomeArgs args = SomeArgs.obtain();
400 args.arg1 = callId1;
401 args.arg2 = callId2;
402 args.arg3 = Log.createSubsession();
403 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
404 } finally {
405 Log.endSession();
406 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700407 }
408
409 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700410 public void splitFromConference(String callId, Session.Info sessionInfo) {
411 Log.startSession(sessionInfo, SESSION_SPLIT_CONFERENCE);
412 try {
413 SomeArgs args = SomeArgs.obtain();
414 args.arg1 = callId;
415 args.arg2 = Log.createSubsession();
416 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, args).sendToTarget();
417 } finally {
418 Log.endSession();
419 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700420 }
421
422 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700423 public void mergeConference(String callId, Session.Info sessionInfo) {
424 Log.startSession(sessionInfo, SESSION_MERGE_CONFERENCE);
425 try {
426 SomeArgs args = SomeArgs.obtain();
427 args.arg1 = callId;
428 args.arg2 = Log.createSubsession();
429 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, args).sendToTarget();
430 } finally {
431 Log.endSession();
432 }
Santos Cordona4868042014-09-04 17:39:22 -0700433 }
434
435 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700436 public void swapConference(String callId, Session.Info sessionInfo) {
437 Log.startSession(sessionInfo, SESSION_SWAP_CONFERENCE);
438 try {
439 SomeArgs args = SomeArgs.obtain();
440 args.arg1 = callId;
441 args.arg2 = Log.createSubsession();
442 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, args).sendToTarget();
443 } finally {
444 Log.endSession();
445 }
Santos Cordona4868042014-09-04 17:39:22 -0700446 }
447
448 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700449 public void onPostDialContinue(String callId, boolean proceed, Session.Info sessionInfo) {
450 Log.startSession(sessionInfo, SESSION_POST_DIAL_CONT);
451 try {
452 SomeArgs args = SomeArgs.obtain();
453 args.arg1 = callId;
454 args.arg2 = Log.createSubsession();
455 args.argi1 = proceed ? 1 : 0;
456 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
457 } finally {
458 Log.endSession();
459 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700460 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700461
462 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700463 public void pullExternalCall(String callId, Session.Info sessionInfo) {
464 Log.startSession(sessionInfo, SESSION_PULL_EXTERNAL_CALL);
465 try {
466 SomeArgs args = SomeArgs.obtain();
467 args.arg1 = callId;
468 args.arg2 = Log.createSubsession();
469 mHandler.obtainMessage(MSG_PULL_EXTERNAL_CALL, args).sendToTarget();
470 } finally {
471 Log.endSession();
472 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700473 }
474
475 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700476 public void sendCallEvent(String callId, String event, Bundle extras,
477 Session.Info sessionInfo) {
478 Log.startSession(sessionInfo, SESSION_SEND_CALL_EVENT);
479 try {
480 SomeArgs args = SomeArgs.obtain();
481 args.arg1 = callId;
482 args.arg2 = event;
483 args.arg3 = extras;
484 args.arg4 = Log.createSubsession();
485 mHandler.obtainMessage(MSG_SEND_CALL_EVENT, args).sendToTarget();
486 } finally {
487 Log.endSession();
488 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700489 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700490
491 @Override
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700492 public void onExtrasChanged(String callId, Bundle extras, Session.Info sessionInfo) {
493 Log.startSession(sessionInfo, SESSION_EXTRAS_CHANGED);
494 try {
495 SomeArgs args = SomeArgs.obtain();
496 args.arg1 = callId;
497 args.arg2 = extras;
498 args.arg3 = Log.createSubsession();
499 mHandler.obtainMessage(MSG_ON_EXTRAS_CHANGED, args).sendToTarget();
500 } finally {
501 Log.endSession();
502 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700503 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700504 };
505
506 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
507 @Override
508 public void handleMessage(Message msg) {
509 switch (msg.what) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700510 case MSG_ADD_CONNECTION_SERVICE_ADAPTER: {
511 SomeArgs args = (SomeArgs) msg.obj;
512 try {
513 IConnectionServiceAdapter adapter = (IConnectionServiceAdapter) args.arg1;
514 Log.continueSession((Session) args.arg2,
515 SESSION_HANDLER + SESSION_ADD_CS_ADAPTER);
516 mAdapter.addAdapter(adapter);
517 onAdapterAttached();
518 } finally {
519 args.recycle();
520 Log.endSession();
521 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700522 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700523 }
524 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER: {
525 SomeArgs args = (SomeArgs) msg.obj;
526 try {
527 Log.continueSession((Session) args.arg2,
528 SESSION_HANDLER + SESSION_REMOVE_CS_ADAPTER);
529 mAdapter.removeAdapter((IConnectionServiceAdapter) args.arg1);
530 } finally {
531 args.recycle();
532 Log.endSession();
533 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700534 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700535 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700536 case MSG_CREATE_CONNECTION: {
537 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700538 Log.continueSession((Session) args.arg4, SESSION_HANDLER + SESSION_CREATE_CONN);
Ihab Awadf8b69882014-07-25 15:14:01 -0700539 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700540 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700541 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700542 final String id = (String) args.arg2;
543 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700544 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700545 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700546 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700547 Log.d(this, "Enqueueing pre-init request %s", id);
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700548 mPreInitializationConnectionRequests.add(
549 new android.telecom.Logging.Runnable(
550 SESSION_HANDLER + SESSION_CREATE_CONN + ".pICR",
551 null /*lock*/) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700552 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700553 public void loggedRun() {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700554 createConnection(
555 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700556 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700557 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700558 isIncoming,
559 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700560 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700561 }.prepare());
Ihab Awad5d0410f2014-07-30 10:07:40 -0700562 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700563 createConnection(
564 connectionManagerPhoneAccount,
565 id,
566 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700567 isIncoming,
568 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700569 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700570 } finally {
571 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700572 Log.endSession();
Ihab Awadf8b69882014-07-25 15:14:01 -0700573 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700574 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700575 }
Tyler Gunn44e01912017-01-31 10:49:05 -0800576 case MSG_CREATE_CONNECTION_FAILED: {
577 SomeArgs args = (SomeArgs) msg.obj;
578 Log.continueSession((Session) args.arg3, SESSION_HANDLER +
579 SESSION_CREATE_CONN_FAILED);
580 try {
581 final String id = (String) args.arg1;
582 final ConnectionRequest request = (ConnectionRequest) args.arg2;
583 final boolean isIncoming = args.argi1 == 1;
584 if (!mAreAccountsInitialized) {
585 Log.d(this, "Enqueueing pre-init request %s", id);
586 mPreInitializationConnectionRequests.add(
587 new android.telecom.Logging.Runnable(
588 SESSION_HANDLER + SESSION_CREATE_CONN_FAILED + ".pICR",
589 null /*lock*/) {
590 @Override
591 public void loggedRun() {
592 createConnectionFailed(id, request, isIncoming);
593 }
594 }.prepare());
595 } else {
596 Log.i(this, "createConnectionFailed %s", id);
597 createConnectionFailed(id, request, isIncoming);
598 }
599 } finally {
600 args.recycle();
601 Log.endSession();
602 }
603 break;
604 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700605 case MSG_ABORT: {
606 SomeArgs args = (SomeArgs) msg.obj;
607 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ABORT);
608 try {
609 abort((String) args.arg1);
610 } finally {
611 args.recycle();
612 Log.endSession();
613 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700614 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700615 }
616 case MSG_ANSWER: {
617 SomeArgs args = (SomeArgs) msg.obj;
618 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ANSWER);
619 try {
620 answer((String) args.arg1);
621 } finally {
622 args.recycle();
623 Log.endSession();
624 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700625 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700626 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700627 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700628 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700629 Log.continueSession((Session) args.arg2,
630 SESSION_HANDLER + SESSION_ANSWER_VIDEO);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700631 try {
632 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700633 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700634 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700635 } finally {
636 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700637 Log.endSession();
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700638 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700639 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700640 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700641 case MSG_REJECT: {
642 SomeArgs args = (SomeArgs) msg.obj;
643 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
644 try {
645 reject((String) args.arg1);
646 } finally {
647 args.recycle();
648 Log.endSession();
649 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700650 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700651 }
Bryce Lee81901682015-08-28 16:38:02 -0700652 case MSG_REJECT_WITH_MESSAGE: {
653 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700654 Log.continueSession((Session) args.arg3,
655 SESSION_HANDLER + SESSION_REJECT_MESSAGE);
Bryce Lee81901682015-08-28 16:38:02 -0700656 try {
657 reject((String) args.arg1, (String) args.arg2);
658 } finally {
659 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700660 Log.endSession();
Bryce Lee81901682015-08-28 16:38:02 -0700661 }
662 break;
663 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700664 case MSG_DISCONNECT: {
665 SomeArgs args = (SomeArgs) msg.obj;
666 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_DISCONNECT);
667 try {
668 disconnect((String) args.arg1);
669 } finally {
670 args.recycle();
671 Log.endSession();
672 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700673 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700674 }
675 case MSG_SILENCE: {
676 SomeArgs args = (SomeArgs) msg.obj;
677 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_SILENCE);
678 try {
679 silence((String) args.arg1);
680 } finally {
681 args.recycle();
682 Log.endSession();
683 }
Bryce Leecac50772015-11-17 15:13:29 -0800684 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700685 }
686 case MSG_HOLD: {
687 SomeArgs args = (SomeArgs) msg.obj;
688 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
689 try {
690 hold((String) args.arg1);
691 } finally {
692 args.recycle();
693 Log.endSession();
694 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700695 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700696 }
697 case MSG_UNHOLD: {
698 SomeArgs args = (SomeArgs) msg.obj;
699 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_UNHOLD);
700 try {
701 unhold((String) args.arg1);
702 } finally {
703 args.recycle();
704 Log.endSession();
705 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700706 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700707 }
Yorke Lee4af59352015-05-13 14:14:54 -0700708 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700709 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700710 Log.continueSession((Session) args.arg3,
711 SESSION_HANDLER + SESSION_CALL_AUDIO_SC);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700712 try {
713 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700714 CallAudioState audioState = (CallAudioState) args.arg2;
715 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700716 } finally {
717 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700718 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700719 }
720 break;
721 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700722 case MSG_PLAY_DTMF_TONE: {
723 SomeArgs args = (SomeArgs) msg.obj;
724 try {
725 Log.continueSession((Session) args.arg3,
726 SESSION_HANDLER + SESSION_PLAY_DTMF);
727 playDtmfTone((String) args.arg2, (char) args.arg1);
728 } finally {
729 args.recycle();
730 Log.endSession();
731 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700732 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700733 }
734 case MSG_STOP_DTMF_TONE: {
735 SomeArgs args = (SomeArgs) msg.obj;
736 try {
737 Log.continueSession((Session) args.arg2,
738 SESSION_HANDLER + SESSION_STOP_DTMF);
739 stopDtmfTone((String) args.arg1);
740 } finally {
741 args.recycle();
742 Log.endSession();
743 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700744 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700745 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700746 case MSG_CONFERENCE: {
747 SomeArgs args = (SomeArgs) msg.obj;
748 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700749 Log.continueSession((Session) args.arg3,
750 SESSION_HANDLER + SESSION_CONFERENCE);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700751 String callId1 = (String) args.arg1;
752 String callId2 = (String) args.arg2;
753 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700754 } finally {
755 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700756 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700757 }
758 break;
759 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700760 case MSG_SPLIT_FROM_CONFERENCE: {
761 SomeArgs args = (SomeArgs) msg.obj;
762 try {
763 Log.continueSession((Session) args.arg2,
764 SESSION_HANDLER + SESSION_SPLIT_CONFERENCE);
765 splitFromConference((String) args.arg1);
766 } finally {
767 args.recycle();
768 Log.endSession();
769 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700770 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700771 }
772 case MSG_MERGE_CONFERENCE: {
773 SomeArgs args = (SomeArgs) msg.obj;
774 try {
775 Log.continueSession((Session) args.arg2,
776 SESSION_HANDLER + SESSION_MERGE_CONFERENCE);
777 mergeConference((String) args.arg1);
778 } finally {
779 args.recycle();
780 Log.endSession();
781 }
Santos Cordona4868042014-09-04 17:39:22 -0700782 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700783 }
784 case MSG_SWAP_CONFERENCE: {
785 SomeArgs args = (SomeArgs) msg.obj;
786 try {
787 Log.continueSession((Session) args.arg2,
788 SESSION_HANDLER + SESSION_SWAP_CONFERENCE);
789 swapConference((String) args.arg1);
790 } finally {
791 args.recycle();
792 Log.endSession();
793 }
Santos Cordona4868042014-09-04 17:39:22 -0700794 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700795 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700796 case MSG_ON_POST_DIAL_CONTINUE: {
797 SomeArgs args = (SomeArgs) msg.obj;
798 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700799 Log.continueSession((Session) args.arg2,
800 SESSION_HANDLER + SESSION_POST_DIAL_CONT);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700801 String callId = (String) args.arg1;
802 boolean proceed = (args.argi1 == 1);
803 onPostDialContinue(callId, proceed);
804 } finally {
805 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700806 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700807 }
808 break;
809 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700810 case MSG_PULL_EXTERNAL_CALL: {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700811 SomeArgs args = (SomeArgs) msg.obj;
812 try {
813 Log.continueSession((Session) args.arg2,
814 SESSION_HANDLER + SESSION_PULL_EXTERNAL_CALL);
815 pullExternalCall((String) args.arg1);
816 } finally {
817 args.recycle();
818 Log.endSession();
819 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700820 break;
821 }
822 case MSG_SEND_CALL_EVENT: {
823 SomeArgs args = (SomeArgs) msg.obj;
824 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700825 Log.continueSession((Session) args.arg4,
826 SESSION_HANDLER + SESSION_SEND_CALL_EVENT);
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700827 String callId = (String) args.arg1;
828 String event = (String) args.arg2;
829 Bundle extras = (Bundle) args.arg3;
830 sendCallEvent(callId, event, extras);
831 } finally {
832 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700833 Log.endSession();
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700834 }
835 break;
836 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700837 case MSG_ON_EXTRAS_CHANGED: {
838 SomeArgs args = (SomeArgs) msg.obj;
839 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700840 Log.continueSession((Session) args.arg3,
841 SESSION_HANDLER + SESSION_EXTRAS_CHANGED);
Tyler Gunndee56a82016-03-23 16:06:34 -0700842 String callId = (String) args.arg1;
843 Bundle extras = (Bundle) args.arg2;
844 handleExtrasChanged(callId, extras);
845 } finally {
846 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700847 Log.endSession();
Tyler Gunndee56a82016-03-23 16:06:34 -0700848 }
849 break;
850 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700851 default:
852 break;
853 }
854 }
855 };
856
Santos Cordon823fd3c2014-08-07 18:35:18 -0700857 private final Conference.Listener mConferenceListener = new Conference.Listener() {
858 @Override
859 public void onStateChanged(Conference conference, int oldState, int newState) {
860 String id = mIdByConference.get(conference);
861 switch (newState) {
862 case Connection.STATE_ACTIVE:
863 mAdapter.setActive(id);
864 break;
865 case Connection.STATE_HOLDING:
866 mAdapter.setOnHold(id);
867 break;
868 case Connection.STATE_DISCONNECTED:
869 // handled by onDisconnected
870 break;
871 }
872 }
873
874 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700875 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700876 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700877 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700878 }
879
880 @Override
881 public void onConnectionAdded(Conference conference, Connection connection) {
882 }
883
884 @Override
885 public void onConnectionRemoved(Conference conference, Connection connection) {
886 }
887
888 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700889 public void onConferenceableConnectionsChanged(
890 Conference conference, List<Connection> conferenceableConnections) {
891 mAdapter.setConferenceableConnections(
892 mIdByConference.get(conference),
893 createConnectionIdList(conferenceableConnections));
894 }
895
896 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700897 public void onDestroyed(Conference conference) {
898 removeConference(conference);
899 }
900
901 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800902 public void onConnectionCapabilitiesChanged(
903 Conference conference,
904 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700905 String id = mIdByConference.get(conference);
906 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800907 Connection.capabilitiesToString(connectionCapabilities));
908 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700909 }
Rekha Kumar07366812015-03-24 16:42:31 -0700910
911 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700912 public void onConnectionPropertiesChanged(
913 Conference conference,
914 int connectionProperties) {
915 String id = mIdByConference.get(conference);
916 Log.d(this, "call capabilities: conference: %s",
917 Connection.propertiesToString(connectionProperties));
918 mAdapter.setConnectionProperties(id, connectionProperties);
919 }
920
921 @Override
Rekha Kumar07366812015-03-24 16:42:31 -0700922 public void onVideoStateChanged(Conference c, int videoState) {
923 String id = mIdByConference.get(c);
924 Log.d(this, "onVideoStateChanged set video state %d", videoState);
925 mAdapter.setVideoState(id, videoState);
926 }
927
928 @Override
929 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
930 String id = mIdByConference.get(c);
931 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
932 videoProvider);
933 mAdapter.setVideoProvider(id, videoProvider);
934 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700935
936 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700937 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
938 String id = mIdByConference.get(conference);
Tyler Gunndee56a82016-03-23 16:06:34 -0700939 if (id != null) {
940 mAdapter.setStatusHints(id, statusHints);
941 }
Andrew Leeedc625f2015-04-14 13:38:12 -0700942 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700943
944 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700945 public void onExtrasChanged(Conference c, Bundle extras) {
946 String id = mIdByConference.get(c);
947 if (id != null) {
948 mAdapter.putExtras(id, extras);
949 }
950 }
951
952 @Override
953 public void onExtrasRemoved(Conference c, List<String> keys) {
954 String id = mIdByConference.get(c);
955 if (id != null) {
956 mAdapter.removeExtras(id, keys);
957 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700958 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700959 };
960
Ihab Awad542e0ea2014-05-16 10:22:16 -0700961 private final Connection.Listener mConnectionListener = new Connection.Listener() {
962 @Override
963 public void onStateChanged(Connection c, int state) {
964 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700965 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700966 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700967 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700968 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700969 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700970 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700971 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700972 break;
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700973 case Connection.STATE_PULLING_CALL:
974 mAdapter.setPulling(id);
975 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700976 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700977 // Handled in onDisconnected()
978 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700979 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700980 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700981 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700982 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700983 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700984 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700985 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700986 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700987 break;
988 }
989 }
990
991 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700992 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700993 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700994 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700995 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700996 }
997
998 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700999 public void onVideoStateChanged(Connection c, int videoState) {
1000 String id = mIdByConnection.get(c);
1001 Log.d(this, "Adapter set video state %d", videoState);
1002 mAdapter.setVideoState(id, videoState);
1003 }
1004
1005 @Override
Andrew Lee100e2932014-09-08 15:34:24 -07001006 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -07001007 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001008 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -07001009 }
1010
1011 @Override
1012 public void onCallerDisplayNameChanged(
1013 Connection c, String callerDisplayName, int presentation) {
1014 String id = mIdByConnection.get(c);
1015 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001016 }
1017
1018 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -07001019 public void onDestroyed(Connection c) {
1020 removeConnection(c);
1021 }
Ihab Awadf8358972014-05-28 16:46:42 -07001022
1023 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001024 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -07001025 String id = mIdByConnection.get(c);
1026 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001027 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -07001028 }
1029
1030 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -08001031 public void onPostDialChar(Connection c, char nextChar) {
1032 String id = mIdByConnection.get(c);
1033 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
1034 mAdapter.onPostDialChar(id, nextChar);
1035 }
1036
1037 @Override
Andrew Lee100e2932014-09-08 15:34:24 -07001038 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -07001039 String id = mIdByConnection.get(c);
1040 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -07001041 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -07001042 }
Santos Cordonb6939982014-06-04 20:20:58 -07001043
1044 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001045 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -07001046 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001047 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001048 Connection.capabilitiesToString(capabilities));
1049 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -07001050 }
1051
Santos Cordonb6939982014-06-04 20:20:58 -07001052 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -07001053 public void onConnectionPropertiesChanged(Connection c, int properties) {
1054 String id = mIdByConnection.get(c);
1055 Log.d(this, "properties: parcelableconnection: %s",
1056 Connection.propertiesToString(properties));
1057 mAdapter.setConnectionProperties(id, properties);
1058 }
1059
1060 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001061 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001062 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -07001063 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
1064 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001065 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001066 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001067
1068 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001069 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001070 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001071 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001072 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001073
1074 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001075 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001076 String id = mIdByConnection.get(c);
1077 mAdapter.setStatusHints(id, statusHints);
1078 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -07001079
1080 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001081 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001082 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -07001083 mAdapter.setConferenceableConnections(
1084 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001085 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001086 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001087
1088 @Override
1089 public void onConferenceChanged(Connection connection, Conference conference) {
1090 String id = mIdByConnection.get(connection);
1091 if (id != null) {
1092 String conferenceId = null;
1093 if (conference != null) {
1094 conferenceId = mIdByConference.get(conference);
1095 }
1096 mAdapter.setIsConferenced(id, conferenceId);
1097 }
1098 }
Anthony Lee17455a32015-04-24 15:25:29 -07001099
1100 @Override
1101 public void onConferenceMergeFailed(Connection connection) {
1102 String id = mIdByConnection.get(connection);
1103 if (id != null) {
1104 mAdapter.onConferenceMergeFailed(id);
1105 }
1106 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001107
1108 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001109 public void onExtrasChanged(Connection c, Bundle extras) {
1110 String id = mIdByConnection.get(c);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001111 if (id != null) {
Tyler Gunndee56a82016-03-23 16:06:34 -07001112 mAdapter.putExtras(id, extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001113 }
1114 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001115
Tyler Gunnf5035432017-01-09 09:43:12 -08001116 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001117 public void onExtrasRemoved(Connection c, List<String> keys) {
1118 String id = mIdByConnection.get(c);
1119 if (id != null) {
1120 mAdapter.removeExtras(id, keys);
1121 }
1122 }
1123
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001124 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001125 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001126 String id = mIdByConnection.get(connection);
1127 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001128 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001129 }
1130 }
Tyler Gunnf5035432017-01-09 09:43:12 -08001131
1132 @Override
1133 public void onAudioRouteChanged(Connection c, int audioRoute) {
1134 String id = mIdByConnection.get(c);
1135 if (id != null) {
1136 mAdapter.setAudioRoute(id, audioRoute);
1137 }
1138 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001139 };
1140
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001141 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -07001142 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001143 public final IBinder onBind(Intent intent) {
1144 return mBinder;
1145 }
1146
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001147 /** {@inheritDoc} */
1148 @Override
1149 public boolean onUnbind(Intent intent) {
1150 endAllConnections();
1151 return super.onUnbind(intent);
1152 }
1153
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001154 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001155 * This can be used by telecom to either create a new outgoing call or attach to an existing
1156 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001157 * createConnection util a connection service cancels the process or completes it successfully.
1158 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001159 private void createConnection(
1160 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001161 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -07001162 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -07001163 boolean isIncoming,
1164 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001165 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001166 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
1167 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -07001168 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001169
Yorke Leec3cf9822014-10-02 09:38:39 -07001170 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
1171 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -07001172 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001173 Log.d(this, "createConnection, connection: %s", connection);
1174 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001175 connection = Connection.createFailedConnection(
1176 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001177 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001178
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001179 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001180 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001181 addConnection(callId, connection);
1182 }
1183
Andrew Lee100e2932014-09-08 15:34:24 -07001184 Uri address = connection.getAddress();
1185 String number = address == null ? "null" : address.getSchemeSpecificPart();
Tyler Gunn720c6642016-03-22 09:02:47 -07001186 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s, properties: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001187 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001188 Connection.stateToString(connection.getState()),
Tyler Gunn720c6642016-03-22 09:02:47 -07001189 Connection.capabilitiesToString(connection.getConnectionCapabilities()),
1190 Connection.propertiesToString(connection.getConnectionProperties()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001191
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001192 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -07001193 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001194 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -07001195 request,
1196 new ParcelableConnection(
1197 request.getAccountHandle(),
1198 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001199 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001200 connection.getConnectionProperties(),
Christine Hallstrom2830ce92016-11-30 16:06:42 -08001201 connection.getSupportedAudioRoutes(),
Andrew Lee100e2932014-09-08 15:34:24 -07001202 connection.getAddress(),
1203 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -07001204 connection.getCallerDisplayName(),
1205 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001206 connection.getVideoProvider() == null ?
1207 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001208 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -07001209 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001210 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001211 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -07001212 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001213 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001214 createIdList(connection.getConferenceables()),
1215 connection.getExtras()));
Tyler Gunnf5035432017-01-09 09:43:12 -08001216
1217 if (isIncoming && request.shouldShowIncomingCallUi() &&
1218 (connection.getConnectionProperties() & Connection.PROPERTY_SELF_MANAGED) ==
1219 Connection.PROPERTY_SELF_MANAGED) {
1220 // Tell ConnectionService to show its incoming call UX.
1221 connection.onShowIncomingCallUi();
1222 }
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001223 if (isUnknown) {
1224 triggerConferenceRecalculate();
1225 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001226 }
1227
Tyler Gunn44e01912017-01-31 10:49:05 -08001228 private void createConnectionFailed(final String callId, final ConnectionRequest request,
1229 boolean isIncoming) {
1230
1231 Log.i(this, "createConnectionFailed %s", callId);
1232 if (isIncoming) {
1233 onCreateIncomingConnectionFailed(request);
1234 } else {
1235 onCreateOutgoingConnectionFailed(request);
1236 }
1237 }
1238
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001239 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001240 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001241 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001242 }
1243
Tyler Gunnbe74de02014-08-29 14:51:48 -07001244 private void answerVideo(String callId, int videoState) {
1245 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001246 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001247 }
1248
Tyler Gunnbe74de02014-08-29 14:51:48 -07001249 private void answer(String callId) {
1250 Log.d(this, "answer %s", callId);
1251 findConnectionForAction(callId, "answer").onAnswer();
1252 }
1253
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001254 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001255 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001256 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001257 }
1258
Bryce Lee81901682015-08-28 16:38:02 -07001259 private void reject(String callId, String rejectWithMessage) {
1260 Log.d(this, "reject %s with message", callId);
1261 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
1262 }
1263
Bryce Leecac50772015-11-17 15:13:29 -08001264 private void silence(String callId) {
1265 Log.d(this, "silence %s", callId);
1266 findConnectionForAction(callId, "silence").onSilence();
1267 }
1268
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001269 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001270 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001271 if (mConnectionById.containsKey(callId)) {
1272 findConnectionForAction(callId, "disconnect").onDisconnect();
1273 } else {
1274 findConferenceForAction(callId, "disconnect").onDisconnect();
1275 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001276 }
1277
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001278 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001279 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001280 if (mConnectionById.containsKey(callId)) {
1281 findConnectionForAction(callId, "hold").onHold();
1282 } else {
1283 findConferenceForAction(callId, "hold").onHold();
1284 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001285 }
1286
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001287 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001288 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001289 if (mConnectionById.containsKey(callId)) {
1290 findConnectionForAction(callId, "unhold").onUnhold();
1291 } else {
1292 findConferenceForAction(callId, "unhold").onUnhold();
1293 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001294 }
1295
Yorke Lee4af59352015-05-13 14:14:54 -07001296 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
1297 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001298 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -07001299 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1300 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001301 } else {
Yorke Lee4af59352015-05-13 14:14:54 -07001302 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1303 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001304 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001305 }
1306
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001307 private void playDtmfTone(String callId, char digit) {
1308 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001309 if (mConnectionById.containsKey(callId)) {
1310 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1311 } else {
1312 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1313 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001314 }
1315
1316 private void stopDtmfTone(String callId) {
1317 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001318 if (mConnectionById.containsKey(callId)) {
1319 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
1320 } else {
1321 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
1322 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001323 }
1324
Santos Cordon823fd3c2014-08-07 18:35:18 -07001325 private void conference(String callId1, String callId2) {
1326 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -07001327
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001328 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001329 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001330 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001331 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001332 conference2 = findConferenceForAction(callId2, "conference");
1333 if (conference2 == getNullConference()) {
1334 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
1335 callId2);
1336 return;
1337 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001338 }
Santos Cordonb6939982014-06-04 20:20:58 -07001339
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001340 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -07001341 Connection connection1 = findConnectionForAction(callId1, "conference");
1342 if (connection1 == getNullConnection()) {
1343 Conference conference1 = findConferenceForAction(callId1, "addConnection");
1344 if (conference1 == getNullConference()) {
1345 Log.w(this,
1346 "Connection1 or Conference1 missing in conference request %s.",
1347 callId1);
1348 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001349 // Call 1 is a conference.
1350 if (connection2 != getNullConnection()) {
1351 // Call 2 is a connection so merge via call 1 (conference).
1352 conference1.onMerge(connection2);
1353 } else {
1354 // Call 2 is ALSO a conference; this should never happen.
1355 Log.wtf(this, "There can only be one conference and an attempt was made to " +
1356 "merge two conferences.");
1357 return;
1358 }
Ihab Awad50e35062014-09-30 09:17:03 -07001359 }
1360 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001361 // Call 1 is a connection.
1362 if (conference2 != getNullConference()) {
1363 // Call 2 is a conference, so merge via call 2.
1364 conference2.onMerge(connection1);
1365 } else {
1366 // Call 2 is a connection, so merge together.
1367 onConference(connection1, connection2);
1368 }
Ihab Awad50e35062014-09-30 09:17:03 -07001369 }
Santos Cordon980acb92014-05-31 10:31:19 -07001370 }
1371
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001372 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -07001373 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -07001374
1375 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001376 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -07001377 Log.w(this, "Connection missing in conference request %s.", callId);
1378 return;
1379 }
1380
Santos Cordon0159ac02014-08-21 14:28:11 -07001381 Conference conference = connection.getConference();
1382 if (conference != null) {
1383 conference.onSeparate(connection);
1384 }
Santos Cordon980acb92014-05-31 10:31:19 -07001385 }
1386
Santos Cordona4868042014-09-04 17:39:22 -07001387 private void mergeConference(String callId) {
1388 Log.d(this, "mergeConference(%s)", callId);
1389 Conference conference = findConferenceForAction(callId, "mergeConference");
1390 if (conference != null) {
1391 conference.onMerge();
1392 }
1393 }
1394
1395 private void swapConference(String callId) {
1396 Log.d(this, "swapConference(%s)", callId);
1397 Conference conference = findConferenceForAction(callId, "swapConference");
1398 if (conference != null) {
1399 conference.onSwap();
1400 }
1401 }
1402
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001403 /**
1404 * Notifies a {@link Connection} of a request to pull an external call.
1405 *
1406 * See {@link Call#pullExternalCall()}.
1407 *
1408 * @param callId The ID of the call to pull.
1409 */
1410 private void pullExternalCall(String callId) {
1411 Log.d(this, "pullExternalCall(%s)", callId);
1412 Connection connection = findConnectionForAction(callId, "pullExternalCall");
1413 if (connection != null) {
1414 connection.onPullExternalCall();
1415 }
1416 }
1417
1418 /**
1419 * Notifies a {@link Connection} of a call event.
1420 *
1421 * See {@link Call#sendCallEvent(String, Bundle)}.
1422 *
1423 * @param callId The ID of the call receiving the event.
1424 * @param event The event.
1425 * @param extras Extras associated with the event.
1426 */
1427 private void sendCallEvent(String callId, String event, Bundle extras) {
1428 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
1429 Connection connection = findConnectionForAction(callId, "sendCallEvent");
1430 if (connection != null) {
1431 connection.onCallEvent(event, extras);
1432 }
1433
1434 }
1435
Tyler Gunndee56a82016-03-23 16:06:34 -07001436 /**
1437 * Notifies a {@link Connection} or {@link Conference} of a change to the extras from Telecom.
1438 * <p>
1439 * These extra changes can originate from Telecom itself, or from an {@link InCallService} via
1440 * the {@link android.telecom.Call#putExtra(String, boolean)},
1441 * {@link android.telecom.Call#putExtra(String, int)},
1442 * {@link android.telecom.Call#putExtra(String, String)},
1443 * {@link Call#removeExtras(List)}.
1444 *
1445 * @param callId The ID of the call receiving the event.
1446 * @param extras The new extras bundle.
1447 */
1448 private void handleExtrasChanged(String callId, Bundle extras) {
1449 Log.d(this, "handleExtrasChanged(%s, %s)", callId, extras);
1450 if (mConnectionById.containsKey(callId)) {
1451 findConnectionForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1452 } else if (mConferenceById.containsKey(callId)) {
1453 findConferenceForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1454 }
1455 }
1456
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001457 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001458 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001459 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001460 }
1461
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001462 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001463 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001464 // No need to query again if we already did it.
1465 return;
1466 }
1467
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001468 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -07001469 @Override
1470 public void onResult(
1471 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001472 final List<IBinder> services) {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001473 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oR", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001474 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001475 public void loggedRun() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001476 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001477 mRemoteConnectionManager.addConnectionService(
1478 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001479 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -07001480 }
Ihab Awad5d0410f2014-07-30 10:07:40 -07001481 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001482 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -07001483 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001484 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001485 }
1486
1487 @Override
1488 public void onError() {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001489 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oE", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001490 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001491 public void loggedRun() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001492 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -07001493 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001494 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001495 }
1496 });
1497 }
1498
Ihab Awadf8b69882014-07-25 15:14:01 -07001499 /**
1500 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001501 * incoming request. This is used by {@code ConnectionService}s that are registered with
1502 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
1503 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001504 *
1505 * @param connectionManagerPhoneAccount See description at
1506 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1507 * @param request Details about the incoming call.
1508 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1509 * not handle the call.
1510 */
1511 public final RemoteConnection createRemoteIncomingConnection(
1512 PhoneAccountHandle connectionManagerPhoneAccount,
1513 ConnectionRequest request) {
1514 return mRemoteConnectionManager.createRemoteConnection(
1515 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -07001516 }
1517
1518 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001519 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001520 * outgoing request. This is used by {@code ConnectionService}s that are registered with
1521 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
1522 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001523 *
1524 * @param connectionManagerPhoneAccount See description at
1525 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Cuihtlauac ALVARADO0b3b2a52016-09-13 14:49:41 +02001526 * @param request Details about the outgoing call.
Ihab Awadf8b69882014-07-25 15:14:01 -07001527 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1528 * not handle the call.
1529 */
1530 public final RemoteConnection createRemoteOutgoingConnection(
1531 PhoneAccountHandle connectionManagerPhoneAccount,
1532 ConnectionRequest request) {
1533 return mRemoteConnectionManager.createRemoteConnection(
1534 connectionManagerPhoneAccount, request, false);
1535 }
1536
1537 /**
Santos Cordona663f862014-10-29 13:49:58 -07001538 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1539 * {@link RemoteConnection}s should be merged into a conference call.
1540 * <p>
1541 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1542 * be invoked.
1543 *
1544 * @param remoteConnection1 The first of the remote connections to conference.
1545 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001546 */
1547 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001548 RemoteConnection remoteConnection1,
1549 RemoteConnection remoteConnection2) {
1550 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001551 }
1552
1553 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001554 * Adds a new conference call. When a conference call is created either as a result of an
1555 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1556 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1557 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1558 *
1559 * @param conference The new conference object.
1560 */
1561 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001562 Log.d(this, "addConference: conference=%s", conference);
1563
Santos Cordon823fd3c2014-08-07 18:35:18 -07001564 String id = addConferenceInternal(conference);
1565 if (id != null) {
1566 List<String> connectionIds = new ArrayList<>(2);
1567 for (Connection connection : conference.getConnections()) {
1568 if (mIdByConnection.containsKey(connection)) {
1569 connectionIds.add(mIdByConnection.get(connection));
1570 }
1571 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001572 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001573 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001574 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001575 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001576 conference.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001577 conference.getConnectionProperties(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001578 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001579 conference.getVideoProvider() == null ?
1580 null : conference.getVideoProvider().getInterface(),
1581 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001582 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001583 conference.getStatusHints(),
1584 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001585
Santos Cordon823fd3c2014-08-07 18:35:18 -07001586 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001587 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1588 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001589
1590 // Go through any child calls and set the parent.
1591 for (Connection connection : conference.getConnections()) {
1592 String connectionId = mIdByConnection.get(connection);
1593 if (connectionId != null) {
1594 mAdapter.setIsConferenced(connectionId, id);
1595 }
1596 }
1597 }
1598 }
1599
1600 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001601 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1602 * connection.
1603 *
1604 * @param phoneAccountHandle The phone account handle for the connection.
1605 * @param connection The connection to add.
1606 */
1607 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1608 Connection connection) {
1609
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001610 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001611 if (id != null) {
1612 List<String> emptyList = new ArrayList<>(0);
1613
1614 ParcelableConnection parcelableConnection = new ParcelableConnection(
1615 phoneAccountHandle,
1616 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001617 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001618 connection.getConnectionProperties(),
Christine Hallstrom2830ce92016-11-30 16:06:42 -08001619 connection.getSupportedAudioRoutes(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001620 connection.getAddress(),
1621 connection.getAddressPresentation(),
1622 connection.getCallerDisplayName(),
1623 connection.getCallerDisplayNamePresentation(),
1624 connection.getVideoProvider() == null ?
1625 null : connection.getVideoProvider().getInterface(),
1626 connection.getVideoState(),
1627 connection.isRingbackRequested(),
1628 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001629 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001630 connection.getStatusHints(),
1631 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001632 emptyList,
1633 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001634 mAdapter.addExistingConnection(id, parcelableConnection);
1635 }
1636 }
1637
1638 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001639 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1640 * has taken responsibility.
1641 *
1642 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001643 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001644 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001645 return mConnectionById.values();
1646 }
1647
1648 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001649 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1650 * has taken responsibility.
1651 *
1652 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1653 */
1654 public final Collection<Conference> getAllConferences() {
1655 return mConferenceById.values();
1656 }
1657
1658 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001659 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1660 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001661 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001662 * @param connectionManagerPhoneAccount See description at
1663 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1664 * @param request Details about the incoming call.
1665 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1666 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001667 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001668 public Connection onCreateIncomingConnection(
1669 PhoneAccountHandle connectionManagerPhoneAccount,
1670 ConnectionRequest request) {
1671 return null;
1672 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001673
1674 /**
Tyler Gunnf5035432017-01-09 09:43:12 -08001675 * Called by Telecom to inform the {@link ConnectionService} that its request to create a new
1676 * incoming {@link Connection} was denied.
1677 * <p>
1678 * Used when a self-managed {@link ConnectionService} attempts to create a new incoming
1679 * {@link Connection}, but Telecom has determined that the call cannot be allowed at this time.
1680 * The {@link ConnectionService} is responsible for silently rejecting the new incoming
1681 * {@link Connection}.
1682 * <p>
1683 * See {@link TelecomManager#isIncomingCallPermitted(PhoneAccountHandle)} for more information.
1684 *
1685 * @param request The incoming connection request.
1686 */
1687 public void onCreateIncomingConnectionFailed(ConnectionRequest request) {
1688 }
1689
1690 /**
1691 * Called by Telecom to inform the {@link ConnectionService} that its request to create a new
1692 * outgoing {@link Connection} was denied.
1693 * <p>
1694 * Used when a self-managed {@link ConnectionService} attempts to create a new outgoing
1695 * {@link Connection}, but Telecom has determined that the call cannot be placed at this time.
1696 * The {@link ConnectionService} is responisible for informing the user that the
1697 * {@link Connection} cannot be made at this time.
1698 * <p>
1699 * See {@link TelecomManager#isOutgoingCallPermitted(PhoneAccountHandle)} for more information.
1700 *
1701 * @param request The outgoing connection request.
1702 */
1703 public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {
1704 }
1705
1706 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001707 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1708 * Connection is part of a conference controller but is not yet added to Connection
1709 * Service and hence cannot be added to the conference call.
1710 *
1711 * @hide
1712 */
1713 public void triggerConferenceRecalculate() {
1714 }
1715
1716 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001717 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1718 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001719 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001720 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1721 * this call.
1722 * <p>
1723 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1724 * has registered one or more {@code PhoneAccount}s having
1725 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1726 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1727 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1728 * making the connection.
1729 * <p>
1730 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1731 * being asked to make a direct connection. The
1732 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1733 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1734 * making the connection.
1735 * @param request Details about the outgoing call.
1736 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001737 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001738 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001739 public Connection onCreateOutgoingConnection(
1740 PhoneAccountHandle connectionManagerPhoneAccount,
1741 ConnectionRequest request) {
1742 return null;
1743 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001744
1745 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001746 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1747 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1748 * call created using
1749 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1750 *
Yorke Lee770ed6e2014-10-06 18:58:52 -07001751 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001752 */
1753 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1754 ConnectionRequest request) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001755 return null;
Yorke Leec3cf9822014-10-02 09:38:39 -07001756 }
1757
1758 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001759 * Conference two specified connections. Invoked when the user has made a request to merge the
1760 * specified connections into a conference call. In response, the connection service should
1761 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001762 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001763 * @param connection1 A connection to merge into a conference call.
1764 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001765 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001766 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001767
Santos Cordona663f862014-10-29 13:49:58 -07001768 /**
1769 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1770 * When this method is invoked, this {@link ConnectionService} should create its own
1771 * representation of the conference call and send it to telecom using {@link #addConference}.
1772 * <p>
1773 * This is only relevant to {@link ConnectionService}s which are registered with
1774 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1775 *
1776 * @param conference The remote conference call.
1777 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001778 public void onRemoteConferenceAdded(RemoteConference conference) {}
1779
Santos Cordon823fd3c2014-08-07 18:35:18 -07001780 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001781 * Called when an existing connection is added remotely.
1782 * @param connection The existing connection which was added.
1783 */
1784 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1785
1786 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001787 * @hide
1788 */
1789 public boolean containsConference(Conference conference) {
1790 return mIdByConference.containsKey(conference);
1791 }
1792
Ihab Awadb8e85c72014-08-23 20:34:57 -07001793 /** {@hide} */
1794 void addRemoteConference(RemoteConference remoteConference) {
1795 onRemoteConferenceAdded(remoteConference);
1796 }
1797
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001798 /** {@hide} */
1799 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1800 onRemoteExistingConnectionAdded(remoteConnection);
1801 }
1802
Ihab Awad5d0410f2014-07-30 10:07:40 -07001803 private void onAccountsInitialized() {
1804 mAreAccountsInitialized = true;
1805 for (Runnable r : mPreInitializationConnectionRequests) {
1806 r.run();
1807 }
1808 mPreInitializationConnectionRequests.clear();
1809 }
1810
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001811 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001812 * Adds an existing connection to the list of connections, identified by a new call ID unique
1813 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001814 *
1815 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001816 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001817 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001818 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1819 String id;
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07001820
1821 if (connection.getExtras() != null && connection.getExtras()
1822 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1823 id = connection.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1824 Log.d(this, "addExistingConnectionInternal - conn %s reusing original id %s",
1825 connection.getTelecomCallId(), id);
1826 } else if (handle == null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001827 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1828 // so just use a random UUID.
1829 id = UUID.randomUUID().toString();
1830 } else {
1831 // Phone account handle was provided, so use the ConnectionService class name as a
1832 // prefix for a unique incremental call ID.
1833 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1834 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001835 addConnection(id, connection);
1836 return id;
1837 }
1838
Ihab Awad542e0ea2014-05-16 10:22:16 -07001839 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001840 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001841 mConnectionById.put(callId, connection);
1842 mIdByConnection.put(connection, callId);
1843 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001844 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001845 }
1846
Anthony Lee30e65842014-11-06 16:30:53 -08001847 /** {@hide} */
1848 protected void removeConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001849 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001850 connection.removeConnectionListener(mConnectionListener);
Chenjie Luoe370b532016-05-12 16:59:43 -07001851 String id = mIdByConnection.get(connection);
1852 if (id != null) {
1853 mConnectionById.remove(id);
1854 mIdByConnection.remove(connection);
1855 mAdapter.removeCall(id);
1856 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001857 }
1858
Santos Cordon823fd3c2014-08-07 18:35:18 -07001859 private String addConferenceInternal(Conference conference) {
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07001860 String originalId = null;
1861 if (conference.getExtras() != null && conference.getExtras()
1862 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1863 originalId = conference.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1864 Log.d(this, "addConferenceInternal: conf %s reusing original id %s",
1865 conference.getTelecomCallId(),
1866 originalId);
1867 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001868 if (mIdByConference.containsKey(conference)) {
1869 Log.w(this, "Re-adding an existing conference: %s.", conference);
1870 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001871 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1872 // cannot determine a ConnectionService class name to associate with the ID, so use
1873 // a unique UUID (for now).
Tyler Gunncd6ccfd2016-10-17 15:48:19 -07001874 String id = originalId == null ? UUID.randomUUID().toString() : originalId;
Santos Cordon823fd3c2014-08-07 18:35:18 -07001875 mConferenceById.put(id, conference);
1876 mIdByConference.put(conference, id);
1877 conference.addListener(mConferenceListener);
1878 return id;
1879 }
1880
1881 return null;
1882 }
1883
1884 private void removeConference(Conference conference) {
1885 if (mIdByConference.containsKey(conference)) {
1886 conference.removeListener(mConferenceListener);
1887
1888 String id = mIdByConference.get(conference);
1889 mConferenceById.remove(id);
1890 mIdByConference.remove(conference);
1891 mAdapter.removeCall(id);
1892 }
1893 }
1894
Ihab Awad542e0ea2014-05-16 10:22:16 -07001895 private Connection findConnectionForAction(String callId, String action) {
1896 if (mConnectionById.containsKey(callId)) {
1897 return mConnectionById.get(callId);
1898 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001899 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001900 return getNullConnection();
1901 }
1902
1903 static synchronized Connection getNullConnection() {
1904 if (sNullConnection == null) {
1905 sNullConnection = new Connection() {};
1906 }
1907 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001908 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001909
1910 private Conference findConferenceForAction(String conferenceId, String action) {
1911 if (mConferenceById.containsKey(conferenceId)) {
1912 return mConferenceById.get(conferenceId);
1913 }
1914 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1915 return getNullConference();
1916 }
1917
Ihab Awadb8e85c72014-08-23 20:34:57 -07001918 private List<String> createConnectionIdList(List<Connection> connections) {
1919 List<String> ids = new ArrayList<>();
1920 for (Connection c : connections) {
1921 if (mIdByConnection.containsKey(c)) {
1922 ids.add(mIdByConnection.get(c));
1923 }
1924 }
1925 Collections.sort(ids);
1926 return ids;
1927 }
1928
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001929 /**
1930 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001931 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001932 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001933 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001934 * @return List of string conference and call Ids.
1935 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001936 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001937 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001938 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001939 // Only allow Connection and Conference conferenceables.
1940 if (c instanceof Connection) {
1941 Connection connection = (Connection) c;
1942 if (mIdByConnection.containsKey(connection)) {
1943 ids.add(mIdByConnection.get(connection));
1944 }
1945 } else if (c instanceof Conference) {
1946 Conference conference = (Conference) c;
1947 if (mIdByConference.containsKey(conference)) {
1948 ids.add(mIdByConference.get(conference));
1949 }
1950 }
1951 }
1952 Collections.sort(ids);
1953 return ids;
1954 }
1955
Santos Cordon0159ac02014-08-21 14:28:11 -07001956 private Conference getNullConference() {
1957 if (sNullConference == null) {
1958 sNullConference = new Conference(null) {};
1959 }
1960 return sNullConference;
1961 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001962
1963 private void endAllConnections() {
1964 // Unbound from telecomm. We should end all connections and conferences.
1965 for (Connection connection : mIdByConnection.keySet()) {
1966 // only operate on top-level calls. Conference calls will be removed on their own.
1967 if (connection.getConference() == null) {
1968 connection.onDisconnect();
1969 }
1970 }
1971 for (Conference conference : mIdByConference.keySet()) {
1972 conference.onDisconnect();
1973 }
1974 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001975
1976 /**
1977 * Retrieves the next call ID as maintainted by the connection service.
1978 *
1979 * @return The call ID.
1980 */
1981 private int getNextCallId() {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001982 synchronized (mIdSyncRoot) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001983 return ++mId;
1984 }
1985 }
Santos Cordon980acb92014-05-31 10:31:19 -07001986}