blob: b4a7ce0f56b13ef871a2ae0d75b48aa8496a7354 [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;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700120 private Object mIdSyncRoot = new Object();
121 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700122
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700123 private final IBinder mBinder = new IConnectionService.Stub() {
124 @Override
125 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700126 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
127 }
128
129 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
130 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700131 }
132
133 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700134 public void createConnection(
135 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700136 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700137 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700138 boolean isIncoming,
139 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700140 SomeArgs args = SomeArgs.obtain();
141 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700142 args.arg2 = id;
143 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700144 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700145 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700146 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700147 }
148
149 @Override
150 public void abort(String callId) {
151 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
152 }
153
154 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700155 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700156 SomeArgs args = SomeArgs.obtain();
157 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700158 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700159 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
160 }
161
162 @Override
163 public void answer(String callId) {
164 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700165 }
166
167 @Override
168 public void reject(String callId) {
169 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
170 }
171
172 @Override
Bryce Lee81901682015-08-28 16:38:02 -0700173 public void rejectWithMessage(String callId, String message) {
174 SomeArgs args = SomeArgs.obtain();
175 args.arg1 = callId;
176 args.arg2 = message;
177 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
178 }
179
180 @Override
Bryce Leecac50772015-11-17 15:13:29 -0800181 public void silence(String callId) {
182 mHandler.obtainMessage(MSG_SILENCE, callId).sendToTarget();
183 }
184
185 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700186 public void disconnect(String callId) {
187 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
188 }
189
190 @Override
191 public void hold(String callId) {
192 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
193 }
194
195 @Override
196 public void unhold(String callId) {
197 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
198 }
199
200 @Override
Yorke Lee4af59352015-05-13 14:14:54 -0700201 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700202 SomeArgs args = SomeArgs.obtain();
203 args.arg1 = callId;
Yorke Lee4af59352015-05-13 14:14:54 -0700204 args.arg2 = callAudioState;
205 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700206 }
207
208 @Override
209 public void playDtmfTone(String callId, char digit) {
210 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
211 }
212
213 @Override
214 public void stopDtmfTone(String callId) {
215 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
216 }
217
218 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700219 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700220 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700221 args.arg1 = callId1;
222 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700223 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
224 }
225
226 @Override
227 public void splitFromConference(String callId) {
228 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
229 }
230
231 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700232 public void mergeConference(String callId) {
233 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
234 }
235
236 @Override
237 public void swapConference(String callId) {
238 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
239 }
240
241 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700242 public void onPostDialContinue(String callId, boolean proceed) {
243 SomeArgs args = SomeArgs.obtain();
244 args.arg1 = callId;
245 args.argi1 = proceed ? 1 : 0;
246 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
247 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700248 };
249
250 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
251 @Override
252 public void handleMessage(Message msg) {
253 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700254 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700255 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
256 onAdapterAttached();
257 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700258 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
259 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
260 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700261 case MSG_CREATE_CONNECTION: {
262 SomeArgs args = (SomeArgs) msg.obj;
263 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700264 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700265 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700266 final String id = (String) args.arg2;
267 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700268 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700269 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700270 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700271 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700272 mPreInitializationConnectionRequests.add(new Runnable() {
273 @Override
274 public void run() {
275 createConnection(
276 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700277 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700278 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700279 isIncoming,
280 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700281 }
282 });
283 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700284 createConnection(
285 connectionManagerPhoneAccount,
286 id,
287 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700288 isIncoming,
289 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700290 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700291 } finally {
292 args.recycle();
293 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700294 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700295 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700296 case MSG_ABORT:
297 abort((String) msg.obj);
298 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700299 case MSG_ANSWER:
300 answer((String) msg.obj);
301 break;
302 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700303 SomeArgs args = (SomeArgs) msg.obj;
304 try {
305 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700306 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700307 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700308 } finally {
309 args.recycle();
310 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700311 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700312 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700313 case MSG_REJECT:
314 reject((String) msg.obj);
315 break;
Bryce Lee81901682015-08-28 16:38:02 -0700316 case MSG_REJECT_WITH_MESSAGE: {
317 SomeArgs args = (SomeArgs) msg.obj;
318 try {
319 reject((String) args.arg1, (String) args.arg2);
320 } finally {
321 args.recycle();
322 }
323 break;
324 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700325 case MSG_DISCONNECT:
326 disconnect((String) msg.obj);
327 break;
Bryce Leecac50772015-11-17 15:13:29 -0800328 case MSG_SILENCE:
329 silence((String) msg.obj);
330 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700331 case MSG_HOLD:
332 hold((String) msg.obj);
333 break;
334 case MSG_UNHOLD:
335 unhold((String) msg.obj);
336 break;
Yorke Lee4af59352015-05-13 14:14:54 -0700337 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700338 SomeArgs args = (SomeArgs) msg.obj;
339 try {
340 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700341 CallAudioState audioState = (CallAudioState) args.arg2;
342 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700343 } finally {
344 args.recycle();
345 }
346 break;
347 }
348 case MSG_PLAY_DTMF_TONE:
349 playDtmfTone((String) msg.obj, (char) msg.arg1);
350 break;
351 case MSG_STOP_DTMF_TONE:
352 stopDtmfTone((String) msg.obj);
353 break;
354 case MSG_CONFERENCE: {
355 SomeArgs args = (SomeArgs) msg.obj;
356 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700357 String callId1 = (String) args.arg1;
358 String callId2 = (String) args.arg2;
359 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700360 } finally {
361 args.recycle();
362 }
363 break;
364 }
365 case MSG_SPLIT_FROM_CONFERENCE:
366 splitFromConference((String) msg.obj);
367 break;
Santos Cordona4868042014-09-04 17:39:22 -0700368 case MSG_MERGE_CONFERENCE:
369 mergeConference((String) msg.obj);
370 break;
371 case MSG_SWAP_CONFERENCE:
372 swapConference((String) msg.obj);
373 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700374 case MSG_ON_POST_DIAL_CONTINUE: {
375 SomeArgs args = (SomeArgs) msg.obj;
376 try {
377 String callId = (String) args.arg1;
378 boolean proceed = (args.argi1 == 1);
379 onPostDialContinue(callId, proceed);
380 } finally {
381 args.recycle();
382 }
383 break;
384 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700385 default:
386 break;
387 }
388 }
389 };
390
Santos Cordon823fd3c2014-08-07 18:35:18 -0700391 private final Conference.Listener mConferenceListener = new Conference.Listener() {
392 @Override
393 public void onStateChanged(Conference conference, int oldState, int newState) {
394 String id = mIdByConference.get(conference);
395 switch (newState) {
396 case Connection.STATE_ACTIVE:
397 mAdapter.setActive(id);
398 break;
399 case Connection.STATE_HOLDING:
400 mAdapter.setOnHold(id);
401 break;
402 case Connection.STATE_DISCONNECTED:
403 // handled by onDisconnected
404 break;
405 }
406 }
407
408 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700409 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700410 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700411 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700412 }
413
414 @Override
415 public void onConnectionAdded(Conference conference, Connection connection) {
416 }
417
418 @Override
419 public void onConnectionRemoved(Conference conference, Connection connection) {
420 }
421
422 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700423 public void onConferenceableConnectionsChanged(
424 Conference conference, List<Connection> conferenceableConnections) {
425 mAdapter.setConferenceableConnections(
426 mIdByConference.get(conference),
427 createConnectionIdList(conferenceableConnections));
428 }
429
430 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700431 public void onDestroyed(Conference conference) {
432 removeConference(conference);
433 }
434
435 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800436 public void onConnectionCapabilitiesChanged(
437 Conference conference,
438 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700439 String id = mIdByConference.get(conference);
440 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800441 Connection.capabilitiesToString(connectionCapabilities));
442 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700443 }
Rekha Kumar07366812015-03-24 16:42:31 -0700444
445 @Override
446 public void onVideoStateChanged(Conference c, int videoState) {
447 String id = mIdByConference.get(c);
448 Log.d(this, "onVideoStateChanged set video state %d", videoState);
449 mAdapter.setVideoState(id, videoState);
450 }
451
452 @Override
453 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
454 String id = mIdByConference.get(c);
455 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
456 videoProvider);
457 mAdapter.setVideoProvider(id, videoProvider);
458 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700459
460 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700461 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
462 String id = mIdByConference.get(conference);
463 mAdapter.setStatusHints(id, statusHints);
464 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700465
466 @Override
467 public void onExtrasChanged(Conference conference, Bundle extras) {
468 String id = mIdByConference.get(conference);
469 mAdapter.setExtras(id, extras);
470 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700471 };
472
Ihab Awad542e0ea2014-05-16 10:22:16 -0700473 private final Connection.Listener mConnectionListener = new Connection.Listener() {
474 @Override
475 public void onStateChanged(Connection c, int state) {
476 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700477 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700478 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700479 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700480 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700481 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700482 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700483 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700484 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700485 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700486 // Handled in onDisconnected()
487 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700488 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700489 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700490 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700491 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700492 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700493 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700494 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700495 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700496 break;
497 }
498 }
499
500 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700501 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700502 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700503 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700504 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700505 }
506
507 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700508 public void onVideoStateChanged(Connection c, int videoState) {
509 String id = mIdByConnection.get(c);
510 Log.d(this, "Adapter set video state %d", videoState);
511 mAdapter.setVideoState(id, videoState);
512 }
513
514 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700515 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700516 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700517 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700518 }
519
520 @Override
521 public void onCallerDisplayNameChanged(
522 Connection c, String callerDisplayName, int presentation) {
523 String id = mIdByConnection.get(c);
524 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700525 }
526
527 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700528 public void onDestroyed(Connection c) {
529 removeConnection(c);
530 }
Ihab Awadf8358972014-05-28 16:46:42 -0700531
532 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700533 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700534 String id = mIdByConnection.get(c);
535 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700536 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700537 }
538
539 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800540 public void onPostDialChar(Connection c, char nextChar) {
541 String id = mIdByConnection.get(c);
542 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
543 mAdapter.onPostDialChar(id, nextChar);
544 }
545
546 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700547 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700548 String id = mIdByConnection.get(c);
549 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700550 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700551 }
Santos Cordonb6939982014-06-04 20:20:58 -0700552
553 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800554 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700555 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700556 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800557 Connection.capabilitiesToString(capabilities));
558 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700559 }
560
Santos Cordonb6939982014-06-04 20:20:58 -0700561 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700562 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b82014-06-20 16:29:33 -0700563 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700564 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
565 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700566 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b82014-06-20 16:29:33 -0700567 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700568
569 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700570 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700571 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700572 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700573 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700574
575 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700576 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700577 String id = mIdByConnection.get(c);
578 mAdapter.setStatusHints(id, statusHints);
579 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700580
581 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800582 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700583 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700584 mAdapter.setConferenceableConnections(
585 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800586 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700587 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700588
589 @Override
590 public void onConferenceChanged(Connection connection, Conference conference) {
591 String id = mIdByConnection.get(connection);
592 if (id != null) {
593 String conferenceId = null;
594 if (conference != null) {
595 conferenceId = mIdByConference.get(conference);
596 }
597 mAdapter.setIsConferenced(id, conferenceId);
598 }
599 }
Anthony Lee17455a32015-04-24 15:25:29 -0700600
601 @Override
602 public void onConferenceMergeFailed(Connection connection) {
603 String id = mIdByConnection.get(connection);
604 if (id != null) {
605 mAdapter.onConferenceMergeFailed(id);
606 }
607 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700608
609 @Override
610 public void onExtrasChanged(Connection connection, Bundle extras) {
611 String id = mIdByConnection.get(connection);
612 if (id != null) {
613 mAdapter.setExtras(id, extras);
614 }
615 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700616 };
617
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700618 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700619 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700620 public final IBinder onBind(Intent intent) {
621 return mBinder;
622 }
623
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700624 /** {@inheritDoc} */
625 @Override
626 public boolean onUnbind(Intent intent) {
627 endAllConnections();
628 return super.onUnbind(intent);
629 }
630
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700631 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700632 * This can be used by telecom to either create a new outgoing call or attach to an existing
633 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700634 * createConnection util a connection service cancels the process or completes it successfully.
635 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700636 private void createConnection(
637 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700638 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700639 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700640 boolean isIncoming,
641 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700642 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700643 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
644 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -0700645 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700646
Yorke Leec3cf9822014-10-02 09:38:39 -0700647 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
648 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700649 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700650 Log.d(this, "createConnection, connection: %s", connection);
651 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700652 connection = Connection.createFailedConnection(
653 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700654 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700655
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700656 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700657 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700658 addConnection(callId, connection);
659 }
660
Andrew Lee100e2932014-09-08 15:34:24 -0700661 Uri address = connection.getAddress();
662 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700663 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700664 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700665 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800666 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700667
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700668 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700669 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700670 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700671 request,
672 new ParcelableConnection(
673 request.getAccountHandle(),
674 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800675 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700676 connection.getAddress(),
677 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700678 connection.getCallerDisplayName(),
679 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700680 connection.getVideoProvider() == null ?
681 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700682 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700683 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700684 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -0700685 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700686 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700687 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700688 createIdList(connection.getConferenceables()),
689 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -0800690 if (isUnknown) {
691 triggerConferenceRecalculate();
692 }
Evan Charltonbf11f982014-07-20 22:06:28 -0700693 }
694
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700695 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700696 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700697 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700698 }
699
Tyler Gunnbe74de02014-08-29 14:51:48 -0700700 private void answerVideo(String callId, int videoState) {
701 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700702 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700703 }
704
Tyler Gunnbe74de02014-08-29 14:51:48 -0700705 private void answer(String callId) {
706 Log.d(this, "answer %s", callId);
707 findConnectionForAction(callId, "answer").onAnswer();
708 }
709
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700710 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700711 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700712 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700713 }
714
Bryce Lee81901682015-08-28 16:38:02 -0700715 private void reject(String callId, String rejectWithMessage) {
716 Log.d(this, "reject %s with message", callId);
717 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
718 }
719
Bryce Leecac50772015-11-17 15:13:29 -0800720 private void silence(String callId) {
721 Log.d(this, "silence %s", callId);
722 findConnectionForAction(callId, "silence").onSilence();
723 }
724
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700725 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700726 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700727 if (mConnectionById.containsKey(callId)) {
728 findConnectionForAction(callId, "disconnect").onDisconnect();
729 } else {
730 findConferenceForAction(callId, "disconnect").onDisconnect();
731 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700732 }
733
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700734 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700735 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700736 if (mConnectionById.containsKey(callId)) {
737 findConnectionForAction(callId, "hold").onHold();
738 } else {
739 findConferenceForAction(callId, "hold").onHold();
740 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700741 }
742
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700743 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700744 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700745 if (mConnectionById.containsKey(callId)) {
746 findConnectionForAction(callId, "unhold").onUnhold();
747 } else {
748 findConferenceForAction(callId, "unhold").onUnhold();
749 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700750 }
751
Yorke Lee4af59352015-05-13 14:14:54 -0700752 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
753 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700754 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -0700755 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
756 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700757 } else {
Yorke Lee4af59352015-05-13 14:14:54 -0700758 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
759 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700760 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700761 }
762
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700763 private void playDtmfTone(String callId, char digit) {
764 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700765 if (mConnectionById.containsKey(callId)) {
766 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
767 } else {
768 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
769 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700770 }
771
772 private void stopDtmfTone(String callId) {
773 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700774 if (mConnectionById.containsKey(callId)) {
775 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
776 } else {
777 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
778 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700779 }
780
Santos Cordon823fd3c2014-08-07 18:35:18 -0700781 private void conference(String callId1, String callId2) {
782 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700783
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800784 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700785 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800786 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700787 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800788 conference2 = findConferenceForAction(callId2, "conference");
789 if (conference2 == getNullConference()) {
790 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
791 callId2);
792 return;
793 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700794 }
Santos Cordonb6939982014-06-04 20:20:58 -0700795
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800796 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700797 Connection connection1 = findConnectionForAction(callId1, "conference");
798 if (connection1 == getNullConnection()) {
799 Conference conference1 = findConferenceForAction(callId1, "addConnection");
800 if (conference1 == getNullConference()) {
801 Log.w(this,
802 "Connection1 or Conference1 missing in conference request %s.",
803 callId1);
804 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800805 // Call 1 is a conference.
806 if (connection2 != getNullConnection()) {
807 // Call 2 is a connection so merge via call 1 (conference).
808 conference1.onMerge(connection2);
809 } else {
810 // Call 2 is ALSO a conference; this should never happen.
811 Log.wtf(this, "There can only be one conference and an attempt was made to " +
812 "merge two conferences.");
813 return;
814 }
Ihab Awad50e35062014-09-30 09:17:03 -0700815 }
816 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800817 // Call 1 is a connection.
818 if (conference2 != getNullConference()) {
819 // Call 2 is a conference, so merge via call 2.
820 conference2.onMerge(connection1);
821 } else {
822 // Call 2 is a connection, so merge together.
823 onConference(connection1, connection2);
824 }
Ihab Awad50e35062014-09-30 09:17:03 -0700825 }
Santos Cordon980acb92014-05-31 10:31:19 -0700826 }
827
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700828 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700829 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700830
831 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700832 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700833 Log.w(this, "Connection missing in conference request %s.", callId);
834 return;
835 }
836
Santos Cordon0159ac02014-08-21 14:28:11 -0700837 Conference conference = connection.getConference();
838 if (conference != null) {
839 conference.onSeparate(connection);
840 }
Santos Cordon980acb92014-05-31 10:31:19 -0700841 }
842
Santos Cordona4868042014-09-04 17:39:22 -0700843 private void mergeConference(String callId) {
844 Log.d(this, "mergeConference(%s)", callId);
845 Conference conference = findConferenceForAction(callId, "mergeConference");
846 if (conference != null) {
847 conference.onMerge();
848 }
849 }
850
851 private void swapConference(String callId) {
852 Log.d(this, "swapConference(%s)", callId);
853 Conference conference = findConferenceForAction(callId, "swapConference");
854 if (conference != null) {
855 conference.onSwap();
856 }
857 }
858
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700859 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700860 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700861 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700862 }
863
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700864 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700865 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700866 // No need to query again if we already did it.
867 return;
868 }
869
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700870 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700871 @Override
872 public void onResult(
873 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700874 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700875 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700876 @Override
877 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700878 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700879 mRemoteConnectionManager.addConnectionService(
880 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700881 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700882 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700883 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700884 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700885 }
886 });
887 }
888
889 @Override
890 public void onError() {
891 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700892 @Override
893 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700894 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700895 }
896 });
897 }
898 });
899 }
900
Ihab Awadf8b69882014-07-25 15:14:01 -0700901 /**
902 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700903 * incoming request. This is used by {@code ConnectionService}s that are registered with
904 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
905 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700906 *
907 * @param connectionManagerPhoneAccount See description at
908 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
909 * @param request Details about the incoming call.
910 * @return The {@code Connection} object to satisfy this call, or {@code null} to
911 * not handle the call.
912 */
913 public final RemoteConnection createRemoteIncomingConnection(
914 PhoneAccountHandle connectionManagerPhoneAccount,
915 ConnectionRequest request) {
916 return mRemoteConnectionManager.createRemoteConnection(
917 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700918 }
919
920 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700921 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700922 * outgoing request. This is used by {@code ConnectionService}s that are registered with
923 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
924 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700925 *
926 * @param connectionManagerPhoneAccount See description at
927 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
928 * @param request Details about the incoming call.
929 * @return The {@code Connection} object to satisfy this call, or {@code null} to
930 * not handle the call.
931 */
932 public final RemoteConnection createRemoteOutgoingConnection(
933 PhoneAccountHandle connectionManagerPhoneAccount,
934 ConnectionRequest request) {
935 return mRemoteConnectionManager.createRemoteConnection(
936 connectionManagerPhoneAccount, request, false);
937 }
938
939 /**
Santos Cordona663f862014-10-29 13:49:58 -0700940 * Indicates to the relevant {@code RemoteConnectionService} that the specified
941 * {@link RemoteConnection}s should be merged into a conference call.
942 * <p>
943 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
944 * be invoked.
945 *
946 * @param remoteConnection1 The first of the remote connections to conference.
947 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700948 */
949 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700950 RemoteConnection remoteConnection1,
951 RemoteConnection remoteConnection2) {
952 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700953 }
954
955 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700956 * Adds a new conference call. When a conference call is created either as a result of an
957 * explicit request via {@link #onConference} or otherwise, the connection service should supply
958 * an instance of {@link Conference} by invoking this method. A conference call provided by this
959 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
960 *
961 * @param conference The new conference object.
962 */
963 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -0700964 Log.d(this, "addConference: conference=%s", conference);
965
Santos Cordon823fd3c2014-08-07 18:35:18 -0700966 String id = addConferenceInternal(conference);
967 if (id != null) {
968 List<String> connectionIds = new ArrayList<>(2);
969 for (Connection connection : conference.getConnections()) {
970 if (mIdByConnection.containsKey(connection)) {
971 connectionIds.add(mIdByConnection.get(connection));
972 }
973 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700974 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700975 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700976 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700977 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800978 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800979 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -0700980 conference.getVideoProvider() == null ?
981 null : conference.getVideoProvider().getInterface(),
982 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -0700983 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700984 conference.getStatusHints(),
985 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -0700986
Santos Cordon823fd3c2014-08-07 18:35:18 -0700987 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -0700988 mAdapter.setVideoProvider(id, conference.getVideoProvider());
989 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700990
991 // Go through any child calls and set the parent.
992 for (Connection connection : conference.getConnections()) {
993 String connectionId = mIdByConnection.get(connection);
994 if (connectionId != null) {
995 mAdapter.setIsConferenced(connectionId, id);
996 }
997 }
998 }
999 }
1000
1001 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001002 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1003 * connection.
1004 *
1005 * @param phoneAccountHandle The phone account handle for the connection.
1006 * @param connection The connection to add.
1007 */
1008 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1009 Connection connection) {
1010
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001011 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001012 if (id != null) {
1013 List<String> emptyList = new ArrayList<>(0);
1014
1015 ParcelableConnection parcelableConnection = new ParcelableConnection(
1016 phoneAccountHandle,
1017 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001018 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001019 connection.getAddress(),
1020 connection.getAddressPresentation(),
1021 connection.getCallerDisplayName(),
1022 connection.getCallerDisplayNamePresentation(),
1023 connection.getVideoProvider() == null ?
1024 null : connection.getVideoProvider().getInterface(),
1025 connection.getVideoState(),
1026 connection.isRingbackRequested(),
1027 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001028 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001029 connection.getStatusHints(),
1030 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001031 emptyList,
1032 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001033 mAdapter.addExistingConnection(id, parcelableConnection);
1034 }
1035 }
1036
1037 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001038 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1039 * has taken responsibility.
1040 *
1041 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001042 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001043 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001044 return mConnectionById.values();
1045 }
1046
1047 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001048 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1049 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001050 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001051 * @param connectionManagerPhoneAccount See description at
1052 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1053 * @param request Details about the incoming call.
1054 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1055 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001056 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001057 public Connection onCreateIncomingConnection(
1058 PhoneAccountHandle connectionManagerPhoneAccount,
1059 ConnectionRequest request) {
1060 return null;
1061 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001062
1063 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001064 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1065 * Connection is part of a conference controller but is not yet added to Connection
1066 * Service and hence cannot be added to the conference call.
1067 *
1068 * @hide
1069 */
1070 public void triggerConferenceRecalculate() {
1071 }
1072
1073 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001074 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1075 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001076 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001077 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1078 * this call.
1079 * <p>
1080 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1081 * has registered one or more {@code PhoneAccount}s having
1082 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1083 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1084 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1085 * making the connection.
1086 * <p>
1087 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1088 * being asked to make a direct connection. The
1089 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1090 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1091 * making the connection.
1092 * @param request Details about the outgoing call.
1093 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001094 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001095 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001096 public Connection onCreateOutgoingConnection(
1097 PhoneAccountHandle connectionManagerPhoneAccount,
1098 ConnectionRequest request) {
1099 return null;
1100 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001101
1102 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001103 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1104 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1105 * call created using
1106 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1107 *
1108 * @param connectionManagerPhoneAccount
1109 * @param request
1110 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001111 *
1112 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001113 */
1114 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1115 ConnectionRequest request) {
1116 return null;
1117 }
1118
1119 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001120 * Conference two specified connections. Invoked when the user has made a request to merge the
1121 * specified connections into a conference call. In response, the connection service should
1122 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001123 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001124 * @param connection1 A connection to merge into a conference call.
1125 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001126 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001127 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001128
Santos Cordona663f862014-10-29 13:49:58 -07001129 /**
1130 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1131 * When this method is invoked, this {@link ConnectionService} should create its own
1132 * representation of the conference call and send it to telecom using {@link #addConference}.
1133 * <p>
1134 * This is only relevant to {@link ConnectionService}s which are registered with
1135 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1136 *
1137 * @param conference The remote conference call.
1138 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001139 public void onRemoteConferenceAdded(RemoteConference conference) {}
1140
Santos Cordon823fd3c2014-08-07 18:35:18 -07001141 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001142 * Called when an existing connection is added remotely.
1143 * @param connection The existing connection which was added.
1144 */
1145 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1146
1147 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001148 * @hide
1149 */
1150 public boolean containsConference(Conference conference) {
1151 return mIdByConference.containsKey(conference);
1152 }
1153
Ihab Awadb8e85c72014-08-23 20:34:57 -07001154 /** {@hide} */
1155 void addRemoteConference(RemoteConference remoteConference) {
1156 onRemoteConferenceAdded(remoteConference);
1157 }
1158
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001159 /** {@hide} */
1160 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1161 onRemoteExistingConnectionAdded(remoteConnection);
1162 }
1163
Ihab Awad5d0410f2014-07-30 10:07:40 -07001164 private void onAccountsInitialized() {
1165 mAreAccountsInitialized = true;
1166 for (Runnable r : mPreInitializationConnectionRequests) {
1167 r.run();
1168 }
1169 mPreInitializationConnectionRequests.clear();
1170 }
1171
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001172 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001173 * Adds an existing connection to the list of connections, identified by a new call ID unique
1174 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001175 *
1176 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001177 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001178 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001179 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1180 String id;
1181 if (handle == null) {
1182 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1183 // so just use a random UUID.
1184 id = UUID.randomUUID().toString();
1185 } else {
1186 // Phone account handle was provided, so use the ConnectionService class name as a
1187 // prefix for a unique incremental call ID.
1188 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1189 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001190 addConnection(id, connection);
1191 return id;
1192 }
1193
Ihab Awad542e0ea2014-05-16 10:22:16 -07001194 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001195 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001196 mConnectionById.put(callId, connection);
1197 mIdByConnection.put(connection, callId);
1198 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001199 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001200 }
1201
Anthony Lee30e65842014-11-06 16:30:53 -08001202 /** {@hide} */
1203 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001204 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001205 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001206 connection.removeConnectionListener(mConnectionListener);
1207 mConnectionById.remove(mIdByConnection.get(connection));
1208 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001209 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001210 }
1211
Santos Cordon823fd3c2014-08-07 18:35:18 -07001212 private String addConferenceInternal(Conference conference) {
1213 if (mIdByConference.containsKey(conference)) {
1214 Log.w(this, "Re-adding an existing conference: %s.", conference);
1215 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001216 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1217 // cannot determine a ConnectionService class name to associate with the ID, so use
1218 // a unique UUID (for now).
Santos Cordon823fd3c2014-08-07 18:35:18 -07001219 String id = UUID.randomUUID().toString();
1220 mConferenceById.put(id, conference);
1221 mIdByConference.put(conference, id);
1222 conference.addListener(mConferenceListener);
1223 return id;
1224 }
1225
1226 return null;
1227 }
1228
1229 private void removeConference(Conference conference) {
1230 if (mIdByConference.containsKey(conference)) {
1231 conference.removeListener(mConferenceListener);
1232
1233 String id = mIdByConference.get(conference);
1234 mConferenceById.remove(id);
1235 mIdByConference.remove(conference);
1236 mAdapter.removeCall(id);
1237 }
1238 }
1239
Ihab Awad542e0ea2014-05-16 10:22:16 -07001240 private Connection findConnectionForAction(String callId, String action) {
1241 if (mConnectionById.containsKey(callId)) {
1242 return mConnectionById.get(callId);
1243 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001244 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001245 return getNullConnection();
1246 }
1247
1248 static synchronized Connection getNullConnection() {
1249 if (sNullConnection == null) {
1250 sNullConnection = new Connection() {};
1251 }
1252 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001253 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001254
1255 private Conference findConferenceForAction(String conferenceId, String action) {
1256 if (mConferenceById.containsKey(conferenceId)) {
1257 return mConferenceById.get(conferenceId);
1258 }
1259 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1260 return getNullConference();
1261 }
1262
Ihab Awadb8e85c72014-08-23 20:34:57 -07001263 private List<String> createConnectionIdList(List<Connection> connections) {
1264 List<String> ids = new ArrayList<>();
1265 for (Connection c : connections) {
1266 if (mIdByConnection.containsKey(c)) {
1267 ids.add(mIdByConnection.get(c));
1268 }
1269 }
1270 Collections.sort(ids);
1271 return ids;
1272 }
1273
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001274 /**
1275 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001276 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001277 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001278 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001279 * @return List of string conference and call Ids.
1280 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001281 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001282 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001283 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001284 // Only allow Connection and Conference conferenceables.
1285 if (c instanceof Connection) {
1286 Connection connection = (Connection) c;
1287 if (mIdByConnection.containsKey(connection)) {
1288 ids.add(mIdByConnection.get(connection));
1289 }
1290 } else if (c instanceof Conference) {
1291 Conference conference = (Conference) c;
1292 if (mIdByConference.containsKey(conference)) {
1293 ids.add(mIdByConference.get(conference));
1294 }
1295 }
1296 }
1297 Collections.sort(ids);
1298 return ids;
1299 }
1300
Santos Cordon0159ac02014-08-21 14:28:11 -07001301 private Conference getNullConference() {
1302 if (sNullConference == null) {
1303 sNullConference = new Conference(null) {};
1304 }
1305 return sNullConference;
1306 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001307
1308 private void endAllConnections() {
1309 // Unbound from telecomm. We should end all connections and conferences.
1310 for (Connection connection : mIdByConnection.keySet()) {
1311 // only operate on top-level calls. Conference calls will be removed on their own.
1312 if (connection.getConference() == null) {
1313 connection.onDisconnect();
1314 }
1315 }
1316 for (Conference conference : mIdByConference.keySet()) {
1317 conference.onDisconnect();
1318 }
1319 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001320
1321 /**
1322 * Retrieves the next call ID as maintainted by the connection service.
1323 *
1324 * @return The call ID.
1325 */
1326 private int getNextCallId() {
1327 synchronized(mIdSyncRoot) {
1328 return ++mId;
1329 }
1330 }
Santos Cordon980acb92014-05-31 10:31:19 -07001331}