blob: 6a7663fcf75218422823945cfd6d0756a65bc10e [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 Cordon52d8a152014-06-17 19:08:45 -070024import android.os.Handler;
25import android.os.IBinder;
26import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070027import android.os.Message;
Andrew Lee14185762014-07-25 09:41:56 -070028
Sailesh Nepal2a46b902014-07-04 17:21:07 -070029import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070030import com.android.internal.telecom.IConnectionService;
31import com.android.internal.telecom.IConnectionServiceAdapter;
32import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070033
Ihab Awad5d0410f2014-07-30 10:07:40 -070034import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070035import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070036import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070037import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070038import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070039import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070040import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070041
42/**
Santos Cordona663f862014-10-29 13:49:58 -070043 * {@code ConnectionService} is an abstract service that should be implemented by any app which can
44 * make phone calls and want those calls to be integrated into the built-in phone app.
45 * Once implemented, the {@code ConnectionService} needs two additional steps before it will be
46 * integrated into the phone app:
47 * <p>
48 * 1. <i>Registration in AndroidManifest.xml</i>
49 * <br/>
50 * <pre>
51 * &lt;service android:name="com.example.package.MyConnectionService"
52 * android:label="@string/some_label_for_my_connection_service"
Yorke Lee249c12e2015-05-13 15:59:29 -070053 * android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"&gt;
Santos Cordona663f862014-10-29 13:49:58 -070054 * &lt;intent-filter&gt;
55 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
56 * &lt;/intent-filter&gt;
57 * &lt;/service&gt;
58 * </pre>
59 * <p>
60 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
61 * <br/>
62 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
63 * <p>
64 * Once registered and enabled by the user in the dialer settings, telecom will bind to a
65 * {@code ConnectionService} implementation when it wants that {@code ConnectionService} to place
66 * a call or the service has indicated that is has an incoming call through
67 * {@link TelecomManager#addNewIncomingCall}. The {@code ConnectionService} can then expect a call
68 * to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection} wherein it
69 * should provide a new instance of a {@link Connection} object. It is through this
70 * {@link Connection} object that telecom receives state updates and the {@code ConnectionService}
71 * receives call-commands such as answer, reject, hold and disconnect.
72 * <p>
73 * When there are no more live calls, telecom will unbind from the {@code ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070074 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070075public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070076 /**
77 * The {@link Intent} that must be declared as handled by the service.
78 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070079 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070080 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070081
Ihab Awad542e0ea2014-05-16 10:22:16 -070082 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070083 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070084
Ihab Awad8aecfed2014-08-08 17:06:11 -070085 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070086 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070087 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070088 private static final int MSG_ANSWER = 4;
89 private static final int MSG_REJECT = 5;
90 private static final int MSG_DISCONNECT = 6;
91 private static final int MSG_HOLD = 7;
92 private static final int MSG_UNHOLD = 8;
93 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
94 private static final int MSG_PLAY_DTMF_TONE = 10;
95 private static final int MSG_STOP_DTMF_TONE = 11;
96 private static final int MSG_CONFERENCE = 12;
97 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070098 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070099 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700100 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700101 private static final int MSG_MERGE_CONFERENCE = 18;
102 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700103
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700104 private static Connection sNullConnection;
105
mike dooley95e80702014-09-18 14:07:52 -0700106 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
107 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
108 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
109 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700110 private final RemoteConnectionManager mRemoteConnectionManager =
111 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700112 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700113 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700114
Santos Cordon823fd3c2014-08-07 18:35:18 -0700115 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700116 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700117
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700118 private final IBinder mBinder = new IConnectionService.Stub() {
119 @Override
120 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700121 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
122 }
123
124 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
125 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700126 }
127
128 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700129 public void createConnection(
130 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700131 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700132 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700133 boolean isIncoming,
134 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700135 SomeArgs args = SomeArgs.obtain();
136 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700137 args.arg2 = id;
138 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700139 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700140 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700141 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700142 }
143
144 @Override
145 public void abort(String callId) {
146 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
147 }
148
149 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700150 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700151 SomeArgs args = SomeArgs.obtain();
152 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700153 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700154 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
155 }
156
157 @Override
158 public void answer(String callId) {
159 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700160 }
161
162 @Override
163 public void reject(String callId) {
164 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
165 }
166
167 @Override
168 public void disconnect(String callId) {
169 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
170 }
171
172 @Override
173 public void hold(String callId) {
174 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
175 }
176
177 @Override
178 public void unhold(String callId) {
179 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
180 }
181
182 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700183 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700184 SomeArgs args = SomeArgs.obtain();
185 args.arg1 = callId;
186 args.arg2 = audioState;
187 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
188 }
189
190 @Override
191 public void playDtmfTone(String callId, char digit) {
192 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
193 }
194
195 @Override
196 public void stopDtmfTone(String callId) {
197 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
198 }
199
200 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700201 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700202 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700203 args.arg1 = callId1;
204 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700205 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
206 }
207
208 @Override
209 public void splitFromConference(String callId) {
210 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
211 }
212
213 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700214 public void mergeConference(String callId) {
215 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
216 }
217
218 @Override
219 public void swapConference(String callId) {
220 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
221 }
222
223 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700224 public void onPostDialContinue(String callId, boolean proceed) {
225 SomeArgs args = SomeArgs.obtain();
226 args.arg1 = callId;
227 args.argi1 = proceed ? 1 : 0;
228 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
229 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700230 };
231
232 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
233 @Override
234 public void handleMessage(Message msg) {
235 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700236 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700237 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
238 onAdapterAttached();
239 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700240 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
241 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
242 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700243 case MSG_CREATE_CONNECTION: {
244 SomeArgs args = (SomeArgs) msg.obj;
245 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700246 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700247 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700248 final String id = (String) args.arg2;
249 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700250 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700251 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700252 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700253 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700254 mPreInitializationConnectionRequests.add(new Runnable() {
255 @Override
256 public void run() {
257 createConnection(
258 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700259 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700260 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700261 isIncoming,
262 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700263 }
264 });
265 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700266 createConnection(
267 connectionManagerPhoneAccount,
268 id,
269 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700270 isIncoming,
271 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700272 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700273 } finally {
274 args.recycle();
275 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700276 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700277 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700278 case MSG_ABORT:
279 abort((String) msg.obj);
280 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700281 case MSG_ANSWER:
282 answer((String) msg.obj);
283 break;
284 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700285 SomeArgs args = (SomeArgs) msg.obj;
286 try {
287 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700288 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700289 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700290 } finally {
291 args.recycle();
292 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700293 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700294 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700295 case MSG_REJECT:
296 reject((String) msg.obj);
297 break;
298 case MSG_DISCONNECT:
299 disconnect((String) msg.obj);
300 break;
301 case MSG_HOLD:
302 hold((String) msg.obj);
303 break;
304 case MSG_UNHOLD:
305 unhold((String) msg.obj);
306 break;
307 case MSG_ON_AUDIO_STATE_CHANGED: {
308 SomeArgs args = (SomeArgs) msg.obj;
309 try {
310 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700311 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700312 onAudioStateChanged(callId, audioState);
313 } finally {
314 args.recycle();
315 }
316 break;
317 }
318 case MSG_PLAY_DTMF_TONE:
319 playDtmfTone((String) msg.obj, (char) msg.arg1);
320 break;
321 case MSG_STOP_DTMF_TONE:
322 stopDtmfTone((String) msg.obj);
323 break;
324 case MSG_CONFERENCE: {
325 SomeArgs args = (SomeArgs) msg.obj;
326 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700327 String callId1 = (String) args.arg1;
328 String callId2 = (String) args.arg2;
329 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700330 } finally {
331 args.recycle();
332 }
333 break;
334 }
335 case MSG_SPLIT_FROM_CONFERENCE:
336 splitFromConference((String) msg.obj);
337 break;
Santos Cordona4868042014-09-04 17:39:22 -0700338 case MSG_MERGE_CONFERENCE:
339 mergeConference((String) msg.obj);
340 break;
341 case MSG_SWAP_CONFERENCE:
342 swapConference((String) msg.obj);
343 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700344 case MSG_ON_POST_DIAL_CONTINUE: {
345 SomeArgs args = (SomeArgs) msg.obj;
346 try {
347 String callId = (String) args.arg1;
348 boolean proceed = (args.argi1 == 1);
349 onPostDialContinue(callId, proceed);
350 } finally {
351 args.recycle();
352 }
353 break;
354 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700355 default:
356 break;
357 }
358 }
359 };
360
Santos Cordon823fd3c2014-08-07 18:35:18 -0700361 private final Conference.Listener mConferenceListener = new Conference.Listener() {
362 @Override
363 public void onStateChanged(Conference conference, int oldState, int newState) {
364 String id = mIdByConference.get(conference);
365 switch (newState) {
366 case Connection.STATE_ACTIVE:
367 mAdapter.setActive(id);
368 break;
369 case Connection.STATE_HOLDING:
370 mAdapter.setOnHold(id);
371 break;
372 case Connection.STATE_DISCONNECTED:
373 // handled by onDisconnected
374 break;
375 }
376 }
377
378 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700379 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700380 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700381 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700382 }
383
384 @Override
385 public void onConnectionAdded(Conference conference, Connection connection) {
386 }
387
388 @Override
389 public void onConnectionRemoved(Conference conference, Connection connection) {
390 }
391
392 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700393 public void onConferenceableConnectionsChanged(
394 Conference conference, List<Connection> conferenceableConnections) {
395 mAdapter.setConferenceableConnections(
396 mIdByConference.get(conference),
397 createConnectionIdList(conferenceableConnections));
398 }
399
400 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700401 public void onDestroyed(Conference conference) {
402 removeConference(conference);
403 }
404
405 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800406 public void onConnectionCapabilitiesChanged(
407 Conference conference,
408 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700409 String id = mIdByConference.get(conference);
410 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800411 Connection.capabilitiesToString(connectionCapabilities));
412 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700413 }
Rekha Kumar07366812015-03-24 16:42:31 -0700414
415 @Override
416 public void onVideoStateChanged(Conference c, int videoState) {
417 String id = mIdByConference.get(c);
418 Log.d(this, "onVideoStateChanged set video state %d", videoState);
419 mAdapter.setVideoState(id, videoState);
420 }
421
422 @Override
423 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
424 String id = mIdByConference.get(c);
425 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
426 videoProvider);
427 mAdapter.setVideoProvider(id, videoProvider);
428 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700429
430 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700431 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
432 String id = mIdByConference.get(conference);
433 mAdapter.setStatusHints(id, statusHints);
434 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700435 };
436
Ihab Awad542e0ea2014-05-16 10:22:16 -0700437 private final Connection.Listener mConnectionListener = new Connection.Listener() {
438 @Override
439 public void onStateChanged(Connection c, int state) {
440 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700441 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700442 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700443 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700444 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700445 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700446 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700447 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700448 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700449 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700450 // Handled in onDisconnected()
451 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700452 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700453 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700454 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700455 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700456 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700457 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700458 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700459 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700460 break;
461 }
462 }
463
464 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700465 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700466 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700467 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700468 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700469 }
470
471 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700472 public void onVideoStateChanged(Connection c, int videoState) {
473 String id = mIdByConnection.get(c);
474 Log.d(this, "Adapter set video state %d", videoState);
475 mAdapter.setVideoState(id, videoState);
476 }
477
478 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700479 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700480 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700481 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700482 }
483
484 @Override
485 public void onCallerDisplayNameChanged(
486 Connection c, String callerDisplayName, int presentation) {
487 String id = mIdByConnection.get(c);
488 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700489 }
490
491 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700492 public void onDestroyed(Connection c) {
493 removeConnection(c);
494 }
Ihab Awadf8358972014-05-28 16:46:42 -0700495
496 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700497 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700498 String id = mIdByConnection.get(c);
499 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700500 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700501 }
502
503 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800504 public void onPostDialChar(Connection c, char nextChar) {
505 String id = mIdByConnection.get(c);
506 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
507 mAdapter.onPostDialChar(id, nextChar);
508 }
509
510 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700511 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700512 String id = mIdByConnection.get(c);
513 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700514 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700515 }
Santos Cordonb6939982014-06-04 20:20:58 -0700516
517 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800518 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700519 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700520 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800521 Connection.capabilitiesToString(capabilities));
522 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700523 }
524
Santos Cordonb6939982014-06-04 20:20:58 -0700525 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700526 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700527 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700528 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
529 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700530 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700531 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700532
533 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700534 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700535 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700536 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700537 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700538
539 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700540 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700541 String id = mIdByConnection.get(c);
542 mAdapter.setStatusHints(id, statusHints);
543 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700544
545 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800546 public void onConferenceablesChanged(
Tyler Gunndf2cbc82015-04-20 09:13:01 -0700547 Connection connection, List<Conferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700548 mAdapter.setConferenceableConnections(
549 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800550 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700551 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700552
553 @Override
554 public void onConferenceChanged(Connection connection, Conference conference) {
555 String id = mIdByConnection.get(connection);
556 if (id != null) {
557 String conferenceId = null;
558 if (conference != null) {
559 conferenceId = mIdByConference.get(conference);
560 }
561 mAdapter.setIsConferenced(id, conferenceId);
562 }
563 }
Anthony Lee17455a32015-04-24 15:25:29 -0700564
565 @Override
566 public void onConferenceMergeFailed(Connection connection) {
567 String id = mIdByConnection.get(connection);
568 if (id != null) {
569 mAdapter.onConferenceMergeFailed(id);
570 }
571 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700572 };
573
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700574 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700575 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700576 public final IBinder onBind(Intent intent) {
577 return mBinder;
578 }
579
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700580 /** {@inheritDoc} */
581 @Override
582 public boolean onUnbind(Intent intent) {
583 endAllConnections();
584 return super.onUnbind(intent);
585 }
586
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700587 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700588 * This can be used by telecom to either create a new outgoing call or attach to an existing
589 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700590 * createConnection util a connection service cancels the process or completes it successfully.
591 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700592 private void createConnection(
593 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700594 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700595 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700596 boolean isIncoming,
597 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700598 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700599 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
600 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700601
Yorke Leec3cf9822014-10-02 09:38:39 -0700602 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
603 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700604 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700605 Log.d(this, "createConnection, connection: %s", connection);
606 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700607 connection = Connection.createFailedConnection(
608 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700609 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700610
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700611 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700612 addConnection(callId, connection);
613 }
614
Andrew Lee100e2932014-09-08 15:34:24 -0700615 Uri address = connection.getAddress();
616 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700617 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700618 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700619 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800620 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700621
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700622 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700623 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700624 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700625 request,
626 new ParcelableConnection(
627 request.getAccountHandle(),
628 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800629 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700630 connection.getAddress(),
631 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700632 connection.getCallerDisplayName(),
633 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700634 connection.getVideoProvider() == null ?
635 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700636 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700637 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700638 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700639 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700640 connection.getDisconnectCause(),
Jay Shrauner8f988432015-04-16 12:52:19 -0700641 createIdList(connection.getConferenceables())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700642 }
643
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700644 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700645 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700646 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700647 }
648
Tyler Gunnbe74de02014-08-29 14:51:48 -0700649 private void answerVideo(String callId, int videoState) {
650 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700651 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700652 }
653
Tyler Gunnbe74de02014-08-29 14:51:48 -0700654 private void answer(String callId) {
655 Log.d(this, "answer %s", callId);
656 findConnectionForAction(callId, "answer").onAnswer();
657 }
658
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700659 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700660 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700661 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700662 }
663
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700664 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700665 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700666 if (mConnectionById.containsKey(callId)) {
667 findConnectionForAction(callId, "disconnect").onDisconnect();
668 } else {
669 findConferenceForAction(callId, "disconnect").onDisconnect();
670 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700671 }
672
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700673 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700674 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700675 if (mConnectionById.containsKey(callId)) {
676 findConnectionForAction(callId, "hold").onHold();
677 } else {
678 findConferenceForAction(callId, "hold").onHold();
679 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700680 }
681
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700682 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700683 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700684 if (mConnectionById.containsKey(callId)) {
685 findConnectionForAction(callId, "unhold").onUnhold();
686 } else {
687 findConferenceForAction(callId, "unhold").onUnhold();
688 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700689 }
690
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700691 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700692 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700693 if (mConnectionById.containsKey(callId)) {
694 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
695 } else {
696 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
697 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700698 }
699
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700700 private void playDtmfTone(String callId, char digit) {
701 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700702 if (mConnectionById.containsKey(callId)) {
703 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
704 } else {
705 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
706 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700707 }
708
709 private void stopDtmfTone(String callId) {
710 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700711 if (mConnectionById.containsKey(callId)) {
712 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
713 } else {
714 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
715 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700716 }
717
Santos Cordon823fd3c2014-08-07 18:35:18 -0700718 private void conference(String callId1, String callId2) {
719 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700720
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800721 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700722 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800723 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700724 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800725 conference2 = findConferenceForAction(callId2, "conference");
726 if (conference2 == getNullConference()) {
727 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
728 callId2);
729 return;
730 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700731 }
Santos Cordonb6939982014-06-04 20:20:58 -0700732
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800733 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700734 Connection connection1 = findConnectionForAction(callId1, "conference");
735 if (connection1 == getNullConnection()) {
736 Conference conference1 = findConferenceForAction(callId1, "addConnection");
737 if (conference1 == getNullConference()) {
738 Log.w(this,
739 "Connection1 or Conference1 missing in conference request %s.",
740 callId1);
741 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800742 // Call 1 is a conference.
743 if (connection2 != getNullConnection()) {
744 // Call 2 is a connection so merge via call 1 (conference).
745 conference1.onMerge(connection2);
746 } else {
747 // Call 2 is ALSO a conference; this should never happen.
748 Log.wtf(this, "There can only be one conference and an attempt was made to " +
749 "merge two conferences.");
750 return;
751 }
Ihab Awad50e35062014-09-30 09:17:03 -0700752 }
753 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800754 // Call 1 is a connection.
755 if (conference2 != getNullConference()) {
756 // Call 2 is a conference, so merge via call 2.
757 conference2.onMerge(connection1);
758 } else {
759 // Call 2 is a connection, so merge together.
760 onConference(connection1, connection2);
761 }
Ihab Awad50e35062014-09-30 09:17:03 -0700762 }
Santos Cordon980acb92014-05-31 10:31:19 -0700763 }
764
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700765 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700766 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700767
768 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700769 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700770 Log.w(this, "Connection missing in conference request %s.", callId);
771 return;
772 }
773
Santos Cordon0159ac02014-08-21 14:28:11 -0700774 Conference conference = connection.getConference();
775 if (conference != null) {
776 conference.onSeparate(connection);
777 }
Santos Cordon980acb92014-05-31 10:31:19 -0700778 }
779
Santos Cordona4868042014-09-04 17:39:22 -0700780 private void mergeConference(String callId) {
781 Log.d(this, "mergeConference(%s)", callId);
782 Conference conference = findConferenceForAction(callId, "mergeConference");
783 if (conference != null) {
784 conference.onMerge();
785 }
786 }
787
788 private void swapConference(String callId) {
789 Log.d(this, "swapConference(%s)", callId);
790 Conference conference = findConferenceForAction(callId, "swapConference");
791 if (conference != null) {
792 conference.onSwap();
793 }
794 }
795
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700796 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700797 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700798 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700799 }
800
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700801 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700802 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700803 // No need to query again if we already did it.
804 return;
805 }
806
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700807 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700808 @Override
809 public void onResult(
810 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700811 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700812 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700813 @Override
814 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700815 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700816 mRemoteConnectionManager.addConnectionService(
817 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700818 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700819 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700820 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700821 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700822 }
823 });
824 }
825
826 @Override
827 public void onError() {
828 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700829 @Override
830 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700831 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700832 }
833 });
834 }
835 });
836 }
837
Ihab Awadf8b69882014-07-25 15:14:01 -0700838 /**
839 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700840 * incoming request. This is used by {@code ConnectionService}s that are registered with
841 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
842 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700843 *
844 * @param connectionManagerPhoneAccount See description at
845 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
846 * @param request Details about the incoming call.
847 * @return The {@code Connection} object to satisfy this call, or {@code null} to
848 * not handle the call.
849 */
850 public final RemoteConnection createRemoteIncomingConnection(
851 PhoneAccountHandle connectionManagerPhoneAccount,
852 ConnectionRequest request) {
853 return mRemoteConnectionManager.createRemoteConnection(
854 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700855 }
856
857 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700858 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700859 * outgoing request. This is used by {@code ConnectionService}s that are registered with
860 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
861 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700862 *
863 * @param connectionManagerPhoneAccount See description at
864 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
865 * @param request Details about the incoming call.
866 * @return The {@code Connection} object to satisfy this call, or {@code null} to
867 * not handle the call.
868 */
869 public final RemoteConnection createRemoteOutgoingConnection(
870 PhoneAccountHandle connectionManagerPhoneAccount,
871 ConnectionRequest request) {
872 return mRemoteConnectionManager.createRemoteConnection(
873 connectionManagerPhoneAccount, request, false);
874 }
875
876 /**
Santos Cordona663f862014-10-29 13:49:58 -0700877 * Indicates to the relevant {@code RemoteConnectionService} that the specified
878 * {@link RemoteConnection}s should be merged into a conference call.
879 * <p>
880 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
881 * be invoked.
882 *
883 * @param remoteConnection1 The first of the remote connections to conference.
884 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700885 */
886 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700887 RemoteConnection remoteConnection1,
888 RemoteConnection remoteConnection2) {
889 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700890 }
891
892 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700893 * Adds a new conference call. When a conference call is created either as a result of an
894 * explicit request via {@link #onConference} or otherwise, the connection service should supply
895 * an instance of {@link Conference} by invoking this method. A conference call provided by this
896 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
897 *
898 * @param conference The new conference object.
899 */
900 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -0700901 Log.d(this, "addConference: conference=%s", conference);
902
Santos Cordon823fd3c2014-08-07 18:35:18 -0700903 String id = addConferenceInternal(conference);
904 if (id != null) {
905 List<String> connectionIds = new ArrayList<>(2);
906 for (Connection connection : conference.getConnections()) {
907 if (mIdByConnection.containsKey(connection)) {
908 connectionIds.add(mIdByConnection.get(connection));
909 }
910 }
911 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700912 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700913 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800914 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800915 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -0700916 conference.getVideoProvider() == null ?
917 null : conference.getVideoProvider().getInterface(),
918 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -0700919 conference.getConnectTimeMillis(),
Andrew Leeedc625f2015-04-14 13:38:12 -0700920 conference.getStatusHints());
Andrew Lee0f51da32015-04-16 13:11:55 -0700921
Santos Cordon823fd3c2014-08-07 18:35:18 -0700922 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -0700923 mAdapter.setVideoProvider(id, conference.getVideoProvider());
924 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700925
926 // Go through any child calls and set the parent.
927 for (Connection connection : conference.getConnections()) {
928 String connectionId = mIdByConnection.get(connection);
929 if (connectionId != null) {
930 mAdapter.setIsConferenced(connectionId, id);
931 }
932 }
933 }
934 }
935
936 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700937 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
938 * connection.
939 *
940 * @param phoneAccountHandle The phone account handle for the connection.
941 * @param connection The connection to add.
942 */
943 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
944 Connection connection) {
945
946 String id = addExistingConnectionInternal(connection);
947 if (id != null) {
948 List<String> emptyList = new ArrayList<>(0);
949
950 ParcelableConnection parcelableConnection = new ParcelableConnection(
951 phoneAccountHandle,
952 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800953 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700954 connection.getAddress(),
955 connection.getAddressPresentation(),
956 connection.getCallerDisplayName(),
957 connection.getCallerDisplayNamePresentation(),
958 connection.getVideoProvider() == null ?
959 null : connection.getVideoProvider().getInterface(),
960 connection.getVideoState(),
961 connection.isRingbackRequested(),
962 connection.getAudioModeIsVoip(),
963 connection.getStatusHints(),
964 connection.getDisconnectCause(),
Jay Shrauner8f988432015-04-16 12:52:19 -0700965 emptyList);
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700966 mAdapter.addExistingConnection(id, parcelableConnection);
967 }
968 }
969
970 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700971 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
972 * has taken responsibility.
973 *
974 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700975 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700976 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700977 return mConnectionById.values();
978 }
979
980 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700981 * Create a {@code Connection} given an incoming request. This is used to attach to existing
982 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700983 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700984 * @param connectionManagerPhoneAccount See description at
985 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
986 * @param request Details about the incoming call.
987 * @return The {@code Connection} object to satisfy this call, or {@code null} to
988 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700989 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700990 public Connection onCreateIncomingConnection(
991 PhoneAccountHandle connectionManagerPhoneAccount,
992 ConnectionRequest request) {
993 return null;
994 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700995
996 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700997 * Create a {@code Connection} given an outgoing request. This is used to initiate new
998 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700999 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001000 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1001 * this call.
1002 * <p>
1003 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1004 * has registered one or more {@code PhoneAccount}s having
1005 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1006 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1007 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1008 * making the connection.
1009 * <p>
1010 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1011 * being asked to make a direct connection. The
1012 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1013 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1014 * making the connection.
1015 * @param request Details about the outgoing call.
1016 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001017 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001018 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001019 public Connection onCreateOutgoingConnection(
1020 PhoneAccountHandle connectionManagerPhoneAccount,
1021 ConnectionRequest request) {
1022 return null;
1023 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001024
1025 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001026 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1027 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1028 * call created using
1029 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1030 *
1031 * @param connectionManagerPhoneAccount
1032 * @param request
1033 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001034 *
1035 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001036 */
1037 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1038 ConnectionRequest request) {
1039 return null;
1040 }
1041
1042 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001043 * Conference two specified connections. Invoked when the user has made a request to merge the
1044 * specified connections into a conference call. In response, the connection service should
1045 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001046 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001047 * @param connection1 A connection to merge into a conference call.
1048 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001049 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001050 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001051
Santos Cordona663f862014-10-29 13:49:58 -07001052 /**
1053 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1054 * When this method is invoked, this {@link ConnectionService} should create its own
1055 * representation of the conference call and send it to telecom using {@link #addConference}.
1056 * <p>
1057 * This is only relevant to {@link ConnectionService}s which are registered with
1058 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1059 *
1060 * @param conference The remote conference call.
1061 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001062 public void onRemoteConferenceAdded(RemoteConference conference) {}
1063
Santos Cordon823fd3c2014-08-07 18:35:18 -07001064 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001065 * Called when an existing connection is added remotely.
1066 * @param connection The existing connection which was added.
1067 */
1068 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1069
1070 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001071 * @hide
1072 */
1073 public boolean containsConference(Conference conference) {
1074 return mIdByConference.containsKey(conference);
1075 }
1076
Ihab Awadb8e85c72014-08-23 20:34:57 -07001077 /** {@hide} */
1078 void addRemoteConference(RemoteConference remoteConference) {
1079 onRemoteConferenceAdded(remoteConference);
1080 }
1081
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001082 /** {@hide} */
1083 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1084 onRemoteExistingConnectionAdded(remoteConnection);
1085 }
1086
Ihab Awad5d0410f2014-07-30 10:07:40 -07001087 private void onAccountsInitialized() {
1088 mAreAccountsInitialized = true;
1089 for (Runnable r : mPreInitializationConnectionRequests) {
1090 r.run();
1091 }
1092 mPreInitializationConnectionRequests.clear();
1093 }
1094
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001095 /**
1096 * Adds an existing connection to the list of connections, identified by a new UUID.
1097 *
1098 * @param connection The connection.
1099 * @return The UUID of the connection (e.g. the call-id).
1100 */
1101 private String addExistingConnectionInternal(Connection connection) {
1102 String id = UUID.randomUUID().toString();
1103 addConnection(id, connection);
1104 return id;
1105 }
1106
Ihab Awad542e0ea2014-05-16 10:22:16 -07001107 private void addConnection(String callId, Connection connection) {
1108 mConnectionById.put(callId, connection);
1109 mIdByConnection.put(connection, callId);
1110 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001111 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001112 }
1113
Anthony Lee30e65842014-11-06 16:30:53 -08001114 /** {@hide} */
1115 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001116 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001117 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001118 connection.removeConnectionListener(mConnectionListener);
1119 mConnectionById.remove(mIdByConnection.get(connection));
1120 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001121 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001122 }
1123
Santos Cordon823fd3c2014-08-07 18:35:18 -07001124 private String addConferenceInternal(Conference conference) {
1125 if (mIdByConference.containsKey(conference)) {
1126 Log.w(this, "Re-adding an existing conference: %s.", conference);
1127 } else if (conference != null) {
1128 String id = UUID.randomUUID().toString();
1129 mConferenceById.put(id, conference);
1130 mIdByConference.put(conference, id);
1131 conference.addListener(mConferenceListener);
1132 return id;
1133 }
1134
1135 return null;
1136 }
1137
1138 private void removeConference(Conference conference) {
1139 if (mIdByConference.containsKey(conference)) {
1140 conference.removeListener(mConferenceListener);
1141
1142 String id = mIdByConference.get(conference);
1143 mConferenceById.remove(id);
1144 mIdByConference.remove(conference);
1145 mAdapter.removeCall(id);
1146 }
1147 }
1148
Ihab Awad542e0ea2014-05-16 10:22:16 -07001149 private Connection findConnectionForAction(String callId, String action) {
1150 if (mConnectionById.containsKey(callId)) {
1151 return mConnectionById.get(callId);
1152 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001153 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001154 return getNullConnection();
1155 }
1156
1157 static synchronized Connection getNullConnection() {
1158 if (sNullConnection == null) {
1159 sNullConnection = new Connection() {};
1160 }
1161 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001162 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001163
1164 private Conference findConferenceForAction(String conferenceId, String action) {
1165 if (mConferenceById.containsKey(conferenceId)) {
1166 return mConferenceById.get(conferenceId);
1167 }
1168 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1169 return getNullConference();
1170 }
1171
Ihab Awadb8e85c72014-08-23 20:34:57 -07001172 private List<String> createConnectionIdList(List<Connection> connections) {
1173 List<String> ids = new ArrayList<>();
1174 for (Connection c : connections) {
1175 if (mIdByConnection.containsKey(c)) {
1176 ids.add(mIdByConnection.get(c));
1177 }
1178 }
1179 Collections.sort(ids);
1180 return ids;
1181 }
1182
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001183 /**
1184 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001185 * {@link Conferenceable}s passed in.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001186 *
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001187 * @param conferenceables The {@link Conferenceable} connections and conferences.
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001188 * @return List of string conference and call Ids.
1189 */
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001190 private List<String> createIdList(List<Conferenceable> conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001191 List<String> ids = new ArrayList<>();
Tyler Gunndf2cbc82015-04-20 09:13:01 -07001192 for (Conferenceable c : conferenceables) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001193 // Only allow Connection and Conference conferenceables.
1194 if (c instanceof Connection) {
1195 Connection connection = (Connection) c;
1196 if (mIdByConnection.containsKey(connection)) {
1197 ids.add(mIdByConnection.get(connection));
1198 }
1199 } else if (c instanceof Conference) {
1200 Conference conference = (Conference) c;
1201 if (mIdByConference.containsKey(conference)) {
1202 ids.add(mIdByConference.get(conference));
1203 }
1204 }
1205 }
1206 Collections.sort(ids);
1207 return ids;
1208 }
1209
Santos Cordon0159ac02014-08-21 14:28:11 -07001210 private Conference getNullConference() {
1211 if (sNullConference == null) {
1212 sNullConference = new Conference(null) {};
1213 }
1214 return sNullConference;
1215 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001216
1217 private void endAllConnections() {
1218 // Unbound from telecomm. We should end all connections and conferences.
1219 for (Connection connection : mIdByConnection.keySet()) {
1220 // only operate on top-level calls. Conference calls will be removed on their own.
1221 if (connection.getConference() == null) {
1222 connection.onDisconnect();
1223 }
1224 }
1225 for (Conference conference : mIdByConference.keySet()) {
1226 conference.onDisconnect();
1227 }
1228 }
Santos Cordon980acb92014-05-31 10:31:19 -07001229}