blob: 66d704b2fe816c0e1a9c3a3e6aae3284c818c499 [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 Ebinger3445f822016-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/**
Santos Cordon895d4b82015-06-25 16:41:48 -070045 * An abstract service that should be implemented by any apps which can make phone calls (VoIP or
46 * otherwise) and want those calls to be integrated into the built-in phone app.
Santos Cordona663f862014-10-29 13:49:58 -070047 * Once implemented, the {@code ConnectionService} needs two additional steps before it will be
48 * integrated into the phone app:
49 * <p>
50 * 1. <i>Registration in AndroidManifest.xml</i>
51 * <br/>
52 * <pre>
53 * &lt;service android:name="com.example.package.MyConnectionService"
54 * android:label="@string/some_label_for_my_connection_service"
Yorke Lee249c12e2015-05-13 15:59:29 -070055 * android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"&gt;
Santos Cordona663f862014-10-29 13:49:58 -070056 * &lt;intent-filter&gt;
57 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
58 * &lt;/intent-filter&gt;
59 * &lt;/service&gt;
60 * </pre>
61 * <p>
62 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
63 * <br/>
64 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
65 * <p>
Santos Cordon895d4b82015-06-25 16:41:48 -070066 * Once registered and enabled by the user in the phone app settings, telecom will bind to a
Santos Cordona663f862014-10-29 13:49:58 -070067 * {@code ConnectionService} implementation when it wants that {@code ConnectionService} to place
68 * a call or the service has indicated that is has an incoming call through
69 * {@link TelecomManager#addNewIncomingCall}. The {@code ConnectionService} can then expect a call
70 * to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection} wherein it
71 * should provide a new instance of a {@link Connection} object. It is through this
72 * {@link Connection} object that telecom receives state updates and the {@code ConnectionService}
73 * receives call-commands such as answer, reject, hold and disconnect.
74 * <p>
75 * When there are no more live calls, telecom will unbind from the {@code ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070076 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070077public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070078 /**
79 * The {@link Intent} that must be declared as handled by the service.
80 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070081 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070082 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070083
Ihab Awad542e0ea2014-05-16 10:22:16 -070084 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070085 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070086
Brad Ebinger3445f822016-10-24 16:40:49 -070087 // Session Definitions
88 private static final String SESSION_HANDLER = "H.";
89 private static final String SESSION_ADD_CS_ADAPTER = "CS.aCSA";
90 private static final String SESSION_REMOVE_CS_ADAPTER = "CS.rCSA";
91 private static final String SESSION_CREATE_CONN = "CS.crCo";
92 private static final String SESSION_ABORT = "CS.ab";
93 private static final String SESSION_ANSWER = "CS.an";
94 private static final String SESSION_ANSWER_VIDEO = "CS.anV";
95 private static final String SESSION_REJECT = "CS.r";
96 private static final String SESSION_REJECT_MESSAGE = "CS.rWM";
97 private static final String SESSION_SILENCE = "CS.s";
98 private static final String SESSION_DISCONNECT = "CS.d";
99 private static final String SESSION_HOLD = "CS.h";
100 private static final String SESSION_UNHOLD = "CS.u";
101 private static final String SESSION_CALL_AUDIO_SC = "CS.cASC";
102 private static final String SESSION_PLAY_DTMF = "CS.pDT";
103 private static final String SESSION_STOP_DTMF = "CS.sDT";
104 private static final String SESSION_CONFERENCE = "CS.c";
105 private static final String SESSION_SPLIT_CONFERENCE = "CS.sFC";
106 private static final String SESSION_MERGE_CONFERENCE = "CS.mC";
107 private static final String SESSION_SWAP_CONFERENCE = "CS.sC";
108 private static final String SESSION_POST_DIAL_CONT = "CS.oPDC";
109 private static final String SESSION_PULL_EXTERNAL_CALL = "CS.pEC";
110 private static final String SESSION_SEND_CALL_EVENT = "CS.sCE";
111 private static final String SESSION_EXTRAS_CHANGED = "CS.oEC";
112
Ihab Awad8aecfed2014-08-08 17:06:11 -0700113 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700114 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700115 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700116 private static final int MSG_ANSWER = 4;
117 private static final int MSG_REJECT = 5;
118 private static final int MSG_DISCONNECT = 6;
119 private static final int MSG_HOLD = 7;
120 private static final int MSG_UNHOLD = 8;
Yorke Lee4af59352015-05-13 14:14:54 -0700121 private static final int MSG_ON_CALL_AUDIO_STATE_CHANGED = 9;
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700122 private static final int MSG_PLAY_DTMF_TONE = 10;
123 private static final int MSG_STOP_DTMF_TONE = 11;
124 private static final int MSG_CONFERENCE = 12;
125 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700126 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700127 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700128 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700129 private static final int MSG_MERGE_CONFERENCE = 18;
130 private static final int MSG_SWAP_CONFERENCE = 19;
Bryce Lee81901682015-08-28 16:38:02 -0700131 private static final int MSG_REJECT_WITH_MESSAGE = 20;
Bryce Leecac50772015-11-17 15:13:29 -0800132 private static final int MSG_SILENCE = 21;
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700133 private static final int MSG_PULL_EXTERNAL_CALL = 22;
134 private static final int MSG_SEND_CALL_EVENT = 23;
Tyler Gunndee56a82016-03-23 16:06:34 -0700135 private static final int MSG_ON_EXTRAS_CHANGED = 24;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700136
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700137 private static Connection sNullConnection;
138
mike dooley95e80702014-09-18 14:07:52 -0700139 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
140 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
141 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
142 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700143 private final RemoteConnectionManager mRemoteConnectionManager =
144 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700145 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700146 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700147
Santos Cordon823fd3c2014-08-07 18:35:18 -0700148 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700149 private Conference sNullConference;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700150 private Object mIdSyncRoot = new Object();
151 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700152
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700153 private final IBinder mBinder = new IConnectionService.Stub() {
154 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700155 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter,
156 Session.Info sessionInfo) {
157 Log.startSession(sessionInfo, SESSION_ADD_CS_ADAPTER);
158 try {
159 SomeArgs args = SomeArgs.obtain();
160 args.arg1 = adapter;
161 args.arg2 = Log.createSubsession();
162 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
163 } finally {
164 Log.endSession();
165 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700166 }
167
Brad Ebinger3445f822016-10-24 16:40:49 -0700168 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter,
169 Session.Info sessionInfo) {
170 Log.startSession(sessionInfo, SESSION_REMOVE_CS_ADAPTER);
171 try {
172 SomeArgs args = SomeArgs.obtain();
173 args.arg1 = adapter;
174 args.arg2 = Log.createSubsession();
175 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, args).sendToTarget();
176 } finally {
177 Log.endSession();
178 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700179 }
180
181 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700182 public void createConnection(
183 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700184 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700185 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700186 boolean isIncoming,
Brad Ebinger3445f822016-10-24 16:40:49 -0700187 boolean isUnknown,
188 Session.Info sessionInfo) {
189 Log.startSession(sessionInfo, SESSION_CREATE_CONN);
190 try {
191 SomeArgs args = SomeArgs.obtain();
192 args.arg1 = connectionManagerPhoneAccount;
193 args.arg2 = id;
194 args.arg3 = request;
195 args.arg4 = Log.createSubsession();
196 args.argi1 = isIncoming ? 1 : 0;
197 args.argi2 = isUnknown ? 1 : 0;
198 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
199 } finally {
200 Log.endSession();
201 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700202 }
203
204 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700205 public void abort(String callId, Session.Info sessionInfo) {
206 Log.startSession(sessionInfo, SESSION_ABORT);
207 try {
208 SomeArgs args = SomeArgs.obtain();
209 args.arg1 = callId;
210 args.arg2 = Log.createSubsession();
211 mHandler.obtainMessage(MSG_ABORT, args).sendToTarget();
212 } finally {
213 Log.endSession();
214 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700215 }
216
217 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700218 public void answerVideo(String callId, int videoState, Session.Info sessionInfo) {
219 Log.startSession(sessionInfo, SESSION_ANSWER_VIDEO);
220 try {
221 SomeArgs args = SomeArgs.obtain();
222 args.arg1 = callId;
223 args.arg2 = Log.createSubsession();
224 args.argi1 = videoState;
225 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
226 } finally {
227 Log.endSession();
228 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700229 }
230
231 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700232 public void answer(String callId, Session.Info sessionInfo) {
233 Log.startSession(sessionInfo, SESSION_ANSWER);
234 try {
235 SomeArgs args = SomeArgs.obtain();
236 args.arg1 = callId;
237 args.arg2 = Log.createSubsession();
238 mHandler.obtainMessage(MSG_ANSWER, args).sendToTarget();
239 } finally {
240 Log.endSession();
241 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700242 }
243
244 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700245 public void reject(String callId, Session.Info sessionInfo) {
246 Log.startSession(sessionInfo, SESSION_REJECT);
247 try {
248 SomeArgs args = SomeArgs.obtain();
249 args.arg1 = callId;
250 args.arg2 = Log.createSubsession();
251 mHandler.obtainMessage(MSG_REJECT, args).sendToTarget();
252 } finally {
253 Log.endSession();
254 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700255 }
256
257 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700258 public void rejectWithMessage(String callId, String message, Session.Info sessionInfo) {
259 Log.startSession(sessionInfo, SESSION_REJECT_MESSAGE);
260 try {
261 SomeArgs args = SomeArgs.obtain();
262 args.arg1 = callId;
263 args.arg2 = message;
264 args.arg3 = Log.createSubsession();
265 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
266 } finally {
267 Log.endSession();
268 }
Bryce Lee81901682015-08-28 16:38:02 -0700269 }
270
271 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700272 public void silence(String callId, Session.Info sessionInfo) {
273 Log.startSession(sessionInfo, SESSION_SILENCE);
274 try {
275 SomeArgs args = SomeArgs.obtain();
276 args.arg1 = callId;
277 args.arg2 = Log.createSubsession();
278 mHandler.obtainMessage(MSG_SILENCE, args).sendToTarget();
279 } finally {
280 Log.endSession();
281 }
Bryce Leecac50772015-11-17 15:13:29 -0800282 }
283
284 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700285 public void disconnect(String callId, Session.Info sessionInfo) {
286 Log.startSession(sessionInfo, SESSION_DISCONNECT);
287 try {
288 SomeArgs args = SomeArgs.obtain();
289 args.arg1 = callId;
290 args.arg2 = Log.createSubsession();
291 mHandler.obtainMessage(MSG_DISCONNECT, args).sendToTarget();
292 } finally {
293 Log.endSession();
294 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700295 }
296
297 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700298 public void hold(String callId, Session.Info sessionInfo) {
299 Log.startSession(sessionInfo, SESSION_HOLD);
300 try {
301 SomeArgs args = SomeArgs.obtain();
302 args.arg1 = callId;
303 args.arg2 = Log.createSubsession();
304 mHandler.obtainMessage(MSG_HOLD, args).sendToTarget();
305 } finally {
306 Log.endSession();
307 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700308 }
309
310 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700311 public void unhold(String callId, Session.Info sessionInfo) {
312 Log.startSession(sessionInfo, SESSION_UNHOLD);
313 try {
314 SomeArgs args = SomeArgs.obtain();
315 args.arg1 = callId;
316 args.arg2 = Log.createSubsession();
317 mHandler.obtainMessage(MSG_UNHOLD, args).sendToTarget();
318 } finally {
319 Log.endSession();
320 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700321 }
322
323 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700324 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState,
325 Session.Info sessionInfo) {
326 Log.startSession(sessionInfo, SESSION_CALL_AUDIO_SC);
327 try {
328 SomeArgs args = SomeArgs.obtain();
329 args.arg1 = callId;
330 args.arg2 = callAudioState;
331 args.arg3 = Log.createSubsession();
332 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
333 } finally {
334 Log.endSession();
335 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700336 }
337
338 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700339 public void playDtmfTone(String callId, char digit, Session.Info sessionInfo) {
340 Log.startSession(sessionInfo, SESSION_PLAY_DTMF);
341 try {
342 SomeArgs args = SomeArgs.obtain();
343 args.arg1 = digit;
344 args.arg2 = callId;
345 args.arg3 = Log.createSubsession();
346 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, args).sendToTarget();
347 } finally {
348 Log.endSession();
349 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700350 }
351
352 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700353 public void stopDtmfTone(String callId, Session.Info sessionInfo) {
354 Log.startSession(sessionInfo, SESSION_STOP_DTMF);
355 try {
356 SomeArgs args = SomeArgs.obtain();
357 args.arg1 = callId;
358 args.arg2 = Log.createSubsession();
359 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, args).sendToTarget();
360 } finally {
361 Log.endSession();
362 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700363 }
364
365 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700366 public void conference(String callId1, String callId2, Session.Info sessionInfo) {
367 Log.startSession(sessionInfo, SESSION_CONFERENCE);
368 try {
369 SomeArgs args = SomeArgs.obtain();
370 args.arg1 = callId1;
371 args.arg2 = callId2;
372 args.arg3 = Log.createSubsession();
373 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
374 } finally {
375 Log.endSession();
376 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700377 }
378
379 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700380 public void splitFromConference(String callId, Session.Info sessionInfo) {
381 Log.startSession(sessionInfo, SESSION_SPLIT_CONFERENCE);
382 try {
383 SomeArgs args = SomeArgs.obtain();
384 args.arg1 = callId;
385 args.arg2 = Log.createSubsession();
386 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, args).sendToTarget();
387 } finally {
388 Log.endSession();
389 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700390 }
391
392 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700393 public void mergeConference(String callId, Session.Info sessionInfo) {
394 Log.startSession(sessionInfo, SESSION_MERGE_CONFERENCE);
395 try {
396 SomeArgs args = SomeArgs.obtain();
397 args.arg1 = callId;
398 args.arg2 = Log.createSubsession();
399 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, args).sendToTarget();
400 } finally {
401 Log.endSession();
402 }
Santos Cordona4868042014-09-04 17:39:22 -0700403 }
404
405 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700406 public void swapConference(String callId, Session.Info sessionInfo) {
407 Log.startSession(sessionInfo, SESSION_SWAP_CONFERENCE);
408 try {
409 SomeArgs args = SomeArgs.obtain();
410 args.arg1 = callId;
411 args.arg2 = Log.createSubsession();
412 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, args).sendToTarget();
413 } finally {
414 Log.endSession();
415 }
Santos Cordona4868042014-09-04 17:39:22 -0700416 }
417
418 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700419 public void onPostDialContinue(String callId, boolean proceed, Session.Info sessionInfo) {
420 Log.startSession(sessionInfo, SESSION_POST_DIAL_CONT);
421 try {
422 SomeArgs args = SomeArgs.obtain();
423 args.arg1 = callId;
424 args.arg2 = Log.createSubsession();
425 args.argi1 = proceed ? 1 : 0;
426 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
427 } finally {
428 Log.endSession();
429 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700430 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700431
432 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700433 public void pullExternalCall(String callId, Session.Info sessionInfo) {
434 Log.startSession(sessionInfo, SESSION_PULL_EXTERNAL_CALL);
435 try {
436 SomeArgs args = SomeArgs.obtain();
437 args.arg1 = callId;
438 args.arg2 = Log.createSubsession();
439 mHandler.obtainMessage(MSG_PULL_EXTERNAL_CALL, args).sendToTarget();
440 } finally {
441 Log.endSession();
442 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700443 }
444
445 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700446 public void sendCallEvent(String callId, String event, Bundle extras,
447 Session.Info sessionInfo) {
448 Log.startSession(sessionInfo, SESSION_SEND_CALL_EVENT);
449 try {
450 SomeArgs args = SomeArgs.obtain();
451 args.arg1 = callId;
452 args.arg2 = event;
453 args.arg3 = extras;
454 args.arg4 = Log.createSubsession();
455 mHandler.obtainMessage(MSG_SEND_CALL_EVENT, args).sendToTarget();
456 } finally {
457 Log.endSession();
458 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700459 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700460
461 @Override
Brad Ebinger3445f822016-10-24 16:40:49 -0700462 public void onExtrasChanged(String callId, Bundle extras, Session.Info sessionInfo) {
463 Log.startSession(sessionInfo, SESSION_EXTRAS_CHANGED);
464 try {
465 SomeArgs args = SomeArgs.obtain();
466 args.arg1 = callId;
467 args.arg2 = extras;
468 args.arg3 = Log.createSubsession();
469 mHandler.obtainMessage(MSG_ON_EXTRAS_CHANGED, args).sendToTarget();
470 } finally {
471 Log.endSession();
472 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700473 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700474 };
475
476 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
477 @Override
478 public void handleMessage(Message msg) {
479 switch (msg.what) {
Brad Ebinger3445f822016-10-24 16:40:49 -0700480 case MSG_ADD_CONNECTION_SERVICE_ADAPTER: {
481 SomeArgs args = (SomeArgs) msg.obj;
482 try {
483 IConnectionServiceAdapter adapter = (IConnectionServiceAdapter) args.arg1;
484 Log.continueSession((Session) args.arg2,
485 SESSION_HANDLER + SESSION_ADD_CS_ADAPTER);
486 mAdapter.addAdapter(adapter);
487 onAdapterAttached();
488 } finally {
489 args.recycle();
490 Log.endSession();
491 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700492 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700493 }
494 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER: {
495 SomeArgs args = (SomeArgs) msg.obj;
496 try {
497 Log.continueSession((Session) args.arg2,
498 SESSION_HANDLER + SESSION_REMOVE_CS_ADAPTER);
499 mAdapter.removeAdapter((IConnectionServiceAdapter) args.arg1);
500 } finally {
501 args.recycle();
502 Log.endSession();
503 }
Ihab Awad8aecfed2014-08-08 17:06:11 -0700504 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700505 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700506 case MSG_CREATE_CONNECTION: {
507 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebinger3445f822016-10-24 16:40:49 -0700508 Log.continueSession((Session) args.arg4, SESSION_HANDLER + SESSION_CREATE_CONN);
Ihab Awadf8b69882014-07-25 15:14:01 -0700509 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700510 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700511 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700512 final String id = (String) args.arg2;
513 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700514 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700515 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700516 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700517 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700518 mPreInitializationConnectionRequests.add(new Runnable() {
519 @Override
520 public void run() {
521 createConnection(
522 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700523 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700524 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700525 isIncoming,
526 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700527 }
528 });
529 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700530 createConnection(
531 connectionManagerPhoneAccount,
532 id,
533 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700534 isIncoming,
535 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700536 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700537 } finally {
538 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700539 Log.endSession();
Ihab Awadf8b69882014-07-25 15:14:01 -0700540 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700541 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700542 }
Brad Ebinger3445f822016-10-24 16:40:49 -0700543 case MSG_ABORT: {
544 SomeArgs args = (SomeArgs) msg.obj;
545 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ABORT);
546 try {
547 abort((String) args.arg1);
548 } finally {
549 args.recycle();
550 Log.endSession();
551 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700552 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700553 }
554 case MSG_ANSWER: {
555 SomeArgs args = (SomeArgs) msg.obj;
556 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ANSWER);
557 try {
558 answer((String) args.arg1);
559 } finally {
560 args.recycle();
561 Log.endSession();
562 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700563 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700564 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700565 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700566 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebinger3445f822016-10-24 16:40:49 -0700567 Log.continueSession((Session) args.arg2,
568 SESSION_HANDLER + SESSION_ANSWER_VIDEO);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700569 try {
570 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700571 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700572 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700573 } finally {
574 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700575 Log.endSession();
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700576 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700577 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700578 }
Brad Ebinger3445f822016-10-24 16:40:49 -0700579 case MSG_REJECT: {
580 SomeArgs args = (SomeArgs) msg.obj;
581 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
582 try {
583 reject((String) args.arg1);
584 } finally {
585 args.recycle();
586 Log.endSession();
587 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700588 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700589 }
Bryce Lee81901682015-08-28 16:38:02 -0700590 case MSG_REJECT_WITH_MESSAGE: {
591 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebinger3445f822016-10-24 16:40:49 -0700592 Log.continueSession((Session) args.arg3,
593 SESSION_HANDLER + SESSION_REJECT_MESSAGE);
Bryce Lee81901682015-08-28 16:38:02 -0700594 try {
595 reject((String) args.arg1, (String) args.arg2);
596 } finally {
597 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700598 Log.endSession();
Bryce Lee81901682015-08-28 16:38:02 -0700599 }
600 break;
601 }
Brad Ebinger3445f822016-10-24 16:40:49 -0700602 case MSG_DISCONNECT: {
603 SomeArgs args = (SomeArgs) msg.obj;
604 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_DISCONNECT);
605 try {
606 disconnect((String) args.arg1);
607 } finally {
608 args.recycle();
609 Log.endSession();
610 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700611 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700612 }
613 case MSG_SILENCE: {
614 SomeArgs args = (SomeArgs) msg.obj;
615 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_SILENCE);
616 try {
617 silence((String) args.arg1);
618 } finally {
619 args.recycle();
620 Log.endSession();
621 }
Bryce Leecac50772015-11-17 15:13:29 -0800622 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700623 }
624 case MSG_HOLD: {
625 SomeArgs args = (SomeArgs) msg.obj;
626 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
627 try {
628 hold((String) args.arg1);
629 } finally {
630 args.recycle();
631 Log.endSession();
632 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700633 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700634 }
635 case MSG_UNHOLD: {
636 SomeArgs args = (SomeArgs) msg.obj;
637 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_UNHOLD);
638 try {
639 unhold((String) args.arg1);
640 } finally {
641 args.recycle();
642 Log.endSession();
643 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700644 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700645 }
Yorke Lee4af59352015-05-13 14:14:54 -0700646 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700647 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebinger3445f822016-10-24 16:40:49 -0700648 Log.continueSession((Session) args.arg3,
649 SESSION_HANDLER + SESSION_CALL_AUDIO_SC);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700650 try {
651 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700652 CallAudioState audioState = (CallAudioState) args.arg2;
653 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700654 } finally {
655 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700656 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700657 }
658 break;
659 }
Brad Ebinger3445f822016-10-24 16:40:49 -0700660 case MSG_PLAY_DTMF_TONE: {
661 SomeArgs args = (SomeArgs) msg.obj;
662 try {
663 Log.continueSession((Session) args.arg3,
664 SESSION_HANDLER + SESSION_PLAY_DTMF);
665 playDtmfTone((String) args.arg2, (char) args.arg1);
666 } finally {
667 args.recycle();
668 Log.endSession();
669 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700670 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700671 }
672 case MSG_STOP_DTMF_TONE: {
673 SomeArgs args = (SomeArgs) msg.obj;
674 try {
675 Log.continueSession((Session) args.arg2,
676 SESSION_HANDLER + SESSION_STOP_DTMF);
677 stopDtmfTone((String) args.arg1);
678 } finally {
679 args.recycle();
680 Log.endSession();
681 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700682 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700683 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700684 case MSG_CONFERENCE: {
685 SomeArgs args = (SomeArgs) msg.obj;
686 try {
Brad Ebinger3445f822016-10-24 16:40:49 -0700687 Log.continueSession((Session) args.arg3,
688 SESSION_HANDLER + SESSION_CONFERENCE);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700689 String callId1 = (String) args.arg1;
690 String callId2 = (String) args.arg2;
691 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700692 } finally {
693 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700694 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700695 }
696 break;
697 }
Brad Ebinger3445f822016-10-24 16:40:49 -0700698 case MSG_SPLIT_FROM_CONFERENCE: {
699 SomeArgs args = (SomeArgs) msg.obj;
700 try {
701 Log.continueSession((Session) args.arg2,
702 SESSION_HANDLER + SESSION_SPLIT_CONFERENCE);
703 splitFromConference((String) args.arg1);
704 } finally {
705 args.recycle();
706 Log.endSession();
707 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700708 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700709 }
710 case MSG_MERGE_CONFERENCE: {
711 SomeArgs args = (SomeArgs) msg.obj;
712 try {
713 Log.continueSession((Session) args.arg2,
714 SESSION_HANDLER + SESSION_MERGE_CONFERENCE);
715 mergeConference((String) args.arg1);
716 } finally {
717 args.recycle();
718 Log.endSession();
719 }
Santos Cordona4868042014-09-04 17:39:22 -0700720 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700721 }
722 case MSG_SWAP_CONFERENCE: {
723 SomeArgs args = (SomeArgs) msg.obj;
724 try {
725 Log.continueSession((Session) args.arg2,
726 SESSION_HANDLER + SESSION_SWAP_CONFERENCE);
727 swapConference((String) args.arg1);
728 } finally {
729 args.recycle();
730 Log.endSession();
731 }
Santos Cordona4868042014-09-04 17:39:22 -0700732 break;
Brad Ebinger3445f822016-10-24 16:40:49 -0700733 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700734 case MSG_ON_POST_DIAL_CONTINUE: {
735 SomeArgs args = (SomeArgs) msg.obj;
736 try {
Brad Ebinger3445f822016-10-24 16:40:49 -0700737 Log.continueSession((Session) args.arg2,
738 SESSION_HANDLER + SESSION_POST_DIAL_CONT);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700739 String callId = (String) args.arg1;
740 boolean proceed = (args.argi1 == 1);
741 onPostDialContinue(callId, proceed);
742 } finally {
743 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700744 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700745 }
746 break;
747 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700748 case MSG_PULL_EXTERNAL_CALL: {
Brad Ebinger3445f822016-10-24 16:40:49 -0700749 SomeArgs args = (SomeArgs) msg.obj;
750 try {
751 Log.continueSession((Session) args.arg2,
752 SESSION_HANDLER + SESSION_PULL_EXTERNAL_CALL);
753 pullExternalCall((String) args.arg1);
754 } finally {
755 args.recycle();
756 Log.endSession();
757 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700758 break;
759 }
760 case MSG_SEND_CALL_EVENT: {
761 SomeArgs args = (SomeArgs) msg.obj;
762 try {
Brad Ebinger3445f822016-10-24 16:40:49 -0700763 Log.continueSession((Session) args.arg4,
764 SESSION_HANDLER + SESSION_SEND_CALL_EVENT);
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700765 String callId = (String) args.arg1;
766 String event = (String) args.arg2;
767 Bundle extras = (Bundle) args.arg3;
768 sendCallEvent(callId, event, extras);
769 } finally {
770 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700771 Log.endSession();
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700772 }
773 break;
774 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700775 case MSG_ON_EXTRAS_CHANGED: {
776 SomeArgs args = (SomeArgs) msg.obj;
777 try {
Brad Ebinger3445f822016-10-24 16:40:49 -0700778 Log.continueSession((Session) args.arg3,
779 SESSION_HANDLER + SESSION_EXTRAS_CHANGED);
Tyler Gunndee56a82016-03-23 16:06:34 -0700780 String callId = (String) args.arg1;
781 Bundle extras = (Bundle) args.arg2;
782 handleExtrasChanged(callId, extras);
783 } finally {
784 args.recycle();
Brad Ebinger3445f822016-10-24 16:40:49 -0700785 Log.endSession();
Tyler Gunndee56a82016-03-23 16:06:34 -0700786 }
787 break;
788 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700789 default:
790 break;
791 }
792 }
793 };
794
Santos Cordon823fd3c2014-08-07 18:35:18 -0700795 private final Conference.Listener mConferenceListener = new Conference.Listener() {
796 @Override
797 public void onStateChanged(Conference conference, int oldState, int newState) {
798 String id = mIdByConference.get(conference);
799 switch (newState) {
800 case Connection.STATE_ACTIVE:
801 mAdapter.setActive(id);
802 break;
803 case Connection.STATE_HOLDING:
804 mAdapter.setOnHold(id);
805 break;
806 case Connection.STATE_DISCONNECTED:
807 // handled by onDisconnected
808 break;
809 }
810 }
811
812 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700813 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700814 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700815 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700816 }
817
818 @Override
819 public void onConnectionAdded(Conference conference, Connection connection) {
820 }
821
822 @Override
823 public void onConnectionRemoved(Conference conference, Connection connection) {
824 }
825
826 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700827 public void onConferenceableConnectionsChanged(
828 Conference conference, List<Connection> conferenceableConnections) {
829 mAdapter.setConferenceableConnections(
830 mIdByConference.get(conference),
831 createConnectionIdList(conferenceableConnections));
832 }
833
834 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700835 public void onDestroyed(Conference conference) {
836 removeConference(conference);
837 }
838
839 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800840 public void onConnectionCapabilitiesChanged(
841 Conference conference,
842 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700843 String id = mIdByConference.get(conference);
844 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800845 Connection.capabilitiesToString(connectionCapabilities));
846 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700847 }
Rekha Kumar07366812015-03-24 16:42:31 -0700848
849 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700850 public void onConnectionPropertiesChanged(
851 Conference conference,
852 int connectionProperties) {
853 String id = mIdByConference.get(conference);
854 Log.d(this, "call capabilities: conference: %s",
855 Connection.propertiesToString(connectionProperties));
856 mAdapter.setConnectionProperties(id, connectionProperties);
857 }
858
859 @Override
Rekha Kumar07366812015-03-24 16:42:31 -0700860 public void onVideoStateChanged(Conference c, int videoState) {
861 String id = mIdByConference.get(c);
862 Log.d(this, "onVideoStateChanged set video state %d", videoState);
863 mAdapter.setVideoState(id, videoState);
864 }
865
866 @Override
867 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
868 String id = mIdByConference.get(c);
869 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
870 videoProvider);
871 mAdapter.setVideoProvider(id, videoProvider);
872 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700873
874 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700875 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
876 String id = mIdByConference.get(conference);
Tyler Gunndee56a82016-03-23 16:06:34 -0700877 if (id != null) {
878 mAdapter.setStatusHints(id, statusHints);
879 }
Andrew Leeedc625f2015-04-14 13:38:12 -0700880 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700881
882 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700883 public void onExtrasChanged(Conference c, Bundle extras) {
884 String id = mIdByConference.get(c);
885 if (id != null) {
886 mAdapter.putExtras(id, extras);
887 }
888 }
889
890 @Override
891 public void onExtrasRemoved(Conference c, List<String> keys) {
892 String id = mIdByConference.get(c);
893 if (id != null) {
894 mAdapter.removeExtras(id, keys);
895 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700896 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700897 };
898
Ihab Awad542e0ea2014-05-16 10:22:16 -0700899 private final Connection.Listener mConnectionListener = new Connection.Listener() {
900 @Override
901 public void onStateChanged(Connection c, int state) {
902 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700903 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700904 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700905 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700906 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700907 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700908 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700909 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700910 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700911 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700912 // Handled in onDisconnected()
913 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700914 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700915 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700916 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700917 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700918 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700919 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700920 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700921 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700922 break;
923 }
924 }
925
926 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700927 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700928 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700929 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700930 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700931 }
932
933 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700934 public void onVideoStateChanged(Connection c, int videoState) {
935 String id = mIdByConnection.get(c);
936 Log.d(this, "Adapter set video state %d", videoState);
937 mAdapter.setVideoState(id, videoState);
938 }
939
940 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700941 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700942 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700943 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700944 }
945
946 @Override
947 public void onCallerDisplayNameChanged(
948 Connection c, String callerDisplayName, int presentation) {
949 String id = mIdByConnection.get(c);
950 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700951 }
952
953 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700954 public void onDestroyed(Connection c) {
955 removeConnection(c);
956 }
Ihab Awadf8358972014-05-28 16:46:42 -0700957
958 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700959 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700960 String id = mIdByConnection.get(c);
961 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700962 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700963 }
964
965 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800966 public void onPostDialChar(Connection c, char nextChar) {
967 String id = mIdByConnection.get(c);
968 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
969 mAdapter.onPostDialChar(id, nextChar);
970 }
971
972 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700973 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700974 String id = mIdByConnection.get(c);
975 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700976 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700977 }
Santos Cordonb6939982014-06-04 20:20:58 -0700978
979 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800980 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700981 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700982 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800983 Connection.capabilitiesToString(capabilities));
984 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700985 }
986
Santos Cordonb6939982014-06-04 20:20:58 -0700987 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700988 public void onConnectionPropertiesChanged(Connection c, int properties) {
989 String id = mIdByConnection.get(c);
990 Log.d(this, "properties: parcelableconnection: %s",
991 Connection.propertiesToString(properties));
992 mAdapter.setConnectionProperties(id, properties);
993 }
994
995 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700996 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b82014-06-20 16:29:33 -0700997 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700998 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
999 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001000 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b82014-06-20 16:29:33 -07001001 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001002
1003 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001004 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001005 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001006 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001007 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001008
1009 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001010 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001011 String id = mIdByConnection.get(c);
1012 mAdapter.setStatusHints(id, statusHints);
1013 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -07001014
1015 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001016 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001017 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -07001018 mAdapter.setConferenceableConnections(
1019 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001020 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001021 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001022
1023 @Override
1024 public void onConferenceChanged(Connection connection, Conference conference) {
1025 String id = mIdByConnection.get(connection);
1026 if (id != null) {
1027 String conferenceId = null;
1028 if (conference != null) {
1029 conferenceId = mIdByConference.get(conference);
1030 }
1031 mAdapter.setIsConferenced(id, conferenceId);
1032 }
1033 }
Anthony Lee17455a32015-04-24 15:25:29 -07001034
1035 @Override
1036 public void onConferenceMergeFailed(Connection connection) {
1037 String id = mIdByConnection.get(connection);
1038 if (id != null) {
1039 mAdapter.onConferenceMergeFailed(id);
1040 }
1041 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001042
1043 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001044 public void onExtrasChanged(Connection c, Bundle extras) {
1045 String id = mIdByConnection.get(c);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001046 if (id != null) {
Tyler Gunndee56a82016-03-23 16:06:34 -07001047 mAdapter.putExtras(id, extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001048 }
1049 }
Brad Ebinger3445f822016-10-24 16:40:49 -07001050
Tyler Gunndee56a82016-03-23 16:06:34 -07001051 public void onExtrasRemoved(Connection c, List<String> keys) {
1052 String id = mIdByConnection.get(c);
1053 if (id != null) {
1054 mAdapter.removeExtras(id, keys);
1055 }
1056 }
1057
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001058
1059 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001060 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001061 String id = mIdByConnection.get(connection);
1062 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001063 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001064 }
1065 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001066 };
1067
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001068 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -07001069 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001070 public final IBinder onBind(Intent intent) {
1071 return mBinder;
1072 }
1073
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001074 /** {@inheritDoc} */
1075 @Override
1076 public boolean onUnbind(Intent intent) {
1077 endAllConnections();
1078 return super.onUnbind(intent);
1079 }
1080
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001081 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001082 * This can be used by telecom to either create a new outgoing call or attach to an existing
1083 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001084 * createConnection util a connection service cancels the process or completes it successfully.
1085 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001086 private void createConnection(
1087 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001088 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -07001089 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -07001090 boolean isIncoming,
1091 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001092 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001093 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
1094 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -07001095 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001096
Yorke Leec3cf9822014-10-02 09:38:39 -07001097 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
1098 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -07001099 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001100 Log.d(this, "createConnection, connection: %s", connection);
1101 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001102 connection = Connection.createFailedConnection(
1103 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001104 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001105
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001106 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001107 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001108 addConnection(callId, connection);
1109 }
1110
Andrew Lee100e2932014-09-08 15:34:24 -07001111 Uri address = connection.getAddress();
1112 String number = address == null ? "null" : address.getSchemeSpecificPart();
Tyler Gunn720c6642016-03-22 09:02:47 -07001113 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s, properties: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001114 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001115 Connection.stateToString(connection.getState()),
Tyler Gunn720c6642016-03-22 09:02:47 -07001116 Connection.capabilitiesToString(connection.getConnectionCapabilities()),
1117 Connection.propertiesToString(connection.getConnectionProperties()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001118
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001119 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -07001120 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001121 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -07001122 request,
1123 new ParcelableConnection(
1124 request.getAccountHandle(),
1125 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001126 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001127 connection.getConnectionProperties(),
Andrew Lee100e2932014-09-08 15:34:24 -07001128 connection.getAddress(),
1129 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -07001130 connection.getCallerDisplayName(),
1131 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001132 connection.getVideoProvider() == null ?
1133 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001134 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -07001135 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001136 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001137 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -07001138 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001139 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001140 createIdList(connection.getConferenceables()),
1141 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001142 if (isUnknown) {
1143 triggerConferenceRecalculate();
1144 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001145 }
1146
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001147 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001148 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001149 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001150 }
1151
Tyler Gunnbe74de02014-08-29 14:51:48 -07001152 private void answerVideo(String callId, int videoState) {
1153 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001154 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001155 }
1156
Tyler Gunnbe74de02014-08-29 14:51:48 -07001157 private void answer(String callId) {
1158 Log.d(this, "answer %s", callId);
1159 findConnectionForAction(callId, "answer").onAnswer();
1160 }
1161
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001162 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001163 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001164 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001165 }
1166
Bryce Lee81901682015-08-28 16:38:02 -07001167 private void reject(String callId, String rejectWithMessage) {
1168 Log.d(this, "reject %s with message", callId);
1169 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
1170 }
1171
Bryce Leecac50772015-11-17 15:13:29 -08001172 private void silence(String callId) {
1173 Log.d(this, "silence %s", callId);
1174 findConnectionForAction(callId, "silence").onSilence();
1175 }
1176
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001177 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001178 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001179 if (mConnectionById.containsKey(callId)) {
1180 findConnectionForAction(callId, "disconnect").onDisconnect();
1181 } else {
1182 findConferenceForAction(callId, "disconnect").onDisconnect();
1183 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001184 }
1185
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001186 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001187 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001188 if (mConnectionById.containsKey(callId)) {
1189 findConnectionForAction(callId, "hold").onHold();
1190 } else {
1191 findConferenceForAction(callId, "hold").onHold();
1192 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001193 }
1194
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001195 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001196 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001197 if (mConnectionById.containsKey(callId)) {
1198 findConnectionForAction(callId, "unhold").onUnhold();
1199 } else {
1200 findConferenceForAction(callId, "unhold").onUnhold();
1201 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001202 }
1203
Yorke Lee4af59352015-05-13 14:14:54 -07001204 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
1205 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001206 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -07001207 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1208 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001209 } else {
Yorke Lee4af59352015-05-13 14:14:54 -07001210 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1211 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001212 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001213 }
1214
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001215 private void playDtmfTone(String callId, char digit) {
1216 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001217 if (mConnectionById.containsKey(callId)) {
1218 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1219 } else {
1220 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1221 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001222 }
1223
1224 private void stopDtmfTone(String callId) {
1225 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001226 if (mConnectionById.containsKey(callId)) {
1227 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
1228 } else {
1229 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
1230 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001231 }
1232
Santos Cordon823fd3c2014-08-07 18:35:18 -07001233 private void conference(String callId1, String callId2) {
1234 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -07001235
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001236 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001237 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001238 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001239 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001240 conference2 = findConferenceForAction(callId2, "conference");
1241 if (conference2 == getNullConference()) {
1242 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
1243 callId2);
1244 return;
1245 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001246 }
Santos Cordonb6939982014-06-04 20:20:58 -07001247
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001248 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -07001249 Connection connection1 = findConnectionForAction(callId1, "conference");
1250 if (connection1 == getNullConnection()) {
1251 Conference conference1 = findConferenceForAction(callId1, "addConnection");
1252 if (conference1 == getNullConference()) {
1253 Log.w(this,
1254 "Connection1 or Conference1 missing in conference request %s.",
1255 callId1);
1256 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001257 // Call 1 is a conference.
1258 if (connection2 != getNullConnection()) {
1259 // Call 2 is a connection so merge via call 1 (conference).
1260 conference1.onMerge(connection2);
1261 } else {
1262 // Call 2 is ALSO a conference; this should never happen.
1263 Log.wtf(this, "There can only be one conference and an attempt was made to " +
1264 "merge two conferences.");
1265 return;
1266 }
Ihab Awad50e35062014-09-30 09:17:03 -07001267 }
1268 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001269 // Call 1 is a connection.
1270 if (conference2 != getNullConference()) {
1271 // Call 2 is a conference, so merge via call 2.
1272 conference2.onMerge(connection1);
1273 } else {
1274 // Call 2 is a connection, so merge together.
1275 onConference(connection1, connection2);
1276 }
Ihab Awad50e35062014-09-30 09:17:03 -07001277 }
Santos Cordon980acb92014-05-31 10:31:19 -07001278 }
1279
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001280 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -07001281 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -07001282
1283 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001284 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -07001285 Log.w(this, "Connection missing in conference request %s.", callId);
1286 return;
1287 }
1288
Santos Cordon0159ac02014-08-21 14:28:11 -07001289 Conference conference = connection.getConference();
1290 if (conference != null) {
1291 conference.onSeparate(connection);
1292 }
Santos Cordon980acb92014-05-31 10:31:19 -07001293 }
1294
Santos Cordona4868042014-09-04 17:39:22 -07001295 private void mergeConference(String callId) {
1296 Log.d(this, "mergeConference(%s)", callId);
1297 Conference conference = findConferenceForAction(callId, "mergeConference");
1298 if (conference != null) {
1299 conference.onMerge();
1300 }
1301 }
1302
1303 private void swapConference(String callId) {
1304 Log.d(this, "swapConference(%s)", callId);
1305 Conference conference = findConferenceForAction(callId, "swapConference");
1306 if (conference != null) {
1307 conference.onSwap();
1308 }
1309 }
1310
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001311 /**
1312 * Notifies a {@link Connection} of a request to pull an external call.
1313 *
1314 * See {@link Call#pullExternalCall()}.
1315 *
1316 * @param callId The ID of the call to pull.
1317 */
1318 private void pullExternalCall(String callId) {
1319 Log.d(this, "pullExternalCall(%s)", callId);
1320 Connection connection = findConnectionForAction(callId, "pullExternalCall");
1321 if (connection != null) {
1322 connection.onPullExternalCall();
1323 }
1324 }
1325
1326 /**
1327 * Notifies a {@link Connection} of a call event.
1328 *
1329 * See {@link Call#sendCallEvent(String, Bundle)}.
1330 *
1331 * @param callId The ID of the call receiving the event.
1332 * @param event The event.
1333 * @param extras Extras associated with the event.
1334 */
1335 private void sendCallEvent(String callId, String event, Bundle extras) {
1336 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
1337 Connection connection = findConnectionForAction(callId, "sendCallEvent");
1338 if (connection != null) {
1339 connection.onCallEvent(event, extras);
1340 }
1341
1342 }
1343
Tyler Gunndee56a82016-03-23 16:06:34 -07001344 /**
1345 * Notifies a {@link Connection} or {@link Conference} of a change to the extras from Telecom.
1346 * <p>
1347 * These extra changes can originate from Telecom itself, or from an {@link InCallService} via
1348 * the {@link android.telecom.Call#putExtra(String, boolean)},
1349 * {@link android.telecom.Call#putExtra(String, int)},
1350 * {@link android.telecom.Call#putExtra(String, String)},
1351 * {@link Call#removeExtras(List)}.
1352 *
1353 * @param callId The ID of the call receiving the event.
1354 * @param extras The new extras bundle.
1355 */
1356 private void handleExtrasChanged(String callId, Bundle extras) {
1357 Log.d(this, "handleExtrasChanged(%s, %s)", callId, extras);
1358 if (mConnectionById.containsKey(callId)) {
1359 findConnectionForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1360 } else if (mConferenceById.containsKey(callId)) {
1361 findConferenceForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1362 }
1363 }
1364
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001365 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001366 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001367 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001368 }
1369
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001370 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001371 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001372 // No need to query again if we already did it.
1373 return;
1374 }
1375
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001376 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -07001377 @Override
1378 public void onResult(
1379 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001380 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001381 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -07001382 @Override
1383 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001384 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001385 mRemoteConnectionManager.addConnectionService(
1386 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001387 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -07001388 }
Ihab Awad5d0410f2014-07-30 10:07:40 -07001389 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001390 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -07001391 }
1392 });
1393 }
1394
1395 @Override
1396 public void onError() {
1397 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -07001398 @Override
1399 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001400 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -07001401 }
1402 });
1403 }
1404 });
1405 }
1406
Ihab Awadf8b69882014-07-25 15:14:01 -07001407 /**
1408 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001409 * incoming request. This is used by {@code ConnectionService}s that are registered with
1410 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
1411 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001412 *
1413 * @param connectionManagerPhoneAccount See description at
1414 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1415 * @param request Details about the incoming call.
1416 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1417 * not handle the call.
1418 */
1419 public final RemoteConnection createRemoteIncomingConnection(
1420 PhoneAccountHandle connectionManagerPhoneAccount,
1421 ConnectionRequest request) {
1422 return mRemoteConnectionManager.createRemoteConnection(
1423 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -07001424 }
1425
1426 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001427 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001428 * outgoing request. This is used by {@code ConnectionService}s that are registered with
1429 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
1430 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001431 *
1432 * @param connectionManagerPhoneAccount See description at
1433 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Cuihtlauac ALVARADO0b3b2a52016-09-13 14:49:41 +02001434 * @param request Details about the outgoing call.
Ihab Awadf8b69882014-07-25 15:14:01 -07001435 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1436 * not handle the call.
1437 */
1438 public final RemoteConnection createRemoteOutgoingConnection(
1439 PhoneAccountHandle connectionManagerPhoneAccount,
1440 ConnectionRequest request) {
1441 return mRemoteConnectionManager.createRemoteConnection(
1442 connectionManagerPhoneAccount, request, false);
1443 }
1444
1445 /**
Santos Cordona663f862014-10-29 13:49:58 -07001446 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1447 * {@link RemoteConnection}s should be merged into a conference call.
1448 * <p>
1449 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1450 * be invoked.
1451 *
1452 * @param remoteConnection1 The first of the remote connections to conference.
1453 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001454 */
1455 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001456 RemoteConnection remoteConnection1,
1457 RemoteConnection remoteConnection2) {
1458 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001459 }
1460
1461 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001462 * Adds a new conference call. When a conference call is created either as a result of an
1463 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1464 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1465 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1466 *
1467 * @param conference The new conference object.
1468 */
1469 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001470 Log.d(this, "addConference: conference=%s", conference);
1471
Santos Cordon823fd3c2014-08-07 18:35:18 -07001472 String id = addConferenceInternal(conference);
1473 if (id != null) {
1474 List<String> connectionIds = new ArrayList<>(2);
1475 for (Connection connection : conference.getConnections()) {
1476 if (mIdByConnection.containsKey(connection)) {
1477 connectionIds.add(mIdByConnection.get(connection));
1478 }
1479 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001480 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001481 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001482 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001483 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001484 conference.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001485 conference.getConnectionProperties(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001486 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001487 conference.getVideoProvider() == null ?
1488 null : conference.getVideoProvider().getInterface(),
1489 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001490 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001491 conference.getStatusHints(),
1492 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001493
Santos Cordon823fd3c2014-08-07 18:35:18 -07001494 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001495 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1496 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001497
1498 // Go through any child calls and set the parent.
1499 for (Connection connection : conference.getConnections()) {
1500 String connectionId = mIdByConnection.get(connection);
1501 if (connectionId != null) {
1502 mAdapter.setIsConferenced(connectionId, id);
1503 }
1504 }
1505 }
1506 }
1507
1508 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001509 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1510 * connection.
1511 *
1512 * @param phoneAccountHandle The phone account handle for the connection.
1513 * @param connection The connection to add.
1514 */
1515 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1516 Connection connection) {
1517
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001518 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001519 if (id != null) {
1520 List<String> emptyList = new ArrayList<>(0);
1521
1522 ParcelableConnection parcelableConnection = new ParcelableConnection(
1523 phoneAccountHandle,
1524 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001525 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001526 connection.getConnectionProperties(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001527 connection.getAddress(),
1528 connection.getAddressPresentation(),
1529 connection.getCallerDisplayName(),
1530 connection.getCallerDisplayNamePresentation(),
1531 connection.getVideoProvider() == null ?
1532 null : connection.getVideoProvider().getInterface(),
1533 connection.getVideoState(),
1534 connection.isRingbackRequested(),
1535 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001536 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001537 connection.getStatusHints(),
1538 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001539 emptyList,
1540 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001541 mAdapter.addExistingConnection(id, parcelableConnection);
1542 }
1543 }
1544
1545 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001546 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1547 * has taken responsibility.
1548 *
1549 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001550 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001551 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001552 return mConnectionById.values();
1553 }
1554
1555 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001556 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1557 * has taken responsibility.
1558 *
1559 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1560 */
1561 public final Collection<Conference> getAllConferences() {
1562 return mConferenceById.values();
1563 }
1564
1565 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001566 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1567 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001568 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001569 * @param connectionManagerPhoneAccount See description at
1570 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1571 * @param request Details about the incoming call.
1572 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1573 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001574 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001575 public Connection onCreateIncomingConnection(
1576 PhoneAccountHandle connectionManagerPhoneAccount,
1577 ConnectionRequest request) {
1578 return null;
1579 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001580
1581 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001582 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1583 * Connection is part of a conference controller but is not yet added to Connection
1584 * Service and hence cannot be added to the conference call.
1585 *
1586 * @hide
1587 */
1588 public void triggerConferenceRecalculate() {
1589 }
1590
1591 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001592 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1593 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001594 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001595 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1596 * this call.
1597 * <p>
1598 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1599 * has registered one or more {@code PhoneAccount}s having
1600 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1601 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1602 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1603 * making the connection.
1604 * <p>
1605 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1606 * being asked to make a direct connection. The
1607 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1608 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1609 * making the connection.
1610 * @param request Details about the outgoing call.
1611 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001612 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001613 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001614 public Connection onCreateOutgoingConnection(
1615 PhoneAccountHandle connectionManagerPhoneAccount,
1616 ConnectionRequest request) {
1617 return null;
1618 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001619
1620 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001621 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1622 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1623 * call created using
1624 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1625 *
Yorke Lee770ed6e2014-10-06 18:58:52 -07001626 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001627 */
1628 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1629 ConnectionRequest request) {
Brad Ebinger3445f822016-10-24 16:40:49 -07001630 return null;
Yorke Leec3cf9822014-10-02 09:38:39 -07001631 }
1632
1633 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001634 * Conference two specified connections. Invoked when the user has made a request to merge the
1635 * specified connections into a conference call. In response, the connection service should
1636 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001637 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001638 * @param connection1 A connection to merge into a conference call.
1639 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001640 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001641 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001642
Santos Cordona663f862014-10-29 13:49:58 -07001643 /**
1644 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1645 * When this method is invoked, this {@link ConnectionService} should create its own
1646 * representation of the conference call and send it to telecom using {@link #addConference}.
1647 * <p>
1648 * This is only relevant to {@link ConnectionService}s which are registered with
1649 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1650 *
1651 * @param conference The remote conference call.
1652 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001653 public void onRemoteConferenceAdded(RemoteConference conference) {}
1654
Santos Cordon823fd3c2014-08-07 18:35:18 -07001655 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001656 * Called when an existing connection is added remotely.
1657 * @param connection The existing connection which was added.
1658 */
1659 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1660
1661 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001662 * @hide
1663 */
1664 public boolean containsConference(Conference conference) {
1665 return mIdByConference.containsKey(conference);
1666 }
1667
Ihab Awadb8e85c72014-08-23 20:34:57 -07001668 /** {@hide} */
1669 void addRemoteConference(RemoteConference remoteConference) {
1670 onRemoteConferenceAdded(remoteConference);
1671 }
1672
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001673 /** {@hide} */
1674 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1675 onRemoteExistingConnectionAdded(remoteConnection);
1676 }
1677
Ihab Awad5d0410f2014-07-30 10:07:40 -07001678 private void onAccountsInitialized() {
1679 mAreAccountsInitialized = true;
1680 for (Runnable r : mPreInitializationConnectionRequests) {
1681 r.run();
1682 }
1683 mPreInitializationConnectionRequests.clear();
1684 }
1685
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001686 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001687 * Adds an existing connection to the list of connections, identified by a new call ID unique
1688 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001689 *
1690 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001691 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001692 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001693 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1694 String id;
1695 if (handle == null) {
1696 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1697 // so just use a random UUID.
1698 id = UUID.randomUUID().toString();
1699 } else {
1700 // Phone account handle was provided, so use the ConnectionService class name as a
1701 // prefix for a unique incremental call ID.
1702 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1703 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001704 addConnection(id, connection);
1705 return id;
1706 }
1707
Ihab Awad542e0ea2014-05-16 10:22:16 -07001708 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001709 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001710 mConnectionById.put(callId, connection);
1711 mIdByConnection.put(connection, callId);
1712 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001713 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001714 }
1715
Anthony Lee30e65842014-11-06 16:30:53 -08001716 /** {@hide} */
1717 protected void removeConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001718 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001719 connection.removeConnectionListener(mConnectionListener);
Chenjie Luoe370b532016-05-12 16:59:43 -07001720 String id = mIdByConnection.get(connection);
1721 if (id != null) {
1722 mConnectionById.remove(id);
1723 mIdByConnection.remove(connection);
1724 mAdapter.removeCall(id);
1725 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001726 }
1727
Santos Cordon823fd3c2014-08-07 18:35:18 -07001728 private String addConferenceInternal(Conference conference) {
1729 if (mIdByConference.containsKey(conference)) {
1730 Log.w(this, "Re-adding an existing conference: %s.", conference);
1731 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001732 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1733 // cannot determine a ConnectionService class name to associate with the ID, so use
1734 // a unique UUID (for now).
Santos Cordon823fd3c2014-08-07 18:35:18 -07001735 String id = UUID.randomUUID().toString();
1736 mConferenceById.put(id, conference);
1737 mIdByConference.put(conference, id);
1738 conference.addListener(mConferenceListener);
1739 return id;
1740 }
1741
1742 return null;
1743 }
1744
1745 private void removeConference(Conference conference) {
1746 if (mIdByConference.containsKey(conference)) {
1747 conference.removeListener(mConferenceListener);
1748
1749 String id = mIdByConference.get(conference);
1750 mConferenceById.remove(id);
1751 mIdByConference.remove(conference);
1752 mAdapter.removeCall(id);
1753 }
1754 }
1755
Ihab Awad542e0ea2014-05-16 10:22:16 -07001756 private Connection findConnectionForAction(String callId, String action) {
1757 if (mConnectionById.containsKey(callId)) {
1758 return mConnectionById.get(callId);
1759 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001760 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001761 return getNullConnection();
1762 }
1763
1764 static synchronized Connection getNullConnection() {
1765 if (sNullConnection == null) {
1766 sNullConnection = new Connection() {};
1767 }
1768 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001769 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001770
1771 private Conference findConferenceForAction(String conferenceId, String action) {
1772 if (mConferenceById.containsKey(conferenceId)) {
1773 return mConferenceById.get(conferenceId);
1774 }
1775 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1776 return getNullConference();
1777 }
1778
Ihab Awadb8e85c72014-08-23 20:34:57 -07001779 private List<String> createConnectionIdList(List<Connection> connections) {
1780 List<String> ids = new ArrayList<>();
1781 for (Connection c : connections) {
1782 if (mIdByConnection.containsKey(c)) {
1783 ids.add(mIdByConnection.get(c));
1784 }
1785 }
1786 Collections.sort(ids);
1787 return ids;
1788 }
1789
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001790 /**
1791 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001792 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001793 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001794 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001795 * @return List of string conference and call Ids.
1796 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001797 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001798 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001799 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001800 // Only allow Connection and Conference conferenceables.
1801 if (c instanceof Connection) {
1802 Connection connection = (Connection) c;
1803 if (mIdByConnection.containsKey(connection)) {
1804 ids.add(mIdByConnection.get(connection));
1805 }
1806 } else if (c instanceof Conference) {
1807 Conference conference = (Conference) c;
1808 if (mIdByConference.containsKey(conference)) {
1809 ids.add(mIdByConference.get(conference));
1810 }
1811 }
1812 }
1813 Collections.sort(ids);
1814 return ids;
1815 }
1816
Santos Cordon0159ac02014-08-21 14:28:11 -07001817 private Conference getNullConference() {
1818 if (sNullConference == null) {
1819 sNullConference = new Conference(null) {};
1820 }
1821 return sNullConference;
1822 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001823
1824 private void endAllConnections() {
1825 // Unbound from telecomm. We should end all connections and conferences.
1826 for (Connection connection : mIdByConnection.keySet()) {
1827 // only operate on top-level calls. Conference calls will be removed on their own.
1828 if (connection.getConference() == null) {
1829 connection.onDisconnect();
1830 }
1831 }
1832 for (Conference conference : mIdByConference.keySet()) {
1833 conference.onDisconnect();
1834 }
1835 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001836
1837 /**
1838 * Retrieves the next call ID as maintainted by the connection service.
1839 *
1840 * @return The call ID.
1841 */
1842 private int getNextCallId() {
Brad Ebinger3445f822016-10-24 16:40:49 -07001843 synchronized (mIdSyncRoot) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001844 return ++mId;
1845 }
1846 }
Santos Cordon980acb92014-05-31 10:31:19 -07001847}