blob: b119e160e74c459d4e51b3a0d44dccc0e91f421b [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/**
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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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 Ebingerb32d4f82016-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);
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700518 mPreInitializationConnectionRequests.add(
519 new android.telecom.Logging.Runnable(
520 SESSION_HANDLER + SESSION_CREATE_CONN + ".pICR",
521 null /*lock*/) {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700522 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700523 public void loggedRun() {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700524 createConnection(
525 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700526 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700527 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700528 isIncoming,
529 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700530 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700531 }.prepare());
Ihab Awad5d0410f2014-07-30 10:07:40 -0700532 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700533 createConnection(
534 connectionManagerPhoneAccount,
535 id,
536 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700537 isIncoming,
538 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700539 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700540 } finally {
541 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700542 Log.endSession();
Ihab Awadf8b69882014-07-25 15:14:01 -0700543 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700544 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700545 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700546 case MSG_ABORT: {
547 SomeArgs args = (SomeArgs) msg.obj;
548 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ABORT);
549 try {
550 abort((String) args.arg1);
551 } finally {
552 args.recycle();
553 Log.endSession();
554 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700555 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700556 }
557 case MSG_ANSWER: {
558 SomeArgs args = (SomeArgs) msg.obj;
559 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_ANSWER);
560 try {
561 answer((String) args.arg1);
562 } finally {
563 args.recycle();
564 Log.endSession();
565 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700566 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700567 }
Tyler Gunnbe74de02014-08-29 14:51:48 -0700568 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700569 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700570 Log.continueSession((Session) args.arg2,
571 SESSION_HANDLER + SESSION_ANSWER_VIDEO);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700572 try {
573 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700574 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700575 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700576 } finally {
577 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700578 Log.endSession();
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700579 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700580 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700581 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700582 case MSG_REJECT: {
583 SomeArgs args = (SomeArgs) msg.obj;
584 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
585 try {
586 reject((String) args.arg1);
587 } finally {
588 args.recycle();
589 Log.endSession();
590 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700591 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700592 }
Bryce Lee81901682015-08-28 16:38:02 -0700593 case MSG_REJECT_WITH_MESSAGE: {
594 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700595 Log.continueSession((Session) args.arg3,
596 SESSION_HANDLER + SESSION_REJECT_MESSAGE);
Bryce Lee81901682015-08-28 16:38:02 -0700597 try {
598 reject((String) args.arg1, (String) args.arg2);
599 } finally {
600 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700601 Log.endSession();
Bryce Lee81901682015-08-28 16:38:02 -0700602 }
603 break;
604 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700605 case MSG_DISCONNECT: {
606 SomeArgs args = (SomeArgs) msg.obj;
607 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_DISCONNECT);
608 try {
609 disconnect((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_SILENCE: {
617 SomeArgs args = (SomeArgs) msg.obj;
618 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_SILENCE);
619 try {
620 silence((String) args.arg1);
621 } finally {
622 args.recycle();
623 Log.endSession();
624 }
Bryce Leecac50772015-11-17 15:13:29 -0800625 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700626 }
627 case MSG_HOLD: {
628 SomeArgs args = (SomeArgs) msg.obj;
629 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_REJECT);
630 try {
631 hold((String) args.arg1);
632 } finally {
633 args.recycle();
634 Log.endSession();
635 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700636 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700637 }
638 case MSG_UNHOLD: {
639 SomeArgs args = (SomeArgs) msg.obj;
640 Log.continueSession((Session) args.arg2, SESSION_HANDLER + SESSION_UNHOLD);
641 try {
642 unhold((String) args.arg1);
643 } finally {
644 args.recycle();
645 Log.endSession();
646 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700647 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700648 }
Yorke Lee4af59352015-05-13 14:14:54 -0700649 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700650 SomeArgs args = (SomeArgs) msg.obj;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700651 Log.continueSession((Session) args.arg3,
652 SESSION_HANDLER + SESSION_CALL_AUDIO_SC);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700653 try {
654 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700655 CallAudioState audioState = (CallAudioState) args.arg2;
656 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700657 } finally {
658 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700659 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700660 }
661 break;
662 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700663 case MSG_PLAY_DTMF_TONE: {
664 SomeArgs args = (SomeArgs) msg.obj;
665 try {
666 Log.continueSession((Session) args.arg3,
667 SESSION_HANDLER + SESSION_PLAY_DTMF);
668 playDtmfTone((String) args.arg2, (char) 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_STOP_DTMF_TONE: {
676 SomeArgs args = (SomeArgs) msg.obj;
677 try {
678 Log.continueSession((Session) args.arg2,
679 SESSION_HANDLER + SESSION_STOP_DTMF);
680 stopDtmfTone((String) args.arg1);
681 } finally {
682 args.recycle();
683 Log.endSession();
684 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700685 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700686 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700687 case MSG_CONFERENCE: {
688 SomeArgs args = (SomeArgs) msg.obj;
689 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700690 Log.continueSession((Session) args.arg3,
691 SESSION_HANDLER + SESSION_CONFERENCE);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700692 String callId1 = (String) args.arg1;
693 String callId2 = (String) args.arg2;
694 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700695 } finally {
696 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700697 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700698 }
699 break;
700 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700701 case MSG_SPLIT_FROM_CONFERENCE: {
702 SomeArgs args = (SomeArgs) msg.obj;
703 try {
704 Log.continueSession((Session) args.arg2,
705 SESSION_HANDLER + SESSION_SPLIT_CONFERENCE);
706 splitFromConference((String) args.arg1);
707 } finally {
708 args.recycle();
709 Log.endSession();
710 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700711 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700712 }
713 case MSG_MERGE_CONFERENCE: {
714 SomeArgs args = (SomeArgs) msg.obj;
715 try {
716 Log.continueSession((Session) args.arg2,
717 SESSION_HANDLER + SESSION_MERGE_CONFERENCE);
718 mergeConference((String) args.arg1);
719 } finally {
720 args.recycle();
721 Log.endSession();
722 }
Santos Cordona4868042014-09-04 17:39:22 -0700723 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700724 }
725 case MSG_SWAP_CONFERENCE: {
726 SomeArgs args = (SomeArgs) msg.obj;
727 try {
728 Log.continueSession((Session) args.arg2,
729 SESSION_HANDLER + SESSION_SWAP_CONFERENCE);
730 swapConference((String) args.arg1);
731 } finally {
732 args.recycle();
733 Log.endSession();
734 }
Santos Cordona4868042014-09-04 17:39:22 -0700735 break;
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700736 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700737 case MSG_ON_POST_DIAL_CONTINUE: {
738 SomeArgs args = (SomeArgs) msg.obj;
739 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700740 Log.continueSession((Session) args.arg2,
741 SESSION_HANDLER + SESSION_POST_DIAL_CONT);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700742 String callId = (String) args.arg1;
743 boolean proceed = (args.argi1 == 1);
744 onPostDialContinue(callId, proceed);
745 } finally {
746 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700747 Log.endSession();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700748 }
749 break;
750 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700751 case MSG_PULL_EXTERNAL_CALL: {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700752 SomeArgs args = (SomeArgs) msg.obj;
753 try {
754 Log.continueSession((Session) args.arg2,
755 SESSION_HANDLER + SESSION_PULL_EXTERNAL_CALL);
756 pullExternalCall((String) args.arg1);
757 } finally {
758 args.recycle();
759 Log.endSession();
760 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700761 break;
762 }
763 case MSG_SEND_CALL_EVENT: {
764 SomeArgs args = (SomeArgs) msg.obj;
765 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700766 Log.continueSession((Session) args.arg4,
767 SESSION_HANDLER + SESSION_SEND_CALL_EVENT);
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700768 String callId = (String) args.arg1;
769 String event = (String) args.arg2;
770 Bundle extras = (Bundle) args.arg3;
771 sendCallEvent(callId, event, extras);
772 } finally {
773 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700774 Log.endSession();
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700775 }
776 break;
777 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700778 case MSG_ON_EXTRAS_CHANGED: {
779 SomeArgs args = (SomeArgs) msg.obj;
780 try {
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700781 Log.continueSession((Session) args.arg3,
782 SESSION_HANDLER + SESSION_EXTRAS_CHANGED);
Tyler Gunndee56a82016-03-23 16:06:34 -0700783 String callId = (String) args.arg1;
784 Bundle extras = (Bundle) args.arg2;
785 handleExtrasChanged(callId, extras);
786 } finally {
787 args.recycle();
Brad Ebingerb32d4f82016-10-24 16:40:49 -0700788 Log.endSession();
Tyler Gunndee56a82016-03-23 16:06:34 -0700789 }
790 break;
791 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700792 default:
793 break;
794 }
795 }
796 };
797
Santos Cordon823fd3c2014-08-07 18:35:18 -0700798 private final Conference.Listener mConferenceListener = new Conference.Listener() {
799 @Override
800 public void onStateChanged(Conference conference, int oldState, int newState) {
801 String id = mIdByConference.get(conference);
802 switch (newState) {
803 case Connection.STATE_ACTIVE:
804 mAdapter.setActive(id);
805 break;
806 case Connection.STATE_HOLDING:
807 mAdapter.setOnHold(id);
808 break;
809 case Connection.STATE_DISCONNECTED:
810 // handled by onDisconnected
811 break;
812 }
813 }
814
815 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700816 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700817 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700818 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700819 }
820
821 @Override
822 public void onConnectionAdded(Conference conference, Connection connection) {
823 }
824
825 @Override
826 public void onConnectionRemoved(Conference conference, Connection connection) {
827 }
828
829 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700830 public void onConferenceableConnectionsChanged(
831 Conference conference, List<Connection> conferenceableConnections) {
832 mAdapter.setConferenceableConnections(
833 mIdByConference.get(conference),
834 createConnectionIdList(conferenceableConnections));
835 }
836
837 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700838 public void onDestroyed(Conference conference) {
839 removeConference(conference);
840 }
841
842 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800843 public void onConnectionCapabilitiesChanged(
844 Conference conference,
845 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700846 String id = mIdByConference.get(conference);
847 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800848 Connection.capabilitiesToString(connectionCapabilities));
849 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700850 }
Rekha Kumar07366812015-03-24 16:42:31 -0700851
852 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700853 public void onConnectionPropertiesChanged(
854 Conference conference,
855 int connectionProperties) {
856 String id = mIdByConference.get(conference);
857 Log.d(this, "call capabilities: conference: %s",
858 Connection.propertiesToString(connectionProperties));
859 mAdapter.setConnectionProperties(id, connectionProperties);
860 }
861
862 @Override
Rekha Kumar07366812015-03-24 16:42:31 -0700863 public void onVideoStateChanged(Conference c, int videoState) {
864 String id = mIdByConference.get(c);
865 Log.d(this, "onVideoStateChanged set video state %d", videoState);
866 mAdapter.setVideoState(id, videoState);
867 }
868
869 @Override
870 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
871 String id = mIdByConference.get(c);
872 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
873 videoProvider);
874 mAdapter.setVideoProvider(id, videoProvider);
875 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700876
877 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700878 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
879 String id = mIdByConference.get(conference);
Tyler Gunndee56a82016-03-23 16:06:34 -0700880 if (id != null) {
881 mAdapter.setStatusHints(id, statusHints);
882 }
Andrew Leeedc625f2015-04-14 13:38:12 -0700883 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700884
885 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700886 public void onExtrasChanged(Conference c, Bundle extras) {
887 String id = mIdByConference.get(c);
888 if (id != null) {
889 mAdapter.putExtras(id, extras);
890 }
891 }
892
893 @Override
894 public void onExtrasRemoved(Conference c, List<String> keys) {
895 String id = mIdByConference.get(c);
896 if (id != null) {
897 mAdapter.removeExtras(id, keys);
898 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700899 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700900 };
901
Ihab Awad542e0ea2014-05-16 10:22:16 -0700902 private final Connection.Listener mConnectionListener = new Connection.Listener() {
903 @Override
904 public void onStateChanged(Connection c, int state) {
905 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700906 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700907 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700908 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700909 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700910 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700911 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700912 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700913 break;
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700914 case Connection.STATE_PULLING_CALL:
915 mAdapter.setPulling(id);
916 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700917 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700918 // Handled in onDisconnected()
919 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700920 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700921 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700922 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700923 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700924 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700925 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700926 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700927 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700928 break;
929 }
930 }
931
932 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700933 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700934 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700935 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700936 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700937 }
938
939 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700940 public void onVideoStateChanged(Connection c, int videoState) {
941 String id = mIdByConnection.get(c);
942 Log.d(this, "Adapter set video state %d", videoState);
943 mAdapter.setVideoState(id, videoState);
944 }
945
946 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700947 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700948 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700949 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700950 }
951
952 @Override
953 public void onCallerDisplayNameChanged(
954 Connection c, String callerDisplayName, int presentation) {
955 String id = mIdByConnection.get(c);
956 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700957 }
958
959 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700960 public void onDestroyed(Connection c) {
961 removeConnection(c);
962 }
Ihab Awadf8358972014-05-28 16:46:42 -0700963
964 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700965 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700966 String id = mIdByConnection.get(c);
967 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700968 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700969 }
970
971 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800972 public void onPostDialChar(Connection c, char nextChar) {
973 String id = mIdByConnection.get(c);
974 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
975 mAdapter.onPostDialChar(id, nextChar);
976 }
977
978 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700979 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700980 String id = mIdByConnection.get(c);
981 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700982 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700983 }
Santos Cordonb6939982014-06-04 20:20:58 -0700984
985 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800986 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700987 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700988 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800989 Connection.capabilitiesToString(capabilities));
990 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700991 }
992
Santos Cordonb6939982014-06-04 20:20:58 -0700993 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700994 public void onConnectionPropertiesChanged(Connection c, int properties) {
995 String id = mIdByConnection.get(c);
996 Log.d(this, "properties: parcelableconnection: %s",
997 Connection.propertiesToString(properties));
998 mAdapter.setConnectionProperties(id, properties);
999 }
1000
1001 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001002 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001003 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -07001004 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
1005 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001006 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -07001007 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001008
1009 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001010 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001011 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -07001012 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -07001013 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001014
1015 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -07001016 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -07001017 String id = mIdByConnection.get(c);
1018 mAdapter.setStatusHints(id, statusHints);
1019 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -07001020
1021 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001022 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001023 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -07001024 mAdapter.setConferenceableConnections(
1025 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001026 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001027 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001028
1029 @Override
1030 public void onConferenceChanged(Connection connection, Conference conference) {
1031 String id = mIdByConnection.get(connection);
1032 if (id != null) {
1033 String conferenceId = null;
1034 if (conference != null) {
1035 conferenceId = mIdByConference.get(conference);
1036 }
1037 mAdapter.setIsConferenced(id, conferenceId);
1038 }
1039 }
Anthony Lee17455a32015-04-24 15:25:29 -07001040
1041 @Override
1042 public void onConferenceMergeFailed(Connection connection) {
1043 String id = mIdByConnection.get(connection);
1044 if (id != null) {
1045 mAdapter.onConferenceMergeFailed(id);
1046 }
1047 }
Santos Cordon6b7f9552015-05-27 17:21:45 -07001048
1049 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -07001050 public void onExtrasChanged(Connection c, Bundle extras) {
1051 String id = mIdByConnection.get(c);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001052 if (id != null) {
Tyler Gunndee56a82016-03-23 16:06:34 -07001053 mAdapter.putExtras(id, extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -07001054 }
1055 }
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001056
Tyler Gunndee56a82016-03-23 16:06:34 -07001057 public void onExtrasRemoved(Connection c, List<String> keys) {
1058 String id = mIdByConnection.get(c);
1059 if (id != null) {
1060 mAdapter.removeExtras(id, keys);
1061 }
1062 }
1063
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001064
1065 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001066 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001067 String id = mIdByConnection.get(connection);
1068 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001069 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -08001070 }
1071 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001072 };
1073
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001074 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -07001075 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001076 public final IBinder onBind(Intent intent) {
1077 return mBinder;
1078 }
1079
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001080 /** {@inheritDoc} */
1081 @Override
1082 public boolean onUnbind(Intent intent) {
1083 endAllConnections();
1084 return super.onUnbind(intent);
1085 }
1086
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001087 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -07001088 * This can be used by telecom to either create a new outgoing call or attach to an existing
1089 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001090 * createConnection util a connection service cancels the process or completes it successfully.
1091 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001092 private void createConnection(
1093 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001094 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -07001095 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -07001096 boolean isIncoming,
1097 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001098 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001099 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
1100 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -07001101 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001102
Yorke Leec3cf9822014-10-02 09:38:39 -07001103 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
1104 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -07001105 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001106 Log.d(this, "createConnection, connection: %s", connection);
1107 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001108 connection = Connection.createFailedConnection(
1109 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001110 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001111
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001112 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001113 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001114 addConnection(callId, connection);
1115 }
1116
Andrew Lee100e2932014-09-08 15:34:24 -07001117 Uri address = connection.getAddress();
1118 String number = address == null ? "null" : address.getSchemeSpecificPart();
Tyler Gunn720c6642016-03-22 09:02:47 -07001119 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s, properties: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001120 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001121 Connection.stateToString(connection.getState()),
Tyler Gunn720c6642016-03-22 09:02:47 -07001122 Connection.capabilitiesToString(connection.getConnectionCapabilities()),
1123 Connection.propertiesToString(connection.getConnectionProperties()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001124
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001125 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -07001126 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001127 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -07001128 request,
1129 new ParcelableConnection(
1130 request.getAccountHandle(),
1131 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001132 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001133 connection.getConnectionProperties(),
Christine Hallstrom4e22d6d2016-11-30 16:06:42 -08001134 connection.getSupportedAudioRoutes(),
Andrew Lee100e2932014-09-08 15:34:24 -07001135 connection.getAddress(),
1136 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -07001137 connection.getCallerDisplayName(),
1138 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -07001139 connection.getVideoProvider() == null ?
1140 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001141 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -07001142 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -07001143 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001144 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -07001145 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001146 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001147 createIdList(connection.getConferenceables()),
1148 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001149 if (isUnknown) {
1150 triggerConferenceRecalculate();
1151 }
Evan Charltonbf11f982014-07-20 22:06:28 -07001152 }
1153
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001154 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001155 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001156 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001157 }
1158
Tyler Gunnbe74de02014-08-29 14:51:48 -07001159 private void answerVideo(String callId, int videoState) {
1160 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -07001161 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001162 }
1163
Tyler Gunnbe74de02014-08-29 14:51:48 -07001164 private void answer(String callId) {
1165 Log.d(this, "answer %s", callId);
1166 findConnectionForAction(callId, "answer").onAnswer();
1167 }
1168
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001169 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001170 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001171 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -07001172 }
1173
Bryce Lee81901682015-08-28 16:38:02 -07001174 private void reject(String callId, String rejectWithMessage) {
1175 Log.d(this, "reject %s with message", callId);
1176 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
1177 }
1178
Bryce Leecac50772015-11-17 15:13:29 -08001179 private void silence(String callId) {
1180 Log.d(this, "silence %s", callId);
1181 findConnectionForAction(callId, "silence").onSilence();
1182 }
1183
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001184 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001185 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001186 if (mConnectionById.containsKey(callId)) {
1187 findConnectionForAction(callId, "disconnect").onDisconnect();
1188 } else {
1189 findConferenceForAction(callId, "disconnect").onDisconnect();
1190 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001191 }
1192
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001193 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001194 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001195 if (mConnectionById.containsKey(callId)) {
1196 findConnectionForAction(callId, "hold").onHold();
1197 } else {
1198 findConferenceForAction(callId, "hold").onHold();
1199 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001200 }
1201
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001202 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -07001203 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -07001204 if (mConnectionById.containsKey(callId)) {
1205 findConnectionForAction(callId, "unhold").onUnhold();
1206 } else {
1207 findConferenceForAction(callId, "unhold").onUnhold();
1208 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001209 }
1210
Yorke Lee4af59352015-05-13 14:14:54 -07001211 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
1212 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001213 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -07001214 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1215 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001216 } else {
Yorke Lee4af59352015-05-13 14:14:54 -07001217 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
1218 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001219 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001220 }
1221
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001222 private void playDtmfTone(String callId, char digit) {
1223 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001224 if (mConnectionById.containsKey(callId)) {
1225 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1226 } else {
1227 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
1228 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001229 }
1230
1231 private void stopDtmfTone(String callId) {
1232 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -07001233 if (mConnectionById.containsKey(callId)) {
1234 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
1235 } else {
1236 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
1237 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001238 }
1239
Santos Cordon823fd3c2014-08-07 18:35:18 -07001240 private void conference(String callId1, String callId2) {
1241 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -07001242
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001243 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -07001244 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001245 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001246 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001247 conference2 = findConferenceForAction(callId2, "conference");
1248 if (conference2 == getNullConference()) {
1249 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
1250 callId2);
1251 return;
1252 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001253 }
Santos Cordonb6939982014-06-04 20:20:58 -07001254
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001255 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -07001256 Connection connection1 = findConnectionForAction(callId1, "conference");
1257 if (connection1 == getNullConnection()) {
1258 Conference conference1 = findConferenceForAction(callId1, "addConnection");
1259 if (conference1 == getNullConference()) {
1260 Log.w(this,
1261 "Connection1 or Conference1 missing in conference request %s.",
1262 callId1);
1263 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001264 // Call 1 is a conference.
1265 if (connection2 != getNullConnection()) {
1266 // Call 2 is a connection so merge via call 1 (conference).
1267 conference1.onMerge(connection2);
1268 } else {
1269 // Call 2 is ALSO a conference; this should never happen.
1270 Log.wtf(this, "There can only be one conference and an attempt was made to " +
1271 "merge two conferences.");
1272 return;
1273 }
Ihab Awad50e35062014-09-30 09:17:03 -07001274 }
1275 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001276 // Call 1 is a connection.
1277 if (conference2 != getNullConference()) {
1278 // Call 2 is a conference, so merge via call 2.
1279 conference2.onMerge(connection1);
1280 } else {
1281 // Call 2 is a connection, so merge together.
1282 onConference(connection1, connection2);
1283 }
Ihab Awad50e35062014-09-30 09:17:03 -07001284 }
Santos Cordon980acb92014-05-31 10:31:19 -07001285 }
1286
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001287 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -07001288 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -07001289
1290 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001291 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -07001292 Log.w(this, "Connection missing in conference request %s.", callId);
1293 return;
1294 }
1295
Santos Cordon0159ac02014-08-21 14:28:11 -07001296 Conference conference = connection.getConference();
1297 if (conference != null) {
1298 conference.onSeparate(connection);
1299 }
Santos Cordon980acb92014-05-31 10:31:19 -07001300 }
1301
Santos Cordona4868042014-09-04 17:39:22 -07001302 private void mergeConference(String callId) {
1303 Log.d(this, "mergeConference(%s)", callId);
1304 Conference conference = findConferenceForAction(callId, "mergeConference");
1305 if (conference != null) {
1306 conference.onMerge();
1307 }
1308 }
1309
1310 private void swapConference(String callId) {
1311 Log.d(this, "swapConference(%s)", callId);
1312 Conference conference = findConferenceForAction(callId, "swapConference");
1313 if (conference != null) {
1314 conference.onSwap();
1315 }
1316 }
1317
Tyler Gunn876dbfb2016-03-14 15:18:07 -07001318 /**
1319 * Notifies a {@link Connection} of a request to pull an external call.
1320 *
1321 * See {@link Call#pullExternalCall()}.
1322 *
1323 * @param callId The ID of the call to pull.
1324 */
1325 private void pullExternalCall(String callId) {
1326 Log.d(this, "pullExternalCall(%s)", callId);
1327 Connection connection = findConnectionForAction(callId, "pullExternalCall");
1328 if (connection != null) {
1329 connection.onPullExternalCall();
1330 }
1331 }
1332
1333 /**
1334 * Notifies a {@link Connection} of a call event.
1335 *
1336 * See {@link Call#sendCallEvent(String, Bundle)}.
1337 *
1338 * @param callId The ID of the call receiving the event.
1339 * @param event The event.
1340 * @param extras Extras associated with the event.
1341 */
1342 private void sendCallEvent(String callId, String event, Bundle extras) {
1343 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
1344 Connection connection = findConnectionForAction(callId, "sendCallEvent");
1345 if (connection != null) {
1346 connection.onCallEvent(event, extras);
1347 }
1348
1349 }
1350
Tyler Gunndee56a82016-03-23 16:06:34 -07001351 /**
1352 * Notifies a {@link Connection} or {@link Conference} of a change to the extras from Telecom.
1353 * <p>
1354 * These extra changes can originate from Telecom itself, or from an {@link InCallService} via
1355 * the {@link android.telecom.Call#putExtra(String, boolean)},
1356 * {@link android.telecom.Call#putExtra(String, int)},
1357 * {@link android.telecom.Call#putExtra(String, String)},
1358 * {@link Call#removeExtras(List)}.
1359 *
1360 * @param callId The ID of the call receiving the event.
1361 * @param extras The new extras bundle.
1362 */
1363 private void handleExtrasChanged(String callId, Bundle extras) {
1364 Log.d(this, "handleExtrasChanged(%s, %s)", callId, extras);
1365 if (mConnectionById.containsKey(callId)) {
1366 findConnectionForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1367 } else if (mConferenceById.containsKey(callId)) {
1368 findConferenceForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1369 }
1370 }
1371
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001372 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001373 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001374 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001375 }
1376
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001377 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001378 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001379 // No need to query again if we already did it.
1380 return;
1381 }
1382
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001383 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -07001384 @Override
1385 public void onResult(
1386 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001387 final List<IBinder> services) {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001388 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oR", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001389 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001390 public void loggedRun() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001391 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001392 mRemoteConnectionManager.addConnectionService(
1393 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001394 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -07001395 }
Ihab Awad5d0410f2014-07-30 10:07:40 -07001396 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001397 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -07001398 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001399 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001400 }
1401
1402 @Override
1403 public void onError() {
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001404 mHandler.post(new android.telecom.Logging.Runnable("oAA.qRCS.oE", null /*lock*/) {
Ihab Awad6107bab2014-08-18 09:23:25 -07001405 @Override
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001406 public void loggedRun() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001407 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -07001408 }
Brad Ebinger0c3541b2016-11-01 14:11:38 -07001409 }.prepare());
Santos Cordon52d8a152014-06-17 19:08:45 -07001410 }
1411 });
1412 }
1413
Ihab Awadf8b69882014-07-25 15:14:01 -07001414 /**
1415 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001416 * incoming request. This is used by {@code ConnectionService}s that are registered with
1417 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
1418 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001419 *
1420 * @param connectionManagerPhoneAccount See description at
1421 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1422 * @param request Details about the incoming call.
1423 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1424 * not handle the call.
1425 */
1426 public final RemoteConnection createRemoteIncomingConnection(
1427 PhoneAccountHandle connectionManagerPhoneAccount,
1428 ConnectionRequest request) {
1429 return mRemoteConnectionManager.createRemoteConnection(
1430 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -07001431 }
1432
1433 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001434 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001435 * outgoing request. This is used by {@code ConnectionService}s that are registered with
1436 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
1437 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001438 *
1439 * @param connectionManagerPhoneAccount See description at
1440 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
Cuihtlauac ALVARADO0b3b2a52016-09-13 14:49:41 +02001441 * @param request Details about the outgoing call.
Ihab Awadf8b69882014-07-25 15:14:01 -07001442 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1443 * not handle the call.
1444 */
1445 public final RemoteConnection createRemoteOutgoingConnection(
1446 PhoneAccountHandle connectionManagerPhoneAccount,
1447 ConnectionRequest request) {
1448 return mRemoteConnectionManager.createRemoteConnection(
1449 connectionManagerPhoneAccount, request, false);
1450 }
1451
1452 /**
Santos Cordona663f862014-10-29 13:49:58 -07001453 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1454 * {@link RemoteConnection}s should be merged into a conference call.
1455 * <p>
1456 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1457 * be invoked.
1458 *
1459 * @param remoteConnection1 The first of the remote connections to conference.
1460 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001461 */
1462 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001463 RemoteConnection remoteConnection1,
1464 RemoteConnection remoteConnection2) {
1465 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001466 }
1467
1468 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001469 * Adds a new conference call. When a conference call is created either as a result of an
1470 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1471 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1472 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1473 *
1474 * @param conference The new conference object.
1475 */
1476 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001477 Log.d(this, "addConference: conference=%s", conference);
1478
Santos Cordon823fd3c2014-08-07 18:35:18 -07001479 String id = addConferenceInternal(conference);
1480 if (id != null) {
1481 List<String> connectionIds = new ArrayList<>(2);
1482 for (Connection connection : conference.getConnections()) {
1483 if (mIdByConnection.containsKey(connection)) {
1484 connectionIds.add(mIdByConnection.get(connection));
1485 }
1486 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001487 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001488 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001489 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001490 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001491 conference.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001492 conference.getConnectionProperties(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001493 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001494 conference.getVideoProvider() == null ?
1495 null : conference.getVideoProvider().getInterface(),
1496 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001497 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001498 conference.getStatusHints(),
1499 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001500
Santos Cordon823fd3c2014-08-07 18:35:18 -07001501 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001502 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1503 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001504
1505 // Go through any child calls and set the parent.
1506 for (Connection connection : conference.getConnections()) {
1507 String connectionId = mIdByConnection.get(connection);
1508 if (connectionId != null) {
1509 mAdapter.setIsConferenced(connectionId, id);
1510 }
1511 }
1512 }
1513 }
1514
1515 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001516 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1517 * connection.
1518 *
1519 * @param phoneAccountHandle The phone account handle for the connection.
1520 * @param connection The connection to add.
1521 */
1522 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1523 Connection connection) {
1524
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001525 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001526 if (id != null) {
1527 List<String> emptyList = new ArrayList<>(0);
1528
1529 ParcelableConnection parcelableConnection = new ParcelableConnection(
1530 phoneAccountHandle,
1531 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001532 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001533 connection.getConnectionProperties(),
Christine Hallstrom4e22d6d2016-11-30 16:06:42 -08001534 connection.getSupportedAudioRoutes(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001535 connection.getAddress(),
1536 connection.getAddressPresentation(),
1537 connection.getCallerDisplayName(),
1538 connection.getCallerDisplayNamePresentation(),
1539 connection.getVideoProvider() == null ?
1540 null : connection.getVideoProvider().getInterface(),
1541 connection.getVideoState(),
1542 connection.isRingbackRequested(),
1543 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001544 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001545 connection.getStatusHints(),
1546 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001547 emptyList,
1548 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001549 mAdapter.addExistingConnection(id, parcelableConnection);
1550 }
1551 }
1552
1553 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001554 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1555 * has taken responsibility.
1556 *
1557 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001558 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001559 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001560 return mConnectionById.values();
1561 }
1562
1563 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001564 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1565 * has taken responsibility.
1566 *
1567 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1568 */
1569 public final Collection<Conference> getAllConferences() {
1570 return mConferenceById.values();
1571 }
1572
1573 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001574 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1575 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001576 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001577 * @param connectionManagerPhoneAccount See description at
1578 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1579 * @param request Details about the incoming call.
1580 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1581 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001582 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001583 public Connection onCreateIncomingConnection(
1584 PhoneAccountHandle connectionManagerPhoneAccount,
1585 ConnectionRequest request) {
1586 return null;
1587 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001588
1589 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001590 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1591 * Connection is part of a conference controller but is not yet added to Connection
1592 * Service and hence cannot be added to the conference call.
1593 *
1594 * @hide
1595 */
1596 public void triggerConferenceRecalculate() {
1597 }
1598
1599 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001600 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1601 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001602 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001603 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1604 * this call.
1605 * <p>
1606 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1607 * has registered one or more {@code PhoneAccount}s having
1608 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1609 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1610 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1611 * making the connection.
1612 * <p>
1613 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1614 * being asked to make a direct connection. The
1615 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1616 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1617 * making the connection.
1618 * @param request Details about the outgoing call.
1619 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001620 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001621 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001622 public Connection onCreateOutgoingConnection(
1623 PhoneAccountHandle connectionManagerPhoneAccount,
1624 ConnectionRequest request) {
1625 return null;
1626 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001627
1628 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001629 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1630 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1631 * call created using
1632 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1633 *
Yorke Lee770ed6e2014-10-06 18:58:52 -07001634 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001635 */
1636 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1637 ConnectionRequest request) {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001638 return null;
Yorke Leec3cf9822014-10-02 09:38:39 -07001639 }
1640
1641 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001642 * Conference two specified connections. Invoked when the user has made a request to merge the
1643 * specified connections into a conference call. In response, the connection service should
1644 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001645 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001646 * @param connection1 A connection to merge into a conference call.
1647 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001648 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001649 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001650
Santos Cordona663f862014-10-29 13:49:58 -07001651 /**
1652 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1653 * When this method is invoked, this {@link ConnectionService} should create its own
1654 * representation of the conference call and send it to telecom using {@link #addConference}.
1655 * <p>
1656 * This is only relevant to {@link ConnectionService}s which are registered with
1657 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1658 *
1659 * @param conference The remote conference call.
1660 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001661 public void onRemoteConferenceAdded(RemoteConference conference) {}
1662
Santos Cordon823fd3c2014-08-07 18:35:18 -07001663 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001664 * Called when an existing connection is added remotely.
1665 * @param connection The existing connection which was added.
1666 */
1667 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1668
1669 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001670 * @hide
1671 */
1672 public boolean containsConference(Conference conference) {
1673 return mIdByConference.containsKey(conference);
1674 }
1675
Ihab Awadb8e85c72014-08-23 20:34:57 -07001676 /** {@hide} */
1677 void addRemoteConference(RemoteConference remoteConference) {
1678 onRemoteConferenceAdded(remoteConference);
1679 }
1680
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001681 /** {@hide} */
1682 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1683 onRemoteExistingConnectionAdded(remoteConnection);
1684 }
1685
Ihab Awad5d0410f2014-07-30 10:07:40 -07001686 private void onAccountsInitialized() {
1687 mAreAccountsInitialized = true;
1688 for (Runnable r : mPreInitializationConnectionRequests) {
1689 r.run();
1690 }
1691 mPreInitializationConnectionRequests.clear();
1692 }
1693
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001694 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001695 * Adds an existing connection to the list of connections, identified by a new call ID unique
1696 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001697 *
1698 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001699 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001700 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001701 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1702 String id;
Tyler Gunn2282bb92016-10-17 15:48:19 -07001703
1704 if (connection.getExtras() != null && connection.getExtras()
1705 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1706 id = connection.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1707 Log.d(this, "addExistingConnectionInternal - conn %s reusing original id %s",
1708 connection.getTelecomCallId(), id);
1709 } else if (handle == null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001710 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1711 // so just use a random UUID.
1712 id = UUID.randomUUID().toString();
1713 } else {
1714 // Phone account handle was provided, so use the ConnectionService class name as a
1715 // prefix for a unique incremental call ID.
1716 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1717 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001718 addConnection(id, connection);
1719 return id;
1720 }
1721
Ihab Awad542e0ea2014-05-16 10:22:16 -07001722 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001723 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001724 mConnectionById.put(callId, connection);
1725 mIdByConnection.put(connection, callId);
1726 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001727 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001728 }
1729
Anthony Lee30e65842014-11-06 16:30:53 -08001730 /** {@hide} */
1731 protected void removeConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001732 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001733 connection.removeConnectionListener(mConnectionListener);
Chenjie Luoe370b532016-05-12 16:59:43 -07001734 String id = mIdByConnection.get(connection);
1735 if (id != null) {
1736 mConnectionById.remove(id);
1737 mIdByConnection.remove(connection);
1738 mAdapter.removeCall(id);
1739 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001740 }
1741
Santos Cordon823fd3c2014-08-07 18:35:18 -07001742 private String addConferenceInternal(Conference conference) {
Tyler Gunn2282bb92016-10-17 15:48:19 -07001743 String originalId = null;
1744 if (conference.getExtras() != null && conference.getExtras()
1745 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1746 originalId = conference.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1747 Log.d(this, "addConferenceInternal: conf %s reusing original id %s",
1748 conference.getTelecomCallId(),
1749 originalId);
1750 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001751 if (mIdByConference.containsKey(conference)) {
1752 Log.w(this, "Re-adding an existing conference: %s.", conference);
1753 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001754 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1755 // cannot determine a ConnectionService class name to associate with the ID, so use
1756 // a unique UUID (for now).
Tyler Gunn2282bb92016-10-17 15:48:19 -07001757 String id = originalId == null ? UUID.randomUUID().toString() : originalId;
Santos Cordon823fd3c2014-08-07 18:35:18 -07001758 mConferenceById.put(id, conference);
1759 mIdByConference.put(conference, id);
1760 conference.addListener(mConferenceListener);
1761 return id;
1762 }
1763
1764 return null;
1765 }
1766
1767 private void removeConference(Conference conference) {
1768 if (mIdByConference.containsKey(conference)) {
1769 conference.removeListener(mConferenceListener);
1770
1771 String id = mIdByConference.get(conference);
1772 mConferenceById.remove(id);
1773 mIdByConference.remove(conference);
1774 mAdapter.removeCall(id);
1775 }
1776 }
1777
Ihab Awad542e0ea2014-05-16 10:22:16 -07001778 private Connection findConnectionForAction(String callId, String action) {
1779 if (mConnectionById.containsKey(callId)) {
1780 return mConnectionById.get(callId);
1781 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001782 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001783 return getNullConnection();
1784 }
1785
1786 static synchronized Connection getNullConnection() {
1787 if (sNullConnection == null) {
1788 sNullConnection = new Connection() {};
1789 }
1790 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001791 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001792
1793 private Conference findConferenceForAction(String conferenceId, String action) {
1794 if (mConferenceById.containsKey(conferenceId)) {
1795 return mConferenceById.get(conferenceId);
1796 }
1797 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1798 return getNullConference();
1799 }
1800
Ihab Awadb8e85c72014-08-23 20:34:57 -07001801 private List<String> createConnectionIdList(List<Connection> connections) {
1802 List<String> ids = new ArrayList<>();
1803 for (Connection c : connections) {
1804 if (mIdByConnection.containsKey(c)) {
1805 ids.add(mIdByConnection.get(c));
1806 }
1807 }
1808 Collections.sort(ids);
1809 return ids;
1810 }
1811
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001812 /**
1813 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001814 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001815 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001816 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001817 * @return List of string conference and call Ids.
1818 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001819 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001820 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001821 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001822 // Only allow Connection and Conference conferenceables.
1823 if (c instanceof Connection) {
1824 Connection connection = (Connection) c;
1825 if (mIdByConnection.containsKey(connection)) {
1826 ids.add(mIdByConnection.get(connection));
1827 }
1828 } else if (c instanceof Conference) {
1829 Conference conference = (Conference) c;
1830 if (mIdByConference.containsKey(conference)) {
1831 ids.add(mIdByConference.get(conference));
1832 }
1833 }
1834 }
1835 Collections.sort(ids);
1836 return ids;
1837 }
1838
Santos Cordon0159ac02014-08-21 14:28:11 -07001839 private Conference getNullConference() {
1840 if (sNullConference == null) {
1841 sNullConference = new Conference(null) {};
1842 }
1843 return sNullConference;
1844 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001845
1846 private void endAllConnections() {
1847 // Unbound from telecomm. We should end all connections and conferences.
1848 for (Connection connection : mIdByConnection.keySet()) {
1849 // only operate on top-level calls. Conference calls will be removed on their own.
1850 if (connection.getConference() == null) {
1851 connection.onDisconnect();
1852 }
1853 }
1854 for (Conference conference : mIdByConference.keySet()) {
1855 conference.onDisconnect();
1856 }
1857 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001858
1859 /**
1860 * Retrieves the next call ID as maintainted by the connection service.
1861 *
1862 * @return The call ID.
1863 */
1864 private int getNextCallId() {
Brad Ebingerb32d4f82016-10-24 16:40:49 -07001865 synchronized (mIdSyncRoot) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001866 return ++mId;
1867 }
1868 }
Santos Cordon980acb92014-05-31 10:31:19 -07001869}