blob: f7822325c4cb40c53ada2e3a8b644dea120ab231 [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;
Tyler Gunndee56a82016-03-23 16:06:34 -0700108 private static final int MSG_ON_EXTRAS_CHANGED = 24;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700109
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700110 private static Connection sNullConnection;
111
mike dooley95e80702014-09-18 14:07:52 -0700112 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
113 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
114 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
115 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700116 private final RemoteConnectionManager mRemoteConnectionManager =
117 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700118 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700119 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700120
Santos Cordon823fd3c2014-08-07 18:35:18 -0700121 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700122 private Conference sNullConference;
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700123 private Object mIdSyncRoot = new Object();
124 private int mId = 0;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700125
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700126 private final IBinder mBinder = new IConnectionService.Stub() {
127 @Override
128 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700129 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
130 }
131
132 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
133 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700134 }
135
136 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700137 public void createConnection(
138 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700139 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700140 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700141 boolean isIncoming,
142 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700143 SomeArgs args = SomeArgs.obtain();
144 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700145 args.arg2 = id;
146 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700147 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700148 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700149 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700150 }
151
152 @Override
153 public void abort(String callId) {
154 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
155 }
156
157 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700158 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700159 SomeArgs args = SomeArgs.obtain();
160 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700161 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700162 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
163 }
164
165 @Override
166 public void answer(String callId) {
167 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700168 }
169
170 @Override
171 public void reject(String callId) {
172 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
173 }
174
175 @Override
Bryce Lee81901682015-08-28 16:38:02 -0700176 public void rejectWithMessage(String callId, String message) {
177 SomeArgs args = SomeArgs.obtain();
178 args.arg1 = callId;
179 args.arg2 = message;
180 mHandler.obtainMessage(MSG_REJECT_WITH_MESSAGE, args).sendToTarget();
181 }
182
183 @Override
Bryce Leecac50772015-11-17 15:13:29 -0800184 public void silence(String callId) {
185 mHandler.obtainMessage(MSG_SILENCE, callId).sendToTarget();
186 }
187
188 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700189 public void disconnect(String callId) {
190 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
191 }
192
193 @Override
194 public void hold(String callId) {
195 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
196 }
197
198 @Override
199 public void unhold(String callId) {
200 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
201 }
202
203 @Override
Yorke Lee4af59352015-05-13 14:14:54 -0700204 public void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700205 SomeArgs args = SomeArgs.obtain();
206 args.arg1 = callId;
Yorke Lee4af59352015-05-13 14:14:54 -0700207 args.arg2 = callAudioState;
208 mHandler.obtainMessage(MSG_ON_CALL_AUDIO_STATE_CHANGED, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700209 }
210
211 @Override
212 public void playDtmfTone(String callId, char digit) {
213 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
214 }
215
216 @Override
217 public void stopDtmfTone(String callId) {
218 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
219 }
220
221 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700222 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700223 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700224 args.arg1 = callId1;
225 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700226 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
227 }
228
229 @Override
230 public void splitFromConference(String callId) {
231 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
232 }
233
234 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700235 public void mergeConference(String callId) {
236 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
237 }
238
239 @Override
240 public void swapConference(String callId) {
241 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
242 }
243
244 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700245 public void onPostDialContinue(String callId, boolean proceed) {
246 SomeArgs args = SomeArgs.obtain();
247 args.arg1 = callId;
248 args.argi1 = proceed ? 1 : 0;
249 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
250 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700251
252 @Override
253 public void pullExternalCall(String callId) {
254 mHandler.obtainMessage(MSG_PULL_EXTERNAL_CALL, callId).sendToTarget();
255 }
256
257 @Override
258 public void sendCallEvent(String callId, String event, Bundle extras) {
259 SomeArgs args = SomeArgs.obtain();
260 args.arg1 = callId;
261 args.arg2 = event;
262 args.arg3 = extras;
263 mHandler.obtainMessage(MSG_SEND_CALL_EVENT, args).sendToTarget();
264 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700265
266 @Override
267 public void onExtrasChanged(String callId, Bundle extras) {
268 SomeArgs args = SomeArgs.obtain();
269 args.arg1 = callId;
270 args.arg2 = extras;
271 mHandler.obtainMessage(MSG_ON_EXTRAS_CHANGED, args).sendToTarget();
272 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700273 };
274
275 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
276 @Override
277 public void handleMessage(Message msg) {
278 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700279 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700280 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
281 onAdapterAttached();
282 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700283 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
284 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
285 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700286 case MSG_CREATE_CONNECTION: {
287 SomeArgs args = (SomeArgs) msg.obj;
288 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700289 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700290 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700291 final String id = (String) args.arg2;
292 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700293 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700294 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700295 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700296 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700297 mPreInitializationConnectionRequests.add(new Runnable() {
298 @Override
299 public void run() {
300 createConnection(
301 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700302 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700303 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700304 isIncoming,
305 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700306 }
307 });
308 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700309 createConnection(
310 connectionManagerPhoneAccount,
311 id,
312 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700313 isIncoming,
314 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700315 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700316 } finally {
317 args.recycle();
318 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700319 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700320 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700321 case MSG_ABORT:
322 abort((String) msg.obj);
323 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700324 case MSG_ANSWER:
325 answer((String) msg.obj);
326 break;
327 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700328 SomeArgs args = (SomeArgs) msg.obj;
329 try {
330 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700331 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700332 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700333 } finally {
334 args.recycle();
335 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700336 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700337 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700338 case MSG_REJECT:
339 reject((String) msg.obj);
340 break;
Bryce Lee81901682015-08-28 16:38:02 -0700341 case MSG_REJECT_WITH_MESSAGE: {
342 SomeArgs args = (SomeArgs) msg.obj;
343 try {
344 reject((String) args.arg1, (String) args.arg2);
345 } finally {
346 args.recycle();
347 }
348 break;
349 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700350 case MSG_DISCONNECT:
351 disconnect((String) msg.obj);
352 break;
Bryce Leecac50772015-11-17 15:13:29 -0800353 case MSG_SILENCE:
354 silence((String) msg.obj);
355 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700356 case MSG_HOLD:
357 hold((String) msg.obj);
358 break;
359 case MSG_UNHOLD:
360 unhold((String) msg.obj);
361 break;
Yorke Lee4af59352015-05-13 14:14:54 -0700362 case MSG_ON_CALL_AUDIO_STATE_CHANGED: {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700363 SomeArgs args = (SomeArgs) msg.obj;
364 try {
365 String callId = (String) args.arg1;
Yorke Lee4af59352015-05-13 14:14:54 -0700366 CallAudioState audioState = (CallAudioState) args.arg2;
367 onCallAudioStateChanged(callId, new CallAudioState(audioState));
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700368 } finally {
369 args.recycle();
370 }
371 break;
372 }
373 case MSG_PLAY_DTMF_TONE:
374 playDtmfTone((String) msg.obj, (char) msg.arg1);
375 break;
376 case MSG_STOP_DTMF_TONE:
377 stopDtmfTone((String) msg.obj);
378 break;
379 case MSG_CONFERENCE: {
380 SomeArgs args = (SomeArgs) msg.obj;
381 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700382 String callId1 = (String) args.arg1;
383 String callId2 = (String) args.arg2;
384 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700385 } finally {
386 args.recycle();
387 }
388 break;
389 }
390 case MSG_SPLIT_FROM_CONFERENCE:
391 splitFromConference((String) msg.obj);
392 break;
Santos Cordona4868042014-09-04 17:39:22 -0700393 case MSG_MERGE_CONFERENCE:
394 mergeConference((String) msg.obj);
395 break;
396 case MSG_SWAP_CONFERENCE:
397 swapConference((String) msg.obj);
398 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700399 case MSG_ON_POST_DIAL_CONTINUE: {
400 SomeArgs args = (SomeArgs) msg.obj;
401 try {
402 String callId = (String) args.arg1;
403 boolean proceed = (args.argi1 == 1);
404 onPostDialContinue(callId, proceed);
405 } finally {
406 args.recycle();
407 }
408 break;
409 }
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700410 case MSG_PULL_EXTERNAL_CALL: {
411 pullExternalCall((String) msg.obj);
412 break;
413 }
414 case MSG_SEND_CALL_EVENT: {
415 SomeArgs args = (SomeArgs) msg.obj;
416 try {
417 String callId = (String) args.arg1;
418 String event = (String) args.arg2;
419 Bundle extras = (Bundle) args.arg3;
420 sendCallEvent(callId, event, extras);
421 } finally {
422 args.recycle();
423 }
424 break;
425 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700426 case MSG_ON_EXTRAS_CHANGED: {
427 SomeArgs args = (SomeArgs) msg.obj;
428 try {
429 String callId = (String) args.arg1;
430 Bundle extras = (Bundle) args.arg2;
431 handleExtrasChanged(callId, extras);
432 } finally {
433 args.recycle();
434 }
435 break;
436 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700437 default:
438 break;
439 }
440 }
441 };
442
Santos Cordon823fd3c2014-08-07 18:35:18 -0700443 private final Conference.Listener mConferenceListener = new Conference.Listener() {
444 @Override
445 public void onStateChanged(Conference conference, int oldState, int newState) {
446 String id = mIdByConference.get(conference);
447 switch (newState) {
448 case Connection.STATE_ACTIVE:
449 mAdapter.setActive(id);
450 break;
451 case Connection.STATE_HOLDING:
452 mAdapter.setOnHold(id);
453 break;
454 case Connection.STATE_DISCONNECTED:
455 // handled by onDisconnected
456 break;
457 }
458 }
459
460 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700461 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700462 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700463 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700464 }
465
466 @Override
467 public void onConnectionAdded(Conference conference, Connection connection) {
468 }
469
470 @Override
471 public void onConnectionRemoved(Conference conference, Connection connection) {
472 }
473
474 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700475 public void onConferenceableConnectionsChanged(
476 Conference conference, List<Connection> conferenceableConnections) {
477 mAdapter.setConferenceableConnections(
478 mIdByConference.get(conference),
479 createConnectionIdList(conferenceableConnections));
480 }
481
482 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700483 public void onDestroyed(Conference conference) {
484 removeConference(conference);
485 }
486
487 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800488 public void onConnectionCapabilitiesChanged(
489 Conference conference,
490 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700491 String id = mIdByConference.get(conference);
492 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800493 Connection.capabilitiesToString(connectionCapabilities));
494 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700495 }
Rekha Kumar07366812015-03-24 16:42:31 -0700496
497 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700498 public void onConnectionPropertiesChanged(
499 Conference conference,
500 int connectionProperties) {
501 String id = mIdByConference.get(conference);
502 Log.d(this, "call capabilities: conference: %s",
503 Connection.propertiesToString(connectionProperties));
504 mAdapter.setConnectionProperties(id, connectionProperties);
505 }
506
507 @Override
Rekha Kumar07366812015-03-24 16:42:31 -0700508 public void onVideoStateChanged(Conference c, int videoState) {
509 String id = mIdByConference.get(c);
510 Log.d(this, "onVideoStateChanged set video state %d", videoState);
511 mAdapter.setVideoState(id, videoState);
512 }
513
514 @Override
515 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
516 String id = mIdByConference.get(c);
517 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
518 videoProvider);
519 mAdapter.setVideoProvider(id, videoProvider);
520 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700521
522 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700523 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
524 String id = mIdByConference.get(conference);
Tyler Gunndee56a82016-03-23 16:06:34 -0700525 if (id != null) {
526 mAdapter.setStatusHints(id, statusHints);
527 }
Andrew Leeedc625f2015-04-14 13:38:12 -0700528 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700529
530 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700531 public void onExtrasChanged(Conference c, Bundle extras) {
532 String id = mIdByConference.get(c);
533 if (id != null) {
534 mAdapter.putExtras(id, extras);
535 }
536 }
537
538 @Override
539 public void onExtrasRemoved(Conference c, List<String> keys) {
540 String id = mIdByConference.get(c);
541 if (id != null) {
542 mAdapter.removeExtras(id, keys);
543 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700544 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700545 };
546
Ihab Awad542e0ea2014-05-16 10:22:16 -0700547 private final Connection.Listener mConnectionListener = new Connection.Listener() {
548 @Override
549 public void onStateChanged(Connection c, int state) {
550 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700551 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700552 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700553 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700554 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700555 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700556 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700557 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700558 break;
Tyler Gunnc96b5e02016-07-07 22:53:57 -0700559 case Connection.STATE_PULLING_CALL:
560 mAdapter.setPulling(id);
561 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700562 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700563 // Handled in onDisconnected()
564 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700565 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700566 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700567 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700568 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700569 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700570 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700571 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700572 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700573 break;
574 }
575 }
576
577 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700578 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700579 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700580 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700581 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700582 }
583
584 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700585 public void onVideoStateChanged(Connection c, int videoState) {
586 String id = mIdByConnection.get(c);
587 Log.d(this, "Adapter set video state %d", videoState);
588 mAdapter.setVideoState(id, videoState);
589 }
590
591 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700592 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700593 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700594 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700595 }
596
597 @Override
598 public void onCallerDisplayNameChanged(
599 Connection c, String callerDisplayName, int presentation) {
600 String id = mIdByConnection.get(c);
601 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700602 }
603
604 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700605 public void onDestroyed(Connection c) {
606 removeConnection(c);
607 }
Ihab Awadf8358972014-05-28 16:46:42 -0700608
609 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700610 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700611 String id = mIdByConnection.get(c);
612 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700613 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700614 }
615
616 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800617 public void onPostDialChar(Connection c, char nextChar) {
618 String id = mIdByConnection.get(c);
619 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
620 mAdapter.onPostDialChar(id, nextChar);
621 }
622
623 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700624 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700625 String id = mIdByConnection.get(c);
626 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700627 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700628 }
Santos Cordonb6939982014-06-04 20:20:58 -0700629
630 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800631 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700632 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700633 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800634 Connection.capabilitiesToString(capabilities));
635 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700636 }
637
Santos Cordonb6939982014-06-04 20:20:58 -0700638 @Override
Tyler Gunn720c6642016-03-22 09:02:47 -0700639 public void onConnectionPropertiesChanged(Connection c, int properties) {
640 String id = mIdByConnection.get(c);
641 Log.d(this, "properties: parcelableconnection: %s",
642 Connection.propertiesToString(properties));
643 mAdapter.setConnectionProperties(id, properties);
644 }
645
646 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700647 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700648 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700649 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
650 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700651 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700652 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700653
654 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700655 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700656 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700657 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700658 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700659
660 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700661 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700662 String id = mIdByConnection.get(c);
663 mAdapter.setStatusHints(id, statusHints);
664 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700665
666 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800667 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700668 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700669 mAdapter.setConferenceableConnections(
670 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800671 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700672 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700673
674 @Override
675 public void onConferenceChanged(Connection connection, Conference conference) {
676 String id = mIdByConnection.get(connection);
677 if (id != null) {
678 String conferenceId = null;
679 if (conference != null) {
680 conferenceId = mIdByConference.get(conference);
681 }
682 mAdapter.setIsConferenced(id, conferenceId);
683 }
684 }
Anthony Lee17455a32015-04-24 15:25:29 -0700685
686 @Override
687 public void onConferenceMergeFailed(Connection connection) {
688 String id = mIdByConnection.get(connection);
689 if (id != null) {
690 mAdapter.onConferenceMergeFailed(id);
691 }
692 }
Santos Cordon6b7f9552015-05-27 17:21:45 -0700693
694 @Override
Tyler Gunndee56a82016-03-23 16:06:34 -0700695 public void onExtrasChanged(Connection c, Bundle extras) {
696 String id = mIdByConnection.get(c);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700697 if (id != null) {
Tyler Gunndee56a82016-03-23 16:06:34 -0700698 mAdapter.putExtras(id, extras);
Santos Cordon6b7f9552015-05-27 17:21:45 -0700699 }
700 }
Tyler Gunndee56a82016-03-23 16:06:34 -0700701
702 public void onExtrasRemoved(Connection c, List<String> keys) {
703 String id = mIdByConnection.get(c);
704 if (id != null) {
705 mAdapter.removeExtras(id, keys);
706 }
707 }
708
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800709
710 @Override
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700711 public void onConnectionEvent(Connection connection, String event, Bundle extras) {
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800712 String id = mIdByConnection.get(connection);
713 if (id != null) {
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700714 mAdapter.onConnectionEvent(id, event, extras);
Tyler Gunnbd1eb1f2016-02-16 14:36:20 -0800715 }
716 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700717 };
718
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700719 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700720 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700721 public final IBinder onBind(Intent intent) {
722 return mBinder;
723 }
724
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700725 /** {@inheritDoc} */
726 @Override
727 public boolean onUnbind(Intent intent) {
728 endAllConnections();
729 return super.onUnbind(intent);
730 }
731
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700732 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700733 * This can be used by telecom to either create a new outgoing call or attach to an existing
734 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700735 * createConnection util a connection service cancels the process or completes it successfully.
736 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700737 private void createConnection(
738 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700739 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700740 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700741 boolean isIncoming,
742 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700743 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700744 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request,
745 isIncoming,
Yorke Leec3cf9822014-10-02 09:38:39 -0700746 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700747
Yorke Leec3cf9822014-10-02 09:38:39 -0700748 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
749 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700750 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700751 Log.d(this, "createConnection, connection: %s", connection);
752 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700753 connection = Connection.createFailedConnection(
754 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700755 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700756
Tyler Gunnf0500bd2015-09-01 10:59:48 -0700757 connection.setTelecomCallId(callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700758 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700759 addConnection(callId, connection);
760 }
761
Andrew Lee100e2932014-09-08 15:34:24 -0700762 Uri address = connection.getAddress();
763 String number = address == null ? "null" : address.getSchemeSpecificPart();
Tyler Gunn720c6642016-03-22 09:02:47 -0700764 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s, properties: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700765 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700766 Connection.stateToString(connection.getState()),
Tyler Gunn720c6642016-03-22 09:02:47 -0700767 Connection.capabilitiesToString(connection.getConnectionCapabilities()),
768 Connection.propertiesToString(connection.getConnectionProperties()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700769
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700770 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700771 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700772 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700773 request,
774 new ParcelableConnection(
775 request.getAccountHandle(),
776 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800777 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -0700778 connection.getConnectionProperties(),
Christine Hallstrom4e22d6d2016-11-30 16:06:42 -0800779 connection.getSupportedAudioRoutes(),
Andrew Lee100e2932014-09-08 15:34:24 -0700780 connection.getAddress(),
781 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700782 connection.getCallerDisplayName(),
783 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700784 connection.getVideoProvider() == null ?
785 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700786 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700787 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700788 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -0700789 connection.getConnectTimeMillis(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700790 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700791 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -0700792 createIdList(connection.getConferenceables()),
793 connection.getExtras()));
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -0800794 if (isUnknown) {
795 triggerConferenceRecalculate();
796 }
Evan Charltonbf11f982014-07-20 22:06:28 -0700797 }
798
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700799 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700800 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700801 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700802 }
803
Tyler Gunnbe74de02014-08-29 14:51:48 -0700804 private void answerVideo(String callId, int videoState) {
805 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700806 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700807 }
808
Tyler Gunnbe74de02014-08-29 14:51:48 -0700809 private void answer(String callId) {
810 Log.d(this, "answer %s", callId);
811 findConnectionForAction(callId, "answer").onAnswer();
812 }
813
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700814 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700815 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700816 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700817 }
818
Bryce Lee81901682015-08-28 16:38:02 -0700819 private void reject(String callId, String rejectWithMessage) {
820 Log.d(this, "reject %s with message", callId);
821 findConnectionForAction(callId, "reject").onReject(rejectWithMessage);
822 }
823
Bryce Leecac50772015-11-17 15:13:29 -0800824 private void silence(String callId) {
825 Log.d(this, "silence %s", callId);
826 findConnectionForAction(callId, "silence").onSilence();
827 }
828
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700829 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700830 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700831 if (mConnectionById.containsKey(callId)) {
832 findConnectionForAction(callId, "disconnect").onDisconnect();
833 } else {
834 findConferenceForAction(callId, "disconnect").onDisconnect();
835 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700836 }
837
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700838 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700839 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700840 if (mConnectionById.containsKey(callId)) {
841 findConnectionForAction(callId, "hold").onHold();
842 } else {
843 findConferenceForAction(callId, "hold").onHold();
844 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700845 }
846
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700847 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700848 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700849 if (mConnectionById.containsKey(callId)) {
850 findConnectionForAction(callId, "unhold").onUnhold();
851 } else {
852 findConferenceForAction(callId, "unhold").onUnhold();
853 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700854 }
855
Yorke Lee4af59352015-05-13 14:14:54 -0700856 private void onCallAudioStateChanged(String callId, CallAudioState callAudioState) {
857 Log.d(this, "onAudioStateChanged %s %s", callId, callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700858 if (mConnectionById.containsKey(callId)) {
Yorke Lee4af59352015-05-13 14:14:54 -0700859 findConnectionForAction(callId, "onCallAudioStateChanged").setCallAudioState(
860 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700861 } else {
Yorke Lee4af59352015-05-13 14:14:54 -0700862 findConferenceForAction(callId, "onCallAudioStateChanged").setCallAudioState(
863 callAudioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700864 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700865 }
866
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700867 private void playDtmfTone(String callId, char digit) {
868 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700869 if (mConnectionById.containsKey(callId)) {
870 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
871 } else {
872 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
873 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700874 }
875
876 private void stopDtmfTone(String callId) {
877 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700878 if (mConnectionById.containsKey(callId)) {
879 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
880 } else {
881 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
882 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700883 }
884
Santos Cordon823fd3c2014-08-07 18:35:18 -0700885 private void conference(String callId1, String callId2) {
886 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700887
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800888 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700889 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800890 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700891 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800892 conference2 = findConferenceForAction(callId2, "conference");
893 if (conference2 == getNullConference()) {
894 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
895 callId2);
896 return;
897 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700898 }
Santos Cordonb6939982014-06-04 20:20:58 -0700899
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800900 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700901 Connection connection1 = findConnectionForAction(callId1, "conference");
902 if (connection1 == getNullConnection()) {
903 Conference conference1 = findConferenceForAction(callId1, "addConnection");
904 if (conference1 == getNullConference()) {
905 Log.w(this,
906 "Connection1 or Conference1 missing in conference request %s.",
907 callId1);
908 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800909 // Call 1 is a conference.
910 if (connection2 != getNullConnection()) {
911 // Call 2 is a connection so merge via call 1 (conference).
912 conference1.onMerge(connection2);
913 } else {
914 // Call 2 is ALSO a conference; this should never happen.
915 Log.wtf(this, "There can only be one conference and an attempt was made to " +
916 "merge two conferences.");
917 return;
918 }
Ihab Awad50e35062014-09-30 09:17:03 -0700919 }
920 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800921 // Call 1 is a connection.
922 if (conference2 != getNullConference()) {
923 // Call 2 is a conference, so merge via call 2.
924 conference2.onMerge(connection1);
925 } else {
926 // Call 2 is a connection, so merge together.
927 onConference(connection1, connection2);
928 }
Ihab Awad50e35062014-09-30 09:17:03 -0700929 }
Santos Cordon980acb92014-05-31 10:31:19 -0700930 }
931
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700932 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700933 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700934
935 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700936 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700937 Log.w(this, "Connection missing in conference request %s.", callId);
938 return;
939 }
940
Santos Cordon0159ac02014-08-21 14:28:11 -0700941 Conference conference = connection.getConference();
942 if (conference != null) {
943 conference.onSeparate(connection);
944 }
Santos Cordon980acb92014-05-31 10:31:19 -0700945 }
946
Santos Cordona4868042014-09-04 17:39:22 -0700947 private void mergeConference(String callId) {
948 Log.d(this, "mergeConference(%s)", callId);
949 Conference conference = findConferenceForAction(callId, "mergeConference");
950 if (conference != null) {
951 conference.onMerge();
952 }
953 }
954
955 private void swapConference(String callId) {
956 Log.d(this, "swapConference(%s)", callId);
957 Conference conference = findConferenceForAction(callId, "swapConference");
958 if (conference != null) {
959 conference.onSwap();
960 }
961 }
962
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700963 /**
964 * Notifies a {@link Connection} of a request to pull an external call.
965 *
966 * See {@link Call#pullExternalCall()}.
967 *
968 * @param callId The ID of the call to pull.
969 */
970 private void pullExternalCall(String callId) {
971 Log.d(this, "pullExternalCall(%s)", callId);
972 Connection connection = findConnectionForAction(callId, "pullExternalCall");
973 if (connection != null) {
974 connection.onPullExternalCall();
975 }
976 }
977
978 /**
979 * Notifies a {@link Connection} of a call event.
980 *
981 * See {@link Call#sendCallEvent(String, Bundle)}.
982 *
983 * @param callId The ID of the call receiving the event.
984 * @param event The event.
985 * @param extras Extras associated with the event.
986 */
987 private void sendCallEvent(String callId, String event, Bundle extras) {
988 Log.d(this, "sendCallEvent(%s, %s)", callId, event);
989 Connection connection = findConnectionForAction(callId, "sendCallEvent");
990 if (connection != null) {
991 connection.onCallEvent(event, extras);
992 }
993
994 }
995
Tyler Gunndee56a82016-03-23 16:06:34 -0700996 /**
997 * Notifies a {@link Connection} or {@link Conference} of a change to the extras from Telecom.
998 * <p>
999 * These extra changes can originate from Telecom itself, or from an {@link InCallService} via
1000 * the {@link android.telecom.Call#putExtra(String, boolean)},
1001 * {@link android.telecom.Call#putExtra(String, int)},
1002 * {@link android.telecom.Call#putExtra(String, String)},
1003 * {@link Call#removeExtras(List)}.
1004 *
1005 * @param callId The ID of the call receiving the event.
1006 * @param extras The new extras bundle.
1007 */
1008 private void handleExtrasChanged(String callId, Bundle extras) {
1009 Log.d(this, "handleExtrasChanged(%s, %s)", callId, extras);
1010 if (mConnectionById.containsKey(callId)) {
1011 findConnectionForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1012 } else if (mConferenceById.containsKey(callId)) {
1013 findConferenceForAction(callId, "handleExtrasChanged").handleExtrasChanged(extras);
1014 }
1015 }
1016
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001017 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001018 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001019 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -07001020 }
1021
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001022 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001023 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001024 // No need to query again if we already did it.
1025 return;
1026 }
1027
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001028 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -07001029 @Override
1030 public void onResult(
1031 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001032 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001033 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -07001034 @Override
1035 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001036 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -07001037 mRemoteConnectionManager.addConnectionService(
1038 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -07001039 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -07001040 }
Ihab Awad5d0410f2014-07-30 10:07:40 -07001041 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001042 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -07001043 }
1044 });
1045 }
1046
1047 @Override
1048 public void onError() {
1049 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -07001050 @Override
1051 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -07001052 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -07001053 }
1054 });
1055 }
1056 });
1057 }
1058
Ihab Awadf8b69882014-07-25 15:14:01 -07001059 /**
1060 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001061 * incoming request. This is used by {@code ConnectionService}s that are registered with
1062 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
1063 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001064 *
1065 * @param connectionManagerPhoneAccount See description at
1066 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1067 * @param request Details about the incoming call.
1068 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1069 * not handle the call.
1070 */
1071 public final RemoteConnection createRemoteIncomingConnection(
1072 PhoneAccountHandle connectionManagerPhoneAccount,
1073 ConnectionRequest request) {
1074 return mRemoteConnectionManager.createRemoteConnection(
1075 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -07001076 }
1077
1078 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001079 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -07001080 * outgoing request. This is used by {@code ConnectionService}s that are registered with
1081 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
1082 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -07001083 *
1084 * @param connectionManagerPhoneAccount See description at
1085 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1086 * @param request Details about the incoming call.
1087 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1088 * not handle the call.
1089 */
1090 public final RemoteConnection createRemoteOutgoingConnection(
1091 PhoneAccountHandle connectionManagerPhoneAccount,
1092 ConnectionRequest request) {
1093 return mRemoteConnectionManager.createRemoteConnection(
1094 connectionManagerPhoneAccount, request, false);
1095 }
1096
1097 /**
Santos Cordona663f862014-10-29 13:49:58 -07001098 * Indicates to the relevant {@code RemoteConnectionService} that the specified
1099 * {@link RemoteConnection}s should be merged into a conference call.
1100 * <p>
1101 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
1102 * be invoked.
1103 *
1104 * @param remoteConnection1 The first of the remote connections to conference.
1105 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -07001106 */
1107 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -07001108 RemoteConnection remoteConnection1,
1109 RemoteConnection remoteConnection2) {
1110 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -07001111 }
1112
1113 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001114 * Adds a new conference call. When a conference call is created either as a result of an
1115 * explicit request via {@link #onConference} or otherwise, the connection service should supply
1116 * an instance of {@link Conference} by invoking this method. A conference call provided by this
1117 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
1118 *
1119 * @param conference The new conference object.
1120 */
1121 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -07001122 Log.d(this, "addConference: conference=%s", conference);
1123
Santos Cordon823fd3c2014-08-07 18:35:18 -07001124 String id = addConferenceInternal(conference);
1125 if (id != null) {
1126 List<String> connectionIds = new ArrayList<>(2);
1127 for (Connection connection : conference.getConnections()) {
1128 if (mIdByConnection.containsKey(connection)) {
1129 connectionIds.add(mIdByConnection.get(connection));
1130 }
1131 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001132 conference.setTelecomCallId(id);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001133 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -07001134 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -07001135 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001136 conference.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001137 conference.getConnectionProperties(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -08001138 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -07001139 conference.getVideoProvider() == null ?
1140 null : conference.getVideoProvider().getInterface(),
1141 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -07001142 conference.getConnectTimeMillis(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001143 conference.getStatusHints(),
1144 conference.getExtras());
Andrew Lee0f51da32015-04-16 13:11:55 -07001145
Santos Cordon823fd3c2014-08-07 18:35:18 -07001146 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -07001147 mAdapter.setVideoProvider(id, conference.getVideoProvider());
1148 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -07001149
1150 // Go through any child calls and set the parent.
1151 for (Connection connection : conference.getConnections()) {
1152 String connectionId = mIdByConnection.get(connection);
1153 if (connectionId != null) {
1154 mAdapter.setIsConferenced(connectionId, id);
1155 }
1156 }
1157 }
1158 }
1159
1160 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001161 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
1162 * connection.
1163 *
1164 * @param phoneAccountHandle The phone account handle for the connection.
1165 * @param connection The connection to add.
1166 */
1167 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
1168 Connection connection) {
1169
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001170 String id = addExistingConnectionInternal(phoneAccountHandle, connection);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001171 if (id != null) {
1172 List<String> emptyList = new ArrayList<>(0);
1173
1174 ParcelableConnection parcelableConnection = new ParcelableConnection(
1175 phoneAccountHandle,
1176 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -08001177 connection.getConnectionCapabilities(),
Tyler Gunn720c6642016-03-22 09:02:47 -07001178 connection.getConnectionProperties(),
Christine Hallstrom4e22d6d2016-11-30 16:06:42 -08001179 connection.getSupportedAudioRoutes(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001180 connection.getAddress(),
1181 connection.getAddressPresentation(),
1182 connection.getCallerDisplayName(),
1183 connection.getCallerDisplayNamePresentation(),
1184 connection.getVideoProvider() == null ?
1185 null : connection.getVideoProvider().getInterface(),
1186 connection.getVideoState(),
1187 connection.isRingbackRequested(),
1188 connection.getAudioModeIsVoip(),
Roshan Piuse927ec02015-07-15 15:47:21 -07001189 connection.getConnectTimeMillis(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001190 connection.getStatusHints(),
1191 connection.getDisconnectCause(),
Santos Cordon6b7f9552015-05-27 17:21:45 -07001192 emptyList,
1193 connection.getExtras());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001194 mAdapter.addExistingConnection(id, parcelableConnection);
1195 }
1196 }
1197
1198 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001199 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
1200 * has taken responsibility.
1201 *
1202 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -07001203 */
Sailesh Nepal091768c2014-06-30 15:15:23 -07001204 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -07001205 return mConnectionById.values();
1206 }
1207
1208 /**
Santos Cordona6018b92016-02-16 14:23:12 -08001209 * Returns all the active {@code Conference}s for which this {@code ConnectionService}
1210 * has taken responsibility.
1211 *
1212 * @return A collection of {@code Conference}s created by this {@code ConnectionService}.
1213 */
1214 public final Collection<Conference> getAllConferences() {
1215 return mConferenceById.values();
1216 }
1217
1218 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001219 * Create a {@code Connection} given an incoming request. This is used to attach to existing
1220 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -07001221 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001222 * @param connectionManagerPhoneAccount See description at
1223 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
1224 * @param request Details about the incoming call.
1225 * @return The {@code Connection} object to satisfy this call, or {@code null} to
1226 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -07001227 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001228 public Connection onCreateIncomingConnection(
1229 PhoneAccountHandle connectionManagerPhoneAccount,
1230 ConnectionRequest request) {
1231 return null;
1232 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001233
1234 /**
Shriram Ganesh6bf35ac2014-12-11 17:53:38 -08001235 * Trigger recalculate functinality for conference calls. This is used when a Telephony
1236 * Connection is part of a conference controller but is not yet added to Connection
1237 * Service and hence cannot be added to the conference call.
1238 *
1239 * @hide
1240 */
1241 public void triggerConferenceRecalculate() {
1242 }
1243
1244 /**
Ihab Awadf8b69882014-07-25 15:14:01 -07001245 * Create a {@code Connection} given an outgoing request. This is used to initiate new
1246 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001247 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001248 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1249 * this call.
1250 * <p>
1251 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1252 * has registered one or more {@code PhoneAccount}s having
1253 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1254 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1255 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1256 * making the connection.
1257 * <p>
1258 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1259 * being asked to make a direct connection. The
1260 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1261 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1262 * making the connection.
1263 * @param request Details about the outgoing call.
1264 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001265 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001266 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001267 public Connection onCreateOutgoingConnection(
1268 PhoneAccountHandle connectionManagerPhoneAccount,
1269 ConnectionRequest request) {
1270 return null;
1271 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001272
1273 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001274 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1275 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1276 * call created using
1277 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1278 *
1279 * @param connectionManagerPhoneAccount
1280 * @param request
1281 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001282 *
1283 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001284 */
1285 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1286 ConnectionRequest request) {
1287 return null;
1288 }
1289
1290 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001291 * Conference two specified connections. Invoked when the user has made a request to merge the
1292 * specified connections into a conference call. In response, the connection service should
1293 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001294 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001295 * @param connection1 A connection to merge into a conference call.
1296 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001297 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001298 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001299
Santos Cordona663f862014-10-29 13:49:58 -07001300 /**
1301 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1302 * When this method is invoked, this {@link ConnectionService} should create its own
1303 * representation of the conference call and send it to telecom using {@link #addConference}.
1304 * <p>
1305 * This is only relevant to {@link ConnectionService}s which are registered with
1306 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1307 *
1308 * @param conference The remote conference call.
1309 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001310 public void onRemoteConferenceAdded(RemoteConference conference) {}
1311
Santos Cordon823fd3c2014-08-07 18:35:18 -07001312 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001313 * Called when an existing connection is added remotely.
1314 * @param connection The existing connection which was added.
1315 */
1316 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1317
1318 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001319 * @hide
1320 */
1321 public boolean containsConference(Conference conference) {
1322 return mIdByConference.containsKey(conference);
1323 }
1324
Ihab Awadb8e85c72014-08-23 20:34:57 -07001325 /** {@hide} */
1326 void addRemoteConference(RemoteConference remoteConference) {
1327 onRemoteConferenceAdded(remoteConference);
1328 }
1329
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001330 /** {@hide} */
1331 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1332 onRemoteExistingConnectionAdded(remoteConnection);
1333 }
1334
Ihab Awad5d0410f2014-07-30 10:07:40 -07001335 private void onAccountsInitialized() {
1336 mAreAccountsInitialized = true;
1337 for (Runnable r : mPreInitializationConnectionRequests) {
1338 r.run();
1339 }
1340 mPreInitializationConnectionRequests.clear();
1341 }
1342
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001343 /**
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001344 * Adds an existing connection to the list of connections, identified by a new call ID unique
1345 * to this connection service.
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001346 *
1347 * @param connection The connection.
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001348 * @return The ID of the connection (e.g. the call-id).
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001349 */
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001350 private String addExistingConnectionInternal(PhoneAccountHandle handle, Connection connection) {
1351 String id;
Tyler Gunn2282bb92016-10-17 15:48:19 -07001352
1353 if (connection.getExtras() != null && connection.getExtras()
1354 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1355 id = connection.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1356 Log.d(this, "addExistingConnectionInternal - conn %s reusing original id %s",
1357 connection.getTelecomCallId(), id);
1358 } else if (handle == null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001359 // If no phone account handle was provided, we cannot be sure the call ID is unique,
1360 // so just use a random UUID.
1361 id = UUID.randomUUID().toString();
1362 } else {
1363 // Phone account handle was provided, so use the ConnectionService class name as a
1364 // prefix for a unique incremental call ID.
1365 id = handle.getComponentName().getClassName() + "@" + getNextCallId();
1366 }
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001367 addConnection(id, connection);
1368 return id;
1369 }
1370
Ihab Awad542e0ea2014-05-16 10:22:16 -07001371 private void addConnection(String callId, Connection connection) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001372 connection.setTelecomCallId(callId);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001373 mConnectionById.put(callId, connection);
1374 mIdByConnection.put(connection, callId);
1375 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001376 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001377 }
1378
Anthony Lee30e65842014-11-06 16:30:53 -08001379 /** {@hide} */
1380 protected void removeConnection(Connection connection) {
Santos Cordon823fd3c2014-08-07 18:35:18 -07001381 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001382 connection.removeConnectionListener(mConnectionListener);
Chenjie Luoe370b532016-05-12 16:59:43 -07001383 String id = mIdByConnection.get(connection);
1384 if (id != null) {
1385 mConnectionById.remove(id);
1386 mIdByConnection.remove(connection);
1387 mAdapter.removeCall(id);
1388 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001389 }
1390
Santos Cordon823fd3c2014-08-07 18:35:18 -07001391 private String addConferenceInternal(Conference conference) {
Tyler Gunn2282bb92016-10-17 15:48:19 -07001392 String originalId = null;
1393 if (conference.getExtras() != null && conference.getExtras()
1394 .containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1395 originalId = conference.getExtras().getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID);
1396 Log.d(this, "addConferenceInternal: conf %s reusing original id %s",
1397 conference.getTelecomCallId(),
1398 originalId);
1399 }
Santos Cordon823fd3c2014-08-07 18:35:18 -07001400 if (mIdByConference.containsKey(conference)) {
1401 Log.w(this, "Re-adding an existing conference: %s.", conference);
1402 } else if (conference != null) {
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001403 // Conferences do not (yet) have a PhoneAccountHandle associated with them, so we
1404 // cannot determine a ConnectionService class name to associate with the ID, so use
1405 // a unique UUID (for now).
Tyler Gunn2282bb92016-10-17 15:48:19 -07001406 String id = originalId == null ? UUID.randomUUID().toString() : originalId;
Santos Cordon823fd3c2014-08-07 18:35:18 -07001407 mConferenceById.put(id, conference);
1408 mIdByConference.put(conference, id);
1409 conference.addListener(mConferenceListener);
1410 return id;
1411 }
1412
1413 return null;
1414 }
1415
1416 private void removeConference(Conference conference) {
1417 if (mIdByConference.containsKey(conference)) {
1418 conference.removeListener(mConferenceListener);
1419
1420 String id = mIdByConference.get(conference);
1421 mConferenceById.remove(id);
1422 mIdByConference.remove(conference);
1423 mAdapter.removeCall(id);
1424 }
1425 }
1426
Ihab Awad542e0ea2014-05-16 10:22:16 -07001427 private Connection findConnectionForAction(String callId, String action) {
1428 if (mConnectionById.containsKey(callId)) {
1429 return mConnectionById.get(callId);
1430 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001431 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001432 return getNullConnection();
1433 }
1434
1435 static synchronized Connection getNullConnection() {
1436 if (sNullConnection == null) {
1437 sNullConnection = new Connection() {};
1438 }
1439 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001440 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001441
1442 private Conference findConferenceForAction(String conferenceId, String action) {
1443 if (mConferenceById.containsKey(conferenceId)) {
1444 return mConferenceById.get(conferenceId);
1445 }
1446 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1447 return getNullConference();
1448 }
1449
Ihab Awadb8e85c72014-08-23 20:34:57 -07001450 private List<String> createConnectionIdList(List<Connection> connections) {
1451 List<String> ids = new ArrayList<>();
1452 for (Connection c : connections) {
1453 if (mIdByConnection.containsKey(c)) {
1454 ids.add(mIdByConnection.get(c));
1455 }
1456 }
1457 Collections.sort(ids);
1458 return ids;
1459 }
1460
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001461 /**
1462 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001463 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001464 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001465 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001466 * @return List of string conference and call Ids.
1467 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001468 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001469 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001470 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001471 // Only allow Connection and Conference conferenceables.
1472 if (c instanceof Connection) {
1473 Connection connection = (Connection) c;
1474 if (mIdByConnection.containsKey(connection)) {
1475 ids.add(mIdByConnection.get(connection));
1476 }
1477 } else if (c instanceof Conference) {
1478 Conference conference = (Conference) c;
1479 if (mIdByConference.containsKey(conference)) {
1480 ids.add(mIdByConference.get(conference));
1481 }
1482 }
1483 }
1484 Collections.sort(ids);
1485 return ids;
1486 }
1487
Santos Cordon0159ac02014-08-21 14:28:11 -07001488 private Conference getNullConference() {
1489 if (sNullConference == null) {
1490 sNullConference = new Conference(null) {};
1491 }
1492 return sNullConference;
1493 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001494
1495 private void endAllConnections() {
1496 // Unbound from telecomm. We should end all connections and conferences.
1497 for (Connection connection : mIdByConnection.keySet()) {
1498 // only operate on top-level calls. Conference calls will be removed on their own.
1499 if (connection.getConference() == null) {
1500 connection.onDisconnect();
1501 }
1502 }
1503 for (Conference conference : mIdByConference.keySet()) {
1504 conference.onDisconnect();
1505 }
1506 }
Tyler Gunnf0500bd2015-09-01 10:59:48 -07001507
1508 /**
1509 * Retrieves the next call ID as maintainted by the connection service.
1510 *
1511 * @return The call ID.
1512 */
1513 private int getNextCallId() {
1514 synchronized(mIdSyncRoot) {
1515 return ++mId;
1516 }
1517 }
Santos Cordon980acb92014-05-31 10:31:19 -07001518}