blob: ceaa1bfb2754ed12853d1625888a4a7fb2ad8b79 [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;
Bryce Leecac50772015-11-17 15:13:29 -0800105 private static final int MSG_SILENCE = 21;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700106
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700107 private static Connection sNullConnection;
108
mike dooley95e80702014-09-18 14:07:52 -0700109 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
110 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
111 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
112 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700113 private final RemoteConnectionManager mRemoteConnectionManager =
114 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700115 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700116 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700117
Santos Cordon823fd3c2014-08-07 18:35:18 -0700118 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700119 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700120
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700121 private final IBinder mBinder = new IConnectionService.Stub() {
122 @Override
123 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700124 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
125 }
126
127 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
128 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700129 }
130
131 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700132 public void createConnection(
133 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700134 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700135 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700136 boolean isIncoming,
137 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700138 SomeArgs args = SomeArgs.obtain();
139 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700140 args.arg2 = id;
141 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700142 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700143 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700144 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700145 }
146
147 @Override
148 public void abort(String callId) {
149 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
150 }
151
152 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700153 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700154 SomeArgs args = SomeArgs.obtain();
155 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700156 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700157 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
158 }
159
160 @Override
161 public void answer(String callId) {
162 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700163 }
164
165 @Override
166 public void reject(String callId) {
167 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
168 }
169
170 @Override
Bryce Lee81901682015-08-28 16:38:02 -0700171 public void rejectWithMessage(String callId, String message) {
172 SomeArgs args = SomeArgs.obtain();
173 args.arg1 = callId;
174 args.arg2 = message;
175 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
176 }
177
178 @Override
Bryce Leecac50772015-11-17 15:13:29 -0800179 public void silence(String callId) {
180 mHandler.obtainMessage(MSG_SILENCE, callId).sendToTarget();
181 }
182
183 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700184 public void disconnect(String callId) {
185 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
186 }
187
188 @Override
189 public void hold(String callId) {
190 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
191 }
192
193 @Override
194 public void unhold(String callId) {
195 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
196 }
197
198 @Override
Yorke Lee4af59352015-05-13 14:14:54 -0700199 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700200 SomeArgs args = SomeArgs.obtain();
201 args.arg1 = callId;
Yorke Lee4af59352015-05-13 14:14:54 -0700202 args.arg2 = callAudioState;
203 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700204 }
205
206 @Override
207 public void playDtmfTone(String callId, char digit) {
208 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
209 }
210
211 @Override
212 public void stopDtmfTone(String callId) {
213 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
214 }
215
216 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700217 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700218 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700219 args.arg1 = callId1;
220 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700221 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
222 }
223
224 @Override
225 public void splitFromConference(String callId) {
226 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
227 }
228
229 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700230 public void mergeConference(String callId) {
231 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
232 }
233
234 @Override
235 public void swapConference(String callId) {
236 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
237 }
238
239 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700240 public void onPostDialContinue(String callId, boolean proceed) {
241 SomeArgs args = SomeArgs.obtain();
242 args.arg1 = callId;
243 args.argi1 = proceed ? 1 : 0;
244 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
245 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700246 };
247
248 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
249 @Override
250 public void handleMessage(Message msg) {
251 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700252 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700253 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
254 onAdapterAttached();
255 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700256 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
257 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
258 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700259 case MSG_CREATE_CONNECTION: {
260 SomeArgs args = (SomeArgs) msg.obj;
261 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700262 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700263 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700264 final String id = (String) args.arg2;
265 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700266 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700267 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700268 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700269 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700270 mPreInitializationConnectionRequests.add(new Runnable() {
271 @Override
272 public void run() {
273 createConnection(
274 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700275 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700276 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700277 isIncoming,
278 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700279 }
280 });
281 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700282 createConnection(
283 connectionManagerPhoneAccount,
284 id,
285 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700286 isIncoming,
287 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700288 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700289 } finally {
290 args.recycle();
291 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700292 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700293 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700294 case MSG_ABORT:
295 abort((String) msg.obj);
296 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700297 case MSG_ANSWER:
298 answer((String) msg.obj);
299 break;
300 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700301 SomeArgs args = (SomeArgs) msg.obj;
302 try {
303 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700304 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700305 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700306 } finally {
307 args.recycle();
308 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700309 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700310 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700311 case MSG_REJECT:
312 reject((String) msg.obj);
313 break;
Bryce Lee81901682015-08-28 16:38:02 -0700314 case MSG_REJECT_WITH_MESSAGE: {
315 SomeArgs args = (SomeArgs) msg.obj;
316 try {
317 reject((String) args.arg1, (String) args.arg2);
318 } finally {
319 args.recycle();
320 }
321 break;
322 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700323 case MSG_DISCONNECT:
324 disconnect((String) msg.obj);
325 break;
Bryce Leecac50772015-11-17 15:13:29 -0800326 case MSG_SILENCE:
327 silence((String) msg.obj);
328 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700329 case MSG_HOLD:
330 hold((String) msg.obj);
331 break;
332 case MSG_UNHOLD:
333 unhold((String) msg.obj);
334 break;
Yorke Lee4af59352015-05-13 14:14:54 -0700335 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700336 SomeArgs args = (SomeArgs) msg.obj;
337 try {
338 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700339 CallAudioState audioState = (CallAudioState) args.arg2;
340 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700341 } finally {
342 args.recycle();
343 }
344 break;
345 }
346 case MSG_PLAY_DTMF_TONE:
347 playDtmfTone((String) msg.obj, (char) msg.arg1);
348 break;
349 case MSG_STOP_DTMF_TONE:
350 stopDtmfTone((String) msg.obj);
351 break;
352 case MSG_CONFERENCE: {
353 SomeArgs args = (SomeArgs) msg.obj;
354 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700355 String callId1 = (String) args.arg1;
356 String callId2 = (String) args.arg2;
357 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700358 } finally {
359 args.recycle();
360 }
361 break;
362 }
363 case MSG_SPLIT_FROM_CONFERENCE:
364 splitFromConference((String) msg.obj);
365 break;
Santos Cordona4868042014-09-04 17:39:22 -0700366 case MSG_MERGE_CONFERENCE:
367 mergeConference((String) msg.obj);
368 break;
369 case MSG_SWAP_CONFERENCE:
370 swapConference((String) msg.obj);
371 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700372 case MSG_ON_POST_DIAL_CONTINUE: {
373 SomeArgs args = (SomeArgs) msg.obj;
374 try {
375 String callId = (String) args.arg1;
376 boolean proceed = (args.argi1 == 1);
377 onPostDialContinue(callId, proceed);
378 } finally {
379 args.recycle();
380 }
381 break;
382 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700383 default:
384 break;
385 }
386 }
387 };
388
Santos Cordon823fd3c2014-08-07 18:35:18 -0700389 private final Conference.Listener mConferenceListener = new Conference.Listener() {
390 @Override
391 public void onStateChanged(Conference conference, int oldState, int newState) {
392 String id = mIdByConference.get(conference);
393 switch (newState) {
394 case Connection.STATE_ACTIVE:
395 mAdapter.setActive(id);
396 break;
397 case Connection.STATE_HOLDING:
398 mAdapter.setOnHold(id);
399 break;
400 case Connection.STATE_DISCONNECTED:
401 // handled by onDisconnected
402 break;
403 }
404 }
405
406 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700407 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700408 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700409 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700410 }
411
412 @Override
413 public void onConnectionAdded(Conference conference, Connection connection) {
414 }
415
416 @Override
417 public void onConnectionRemoved(Conference conference, Connection connection) {
418 }
419
420 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700421 public void onConferenceableConnectionsChanged(
422 Conference conference, List<Connection> conferenceableConnections) {
423 mAdapter.setConferenceableConnections(
424 mIdByConference.get(conference),
425 createConnectionIdList(conferenceableConnections));
426 }
427
428 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700429 public void onDestroyed(Conference conference) {
430 removeConference(conference);
431 }
432
433 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800434 public void onConnectionCapabilitiesChanged(
435 Conference conference,
436 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700437 String id = mIdByConference.get(conference);
438 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800439 Connection.capabilitiesToString(connectionCapabilities));
440 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700441 }
Rekha Kumar07366812015-03-24 16:42:31 -0700442
443 @Override
444 public void onVideoStateChanged(Conference c, int videoState) {
445 String id = mIdByConference.get(c);
446 Log.d(this, "onVideoStateChanged set video state %d", videoState);
447 mAdapter.setVideoState(id, videoState);
448 }
449
450 @Override
451 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
452 String id = mIdByConference.get(c);
453 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
454 videoProvider);
455 mAdapter.setVideoProvider(id, videoProvider);
456 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700457
458 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700459 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
460 String id = mIdByConference.get(conference);
461 mAdapter.setStatusHints(id, statusHints);
462 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700463
464 @Override
465 public void onExtrasChanged(Conference conference, Bundle extras) {
466 String id = mIdByConference.get(conference);
467 mAdapter.setExtras(id, extras);
468 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700469 };
470
Ihab Awad542e0ea2014-05-16 10:22:16 -0700471 private final Connection.Listener mConnectionListener = new Connection.Listener() {
472 @Override
473 public void onStateChanged(Connection c, int state) {
474 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700475 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700476 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700477 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700478 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700479 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700480 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700481 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700482 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700483 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700484 // Handled in onDisconnected()
485 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700486 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700487 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700488 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700489 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700490 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700491 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700492 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700493 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700494 break;
495 }
496 }
497
498 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700499 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700500 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700501 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700502 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700503 }
504
505 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700506 public void onVideoStateChanged(Connection c, int videoState) {
507 String id = mIdByConnection.get(c);
508 Log.d(this, "Adapter set video state %d", videoState);
509 mAdapter.setVideoState(id, videoState);
510 }
511
512 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700513 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700514 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700515 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700516 }
517
518 @Override
519 public void onCallerDisplayNameChanged(
520 Connection c, String callerDisplayName, int presentation) {
521 String id = mIdByConnection.get(c);
522 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700523 }
524
525 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700526 public void onDestroyed(Connection c) {
527 removeConnection(c);
528 }
Ihab Awadf8358972014-05-28 16:46:42 -0700529
530 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700531 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700532 String id = mIdByConnection.get(c);
533 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700534 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700535 }
536
537 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800538 public void onPostDialChar(Connection c, char nextChar) {
539 String id = mIdByConnection.get(c);
540 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
541 mAdapter.onPostDialChar(id, nextChar);
542 }
543
544 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700545 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700546 String id = mIdByConnection.get(c);
547 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700548 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700549 }
Santos Cordonb6939982014-06-04 20:20:58 -0700550
551 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800552 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700553 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700554 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800555 Connection.capabilitiesToString(capabilities));
556 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700557 }
558
Santos Cordonb6939982014-06-04 20:20:58 -0700559 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700560 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700561 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700562 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
563 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700564 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700565 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700566
567 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700568 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700569 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700570 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700571 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700572
573 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700574 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700575 String id = mIdByConnection.get(c);
576 mAdapter.setStatusHints(id, statusHints);
577 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700578
579 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800580 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700581 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700582 mAdapter.setConferenceableConnections(
583 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800584 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700585 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700586
587 @Override
588 public void onConferenceChanged(Connection connection, Conference conference) {
589 String id = mIdByConnection.get(connection);
590 if (id != null) {
591 String conferenceId = null;
592 if (conference != null) {
593 conferenceId = mIdByConference.get(conference);
594 }
595 mAdapter.setIsConferenced(id, conferenceId);
596 }
597 }
Anthony Lee17455a32015-04-24 15:25:29 -0700598
599 @Override
600 public void onConferenceMergeFailed(Connection connection) {
601 String id = mIdByConnection.get(connection);
602 if (id != null) {
603 mAdapter.onConferenceMergeFailed(id);
604 }
605 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700606
607 @Override
608 public void onExtrasChanged(Connection connection, Bundle extras) {
609 String id = mIdByConnection.get(connection);
610 if (id != null) {
611 mAdapter.setExtras(id, extras);
612 }
613 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700614 };
615
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700616 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700617 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700618 public final IBinder onBind(Intent intent) {
619 return mBinder;
620 }
621
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700622 /** {@inheritDoc} */
623 @Override
624 public boolean onUnbind(Intent intent) {
625 endAllConnections();
626 return super.onUnbind(intent);
627 }
628
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700629 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700630 * This can be used by telecom to either create a new outgoing call or attach to an existing
631 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700632 * createConnection util a connection service cancels the process or completes it successfully.
633 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700634 private void createConnection(
635 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700636 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700637 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700638 boolean isIncoming,
639 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700640 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700641 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
642 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700643
Yorke Leec3cf9822014-10-02 09:38:39 -0700644 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
645 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700646 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700647 Log.d(this, "createConnection, connection: %s", connection);
648 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700649 connection = Connection.createFailedConnection(
650 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700651 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700652
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700653 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700654 addConnection(callId, connection);
655 }
656
Andrew Lee100e2932014-09-08 15:34:24 -0700657 Uri address = connection.getAddress();
658 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700659 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700660 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700661 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800662 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700663
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700664 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700665 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700666 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700667 request,
668 new ParcelableConnection(
669 request.getAccountHandle(),
670 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800671 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700672 connection.getAddress(),
673 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700674 connection.getCallerDisplayName(),
675 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700676 connection.getVideoProvider() == null ?
677 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700678 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700679 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700680 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -0700681 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700682 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700683 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700684 createIdList(connection.getConferenceables()),
685 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -0800686 if (isUnknown) {
687 triggerConferenceRecalculate();
688 }
Evan Charltonbf11f982014-07-20 22:06:28 -0700689 }
690
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700691 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700692 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700693 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700694 }
695
Tyler Gunnbe74de02014-08-29 14:51:48 -0700696 private void answerVideo(String callId, int videoState) {
697 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700698 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700699 }
700
Tyler Gunnbe74de02014-08-29 14:51:48 -0700701 private void answer(String callId) {
702 Log.d(this, "answer %s", callId);
703 findConnectionForAction(callId, "answer").onAnswer();
704 }
705
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700706 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700707 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700708 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700709 }
710
Bryce Lee81901682015-08-28 16:38:02 -0700711 private void reject(String callId, String rejectWithMessage) {
712 Log.d(this, "reject %s with message", callId);
713 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
714 }
715
Bryce Leecac50772015-11-17 15:13:29 -0800716 private void silence(String callId) {
717 Log.d(this, "silence %s", callId);
718 findConnectionForAction(callId, "silence").onSilence();
719 }
720
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700721 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700722 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700723 if (mConnectionById.containsKey(callId)) {
724 findConnectionForAction(callId, "disconnect").onDisconnect();
725 } else {
726 findConferenceForAction(callId, "disconnect").onDisconnect();
727 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700728 }
729
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700730 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700731 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700732 if (mConnectionById.containsKey(callId)) {
733 findConnectionForAction(callId, "hold").onHold();
734 } else {
735 findConferenceForAction(callId, "hold").onHold();
736 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700737 }
738
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700739 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700740 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700741 if (mConnectionById.containsKey(callId)) {
742 findConnectionForAction(callId, "unhold").onUnhold();
743 } else {
744 findConferenceForAction(callId, "unhold").onUnhold();
745 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700746 }
747
Yorke Lee4af59352015-05-13 14:14:54 -0700748 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
749 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700750 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -0700751 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
752 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700753 } else {
Yorke Lee4af59352015-05-13 14:14:54 -0700754 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
755 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700756 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700757 }
758
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700759 private void playDtmfTone(String callId, char digit) {
760 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700761 if (mConnectionById.containsKey(callId)) {
762 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
763 } else {
764 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
765 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700766 }
767
768 private void stopDtmfTone(String callId) {
769 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700770 if (mConnectionById.containsKey(callId)) {
771 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
772 } else {
773 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
774 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700775 }
776
Santos Cordon823fd3c2014-08-07 18:35:18 -0700777 private void conference(String callId1, String callId2) {
778 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700779
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800780 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700781 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800782 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700783 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800784 conference2 = findConferenceForAction(callId2, "conference");
785 if (conference2 == getNullConference()) {
786 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
787 callId2);
788 return;
789 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700790 }
Santos Cordonb6939982014-06-04 20:20:58 -0700791
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800792 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700793 Connection connection1 = findConnectionForAction(callId1, "conference");
794 if (connection1 == getNullConnection()) {
795 Conference conference1 = findConferenceForAction(callId1, "addConnection");
796 if (conference1 == getNullConference()) {
797 Log.w(this,
798 "Connection1 or Conference1 missing in conference request %s.",
799 callId1);
800 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800801 // Call 1 is a conference.
802 if (connection2 != getNullConnection()) {
803 // Call 2 is a connection so merge via call 1 (conference).
804 conference1.onMerge(connection2);
805 } else {
806 // Call 2 is ALSO a conference; this should never happen.
807 Log.wtf(this, "There can only be one conference and an attempt was made to " +
808 "merge two conferences.");
809 return;
810 }
Ihab Awad50e35062014-09-30 09:17:03 -0700811 }
812 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800813 // Call 1 is a connection.
814 if (conference2 != getNullConference()) {
815 // Call 2 is a conference, so merge via call 2.
816 conference2.onMerge(connection1);
817 } else {
818 // Call 2 is a connection, so merge together.
819 onConference(connection1, connection2);
820 }
Ihab Awad50e35062014-09-30 09:17:03 -0700821 }
Santos Cordon980acb92014-05-31 10:31:19 -0700822 }
823
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700824 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700825 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700826
827 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700828 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700829 Log.w(this, "Connection missing in conference request %s.", callId);
830 return;
831 }
832
Santos Cordon0159ac02014-08-21 14:28:11 -0700833 Conference conference = connection.getConference();
834 if (conference != null) {
835 conference.onSeparate(connection);
836 }
Santos Cordon980acb92014-05-31 10:31:19 -0700837 }
838
Santos Cordona4868042014-09-04 17:39:22 -0700839 private void mergeConference(String callId) {
840 Log.d(this, "mergeConference(%s)", callId);
841 Conference conference = findConferenceForAction(callId, "mergeConference");
842 if (conference != null) {
843 conference.onMerge();
844 }
845 }
846
847 private void swapConference(String callId) {
848 Log.d(this, "swapConference(%s)", callId);
849 Conference conference = findConferenceForAction(callId, "swapConference");
850 if (conference != null) {
851 conference.onSwap();
852 }
853 }
854
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700855 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700856 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700857 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700858 }
859
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700860 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700861 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700862 // No need to query again if we already did it.
863 return;
864 }
865
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700866 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700867 @Override
868 public void onResult(
869 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700870 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700871 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700872 @Override
873 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700874 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700875 mRemoteConnectionManager.addConnectionService(
876 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700877 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700878 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700879 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700880 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700881 }
882 });
883 }
884
885 @Override
886 public void onError() {
887 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700888 @Override
889 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700890 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700891 }
892 });
893 }
894 });
895 }
896
Ihab Awadf8b69882014-07-25 15:14:01 -0700897 /**
898 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700899 * incoming request. This is used by {@code ConnectionService}s that are registered with
900 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
901 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700902 *
903 * @param connectionManagerPhoneAccount See description at
904 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
905 * @param request Details about the incoming call.
906 * @return The {@code Connection} object to satisfy this call, or {@code null} to
907 * not handle the call.
908 */
909 public final RemoteConnection createRemoteIncomingConnection(
910 PhoneAccountHandle connectionManagerPhoneAccount,
911 ConnectionRequest request) {
912 return mRemoteConnectionManager.createRemoteConnection(
913 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700914 }
915
916 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700917 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700918 * outgoing request. This is used by {@code ConnectionService}s that are registered with
919 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
920 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700921 *
922 * @param connectionManagerPhoneAccount See description at
923 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
924 * @param request Details about the incoming call.
925 * @return The {@code Connection} object to satisfy this call, or {@code null} to
926 * not handle the call.
927 */
928 public final RemoteConnection createRemoteOutgoingConnection(
929 PhoneAccountHandle connectionManagerPhoneAccount,
930 ConnectionRequest request) {
931 return mRemoteConnectionManager.createRemoteConnection(
932 connectionManagerPhoneAccount, request, false);
933 }
934
935 /**
Santos Cordona663f862014-10-29 13:49:58 -0700936 * Indicates to the relevant {@code RemoteConnectionService} that the specified
937 * {@link RemoteConnection}s should be merged into a conference call.
938 * <p>
939 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
940 * be invoked.
941 *
942 * @param remoteConnection1 The first of the remote connections to conference.
943 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700944 */
945 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700946 RemoteConnection remoteConnection1,
947 RemoteConnection remoteConnection2) {
948 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700949 }
950
951 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700952 * Adds a new conference call. When a conference call is created either as a result of an
953 * explicit request via {@link #onConference} or otherwise, the connection service should supply
954 * an instance of {@link Conference} by invoking this method. A conference call provided by this
955 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
956 *
957 * @param conference The new conference object.
958 */
959 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -0700960 Log.d(this, "addConference: conference=%s", conference);
961
Santos Cordon823fd3c2014-08-07 18:35:18 -0700962 String id = addConferenceInternal(conference);
963 if (id != null) {
964 List<String> connectionIds = new ArrayList<>(2);
965 for (Connection connection : conference.getConnections()) {
966 if (mIdByConnection.containsKey(connection)) {
967 connectionIds.add(mIdByConnection.get(connection));
968 }
969 }
970 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700971 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700972 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800973 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800974 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -0700975 conference.getVideoProvider() == null ?
976 null : conference.getVideoProvider().getInterface(),
977 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -0700978 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700979 conference.getStatusHints(),
980 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -0700981
Santos Cordon823fd3c2014-08-07 18:35:18 -0700982 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -0700983 mAdapter.setVideoProvider(id, conference.getVideoProvider());
984 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700985
986 // Go through any child calls and set the parent.
987 for (Connection connection : conference.getConnections()) {
988 String connectionId = mIdByConnection.get(connection);
989 if (connectionId != null) {
990 mAdapter.setIsConferenced(connectionId, id);
991 }
992 }
993 }
994 }
995
996 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700997 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
998 * connection.
999 *
1000 * @param phoneAccountHandle The phone account handle for the connection.
1001 * @param connection The connection to add.
1002 */
1003 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1004 Connection connection) {
1005
1006 String id = addExistingConnectionInternal(connection);
1007 if (id != null) {
1008 List<String> emptyList = new ArrayList<>(0);
1009
1010 ParcelableConnection parcelableConnection = new ParcelableConnection(
1011 phoneAccountHandle,
1012 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001013 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001014 connection.getAddress(),
1015 connection.getAddressPresentation(),
1016 connection.getCallerDisplayName(),
1017 connection.getCallerDisplayNamePresentation(),
1018 connection.getVideoProvider() == null ?
1019 null : connection.getVideoProvider().getInterface(),
1020 connection.getVideoState(),
1021 connection.isRingbackRequested(),
1022 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001023 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001024 connection.getStatusHints(),
1025 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001026 emptyList,
1027 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001028 mAdapter.addExistingConnection(id, parcelableConnection);
1029 }
1030 }
1031
1032 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001033 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1034 * has taken responsibility.
1035 *
1036 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001037 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001038 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001039 return mConnectionById.values();
1040 }
1041
1042 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001043 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1044 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001045 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001046 * @param connectionManagerPhoneAccount See description at
1047 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1048 * @param request Details about the incoming call.
1049 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1050 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001051 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001052 public Connection onCreateIncomingConnection(
1053 PhoneAccountHandle connectionManagerPhoneAccount,
1054 ConnectionRequest request) {
1055 return null;
1056 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001057
1058 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001059 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1060 * Connection is part of a conference controller but is not yet added to Connection
1061 * Service and hence cannot be added to the conference call.
1062 *
1063 * @hide
1064 */
1065 public void triggerConferenceRecalculate() {
1066 }
1067
1068 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001069 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1070 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001071 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001072 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1073 * this call.
1074 * <p>
1075 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1076 * has registered one or more {@code PhoneAccount}s having
1077 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1078 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1079 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1080 * making the connection.
1081 * <p>
1082 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1083 * being asked to make a direct connection. The
1084 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1085 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1086 * making the connection.
1087 * @param request Details about the outgoing call.
1088 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001089 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001090 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001091 public Connection onCreateOutgoingConnection(
1092 PhoneAccountHandle connectionManagerPhoneAccount,
1093 ConnectionRequest request) {
1094 return null;
1095 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001096
1097 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001098 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1099 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1100 * call created using
1101 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1102 *
1103 * @param connectionManagerPhoneAccount
1104 * @param request
1105 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001106 *
1107 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001108 */
1109 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1110 ConnectionRequest request) {
1111 return null;
1112 }
1113
1114 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001115 * Conference two specified connections. Invoked when the user has made a request to merge the
1116 * specified connections into a conference call. In response, the connection service should
1117 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001118 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001119 * @param connection1 A connection to merge into a conference call.
1120 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001121 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001122 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001123
Santos Cordona663f862014-10-29 13:49:58 -07001124 /**
1125 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1126 * When this method is invoked, this {@link ConnectionService} should create its own
1127 * representation of the conference call and send it to telecom using {@link #addConference}.
1128 * <p>
1129 * This is only relevant to {@link ConnectionService}s which are registered with
1130 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1131 *
1132 * @param conference The remote conference call.
1133 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001134 public void onRemoteConferenceAdded(RemoteConference conference) {}
1135
Santos Cordon823fd3c2014-08-07 18:35:18 -07001136 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001137 * Called when an existing connection is added remotely.
1138 * @param connection The existing connection which was added.
1139 */
1140 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1141
1142 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001143 * @hide
1144 */
1145 public boolean containsConference(Conference conference) {
1146 return mIdByConference.containsKey(conference);
1147 }
1148
Ihab Awadb8e85c72014-08-23 20:34:57 -07001149 /** {@hide} */
1150 void addRemoteConference(RemoteConference remoteConference) {
1151 onRemoteConferenceAdded(remoteConference);
1152 }
1153
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001154 /** {@hide} */
1155 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1156 onRemoteExistingConnectionAdded(remoteConnection);
1157 }
1158
Ihab Awad5d0410f2014-07-30 10:07:40 -07001159 private void onAccountsInitialized() {
1160 mAreAccountsInitialized = true;
1161 for (Runnable r : mPreInitializationConnectionRequests) {
1162 r.run();
1163 }
1164 mPreInitializationConnectionRequests.clear();
1165 }
1166
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001167 /**
1168 * Adds an existing connection to the list of connections, identified by a new UUID.
1169 *
1170 * @param connection The connection.
1171 * @return The UUID of the connection (e.g. the call-id).
1172 */
1173 private String addExistingConnectionInternal(Connection connection) {
1174 String id = UUID.randomUUID().toString();
1175 addConnection(id, connection);
1176 return id;
1177 }
1178
Ihab Awad542e0ea2014-05-16 10:22:16 -07001179 private void addConnection(String callId, Connection connection) {
1180 mConnectionById.put(callId, connection);
1181 mIdByConnection.put(connection, callId);
1182 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001183 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001184 }
1185
Anthony Lee30e65842014-11-06 16:30:53 -08001186 /** {@hide} */
1187 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001188 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001189 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001190 connection.removeConnectionListener(mConnectionListener);
1191 mConnectionById.remove(mIdByConnection.get(connection));
1192 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001193 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001194 }
1195
Santos Cordon823fd3c2014-08-07 18:35:18 -07001196 private String addConferenceInternal(Conference conference) {
1197 if (mIdByConference.containsKey(conference)) {
1198 Log.w(this, "Re-adding an existing conference: %s.", conference);
1199 } else if (conference != null) {
1200 String id = UUID.randomUUID().toString();
1201 mConferenceById.put(id, conference);
1202 mIdByConference.put(conference, id);
1203 conference.addListener(mConferenceListener);
1204 return id;
1205 }
1206
1207 return null;
1208 }
1209
1210 private void removeConference(Conference conference) {
1211 if (mIdByConference.containsKey(conference)) {
1212 conference.removeListener(mConferenceListener);
1213
1214 String id = mIdByConference.get(conference);
1215 mConferenceById.remove(id);
1216 mIdByConference.remove(conference);
1217 mAdapter.removeCall(id);
1218 }
1219 }
1220
Ihab Awad542e0ea2014-05-16 10:22:16 -07001221 private Connection findConnectionForAction(String callId, String action) {
1222 if (mConnectionById.containsKey(callId)) {
1223 return mConnectionById.get(callId);
1224 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001225 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001226 return getNullConnection();
1227 }
1228
1229 static synchronized Connection getNullConnection() {
1230 if (sNullConnection == null) {
1231 sNullConnection = new Connection() {};
1232 }
1233 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001234 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001235
1236 private Conference findConferenceForAction(String conferenceId, String action) {
1237 if (mConferenceById.containsKey(conferenceId)) {
1238 return mConferenceById.get(conferenceId);
1239 }
1240 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1241 return getNullConference();
1242 }
1243
Ihab Awadb8e85c72014-08-23 20:34:57 -07001244 private List<String> createConnectionIdList(List<Connection> connections) {
1245 List<String> ids = new ArrayList<>();
1246 for (Connection c : connections) {
1247 if (mIdByConnection.containsKey(c)) {
1248 ids.add(mIdByConnection.get(c));
1249 }
1250 }
1251 Collections.sort(ids);
1252 return ids;
1253 }
1254
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001255 /**
1256 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001257 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001258 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001259 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001260 * @return List of string conference and call Ids.
1261 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001262 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001263 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001264 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001265 // Only allow Connection and Conference conferenceables.
1266 if (c instanceof Connection) {
1267 Connection connection = (Connection) c;
1268 if (mIdByConnection.containsKey(connection)) {
1269 ids.add(mIdByConnection.get(connection));
1270 }
1271 } else if (c instanceof Conference) {
1272 Conference conference = (Conference) c;
1273 if (mIdByConference.containsKey(conference)) {
1274 ids.add(mIdByConference.get(conference));
1275 }
1276 }
1277 }
1278 Collections.sort(ids);
1279 return ids;
1280 }
1281
Santos Cordon0159ac02014-08-21 14:28:11 -07001282 private Conference getNullConference() {
1283 if (sNullConference == null) {
1284 sNullConference = new Conference(null) {};
1285 }
1286 return sNullConference;
1287 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001288
1289 private void endAllConnections() {
1290 // Unbound from telecomm. We should end all connections and conferences.
1291 for (Connection connection : mIdByConnection.keySet()) {
1292 // only operate on top-level calls. Conference calls will be removed on their own.
1293 if (connection.getConference() == null) {
1294 connection.onDisconnect();
1295 }
1296 }
1297 for (Conference conference : mIdByConference.keySet()) {
1298 conference.onDisconnect();
1299 }
1300 }
Santos Cordon980acb92014-05-31 10:31:19 -07001301}