blob: 4e330bdbf67d9a41927f0e172f13985cc4313daf [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;
Andrew Lee14185762014-07-25 09:41:56 -070029
Sailesh Nepal2a46b902014-07-04 17:21:07 -070030import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070031import com.android.internal.telecom.IConnectionService;
32import com.android.internal.telecom.IConnectionServiceAdapter;
33import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070034
Ihab Awad5d0410f2014-07-30 10:07:40 -070035import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070036import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070037import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070038import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070039import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070040import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070041import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070042
43/**
Santos Cordon895d4b82015-06-25 16:41:48 -070044 * An abstract service that should be implemented by any apps which can make phone calls (VoIP or
45 * otherwise) and want those calls to be integrated into the built-in phone app.
Santos Cordona663f862014-10-29 13:49:58 -070046 * Once implemented, the {@code ConnectionService} needs two additional steps before it will be
47 * integrated into the phone app:
48 * <p>
49 * 1. <i>Registration in AndroidManifest.xml</i>
50 * <br/>
51 * <pre>
52 * &lt;service android:name="com.example.package.MyConnectionService"
53 * android:label="@string/some_label_for_my_connection_service"
Yorke Lee249c12e2015-05-13 15:59:29 -070054 * android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"&gt;
Santos Cordona663f862014-10-29 13:49:58 -070055 * &lt;intent-filter&gt;
56 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
57 * &lt;/intent-filter&gt;
58 * &lt;/service&gt;
59 * </pre>
60 * <p>
61 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
62 * <br/>
63 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
64 * <p>
Santos Cordon895d4b82015-06-25 16:41:48 -070065 * Once registered and enabled by the user in the phone app settings, telecom will bind to a
Santos Cordona663f862014-10-29 13:49:58 -070066 * {@code ConnectionService} implementation when it wants that {@code ConnectionService} to place
67 * a call or the service has indicated that is has an incoming call through
68 * {@link TelecomManager#addNewIncomingCall}. The {@code ConnectionService} can then expect a call
69 * to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection} wherein it
70 * should provide a new instance of a {@link Connection} object. It is through this
71 * {@link Connection} object that telecom receives state updates and the {@code ConnectionService}
72 * receives call-commands such as answer, reject, hold and disconnect.
73 * <p>
74 * When there are no more live calls, telecom will unbind from the {@code ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070075 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070076public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070077 /**
78 * The {@link Intent} that must be declared as handled by the service.
79 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070080 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070081 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070082
Ihab Awad542e0ea2014-05-16 10:22:16 -070083 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070084 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070085
Ihab Awad8aecfed2014-08-08 17:06:11 -070086 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070087 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070088 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070089 private static final int MSG_ANSWER = 4;
90 private static final int MSG_REJECT = 5;
91 private static final int MSG_DISCONNECT = 6;
92 private static final int MSG_HOLD = 7;
93 private static final int MSG_UNHOLD = 8;
Yorke Lee4af59352015-05-13 14:14:54 -070094 private static final int MSG_ON_CALL_AUDIO_STATE_CHANGED = 9;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070095 private static final int MSG_PLAY_DTMF_TONE = 10;
96 private static final int MSG_STOP_DTMF_TONE = 11;
97 private static final int MSG_CONFERENCE = 12;
98 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070099 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700100 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700101 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700102 private static final int MSG_MERGE_CONFERENCE = 18;
103 private static final int MSG_SWAP_CONFERENCE = 19;
Bryce Lee81901682015-08-28 16:38:02 -0700104 private static final int MSG_REJECT_WITH_MESSAGE = 20;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700105
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700106 private static Connection sNullConnection;
107
mike dooley95e80702014-09-18 14:07:52 -0700108 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
109 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
110 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
111 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700112 private final RemoteConnectionManager mRemoteConnectionManager =
113 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700114 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700115 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700116
Santos Cordon823fd3c2014-08-07 18:35:18 -0700117 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700118 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700119
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700120 private final IBinder mBinder = new IConnectionService.Stub() {
121 @Override
122 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700123 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
124 }
125
126 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
127 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700128 }
129
130 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700131 public void createConnection(
132 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700133 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700134 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700135 boolean isIncoming,
136 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700137 SomeArgs args = SomeArgs.obtain();
138 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700139 args.arg2 = id;
140 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700141 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700142 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700143 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700144 }
145
146 @Override
147 public void abort(String callId) {
148 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
149 }
150
151 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700152 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700153 SomeArgs args = SomeArgs.obtain();
154 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700155 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700156 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
157 }
158
159 @Override
160 public void answer(String callId) {
161 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700162 }
163
164 @Override
165 public void reject(String callId) {
166 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
167 }
168
169 @Override
Bryce Lee81901682015-08-28 16:38:02 -0700170 public void rejectWithMessage(String callId, String message) {
171 SomeArgs args = SomeArgs.obtain();
172 args.arg1 = callId;
173 args.arg2 = message;
174 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
175 }
176
177 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700178 public void disconnect(String callId) {
179 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
180 }
181
182 @Override
183 public void hold(String callId) {
184 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
185 }
186
187 @Override
188 public void unhold(String callId) {
189 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
190 }
191
192 @Override
Yorke Lee4af59352015-05-13 14:14:54 -0700193 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700194 SomeArgs args = SomeArgs.obtain();
195 args.arg1 = callId;
Yorke Lee4af59352015-05-13 14:14:54 -0700196 args.arg2 = callAudioState;
197 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700198 }
199
200 @Override
201 public void playDtmfTone(String callId, char digit) {
202 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
203 }
204
205 @Override
206 public void stopDtmfTone(String callId) {
207 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
208 }
209
210 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700211 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700212 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700213 args.arg1 = callId1;
214 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700215 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
216 }
217
218 @Override
219 public void splitFromConference(String callId) {
220 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
221 }
222
223 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700224 public void mergeConference(String callId) {
225 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
226 }
227
228 @Override
229 public void swapConference(String callId) {
230 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
231 }
232
233 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700234 public void onPostDialContinue(String callId, boolean proceed) {
235 SomeArgs args = SomeArgs.obtain();
236 args.arg1 = callId;
237 args.argi1 = proceed ? 1 : 0;
238 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
239 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700240 };
241
242 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
243 @Override
244 public void handleMessage(Message msg) {
245 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700246 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700247 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
248 onAdapterAttached();
249 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700250 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
251 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
252 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700253 case MSG_CREATE_CONNECTION: {
254 SomeArgs args = (SomeArgs) msg.obj;
255 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700256 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700257 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700258 final String id = (String) args.arg2;
259 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700260 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700261 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700262 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700263 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700264 mPreInitializationConnectionRequests.add(new Runnable() {
265 @Override
266 public void run() {
267 createConnection(
268 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700269 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700270 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700271 isIncoming,
272 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700273 }
274 });
275 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700276 createConnection(
277 connectionManagerPhoneAccount,
278 id,
279 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700280 isIncoming,
281 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700282 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700283 } finally {
284 args.recycle();
285 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700286 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700287 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700288 case MSG_ABORT:
289 abort((String) msg.obj);
290 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700291 case MSG_ANSWER:
292 answer((String) msg.obj);
293 break;
294 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700295 SomeArgs args = (SomeArgs) msg.obj;
296 try {
297 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700298 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700299 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700300 } finally {
301 args.recycle();
302 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700303 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700304 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700305 case MSG_REJECT:
306 reject((String) msg.obj);
307 break;
Bryce Lee81901682015-08-28 16:38:02 -0700308 case MSG_REJECT_WITH_MESSAGE: {
309 SomeArgs args = (SomeArgs) msg.obj;
310 try {
311 reject((String) args.arg1, (String) args.arg2);
312 } finally {
313 args.recycle();
314 }
315 break;
316 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700317 case MSG_DISCONNECT:
318 disconnect((String) msg.obj);
319 break;
320 case MSG_HOLD:
321 hold((String) msg.obj);
322 break;
323 case MSG_UNHOLD:
324 unhold((String) msg.obj);
325 break;
Yorke Lee4af59352015-05-13 14:14:54 -0700326 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700327 SomeArgs args = (SomeArgs) msg.obj;
328 try {
329 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700330 CallAudioState audioState = (CallAudioState) args.arg2;
331 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700332 } finally {
333 args.recycle();
334 }
335 break;
336 }
337 case MSG_PLAY_DTMF_TONE:
338 playDtmfTone((String) msg.obj, (char) msg.arg1);
339 break;
340 case MSG_STOP_DTMF_TONE:
341 stopDtmfTone((String) msg.obj);
342 break;
343 case MSG_CONFERENCE: {
344 SomeArgs args = (SomeArgs) msg.obj;
345 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700346 String callId1 = (String) args.arg1;
347 String callId2 = (String) args.arg2;
348 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700349 } finally {
350 args.recycle();
351 }
352 break;
353 }
354 case MSG_SPLIT_FROM_CONFERENCE:
355 splitFromConference((String) msg.obj);
356 break;
Santos Cordona4868042014-09-04 17:39:22 -0700357 case MSG_MERGE_CONFERENCE:
358 mergeConference((String) msg.obj);
359 break;
360 case MSG_SWAP_CONFERENCE:
361 swapConference((String) msg.obj);
362 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700363 case MSG_ON_POST_DIAL_CONTINUE: {
364 SomeArgs args = (SomeArgs) msg.obj;
365 try {
366 String callId = (String) args.arg1;
367 boolean proceed = (args.argi1 == 1);
368 onPostDialContinue(callId, proceed);
369 } finally {
370 args.recycle();
371 }
372 break;
373 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700374 default:
375 break;
376 }
377 }
378 };
379
Santos Cordon823fd3c2014-08-07 18:35:18 -0700380 private final Conference.Listener mConferenceListener = new Conference.Listener() {
381 @Override
382 public void onStateChanged(Conference conference, int oldState, int newState) {
383 String id = mIdByConference.get(conference);
384 switch (newState) {
385 case Connection.STATE_ACTIVE:
386 mAdapter.setActive(id);
387 break;
388 case Connection.STATE_HOLDING:
389 mAdapter.setOnHold(id);
390 break;
391 case Connection.STATE_DISCONNECTED:
392 // handled by onDisconnected
393 break;
394 }
395 }
396
397 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700398 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700399 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700400 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700401 }
402
403 @Override
404 public void onConnectionAdded(Conference conference, Connection connection) {
405 }
406
407 @Override
408 public void onConnectionRemoved(Conference conference, Connection connection) {
409 }
410
411 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700412 public void onConferenceableConnectionsChanged(
413 Conference conference, List<Connection> conferenceableConnections) {
414 mAdapter.setConferenceableConnections(
415 mIdByConference.get(conference),
416 createConnectionIdList(conferenceableConnections));
417 }
418
419 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700420 public void onDestroyed(Conference conference) {
421 removeConference(conference);
422 }
423
424 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800425 public void onConnectionCapabilitiesChanged(
426 Conference conference,
427 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700428 String id = mIdByConference.get(conference);
429 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800430 Connection.capabilitiesToString(connectionCapabilities));
431 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700432 }
Rekha Kumar07366812015-03-24 16:42:31 -0700433
434 @Override
435 public void onVideoStateChanged(Conference c, int videoState) {
436 String id = mIdByConference.get(c);
437 Log.d(this, "onVideoStateChanged set video state %d", videoState);
438 mAdapter.setVideoState(id, videoState);
439 }
440
441 @Override
442 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
443 String id = mIdByConference.get(c);
444 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
445 videoProvider);
446 mAdapter.setVideoProvider(id, videoProvider);
447 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700448
449 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700450 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
451 String id = mIdByConference.get(conference);
452 mAdapter.setStatusHints(id, statusHints);
453 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700454
455 @Override
456 public void onExtrasChanged(Conference conference, Bundle extras) {
457 String id = mIdByConference.get(conference);
458 mAdapter.setExtras(id, extras);
459 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700460 };
461
Ihab Awad542e0ea2014-05-16 10:22:16 -0700462 private final Connection.Listener mConnectionListener = new Connection.Listener() {
463 @Override
464 public void onStateChanged(Connection c, int state) {
465 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700466 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700467 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700468 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700469 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700470 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700471 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700472 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700473 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700474 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700475 // Handled in onDisconnected()
476 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700477 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700478 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700479 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700480 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700481 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700482 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700483 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700484 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700485 break;
486 }
487 }
488
489 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700490 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700491 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700492 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700493 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700494 }
495
496 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700497 public void onVideoStateChanged(Connection c, int videoState) {
498 String id = mIdByConnection.get(c);
499 Log.d(this, "Adapter set video state %d", videoState);
500 mAdapter.setVideoState(id, videoState);
501 }
502
503 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700504 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700505 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700506 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700507 }
508
509 @Override
510 public void onCallerDisplayNameChanged(
511 Connection c, String callerDisplayName, int presentation) {
512 String id = mIdByConnection.get(c);
513 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700514 }
515
516 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700517 public void onDestroyed(Connection c) {
518 removeConnection(c);
519 }
Ihab Awadf8358972014-05-28 16:46:42 -0700520
521 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700522 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700523 String id = mIdByConnection.get(c);
524 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700525 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700526 }
527
528 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800529 public void onPostDialChar(Connection c, char nextChar) {
530 String id = mIdByConnection.get(c);
531 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
532 mAdapter.onPostDialChar(id, nextChar);
533 }
534
535 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700536 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700537 String id = mIdByConnection.get(c);
538 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700539 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700540 }
Santos Cordonb6939982014-06-04 20:20:58 -0700541
542 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800543 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700544 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700545 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800546 Connection.capabilitiesToString(capabilities));
547 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700548 }
549
Santos Cordonb6939982014-06-04 20:20:58 -0700550 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700551 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700552 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700553 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
554 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700555 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700556 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700557
558 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700559 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700560 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700561 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700562 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700563
564 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700565 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700566 String id = mIdByConnection.get(c);
567 mAdapter.setStatusHints(id, statusHints);
568 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700569
570 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800571 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700572 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700573 mAdapter.setConferenceableConnections(
574 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800575 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700576 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700577
578 @Override
579 public void onConferenceChanged(Connection connection, Conference conference) {
580 String id = mIdByConnection.get(connection);
581 if (id != null) {
582 String conferenceId = null;
583 if (conference != null) {
584 conferenceId = mIdByConference.get(conference);
585 }
586 mAdapter.setIsConferenced(id, conferenceId);
587 }
588 }
Anthony Lee17455a32015-04-24 15:25:29 -0700589
590 @Override
591 public void onConferenceMergeFailed(Connection connection) {
592 String id = mIdByConnection.get(connection);
593 if (id != null) {
594 mAdapter.onConferenceMergeFailed(id);
595 }
596 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700597
598 @Override
599 public void onExtrasChanged(Connection connection, Bundle extras) {
600 String id = mIdByConnection.get(connection);
601 if (id != null) {
602 mAdapter.setExtras(id, extras);
603 }
604 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700605 };
606
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700607 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700608 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700609 public final IBinder onBind(Intent intent) {
610 return mBinder;
611 }
612
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700613 /** {@inheritDoc} */
614 @Override
615 public boolean onUnbind(Intent intent) {
616 endAllConnections();
617 return super.onUnbind(intent);
618 }
619
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700620 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700621 * This can be used by telecom to either create a new outgoing call or attach to an existing
622 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700623 * createConnection util a connection service cancels the process or completes it successfully.
624 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700625 private void createConnection(
626 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700627 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700628 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700629 boolean isIncoming,
630 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700631 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700632 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
633 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700634
Yorke Leec3cf9822014-10-02 09:38:39 -0700635 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
636 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700637 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700638 Log.d(this, "createConnection, connection: %s", connection);
639 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700640 connection = Connection.createFailedConnection(
641 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700642 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700643
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700644 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700645 addConnection(callId, connection);
646 }
647
Andrew Lee100e2932014-09-08 15:34:24 -0700648 Uri address = connection.getAddress();
649 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700650 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700651 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700652 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800653 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700654
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700655 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700656 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700657 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700658 request,
659 new ParcelableConnection(
660 request.getAccountHandle(),
661 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800662 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700663 connection.getAddress(),
664 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700665 connection.getCallerDisplayName(),
666 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700667 connection.getVideoProvider() == null ?
668 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700669 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700670 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700671 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -0700672 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700673 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700674 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700675 createIdList(connection.getConferenceables()),
676 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -0800677 if (isUnknown) {
678 triggerConferenceRecalculate();
679 }
Evan Charltonbf11f982014-07-20 22:06:28 -0700680 }
681
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700682 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700683 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700684 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700685 }
686
Tyler Gunnbe74de02014-08-29 14:51:48 -0700687 private void answerVideo(String callId, int videoState) {
688 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700689 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700690 }
691
Tyler Gunnbe74de02014-08-29 14:51:48 -0700692 private void answer(String callId) {
693 Log.d(this, "answer %s", callId);
694 findConnectionForAction(callId, "answer").onAnswer();
695 }
696
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700697 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700698 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700699 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700700 }
701
Bryce Lee81901682015-08-28 16:38:02 -0700702 private void reject(String callId, String rejectWithMessage) {
703 Log.d(this, "reject %s with message", callId);
704 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
705 }
706
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700707 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700708 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700709 if (mConnectionById.containsKey(callId)) {
710 findConnectionForAction(callId, "disconnect").onDisconnect();
711 } else {
712 findConferenceForAction(callId, "disconnect").onDisconnect();
713 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700714 }
715
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700716 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700717 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700718 if (mConnectionById.containsKey(callId)) {
719 findConnectionForAction(callId, "hold").onHold();
720 } else {
721 findConferenceForAction(callId, "hold").onHold();
722 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700723 }
724
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700725 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700726 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700727 if (mConnectionById.containsKey(callId)) {
728 findConnectionForAction(callId, "unhold").onUnhold();
729 } else {
730 findConferenceForAction(callId, "unhold").onUnhold();
731 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700732 }
733
Yorke Lee4af59352015-05-13 14:14:54 -0700734 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
735 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700736 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -0700737 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
738 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700739 } else {
Yorke Lee4af59352015-05-13 14:14:54 -0700740 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
741 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700742 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700743 }
744
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700745 private void playDtmfTone(String callId, char digit) {
746 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700747 if (mConnectionById.containsKey(callId)) {
748 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
749 } else {
750 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
751 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700752 }
753
754 private void stopDtmfTone(String callId) {
755 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700756 if (mConnectionById.containsKey(callId)) {
757 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
758 } else {
759 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
760 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700761 }
762
Santos Cordon823fd3c2014-08-07 18:35:18 -0700763 private void conference(String callId1, String callId2) {
764 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700765
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800766 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700767 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800768 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700769 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800770 conference2 = findConferenceForAction(callId2, "conference");
771 if (conference2 == getNullConference()) {
772 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
773 callId2);
774 return;
775 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700776 }
Santos Cordonb6939982014-06-04 20:20:58 -0700777
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800778 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700779 Connection connection1 = findConnectionForAction(callId1, "conference");
780 if (connection1 == getNullConnection()) {
781 Conference conference1 = findConferenceForAction(callId1, "addConnection");
782 if (conference1 == getNullConference()) {
783 Log.w(this,
784 "Connection1 or Conference1 missing in conference request %s.",
785 callId1);
786 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800787 // Call 1 is a conference.
788 if (connection2 != getNullConnection()) {
789 // Call 2 is a connection so merge via call 1 (conference).
790 conference1.onMerge(connection2);
791 } else {
792 // Call 2 is ALSO a conference; this should never happen.
793 Log.wtf(this, "There can only be one conference and an attempt was made to " +
794 "merge two conferences.");
795 return;
796 }
Ihab Awad50e35062014-09-30 09:17:03 -0700797 }
798 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800799 // Call 1 is a connection.
800 if (conference2 != getNullConference()) {
801 // Call 2 is a conference, so merge via call 2.
802 conference2.onMerge(connection1);
803 } else {
804 // Call 2 is a connection, so merge together.
805 onConference(connection1, connection2);
806 }
Ihab Awad50e35062014-09-30 09:17:03 -0700807 }
Santos Cordon980acb92014-05-31 10:31:19 -0700808 }
809
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700810 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700811 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700812
813 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700814 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700815 Log.w(this, "Connection missing in conference request %s.", callId);
816 return;
817 }
818
Santos Cordon0159ac02014-08-21 14:28:11 -0700819 Conference conference = connection.getConference();
820 if (conference != null) {
821 conference.onSeparate(connection);
822 }
Santos Cordon980acb92014-05-31 10:31:19 -0700823 }
824
Santos Cordona4868042014-09-04 17:39:22 -0700825 private void mergeConference(String callId) {
826 Log.d(this, "mergeConference(%s)", callId);
827 Conference conference = findConferenceForAction(callId, "mergeConference");
828 if (conference != null) {
829 conference.onMerge();
830 }
831 }
832
833 private void swapConference(String callId) {
834 Log.d(this, "swapConference(%s)", callId);
835 Conference conference = findConferenceForAction(callId, "swapConference");
836 if (conference != null) {
837 conference.onSwap();
838 }
839 }
840
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700841 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700842 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700843 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700844 }
845
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700846 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700847 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700848 // No need to query again if we already did it.
849 return;
850 }
851
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700852 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700853 @Override
854 public void onResult(
855 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700856 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700857 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700858 @Override
859 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700860 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700861 mRemoteConnectionManager.addConnectionService(
862 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700863 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700864 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700865 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700866 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700867 }
868 });
869 }
870
871 @Override
872 public void onError() {
873 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700874 @Override
875 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700876 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700877 }
878 });
879 }
880 });
881 }
882
Ihab Awadf8b69882014-07-25 15:14:01 -0700883 /**
884 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700885 * incoming request. This is used by {@code ConnectionService}s that are registered with
886 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
887 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700888 *
889 * @param connectionManagerPhoneAccount See description at
890 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
891 * @param request Details about the incoming call.
892 * @return The {@code Connection} object to satisfy this call, or {@code null} to
893 * not handle the call.
894 */
895 public final RemoteConnection createRemoteIncomingConnection(
896 PhoneAccountHandle connectionManagerPhoneAccount,
897 ConnectionRequest request) {
898 return mRemoteConnectionManager.createRemoteConnection(
899 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700900 }
901
902 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700903 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700904 * outgoing request. This is used by {@code ConnectionService}s that are registered with
905 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
906 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700907 *
908 * @param connectionManagerPhoneAccount See description at
909 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
910 * @param request Details about the incoming call.
911 * @return The {@code Connection} object to satisfy this call, or {@code null} to
912 * not handle the call.
913 */
914 public final RemoteConnection createRemoteOutgoingConnection(
915 PhoneAccountHandle connectionManagerPhoneAccount,
916 ConnectionRequest request) {
917 return mRemoteConnectionManager.createRemoteConnection(
918 connectionManagerPhoneAccount, request, false);
919 }
920
921 /**
Santos Cordona663f862014-10-29 13:49:58 -0700922 * Indicates to the relevant {@code RemoteConnectionService} that the specified
923 * {@link RemoteConnection}s should be merged into a conference call.
924 * <p>
925 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
926 * be invoked.
927 *
928 * @param remoteConnection1 The first of the remote connections to conference.
929 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700930 */
931 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700932 RemoteConnection remoteConnection1,
933 RemoteConnection remoteConnection2) {
934 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700935 }
936
937 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700938 * Adds a new conference call. When a conference call is created either as a result of an
939 * explicit request via {@link #onConference} or otherwise, the connection service should supply
940 * an instance of {@link Conference} by invoking this method. A conference call provided by this
941 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
942 *
943 * @param conference The new conference object.
944 */
945 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -0700946 Log.d(this, "addConference: conference=%s", conference);
947
Santos Cordon823fd3c2014-08-07 18:35:18 -0700948 String id = addConferenceInternal(conference);
949 if (id != null) {
950 List<String> connectionIds = new ArrayList<>(2);
951 for (Connection connection : conference.getConnections()) {
952 if (mIdByConnection.containsKey(connection)) {
953 connectionIds.add(mIdByConnection.get(connection));
954 }
955 }
956 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700957 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700958 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800959 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800960 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -0700961 conference.getVideoProvider() == null ?
962 null : conference.getVideoProvider().getInterface(),
963 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -0700964 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700965 conference.getStatusHints(),
966 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -0700967
Santos Cordon823fd3c2014-08-07 18:35:18 -0700968 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -0700969 mAdapter.setVideoProvider(id, conference.getVideoProvider());
970 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700971
972 // Go through any child calls and set the parent.
973 for (Connection connection : conference.getConnections()) {
974 String connectionId = mIdByConnection.get(connection);
975 if (connectionId != null) {
976 mAdapter.setIsConferenced(connectionId, id);
977 }
978 }
979 }
980 }
981
982 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700983 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
984 * connection.
985 *
986 * @param phoneAccountHandle The phone account handle for the connection.
987 * @param connection The connection to add.
988 */
989 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
990 Connection connection) {
991
992 String id = addExistingConnectionInternal(connection);
993 if (id != null) {
994 List<String> emptyList = new ArrayList<>(0);
995
996 ParcelableConnection parcelableConnection = new ParcelableConnection(
997 phoneAccountHandle,
998 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800999 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001000 connection.getAddress(),
1001 connection.getAddressPresentation(),
1002 connection.getCallerDisplayName(),
1003 connection.getCallerDisplayNamePresentation(),
1004 connection.getVideoProvider() == null ?
1005 null : connection.getVideoProvider().getInterface(),
1006 connection.getVideoState(),
1007 connection.isRingbackRequested(),
1008 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001009 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001010 connection.getStatusHints(),
1011 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001012 emptyList,
1013 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001014 mAdapter.addExistingConnection(id, parcelableConnection);
1015 }
1016 }
1017
1018 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001019 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1020 * has taken responsibility.
1021 *
1022 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001023 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001024 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001025 return mConnectionById.values();
1026 }
1027
1028 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001029 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1030 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001031 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001032 * @param connectionManagerPhoneAccount See description at
1033 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1034 * @param request Details about the incoming call.
1035 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1036 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001037 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001038 public Connection onCreateIncomingConnection(
1039 PhoneAccountHandle connectionManagerPhoneAccount,
1040 ConnectionRequest request) {
1041 return null;
1042 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001043
1044 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001045 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1046 * Connection is part of a conference controller but is not yet added to Connection
1047 * Service and hence cannot be added to the conference call.
1048 *
1049 * @hide
1050 */
1051 public void triggerConferenceRecalculate() {
1052 }
1053
1054 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001055 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1056 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001057 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001058 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1059 * this call.
1060 * <p>
1061 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1062 * has registered one or more {@code PhoneAccount}s having
1063 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1064 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1065 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1066 * making the connection.
1067 * <p>
1068 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1069 * being asked to make a direct connection. The
1070 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1071 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1072 * making the connection.
1073 * @param request Details about the outgoing call.
1074 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001075 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001076 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001077 public Connection onCreateOutgoingConnection(
1078 PhoneAccountHandle connectionManagerPhoneAccount,
1079 ConnectionRequest request) {
1080 return null;
1081 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001082
1083 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001084 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1085 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1086 * call created using
1087 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1088 *
1089 * @param connectionManagerPhoneAccount
1090 * @param request
1091 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001092 *
1093 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001094 */
1095 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1096 ConnectionRequest request) {
1097 return null;
1098 }
1099
1100 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001101 * Conference two specified connections. Invoked when the user has made a request to merge the
1102 * specified connections into a conference call. In response, the connection service should
1103 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001104 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001105 * @param connection1 A connection to merge into a conference call.
1106 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001107 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001108 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001109
Santos Cordona663f862014-10-29 13:49:58 -07001110 /**
1111 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1112 * When this method is invoked, this {@link ConnectionService} should create its own
1113 * representation of the conference call and send it to telecom using {@link #addConference}.
1114 * <p>
1115 * This is only relevant to {@link ConnectionService}s which are registered with
1116 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1117 *
1118 * @param conference The remote conference call.
1119 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001120 public void onRemoteConferenceAdded(RemoteConference conference) {}
1121
Santos Cordon823fd3c2014-08-07 18:35:18 -07001122 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001123 * Called when an existing connection is added remotely.
1124 * @param connection The existing connection which was added.
1125 */
1126 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1127
1128 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001129 * @hide
1130 */
1131 public boolean containsConference(Conference conference) {
1132 return mIdByConference.containsKey(conference);
1133 }
1134
Ihab Awadb8e85c72014-08-23 20:34:57 -07001135 /** {@hide} */
1136 void addRemoteConference(RemoteConference remoteConference) {
1137 onRemoteConferenceAdded(remoteConference);
1138 }
1139
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001140 /** {@hide} */
1141 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1142 onRemoteExistingConnectionAdded(remoteConnection);
1143 }
1144
Ihab Awad5d0410f2014-07-30 10:07:40 -07001145 private void onAccountsInitialized() {
1146 mAreAccountsInitialized = true;
1147 for (Runnable r : mPreInitializationConnectionRequests) {
1148 r.run();
1149 }
1150 mPreInitializationConnectionRequests.clear();
1151 }
1152
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001153 /**
1154 * Adds an existing connection to the list of connections, identified by a new UUID.
1155 *
1156 * @param connection The connection.
1157 * @return The UUID of the connection (e.g. the call-id).
1158 */
1159 private String addExistingConnectionInternal(Connection connection) {
1160 String id = UUID.randomUUID().toString();
1161 addConnection(id, connection);
1162 return id;
1163 }
1164
Ihab Awad542e0ea2014-05-16 10:22:16 -07001165 private void addConnection(String callId, Connection connection) {
1166 mConnectionById.put(callId, connection);
1167 mIdByConnection.put(connection, callId);
1168 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001169 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001170 }
1171
Anthony Lee30e65842014-11-06 16:30:53 -08001172 /** {@hide} */
1173 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001174 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001175 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001176 connection.removeConnectionListener(mConnectionListener);
1177 mConnectionById.remove(mIdByConnection.get(connection));
1178 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001179 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001180 }
1181
Santos Cordon823fd3c2014-08-07 18:35:18 -07001182 private String addConferenceInternal(Conference conference) {
1183 if (mIdByConference.containsKey(conference)) {
1184 Log.w(this, "Re-adding an existing conference: %s.", conference);
1185 } else if (conference != null) {
1186 String id = UUID.randomUUID().toString();
1187 mConferenceById.put(id, conference);
1188 mIdByConference.put(conference, id);
1189 conference.addListener(mConferenceListener);
1190 return id;
1191 }
1192
1193 return null;
1194 }
1195
1196 private void removeConference(Conference conference) {
1197 if (mIdByConference.containsKey(conference)) {
1198 conference.removeListener(mConferenceListener);
1199
1200 String id = mIdByConference.get(conference);
1201 mConferenceById.remove(id);
1202 mIdByConference.remove(conference);
1203 mAdapter.removeCall(id);
1204 }
1205 }
1206
Ihab Awad542e0ea2014-05-16 10:22:16 -07001207 private Connection findConnectionForAction(String callId, String action) {
1208 if (mConnectionById.containsKey(callId)) {
1209 return mConnectionById.get(callId);
1210 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001211 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001212 return getNullConnection();
1213 }
1214
1215 static synchronized Connection getNullConnection() {
1216 if (sNullConnection == null) {
1217 sNullConnection = new Connection() {};
1218 }
1219 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001220 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001221
1222 private Conference findConferenceForAction(String conferenceId, String action) {
1223 if (mConferenceById.containsKey(conferenceId)) {
1224 return mConferenceById.get(conferenceId);
1225 }
1226 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1227 return getNullConference();
1228 }
1229
Ihab Awadb8e85c72014-08-23 20:34:57 -07001230 private List<String> createConnectionIdList(List<Connection> connections) {
1231 List<String> ids = new ArrayList<>();
1232 for (Connection c : connections) {
1233 if (mIdByConnection.containsKey(c)) {
1234 ids.add(mIdByConnection.get(c));
1235 }
1236 }
1237 Collections.sort(ids);
1238 return ids;
1239 }
1240
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001241 /**
1242 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001243 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001244 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001245 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001246 * @return List of string conference and call Ids.
1247 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001248 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001249 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001250 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001251 // Only allow Connection and Conference conferenceables.
1252 if (c instanceof Connection) {
1253 Connection connection = (Connection) c;
1254 if (mIdByConnection.containsKey(connection)) {
1255 ids.add(mIdByConnection.get(connection));
1256 }
1257 } else if (c instanceof Conference) {
1258 Conference conference = (Conference) c;
1259 if (mIdByConference.containsKey(conference)) {
1260 ids.add(mIdByConference.get(conference));
1261 }
1262 }
1263 }
1264 Collections.sort(ids);
1265 return ids;
1266 }
1267
Santos Cordon0159ac02014-08-21 14:28:11 -07001268 private Conference getNullConference() {
1269 if (sNullConference == null) {
1270 sNullConference = new Conference(null) {};
1271 }
1272 return sNullConference;
1273 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001274
1275 private void endAllConnections() {
1276 // Unbound from telecomm. We should end all connections and conferences.
1277 for (Connection connection : mIdByConnection.keySet()) {
1278 // only operate on top-level calls. Conference calls will be removed on their own.
1279 if (connection.getConference() == null) {
1280 connection.onDisconnect();
1281 }
1282 }
1283 for (Conference conference : mIdByConference.keySet()) {
1284 conference.onDisconnect();
1285 }
1286 }
Santos Cordon980acb92014-05-31 10:31:19 -07001287}