blob: 56d94917fc03e218f6bbba3218c18882fcd34f4e [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 Lee5ffbe8b2014-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 Lee5ffbe8b2014-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 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800616
617 @Override
618 public void onConnectionEvent(Connection connection, String event) {
619 String id = mIdByConnection.get(connection);
620 if (id != null) {
621 mAdapter.onConnectionEvent(id, event);
622 }
623 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700624 };
625
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700626 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700627 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700628 public final IBinder onBind(Intent intent) {
629 return mBinder;
630 }
631
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700632 /** {@inheritDoc} */
633 @Override
634 public boolean onUnbind(Intent intent) {
635 endAllConnections();
636 return super.onUnbind(intent);
637 }
638
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700639 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700640 * This can be used by telecom to either create a new outgoing call or attach to an existing
641 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700642 * createConnection util a connection service cancels the process or completes it successfully.
643 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700644 private void createConnection(
645 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700646 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700647 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700648 boolean isIncoming,
649 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700650 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700651 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
652 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -0700653 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700654
Yorke Leec3cf9822014-10-02 09:38:39 -0700655 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
656 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700657 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700658 Log.d(this, "createConnection, connection: %s", connection);
659 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700660 connection = Connection.createFailedConnection(
661 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700662 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700663
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700664 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700665 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700666 addConnection(callId, connection);
667 }
668
Andrew Lee100e2932014-09-08 15:34:24 -0700669 Uri address = connection.getAddress();
670 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700671 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700672 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700673 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800674 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700675
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700676 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700677 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700678 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700679 request,
680 new ParcelableConnection(
681 request.getAccountHandle(),
682 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800683 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700684 connection.getAddress(),
685 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700686 connection.getCallerDisplayName(),
687 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700688 connection.getVideoProvider() == null ?
689 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700690 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700691 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700692 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -0700693 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700694 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700695 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700696 createIdList(connection.getConferenceables()),
697 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -0800698 if (isUnknown) {
699 triggerConferenceRecalculate();
700 }
Evan Charltonbf11f982014-07-20 22:06:28 -0700701 }
702
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700703 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700704 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700705 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700706 }
707
Tyler Gunnbe74de02014-08-29 14:51:48 -0700708 private void answerVideo(String callId, int videoState) {
709 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700710 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700711 }
712
Tyler Gunnbe74de02014-08-29 14:51:48 -0700713 private void answer(String callId) {
714 Log.d(this, "answer %s", callId);
715 findConnectionForAction(callId, "answer").onAnswer();
716 }
717
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700718 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700719 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700720 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700721 }
722
Bryce Lee81901682015-08-28 16:38:02 -0700723 private void reject(String callId, String rejectWithMessage) {
724 Log.d(this, "reject %s with message", callId);
725 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
726 }
727
Bryce Leecac50772015-11-17 15:13:29 -0800728 private void silence(String callId) {
729 Log.d(this, "silence %s", callId);
730 findConnectionForAction(callId, "silence").onSilence();
731 }
732
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700733 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700734 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700735 if (mConnectionById.containsKey(callId)) {
736 findConnectionForAction(callId, "disconnect").onDisconnect();
737 } else {
738 findConferenceForAction(callId, "disconnect").onDisconnect();
739 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700740 }
741
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700742 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700743 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700744 if (mConnectionById.containsKey(callId)) {
745 findConnectionForAction(callId, "hold").onHold();
746 } else {
747 findConferenceForAction(callId, "hold").onHold();
748 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700749 }
750
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700751 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700752 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700753 if (mConnectionById.containsKey(callId)) {
754 findConnectionForAction(callId, "unhold").onUnhold();
755 } else {
756 findConferenceForAction(callId, "unhold").onUnhold();
757 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700758 }
759
Yorke Lee4af59352015-05-13 14:14:54 -0700760 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
761 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700762 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -0700763 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
764 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700765 } else {
Yorke Lee4af59352015-05-13 14:14:54 -0700766 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
767 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700768 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700769 }
770
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700771 private void playDtmfTone(String callId, char digit) {
772 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700773 if (mConnectionById.containsKey(callId)) {
774 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
775 } else {
776 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
777 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700778 }
779
780 private void stopDtmfTone(String callId) {
781 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700782 if (mConnectionById.containsKey(callId)) {
783 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
784 } else {
785 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
786 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700787 }
788
Santos Cordon823fd3c2014-08-07 18:35:18 -0700789 private void conference(String callId1, String callId2) {
790 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700791
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800792 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700793 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800794 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700795 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800796 conference2 = findConferenceForAction(callId2, "conference");
797 if (conference2 == getNullConference()) {
798 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
799 callId2);
800 return;
801 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700802 }
Santos Cordonb6939982014-06-04 20:20:58 -0700803
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800804 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700805 Connection connection1 = findConnectionForAction(callId1, "conference");
806 if (connection1 == getNullConnection()) {
807 Conference conference1 = findConferenceForAction(callId1, "addConnection");
808 if (conference1 == getNullConference()) {
809 Log.w(this,
810 "Connection1 or Conference1 missing in conference request %s.",
811 callId1);
812 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800813 // Call 1 is a conference.
814 if (connection2 != getNullConnection()) {
815 // Call 2 is a connection so merge via call 1 (conference).
816 conference1.onMerge(connection2);
817 } else {
818 // Call 2 is ALSO a conference; this should never happen.
819 Log.wtf(this, "There can only be one conference and an attempt was made to " +
820 "merge two conferences.");
821 return;
822 }
Ihab Awad50e35062014-09-30 09:17:03 -0700823 }
824 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800825 // Call 1 is a connection.
826 if (conference2 != getNullConference()) {
827 // Call 2 is a conference, so merge via call 2.
828 conference2.onMerge(connection1);
829 } else {
830 // Call 2 is a connection, so merge together.
831 onConference(connection1, connection2);
832 }
Ihab Awad50e35062014-09-30 09:17:03 -0700833 }
Santos Cordon980acb92014-05-31 10:31:19 -0700834 }
835
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700836 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700837 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700838
839 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700840 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700841 Log.w(this, "Connection missing in conference request %s.", callId);
842 return;
843 }
844
Santos Cordon0159ac02014-08-21 14:28:11 -0700845 Conference conference = connection.getConference();
846 if (conference != null) {
847 conference.onSeparate(connection);
848 }
Santos Cordon980acb92014-05-31 10:31:19 -0700849 }
850
Santos Cordona4868042014-09-04 17:39:22 -0700851 private void mergeConference(String callId) {
852 Log.d(this, "mergeConference(%s)", callId);
853 Conference conference = findConferenceForAction(callId, "mergeConference");
854 if (conference != null) {
855 conference.onMerge();
856 }
857 }
858
859 private void swapConference(String callId) {
860 Log.d(this, "swapConference(%s)", callId);
861 Conference conference = findConferenceForAction(callId, "swapConference");
862 if (conference != null) {
863 conference.onSwap();
864 }
865 }
866
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700867 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700868 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700869 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700870 }
871
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700872 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700873 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700874 // No need to query again if we already did it.
875 return;
876 }
877
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700878 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700879 @Override
880 public void onResult(
881 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700882 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700883 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700884 @Override
885 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700886 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700887 mRemoteConnectionManager.addConnectionService(
888 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700889 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700890 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700891 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700892 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700893 }
894 });
895 }
896
897 @Override
898 public void onError() {
899 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700900 @Override
901 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700902 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700903 }
904 });
905 }
906 });
907 }
908
Ihab Awadf8b69882014-07-25 15:14:01 -0700909 /**
910 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700911 * incoming request. This is used by {@code ConnectionService}s that are registered with
912 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
913 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700914 *
915 * @param connectionManagerPhoneAccount See description at
916 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
917 * @param request Details about the incoming call.
918 * @return The {@code Connection} object to satisfy this call, or {@code null} to
919 * not handle the call.
920 */
921 public final RemoteConnection createRemoteIncomingConnection(
922 PhoneAccountHandle connectionManagerPhoneAccount,
923 ConnectionRequest request) {
924 return mRemoteConnectionManager.createRemoteConnection(
925 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700926 }
927
928 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700929 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700930 * outgoing request. This is used by {@code ConnectionService}s that are registered with
931 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
932 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700933 *
934 * @param connectionManagerPhoneAccount See description at
935 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
936 * @param request Details about the incoming call.
937 * @return The {@code Connection} object to satisfy this call, or {@code null} to
938 * not handle the call.
939 */
940 public final RemoteConnection createRemoteOutgoingConnection(
941 PhoneAccountHandle connectionManagerPhoneAccount,
942 ConnectionRequest request) {
943 return mRemoteConnectionManager.createRemoteConnection(
944 connectionManagerPhoneAccount, request, false);
945 }
946
947 /**
Santos Cordona663f862014-10-29 13:49:58 -0700948 * Indicates to the relevant {@code RemoteConnectionService} that the specified
949 * {@link RemoteConnection}s should be merged into a conference call.
950 * <p>
951 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
952 * be invoked.
953 *
954 * @param remoteConnection1 The first of the remote connections to conference.
955 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700956 */
957 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700958 RemoteConnection remoteConnection1,
959 RemoteConnection remoteConnection2) {
960 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700961 }
962
963 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700964 * Adds a new conference call. When a conference call is created either as a result of an
965 * explicit request via {@link #onConference} or otherwise, the connection service should supply
966 * an instance of {@link Conference} by invoking this method. A conference call provided by this
967 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
968 *
969 * @param conference The new conference object.
970 */
971 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -0700972 Log.d(this, "addConference: conference=%s", conference);
973
Santos Cordon823fd3c2014-08-07 18:35:18 -0700974 String id = addConferenceInternal(conference);
975 if (id != null) {
976 List<String> connectionIds = new ArrayList<>(2);
977 for (Connection connection : conference.getConnections()) {
978 if (mIdByConnection.containsKey(connection)) {
979 connectionIds.add(mIdByConnection.get(connection));
980 }
981 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700982 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700983 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700984 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700985 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800986 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800987 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -0700988 conference.getVideoProvider() == null ?
989 null : conference.getVideoProvider().getInterface(),
990 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -0700991 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700992 conference.getStatusHints(),
993 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -0700994
Santos Cordon823fd3c2014-08-07 18:35:18 -0700995 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -0700996 mAdapter.setVideoProvider(id, conference.getVideoProvider());
997 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700998
999 // Go through any child calls and set the parent.
1000 for (Connection connection : conference.getConnections()) {
1001 String connectionId = mIdByConnection.get(connection);
1002 if (connectionId != null) {
1003 mAdapter.setIsConferenced(connectionId, id);
1004 }
1005 }
1006 }
1007 }
1008
1009 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001010 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1011 * connection.
1012 *
1013 * @param phoneAccountHandle The phone account handle for the connection.
1014 * @param connection The connection to add.
1015 */
1016 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1017 Connection connection) {
1018
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001019 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001020 if (id != null) {
1021 List<String> emptyList = new ArrayList<>(0);
1022
1023 ParcelableConnection parcelableConnection = new ParcelableConnection(
1024 phoneAccountHandle,
1025 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001026 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001027 connection.getAddress(),
1028 connection.getAddressPresentation(),
1029 connection.getCallerDisplayName(),
1030 connection.getCallerDisplayNamePresentation(),
1031 connection.getVideoProvider() == null ?
1032 null : connection.getVideoProvider().getInterface(),
1033 connection.getVideoState(),
1034 connection.isRingbackRequested(),
1035 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001036 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001037 connection.getStatusHints(),
1038 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001039 emptyList,
1040 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001041 mAdapter.addExistingConnection(id, parcelableConnection);
1042 }
1043 }
1044
1045 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001046 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1047 * has taken responsibility.
1048 *
1049 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001050 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001051 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001052 return mConnectionById.values();
1053 }
1054
1055 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001056 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1057 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001058 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001059 * @param connectionManagerPhoneAccount See description at
1060 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1061 * @param request Details about the incoming call.
1062 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1063 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001064 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001065 public Connection onCreateIncomingConnection(
1066 PhoneAccountHandle connectionManagerPhoneAccount,
1067 ConnectionRequest request) {
1068 return null;
1069 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001070
1071 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001072 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1073 * Connection is part of a conference controller but is not yet added to Connection
1074 * Service and hence cannot be added to the conference call.
1075 *
1076 * @hide
1077 */
1078 public void triggerConferenceRecalculate() {
1079 }
1080
1081 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001082 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1083 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001084 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001085 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1086 * this call.
1087 * <p>
1088 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1089 * has registered one or more {@code PhoneAccount}s having
1090 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1091 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1092 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1093 * making the connection.
1094 * <p>
1095 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1096 * being asked to make a direct connection. The
1097 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1098 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1099 * making the connection.
1100 * @param request Details about the outgoing call.
1101 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001102 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001103 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001104 public Connection onCreateOutgoingConnection(
1105 PhoneAccountHandle connectionManagerPhoneAccount,
1106 ConnectionRequest request) {
1107 return null;
1108 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001109
1110 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001111 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1112 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1113 * call created using
1114 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1115 *
1116 * @param connectionManagerPhoneAccount
1117 * @param request
1118 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001119 *
1120 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001121 */
1122 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1123 ConnectionRequest request) {
1124 return null;
1125 }
1126
1127 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001128 * Conference two specified connections. Invoked when the user has made a request to merge the
1129 * specified connections into a conference call. In response, the connection service should
1130 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001131 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001132 * @param connection1 A connection to merge into a conference call.
1133 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001134 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001135 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001136
Santos Cordona663f862014-10-29 13:49:58 -07001137 /**
1138 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1139 * When this method is invoked, this {@link ConnectionService} should create its own
1140 * representation of the conference call and send it to telecom using {@link #addConference}.
1141 * <p>
1142 * This is only relevant to {@link ConnectionService}s which are registered with
1143 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1144 *
1145 * @param conference The remote conference call.
1146 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001147 public void onRemoteConferenceAdded(RemoteConference conference) {}
1148
Santos Cordon823fd3c2014-08-07 18:35:18 -07001149 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001150 * Called when an existing connection is added remotely.
1151 * @param connection The existing connection which was added.
1152 */
1153 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1154
1155 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001156 * @hide
1157 */
1158 public boolean containsConference(Conference conference) {
1159 return mIdByConference.containsKey(conference);
1160 }
1161
Ihab Awadb8e85c72014-08-23 20:34:57 -07001162 /** {@hide} */
1163 void addRemoteConference(RemoteConference remoteConference) {
1164 onRemoteConferenceAdded(remoteConference);
1165 }
1166
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001167 /** {@hide} */
1168 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1169 onRemoteExistingConnectionAdded(remoteConnection);
1170 }
1171
Ihab Awad5d0410f2014-07-30 10:07:40 -07001172 private void onAccountsInitialized() {
1173 mAreAccountsInitialized = true;
1174 for (Runnable r : mPreInitializationConnectionRequests) {
1175 r.run();
1176 }
1177 mPreInitializationConnectionRequests.clear();
1178 }
1179
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001180 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001181 * Adds an existing connection to the list of connections, identified by a new call ID unique
1182 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001183 *
1184 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001185 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001186 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001187 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1188 String id;
1189 if (handle == null) {
1190 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1191 // so just use a random UUID.
1192 id = UUID.randomUUID().toString();
1193 } else {
1194 // Phone account handle was provided, so use the ConnectionService class name as a
1195 // prefix for a unique incremental call ID.
1196 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1197 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001198 addConnection(id, connection);
1199 return id;
1200 }
1201
Ihab Awad542e0ea2014-05-16 10:22:16 -07001202 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001203 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001204 mConnectionById.put(callId, connection);
1205 mIdByConnection.put(connection, callId);
1206 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001207 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001208 }
1209
Anthony Lee30e65842014-11-06 16:30:53 -08001210 /** {@hide} */
1211 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001212 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001213 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001214 connection.removeConnectionListener(mConnectionListener);
1215 mConnectionById.remove(mIdByConnection.get(connection));
1216 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001217 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001218 }
1219
Santos Cordon823fd3c2014-08-07 18:35:18 -07001220 private String addConferenceInternal(Conference conference) {
1221 if (mIdByConference.containsKey(conference)) {
1222 Log.w(this, "Re-adding an existing conference: %s.", conference);
1223 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001224 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1225 // cannot determine a ConnectionService class name to associate with the ID, so use
1226 // a unique UUID (for now).
Santos Cordon823fd3c2014-08-07 18:35:18 -07001227 String id = UUID.randomUUID().toString();
1228 mConferenceById.put(id, conference);
1229 mIdByConference.put(conference, id);
1230 conference.addListener(mConferenceListener);
1231 return id;
1232 }
1233
1234 return null;
1235 }
1236
1237 private void removeConference(Conference conference) {
1238 if (mIdByConference.containsKey(conference)) {
1239 conference.removeListener(mConferenceListener);
1240
1241 String id = mIdByConference.get(conference);
1242 mConferenceById.remove(id);
1243 mIdByConference.remove(conference);
1244 mAdapter.removeCall(id);
1245 }
1246 }
1247
Ihab Awad542e0ea2014-05-16 10:22:16 -07001248 private Connection findConnectionForAction(String callId, String action) {
1249 if (mConnectionById.containsKey(callId)) {
1250 return mConnectionById.get(callId);
1251 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001252 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001253 return getNullConnection();
1254 }
1255
1256 static synchronized Connection getNullConnection() {
1257 if (sNullConnection == null) {
1258 sNullConnection = new Connection() {};
1259 }
1260 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001261 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001262
1263 private Conference findConferenceForAction(String conferenceId, String action) {
1264 if (mConferenceById.containsKey(conferenceId)) {
1265 return mConferenceById.get(conferenceId);
1266 }
1267 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1268 return getNullConference();
1269 }
1270
Ihab Awadb8e85c72014-08-23 20:34:57 -07001271 private List<String> createConnectionIdList(List<Connection> connections) {
1272 List<String> ids = new ArrayList<>();
1273 for (Connection c : connections) {
1274 if (mIdByConnection.containsKey(c)) {
1275 ids.add(mIdByConnection.get(c));
1276 }
1277 }
1278 Collections.sort(ids);
1279 return ids;
1280 }
1281
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001282 /**
1283 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001284 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001285 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001286 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001287 * @return List of string conference and call Ids.
1288 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001289 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001290 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001291 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001292 // Only allow Connection and Conference conferenceables.
1293 if (c instanceof Connection) {
1294 Connection connection = (Connection) c;
1295 if (mIdByConnection.containsKey(connection)) {
1296 ids.add(mIdByConnection.get(connection));
1297 }
1298 } else if (c instanceof Conference) {
1299 Conference conference = (Conference) c;
1300 if (mIdByConference.containsKey(conference)) {
1301 ids.add(mIdByConference.get(conference));
1302 }
1303 }
1304 }
1305 Collections.sort(ids);
1306 return ids;
1307 }
1308
Santos Cordon0159ac02014-08-21 14:28:11 -07001309 private Conference getNullConference() {
1310 if (sNullConference == null) {
1311 sNullConference = new Conference(null) {};
1312 }
1313 return sNullConference;
1314 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001315
1316 private void endAllConnections() {
1317 // Unbound from telecomm. We should end all connections and conferences.
1318 for (Connection connection : mIdByConnection.keySet()) {
1319 // only operate on top-level calls. Conference calls will be removed on their own.
1320 if (connection.getConference() == null) {
1321 connection.onDisconnect();
1322 }
1323 }
1324 for (Conference conference : mIdByConference.keySet()) {
1325 conference.onDisconnect();
1326 }
1327 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001328
1329 /**
1330 * Retrieves the next call ID as maintainted by the connection service.
1331 *
1332 * @return The call ID.
1333 */
1334 private int getNextCallId() {
1335 synchronized(mIdSyncRoot) {
1336 return ++mId;
1337 }
1338 }
Santos Cordon980acb92014-05-31 10:31:19 -07001339}