blob: d18b31725aaa7e130ee1545871f53ad7eb481769 [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;
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700106 private static final int MSG_PULL_EXTERNAL_CALL = 22;
107 private static final int MSG_SEND_CALL_EVENT = 23;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700108
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700109 private static Connection sNullConnection;
110
mike dooley95e80702014-09-18 14:07:52 -0700111 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
112 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
113 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
114 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700115 private final RemoteConnectionManager mRemoteConnectionManager =
116 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700117 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700118 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700119
Santos Cordon823fd3c2014-08-07 18:35:18 -0700120 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700121 private Conference sNullConference;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700122 private Object mIdSyncRoot = new Object();
123 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700124
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700125 private final IBinder mBinder = new IConnectionService.Stub() {
126 @Override
127 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700128 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
129 }
130
131 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
132 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700133 }
134
135 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700136 public void createConnection(
137 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700138 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700139 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700140 boolean isIncoming,
141 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700142 SomeArgs args = SomeArgs.obtain();
143 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700144 args.arg2 = id;
145 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700146 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700147 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700148 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700149 }
150
151 @Override
152 public void abort(String callId) {
153 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
154 }
155
156 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700157 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700158 SomeArgs args = SomeArgs.obtain();
159 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700160 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700161 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
162 }
163
164 @Override
165 public void answer(String callId) {
166 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700167 }
168
169 @Override
170 public void reject(String callId) {
171 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
172 }
173
174 @Override
Bryce Lee81901682015-08-28 16:38:02 -0700175 public void rejectWithMessage(String callId, String message) {
176 SomeArgs args = SomeArgs.obtain();
177 args.arg1 = callId;
178 args.arg2 = message;
179 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
180 }
181
182 @Override
Bryce Leecac50772015-11-17 15:13:29 -0800183 public void silence(String callId) {
184 mHandler.obtainMessage(MSG_SILENCE, callId).sendToTarget();
185 }
186
187 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700188 public void disconnect(String callId) {
189 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
190 }
191
192 @Override
193 public void hold(String callId) {
194 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
195 }
196
197 @Override
198 public void unhold(String callId) {
199 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
200 }
201
202 @Override
Yorke Lee4af59352015-05-13 14:14:54 -0700203 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700204 SomeArgs args = SomeArgs.obtain();
205 args.arg1 = callId;
Yorke Lee4af59352015-05-13 14:14:54 -0700206 args.arg2 = callAudioState;
207 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700208 }
209
210 @Override
211 public void playDtmfTone(String callId, char digit) {
212 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
213 }
214
215 @Override
216 public void stopDtmfTone(String callId) {
217 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
218 }
219
220 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700221 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700222 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700223 args.arg1 = callId1;
224 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700225 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
226 }
227
228 @Override
229 public void splitFromConference(String callId) {
230 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
231 }
232
233 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700234 public void mergeConference(String callId) {
235 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
236 }
237
238 @Override
239 public void swapConference(String callId) {
240 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
241 }
242
243 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700244 public void onPostDialContinue(String callId, boolean proceed) {
245 SomeArgs args = SomeArgs.obtain();
246 args.arg1 = callId;
247 args.argi1 = proceed ? 1 : 0;
248 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
249 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700250
251 @Override
252 public void pullExternalCall(String callId) {
253 mHandler.obtainMessage(MSG_PULL_EXTERNAL_CALL, callId).sendToTarget();
254 }
255
256 @Override
257 public void sendCallEvent(String callId, String event, Bundle extras) {
258 SomeArgs args = SomeArgs.obtain();
259 args.arg1 = callId;
260 args.arg2 = event;
261 args.arg3 = extras;
262 mHandler.obtainMessage(MSG_SEND_CALL_EVENT, args).sendToTarget();
263 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700264 };
265
266 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
267 @Override
268 public void handleMessage(Message msg) {
269 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700270 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700271 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
272 onAdapterAttached();
273 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700274 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
275 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
276 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700277 case MSG_CREATE_CONNECTION: {
278 SomeArgs args = (SomeArgs) msg.obj;
279 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700280 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700281 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700282 final String id = (String) args.arg2;
283 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700284 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700285 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700286 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700287 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700288 mPreInitializationConnectionRequests.add(new Runnable() {
289 @Override
290 public void run() {
291 createConnection(
292 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700293 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700294 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700295 isIncoming,
296 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700297 }
298 });
299 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700300 createConnection(
301 connectionManagerPhoneAccount,
302 id,
303 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700304 isIncoming,
305 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700306 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700307 } finally {
308 args.recycle();
309 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700310 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700311 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700312 case MSG_ABORT:
313 abort((String) msg.obj);
314 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700315 case MSG_ANSWER:
316 answer((String) msg.obj);
317 break;
318 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700319 SomeArgs args = (SomeArgs) msg.obj;
320 try {
321 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700322 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700323 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700324 } finally {
325 args.recycle();
326 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700327 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700328 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700329 case MSG_REJECT:
330 reject((String) msg.obj);
331 break;
Bryce Lee81901682015-08-28 16:38:02 -0700332 case MSG_REJECT_WITH_MESSAGE: {
333 SomeArgs args = (SomeArgs) msg.obj;
334 try {
335 reject((String) args.arg1, (String) args.arg2);
336 } finally {
337 args.recycle();
338 }
339 break;
340 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700341 case MSG_DISCONNECT:
342 disconnect((String) msg.obj);
343 break;
Bryce Leecac50772015-11-17 15:13:29 -0800344 case MSG_SILENCE:
345 silence((String) msg.obj);
346 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700347 case MSG_HOLD:
348 hold((String) msg.obj);
349 break;
350 case MSG_UNHOLD:
351 unhold((String) msg.obj);
352 break;
Yorke Lee4af59352015-05-13 14:14:54 -0700353 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700354 SomeArgs args = (SomeArgs) msg.obj;
355 try {
356 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700357 CallAudioState audioState = (CallAudioState) args.arg2;
358 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700359 } finally {
360 args.recycle();
361 }
362 break;
363 }
364 case MSG_PLAY_DTMF_TONE:
365 playDtmfTone((String) msg.obj, (char) msg.arg1);
366 break;
367 case MSG_STOP_DTMF_TONE:
368 stopDtmfTone((String) msg.obj);
369 break;
370 case MSG_CONFERENCE: {
371 SomeArgs args = (SomeArgs) msg.obj;
372 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700373 String callId1 = (String) args.arg1;
374 String callId2 = (String) args.arg2;
375 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700376 } finally {
377 args.recycle();
378 }
379 break;
380 }
381 case MSG_SPLIT_FROM_CONFERENCE:
382 splitFromConference((String) msg.obj);
383 break;
Santos Cordona4868042014-09-04 17:39:22 -0700384 case MSG_MERGE_CONFERENCE:
385 mergeConference((String) msg.obj);
386 break;
387 case MSG_SWAP_CONFERENCE:
388 swapConference((String) msg.obj);
389 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700390 case MSG_ON_POST_DIAL_CONTINUE: {
391 SomeArgs args = (SomeArgs) msg.obj;
392 try {
393 String callId = (String) args.arg1;
394 boolean proceed = (args.argi1 == 1);
395 onPostDialContinue(callId, proceed);
396 } finally {
397 args.recycle();
398 }
399 break;
400 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700401 case MSG_PULL_EXTERNAL_CALL: {
402 pullExternalCall((String) msg.obj);
403 break;
404 }
405 case MSG_SEND_CALL_EVENT: {
406 SomeArgs args = (SomeArgs) msg.obj;
407 try {
408 String callId = (String) args.arg1;
409 String event = (String) args.arg2;
410 Bundle extras = (Bundle) args.arg3;
411 sendCallEvent(callId, event, extras);
412 } finally {
413 args.recycle();
414 }
415 break;
416 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700417 default:
418 break;
419 }
420 }
421 };
422
Santos Cordon823fd3c2014-08-07 18:35:18 -0700423 private final Conference.Listener mConferenceListener = new Conference.Listener() {
424 @Override
425 public void onStateChanged(Conference conference, int oldState, int newState) {
426 String id = mIdByConference.get(conference);
427 switch (newState) {
428 case Connection.STATE_ACTIVE:
429 mAdapter.setActive(id);
430 break;
431 case Connection.STATE_HOLDING:
432 mAdapter.setOnHold(id);
433 break;
434 case Connection.STATE_DISCONNECTED:
435 // handled by onDisconnected
436 break;
437 }
438 }
439
440 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700441 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700442 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700443 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700444 }
445
446 @Override
447 public void onConnectionAdded(Conference conference, Connection connection) {
448 }
449
450 @Override
451 public void onConnectionRemoved(Conference conference, Connection connection) {
452 }
453
454 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700455 public void onConferenceableConnectionsChanged(
456 Conference conference, List<Connection> conferenceableConnections) {
457 mAdapter.setConferenceableConnections(
458 mIdByConference.get(conference),
459 createConnectionIdList(conferenceableConnections));
460 }
461
462 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700463 public void onDestroyed(Conference conference) {
464 removeConference(conference);
465 }
466
467 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800468 public void onConnectionCapabilitiesChanged(
469 Conference conference,
470 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700471 String id = mIdByConference.get(conference);
472 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800473 Connection.capabilitiesToString(connectionCapabilities));
474 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700475 }
Rekha Kumar07366812015-03-24 16:42:31 -0700476
477 @Override
478 public void onVideoStateChanged(Conference c, int videoState) {
479 String id = mIdByConference.get(c);
480 Log.d(this, "onVideoStateChanged set video state %d", videoState);
481 mAdapter.setVideoState(id, videoState);
482 }
483
484 @Override
485 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
486 String id = mIdByConference.get(c);
487 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
488 videoProvider);
489 mAdapter.setVideoProvider(id, videoProvider);
490 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700491
492 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700493 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
494 String id = mIdByConference.get(conference);
495 mAdapter.setStatusHints(id, statusHints);
496 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700497
498 @Override
499 public void onExtrasChanged(Conference conference, Bundle extras) {
500 String id = mIdByConference.get(conference);
501 mAdapter.setExtras(id, extras);
502 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700503 };
504
Ihab Awad542e0ea2014-05-16 10:22:16 -0700505 private final Connection.Listener mConnectionListener = new Connection.Listener() {
506 @Override
507 public void onStateChanged(Connection c, int state) {
508 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700509 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700510 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700511 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700512 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700513 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700514 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700515 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700516 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700517 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700518 // Handled in onDisconnected()
519 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700520 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700521 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700522 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700523 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700524 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700525 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700526 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700527 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700528 break;
529 }
530 }
531
532 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700533 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700534 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700535 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700536 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700537 }
538
539 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700540 public void onVideoStateChanged(Connection c, int videoState) {
541 String id = mIdByConnection.get(c);
542 Log.d(this, "Adapter set video state %d", videoState);
543 mAdapter.setVideoState(id, videoState);
544 }
545
546 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700547 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700548 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700549 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700550 }
551
552 @Override
553 public void onCallerDisplayNameChanged(
554 Connection c, String callerDisplayName, int presentation) {
555 String id = mIdByConnection.get(c);
556 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700557 }
558
559 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700560 public void onDestroyed(Connection c) {
561 removeConnection(c);
562 }
Ihab Awadf8358972014-05-28 16:46:42 -0700563
564 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700565 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700566 String id = mIdByConnection.get(c);
567 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700568 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700569 }
570
571 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800572 public void onPostDialChar(Connection c, char nextChar) {
573 String id = mIdByConnection.get(c);
574 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
575 mAdapter.onPostDialChar(id, nextChar);
576 }
577
578 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700579 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700580 String id = mIdByConnection.get(c);
581 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700582 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700583 }
Santos Cordonb6939982014-06-04 20:20:58 -0700584
585 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800586 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700587 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700588 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800589 Connection.capabilitiesToString(capabilities));
590 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700591 }
592
Santos Cordonb6939982014-06-04 20:20:58 -0700593 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700594 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700595 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700596 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
597 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700598 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700599 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700600
601 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700602 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700603 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700604 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700605 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700606
607 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700608 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700609 String id = mIdByConnection.get(c);
610 mAdapter.setStatusHints(id, statusHints);
611 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700612
613 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800614 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700615 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700616 mAdapter.setConferenceableConnections(
617 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800618 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700619 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700620
621 @Override
622 public void onConferenceChanged(Connection connection, Conference conference) {
623 String id = mIdByConnection.get(connection);
624 if (id != null) {
625 String conferenceId = null;
626 if (conference != null) {
627 conferenceId = mIdByConference.get(conference);
628 }
629 mAdapter.setIsConferenced(id, conferenceId);
630 }
631 }
Anthony Lee17455a32015-04-24 15:25:29 -0700632
633 @Override
634 public void onConferenceMergeFailed(Connection connection) {
635 String id = mIdByConnection.get(connection);
636 if (id != null) {
637 mAdapter.onConferenceMergeFailed(id);
638 }
639 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700640
641 @Override
642 public void onExtrasChanged(Connection connection, Bundle extras) {
643 String id = mIdByConnection.get(connection);
644 if (id != null) {
645 mAdapter.setExtras(id, extras);
646 }
647 }
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800648
649 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700650 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800651 String id = mIdByConnection.get(connection);
652 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700653 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800654 }
655 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700656 };
657
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700658 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700659 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700660 public final IBinder onBind(Intent intent) {
661 return mBinder;
662 }
663
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700664 /** {@inheritDoc} */
665 @Override
666 public boolean onUnbind(Intent intent) {
667 endAllConnections();
668 return super.onUnbind(intent);
669 }
670
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700671 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700672 * This can be used by telecom to either create a new outgoing call or attach to an existing
673 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700674 * createConnection util a connection service cancels the process or completes it successfully.
675 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700676 private void createConnection(
677 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700678 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700679 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700680 boolean isIncoming,
681 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700682 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700683 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
684 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -0700685 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700686
Yorke Leec3cf9822014-10-02 09:38:39 -0700687 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
688 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700689 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700690 Log.d(this, "createConnection, connection: %s", connection);
691 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700692 connection = Connection.createFailedConnection(
693 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700694 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700695
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700696 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700697 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700698 addConnection(callId, connection);
699 }
700
Andrew Lee100e2932014-09-08 15:34:24 -0700701 Uri address = connection.getAddress();
702 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700703 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700704 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700705 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800706 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700707
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700708 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700709 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700710 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700711 request,
712 new ParcelableConnection(
713 request.getAccountHandle(),
714 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800715 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700716 connection.getAddress(),
717 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700718 connection.getCallerDisplayName(),
719 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700720 connection.getVideoProvider() == null ?
721 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700722 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700723 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700724 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -0700725 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700726 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700727 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700728 createIdList(connection.getConferenceables()),
729 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -0800730 if (isUnknown) {
731 triggerConferenceRecalculate();
732 }
Evan Charltonbf11f982014-07-20 22:06:28 -0700733 }
734
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700735 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700736 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700737 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700738 }
739
Tyler Gunnbe74de02014-08-29 14:51:48 -0700740 private void answerVideo(String callId, int videoState) {
741 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700742 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700743 }
744
Tyler Gunnbe74de02014-08-29 14:51:48 -0700745 private void answer(String callId) {
746 Log.d(this, "answer %s", callId);
747 findConnectionForAction(callId, "answer").onAnswer();
748 }
749
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700750 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700751 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700752 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700753 }
754
Bryce Lee81901682015-08-28 16:38:02 -0700755 private void reject(String callId, String rejectWithMessage) {
756 Log.d(this, "reject %s with message", callId);
757 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
758 }
759
Bryce Leecac50772015-11-17 15:13:29 -0800760 private void silence(String callId) {
761 Log.d(this, "silence %s", callId);
762 findConnectionForAction(callId, "silence").onSilence();
763 }
764
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700765 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700766 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700767 if (mConnectionById.containsKey(callId)) {
768 findConnectionForAction(callId, "disconnect").onDisconnect();
769 } else {
770 findConferenceForAction(callId, "disconnect").onDisconnect();
771 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700772 }
773
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700774 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700775 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700776 if (mConnectionById.containsKey(callId)) {
777 findConnectionForAction(callId, "hold").onHold();
778 } else {
779 findConferenceForAction(callId, "hold").onHold();
780 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700781 }
782
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700783 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700784 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700785 if (mConnectionById.containsKey(callId)) {
786 findConnectionForAction(callId, "unhold").onUnhold();
787 } else {
788 findConferenceForAction(callId, "unhold").onUnhold();
789 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700790 }
791
Yorke Lee4af59352015-05-13 14:14:54 -0700792 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
793 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700794 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -0700795 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
796 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700797 } else {
Yorke Lee4af59352015-05-13 14:14:54 -0700798 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
799 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700800 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700801 }
802
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700803 private void playDtmfTone(String callId, char digit) {
804 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700805 if (mConnectionById.containsKey(callId)) {
806 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
807 } else {
808 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
809 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700810 }
811
812 private void stopDtmfTone(String callId) {
813 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700814 if (mConnectionById.containsKey(callId)) {
815 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
816 } else {
817 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
818 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700819 }
820
Santos Cordon823fd3c2014-08-07 18:35:18 -0700821 private void conference(String callId1, String callId2) {
822 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700823
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800824 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700825 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800826 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700827 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800828 conference2 = findConferenceForAction(callId2, "conference");
829 if (conference2 == getNullConference()) {
830 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
831 callId2);
832 return;
833 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700834 }
Santos Cordonb6939982014-06-04 20:20:58 -0700835
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800836 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700837 Connection connection1 = findConnectionForAction(callId1, "conference");
838 if (connection1 == getNullConnection()) {
839 Conference conference1 = findConferenceForAction(callId1, "addConnection");
840 if (conference1 == getNullConference()) {
841 Log.w(this,
842 "Connection1 or Conference1 missing in conference request %s.",
843 callId1);
844 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800845 // Call 1 is a conference.
846 if (connection2 != getNullConnection()) {
847 // Call 2 is a connection so merge via call 1 (conference).
848 conference1.onMerge(connection2);
849 } else {
850 // Call 2 is ALSO a conference; this should never happen.
851 Log.wtf(this, "There can only be one conference and an attempt was made to " +
852 "merge two conferences.");
853 return;
854 }
Ihab Awad50e35062014-09-30 09:17:03 -0700855 }
856 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800857 // Call 1 is a connection.
858 if (conference2 != getNullConference()) {
859 // Call 2 is a conference, so merge via call 2.
860 conference2.onMerge(connection1);
861 } else {
862 // Call 2 is a connection, so merge together.
863 onConference(connection1, connection2);
864 }
Ihab Awad50e35062014-09-30 09:17:03 -0700865 }
Santos Cordon980acb92014-05-31 10:31:19 -0700866 }
867
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700868 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700869 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700870
871 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700872 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700873 Log.w(this, "Connection missing in conference request %s.", callId);
874 return;
875 }
876
Santos Cordon0159ac02014-08-21 14:28:11 -0700877 Conference conference = connection.getConference();
878 if (conference != null) {
879 conference.onSeparate(connection);
880 }
Santos Cordon980acb92014-05-31 10:31:19 -0700881 }
882
Santos Cordona4868042014-09-04 17:39:22 -0700883 private void mergeConference(String callId) {
884 Log.d(this, "mergeConference(%s)", callId);
885 Conference conference = findConferenceForAction(callId, "mergeConference");
886 if (conference != null) {
887 conference.onMerge();
888 }
889 }
890
891 private void swapConference(String callId) {
892 Log.d(this, "swapConference(%s)", callId);
893 Conference conference = findConferenceForAction(callId, "swapConference");
894 if (conference != null) {
895 conference.onSwap();
896 }
897 }
898
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700899 /**
900 * Notifies a {@link Connection} of a request to pull an external call.
901 *
902 * See {@link Call#pullExternalCall()}.
903 *
904 * @param callId The ID of the call to pull.
905 */
906 private void pullExternalCall(String callId) {
907 Log.d(this, "pullExternalCall(%s)", callId);
908 Connection connection = findConnectionForAction(callId, "pullExternalCall");
909 if (connection != null) {
910 connection.onPullExternalCall();
911 }
912 }
913
914 /**
915 * Notifies a {@link Connection} of a call event.
916 *
917 * See {@link Call#sendCallEvent(String, Bundle)}.
918 *
919 * @param callId The ID of the call receiving the event.
920 * @param event The event.
921 * @param extras Extras associated with the event.
922 */
923 private void sendCallEvent(String callId, String event, Bundle extras) {
924 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
925 Connection connection = findConnectionForAction(callId, "sendCallEvent");
926 if (connection != null) {
927 connection.onCallEvent(event, extras);
928 }
929
930 }
931
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700932 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700933 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700934 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700935 }
936
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700937 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700938 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700939 // No need to query again if we already did it.
940 return;
941 }
942
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700943 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700944 @Override
945 public void onResult(
946 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700947 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700948 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700949 @Override
950 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700951 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700952 mRemoteConnectionManager.addConnectionService(
953 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700954 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700955 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700956 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700957 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700958 }
959 });
960 }
961
962 @Override
963 public void onError() {
964 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700965 @Override
966 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700967 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700968 }
969 });
970 }
971 });
972 }
973
Ihab Awadf8b69882014-07-25 15:14:01 -0700974 /**
975 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700976 * incoming request. This is used by {@code ConnectionService}s that are registered with
977 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
978 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700979 *
980 * @param connectionManagerPhoneAccount See description at
981 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
982 * @param request Details about the incoming call.
983 * @return The {@code Connection} object to satisfy this call, or {@code null} to
984 * not handle the call.
985 */
986 public final RemoteConnection createRemoteIncomingConnection(
987 PhoneAccountHandle connectionManagerPhoneAccount,
988 ConnectionRequest request) {
989 return mRemoteConnectionManager.createRemoteConnection(
990 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700991 }
992
993 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700994 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700995 * outgoing request. This is used by {@code ConnectionService}s that are registered with
996 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
997 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700998 *
999 * @param connectionManagerPhoneAccount See description at
1000 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1001 * @param request Details about the incoming call.
1002 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1003 * not handle the call.
1004 */
1005 public final RemoteConnection createRemoteOutgoingConnection(
1006 PhoneAccountHandle connectionManagerPhoneAccount,
1007 ConnectionRequest request) {
1008 return mRemoteConnectionManager.createRemoteConnection(
1009 connectionManagerPhoneAccount, request, false);
1010 }
1011
1012 /**
Santos Cordona663f862014-10-29 13:49:58 -07001013 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1014 * {@link RemoteConnection}s should be merged into a conference call.
1015 * <p>
1016 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1017 * be invoked.
1018 *
1019 * @param remoteConnection1 The first of the remote connections to conference.
1020 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001021 */
1022 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001023 RemoteConnection remoteConnection1,
1024 RemoteConnection remoteConnection2) {
1025 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001026 }
1027
1028 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001029 * Adds a new conference call. When a conference call is created either as a result of an
1030 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1031 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1032 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1033 *
1034 * @param conference The new conference object.
1035 */
1036 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001037 Log.d(this, "addConference: conference=%s", conference);
1038
Santos Cordon823fd3c2014-08-07 18:35:18 -07001039 String id = addConferenceInternal(conference);
1040 if (id != null) {
1041 List<String> connectionIds = new ArrayList<>(2);
1042 for (Connection connection : conference.getConnections()) {
1043 if (mIdByConnection.containsKey(connection)) {
1044 connectionIds.add(mIdByConnection.get(connection));
1045 }
1046 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001047 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001048 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001049 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001050 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001051 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001052 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001053 conference.getVideoProvider() == null ?
1054 null : conference.getVideoProvider().getInterface(),
1055 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001056 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001057 conference.getStatusHints(),
1058 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001059
Santos Cordon823fd3c2014-08-07 18:35:18 -07001060 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001061 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1062 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001063
1064 // Go through any child calls and set the parent.
1065 for (Connection connection : conference.getConnections()) {
1066 String connectionId = mIdByConnection.get(connection);
1067 if (connectionId != null) {
1068 mAdapter.setIsConferenced(connectionId, id);
1069 }
1070 }
1071 }
1072 }
1073
1074 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001075 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1076 * connection.
1077 *
1078 * @param phoneAccountHandle The phone account handle for the connection.
1079 * @param connection The connection to add.
1080 */
1081 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1082 Connection connection) {
1083
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001084 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001085 if (id != null) {
1086 List<String> emptyList = new ArrayList<>(0);
1087
1088 ParcelableConnection parcelableConnection = new ParcelableConnection(
1089 phoneAccountHandle,
1090 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001091 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001092 connection.getAddress(),
1093 connection.getAddressPresentation(),
1094 connection.getCallerDisplayName(),
1095 connection.getCallerDisplayNamePresentation(),
1096 connection.getVideoProvider() == null ?
1097 null : connection.getVideoProvider().getInterface(),
1098 connection.getVideoState(),
1099 connection.isRingbackRequested(),
1100 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001101 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001102 connection.getStatusHints(),
1103 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001104 emptyList,
1105 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001106 mAdapter.addExistingConnection(id, parcelableConnection);
1107 }
1108 }
1109
1110 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001111 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1112 * has taken responsibility.
1113 *
1114 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001115 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001116 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001117 return mConnectionById.values();
1118 }
1119
1120 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001121 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1122 * has taken responsibility.
1123 *
1124 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1125 */
1126 public final Collection<Conference> getAllConferences() {
1127 return mConferenceById.values();
1128 }
1129
1130 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001131 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1132 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001133 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001134 * @param connectionManagerPhoneAccount See description at
1135 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1136 * @param request Details about the incoming call.
1137 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1138 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001139 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001140 public Connection onCreateIncomingConnection(
1141 PhoneAccountHandle connectionManagerPhoneAccount,
1142 ConnectionRequest request) {
1143 return null;
1144 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001145
1146 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001147 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1148 * Connection is part of a conference controller but is not yet added to Connection
1149 * Service and hence cannot be added to the conference call.
1150 *
1151 * @hide
1152 */
1153 public void triggerConferenceRecalculate() {
1154 }
1155
1156 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001157 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1158 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001159 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001160 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1161 * this call.
1162 * <p>
1163 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1164 * has registered one or more {@code PhoneAccount}s having
1165 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1166 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1167 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1168 * making the connection.
1169 * <p>
1170 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1171 * being asked to make a direct connection. The
1172 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1173 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1174 * making the connection.
1175 * @param request Details about the outgoing call.
1176 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001177 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001178 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001179 public Connection onCreateOutgoingConnection(
1180 PhoneAccountHandle connectionManagerPhoneAccount,
1181 ConnectionRequest request) {
1182 return null;
1183 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001184
1185 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001186 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1187 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1188 * call created using
1189 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1190 *
1191 * @param connectionManagerPhoneAccount
1192 * @param request
1193 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001194 *
1195 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001196 */
1197 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1198 ConnectionRequest request) {
1199 return null;
1200 }
1201
1202 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001203 * Conference two specified connections. Invoked when the user has made a request to merge the
1204 * specified connections into a conference call. In response, the connection service should
1205 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001206 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001207 * @param connection1 A connection to merge into a conference call.
1208 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001209 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001210 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001211
Santos Cordona663f862014-10-29 13:49:58 -07001212 /**
1213 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1214 * When this method is invoked, this {@link ConnectionService} should create its own
1215 * representation of the conference call and send it to telecom using {@link #addConference}.
1216 * <p>
1217 * This is only relevant to {@link ConnectionService}s which are registered with
1218 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1219 *
1220 * @param conference The remote conference call.
1221 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001222 public void onRemoteConferenceAdded(RemoteConference conference) {}
1223
Santos Cordon823fd3c2014-08-07 18:35:18 -07001224 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001225 * Called when an existing connection is added remotely.
1226 * @param connection The existing connection which was added.
1227 */
1228 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1229
1230 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001231 * @hide
1232 */
1233 public boolean containsConference(Conference conference) {
1234 return mIdByConference.containsKey(conference);
1235 }
1236
Ihab Awadb8e85c72014-08-23 20:34:57 -07001237 /** {@hide} */
1238 void addRemoteConference(RemoteConference remoteConference) {
1239 onRemoteConferenceAdded(remoteConference);
1240 }
1241
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001242 /** {@hide} */
1243 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1244 onRemoteExistingConnectionAdded(remoteConnection);
1245 }
1246
Ihab Awad5d0410f2014-07-30 10:07:40 -07001247 private void onAccountsInitialized() {
1248 mAreAccountsInitialized = true;
1249 for (Runnable r : mPreInitializationConnectionRequests) {
1250 r.run();
1251 }
1252 mPreInitializationConnectionRequests.clear();
1253 }
1254
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001255 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001256 * Adds an existing connection to the list of connections, identified by a new call ID unique
1257 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001258 *
1259 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001260 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001261 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001262 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1263 String id;
1264 if (handle == null) {
1265 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1266 // so just use a random UUID.
1267 id = UUID.randomUUID().toString();
1268 } else {
1269 // Phone account handle was provided, so use the ConnectionService class name as a
1270 // prefix for a unique incremental call ID.
1271 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1272 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001273 addConnection(id, connection);
1274 return id;
1275 }
1276
Ihab Awad542e0ea2014-05-16 10:22:16 -07001277 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001278 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001279 mConnectionById.put(callId, connection);
1280 mIdByConnection.put(connection, callId);
1281 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001282 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001283 }
1284
Anthony Lee30e65842014-11-06 16:30:53 -08001285 /** {@hide} */
1286 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001287 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001288 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001289 connection.removeConnectionListener(mConnectionListener);
1290 mConnectionById.remove(mIdByConnection.get(connection));
1291 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001292 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001293 }
1294
Santos Cordon823fd3c2014-08-07 18:35:18 -07001295 private String addConferenceInternal(Conference conference) {
1296 if (mIdByConference.containsKey(conference)) {
1297 Log.w(this, "Re-adding an existing conference: %s.", conference);
1298 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001299 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1300 // cannot determine a ConnectionService class name to associate with the ID, so use
1301 // a unique UUID (for now).
Santos Cordon823fd3c2014-08-07 18:35:18 -07001302 String id = UUID.randomUUID().toString();
1303 mConferenceById.put(id, conference);
1304 mIdByConference.put(conference, id);
1305 conference.addListener(mConferenceListener);
1306 return id;
1307 }
1308
1309 return null;
1310 }
1311
1312 private void removeConference(Conference conference) {
1313 if (mIdByConference.containsKey(conference)) {
1314 conference.removeListener(mConferenceListener);
1315
1316 String id = mIdByConference.get(conference);
1317 mConferenceById.remove(id);
1318 mIdByConference.remove(conference);
1319 mAdapter.removeCall(id);
1320 }
1321 }
1322
Ihab Awad542e0ea2014-05-16 10:22:16 -07001323 private Connection findConnectionForAction(String callId, String action) {
1324 if (mConnectionById.containsKey(callId)) {
1325 return mConnectionById.get(callId);
1326 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001327 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001328 return getNullConnection();
1329 }
1330
1331 static synchronized Connection getNullConnection() {
1332 if (sNullConnection == null) {
1333 sNullConnection = new Connection() {};
1334 }
1335 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001336 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001337
1338 private Conference findConferenceForAction(String conferenceId, String action) {
1339 if (mConferenceById.containsKey(conferenceId)) {
1340 return mConferenceById.get(conferenceId);
1341 }
1342 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1343 return getNullConference();
1344 }
1345
Ihab Awadb8e85c72014-08-23 20:34:57 -07001346 private List<String> createConnectionIdList(List<Connection> connections) {
1347 List<String> ids = new ArrayList<>();
1348 for (Connection c : connections) {
1349 if (mIdByConnection.containsKey(c)) {
1350 ids.add(mIdByConnection.get(c));
1351 }
1352 }
1353 Collections.sort(ids);
1354 return ids;
1355 }
1356
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001357 /**
1358 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001359 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001360 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001361 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001362 * @return List of string conference and call Ids.
1363 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001364 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001365 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001366 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001367 // Only allow Connection and Conference conferenceables.
1368 if (c instanceof Connection) {
1369 Connection connection = (Connection) c;
1370 if (mIdByConnection.containsKey(connection)) {
1371 ids.add(mIdByConnection.get(connection));
1372 }
1373 } else if (c instanceof Conference) {
1374 Conference conference = (Conference) c;
1375 if (mIdByConference.containsKey(conference)) {
1376 ids.add(mIdByConference.get(conference));
1377 }
1378 }
1379 }
1380 Collections.sort(ids);
1381 return ids;
1382 }
1383
Santos Cordon0159ac02014-08-21 14:28:11 -07001384 private Conference getNullConference() {
1385 if (sNullConference == null) {
1386 sNullConference = new Conference(null) {};
1387 }
1388 return sNullConference;
1389 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001390
1391 private void endAllConnections() {
1392 // Unbound from telecomm. We should end all connections and conferences.
1393 for (Connection connection : mIdByConnection.keySet()) {
1394 // only operate on top-level calls. Conference calls will be removed on their own.
1395 if (connection.getConference() == null) {
1396 connection.onDisconnect();
1397 }
1398 }
1399 for (Conference conference : mIdByConference.keySet()) {
1400 conference.onDisconnect();
1401 }
1402 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001403
1404 /**
1405 * Retrieves the next call ID as maintainted by the connection service.
1406 *
1407 * @return The call ID.
1408 */
1409 private int getNextCallId() {
1410 synchronized(mIdSyncRoot) {
1411 return ++mId;
1412 }
1413 }
Santos Cordon980acb92014-05-31 10:31:19 -07001414}