blob: 1f40842b5ff292e9577bdf7e4b765ebd4c2faa1e [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"
53 * android:permission="android.permission.BIND_CONNECTION_SERVICE"&gt;
54 * &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 /** @hide */
151 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700152 SomeArgs args = SomeArgs.obtain();
153 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700154 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700155 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
156 }
157
158 @Override
159 public void answer(String callId) {
160 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700161 }
162
163 @Override
164 public void reject(String callId) {
165 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
166 }
167
168 @Override
169 public void disconnect(String callId) {
170 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
171 }
172
173 @Override
174 public void hold(String callId) {
175 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
176 }
177
178 @Override
179 public void unhold(String callId) {
180 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
181 }
182
183 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700184 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700185 SomeArgs args = SomeArgs.obtain();
186 args.arg1 = callId;
187 args.arg2 = audioState;
188 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
189 }
190
191 @Override
192 public void playDtmfTone(String callId, char digit) {
193 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
194 }
195
196 @Override
197 public void stopDtmfTone(String callId) {
198 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
199 }
200
201 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700202 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700203 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700204 args.arg1 = callId1;
205 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700206 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
207 }
208
209 @Override
210 public void splitFromConference(String callId) {
211 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
212 }
213
214 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700215 public void mergeConference(String callId) {
216 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
217 }
218
219 @Override
220 public void swapConference(String callId) {
221 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
222 }
223
224 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700225 public void onPostDialContinue(String callId, boolean proceed) {
226 SomeArgs args = SomeArgs.obtain();
227 args.arg1 = callId;
228 args.argi1 = proceed ? 1 : 0;
229 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
230 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700231 };
232
233 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
234 @Override
235 public void handleMessage(Message msg) {
236 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700237 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700238 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
239 onAdapterAttached();
240 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700241 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
242 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
243 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700244 case MSG_CREATE_CONNECTION: {
245 SomeArgs args = (SomeArgs) msg.obj;
246 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700247 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700248 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700249 final String id = (String) args.arg2;
250 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700251 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700252 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700253 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700254 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700255 mPreInitializationConnectionRequests.add(new Runnable() {
256 @Override
257 public void run() {
258 createConnection(
259 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700260 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700261 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700262 isIncoming,
263 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700264 }
265 });
266 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700267 createConnection(
268 connectionManagerPhoneAccount,
269 id,
270 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700271 isIncoming,
272 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700273 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700274 } finally {
275 args.recycle();
276 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700277 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700278 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700279 case MSG_ABORT:
280 abort((String) msg.obj);
281 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700282 case MSG_ANSWER:
283 answer((String) msg.obj);
284 break;
285 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700286 SomeArgs args = (SomeArgs) msg.obj;
287 try {
288 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700289 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700290 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700291 } finally {
292 args.recycle();
293 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700294 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700295 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700296 case MSG_REJECT:
297 reject((String) msg.obj);
298 break;
299 case MSG_DISCONNECT:
300 disconnect((String) msg.obj);
301 break;
302 case MSG_HOLD:
303 hold((String) msg.obj);
304 break;
305 case MSG_UNHOLD:
306 unhold((String) msg.obj);
307 break;
308 case MSG_ON_AUDIO_STATE_CHANGED: {
309 SomeArgs args = (SomeArgs) msg.obj;
310 try {
311 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700312 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700313 onAudioStateChanged(callId, audioState);
314 } finally {
315 args.recycle();
316 }
317 break;
318 }
319 case MSG_PLAY_DTMF_TONE:
320 playDtmfTone((String) msg.obj, (char) msg.arg1);
321 break;
322 case MSG_STOP_DTMF_TONE:
323 stopDtmfTone((String) msg.obj);
324 break;
325 case MSG_CONFERENCE: {
326 SomeArgs args = (SomeArgs) msg.obj;
327 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700328 String callId1 = (String) args.arg1;
329 String callId2 = (String) args.arg2;
330 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700331 } finally {
332 args.recycle();
333 }
334 break;
335 }
336 case MSG_SPLIT_FROM_CONFERENCE:
337 splitFromConference((String) msg.obj);
338 break;
Santos Cordona4868042014-09-04 17:39:22 -0700339 case MSG_MERGE_CONFERENCE:
340 mergeConference((String) msg.obj);
341 break;
342 case MSG_SWAP_CONFERENCE:
343 swapConference((String) msg.obj);
344 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700345 case MSG_ON_POST_DIAL_CONTINUE: {
346 SomeArgs args = (SomeArgs) msg.obj;
347 try {
348 String callId = (String) args.arg1;
349 boolean proceed = (args.argi1 == 1);
350 onPostDialContinue(callId, proceed);
351 } finally {
352 args.recycle();
353 }
354 break;
355 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700356 default:
357 break;
358 }
359 }
360 };
361
Santos Cordon823fd3c2014-08-07 18:35:18 -0700362 private final Conference.Listener mConferenceListener = new Conference.Listener() {
363 @Override
364 public void onStateChanged(Conference conference, int oldState, int newState) {
365 String id = mIdByConference.get(conference);
366 switch (newState) {
367 case Connection.STATE_ACTIVE:
368 mAdapter.setActive(id);
369 break;
370 case Connection.STATE_HOLDING:
371 mAdapter.setOnHold(id);
372 break;
373 case Connection.STATE_DISCONNECTED:
374 // handled by onDisconnected
375 break;
376 }
377 }
378
379 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700380 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700381 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700382 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700383 }
384
385 @Override
386 public void onConnectionAdded(Conference conference, Connection connection) {
387 }
388
389 @Override
390 public void onConnectionRemoved(Conference conference, Connection connection) {
391 }
392
393 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700394 public void onConferenceableConnectionsChanged(
395 Conference conference, List<Connection> conferenceableConnections) {
396 mAdapter.setConferenceableConnections(
397 mIdByConference.get(conference),
398 createConnectionIdList(conferenceableConnections));
399 }
400
401 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700402 public void onDestroyed(Conference conference) {
403 removeConference(conference);
404 }
405
406 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800407 public void onConnectionCapabilitiesChanged(
408 Conference conference,
409 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700410 String id = mIdByConference.get(conference);
411 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800412 Connection.capabilitiesToString(connectionCapabilities));
413 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700414 }
Rekha Kumar07366812015-03-24 16:42:31 -0700415
416 @Override
417 public void onVideoStateChanged(Conference c, int videoState) {
418 String id = mIdByConference.get(c);
419 Log.d(this, "onVideoStateChanged set video state %d", videoState);
420 mAdapter.setVideoState(id, videoState);
421 }
422
423 @Override
424 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {
425 String id = mIdByConference.get(c);
426 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
427 videoProvider);
428 mAdapter.setVideoProvider(id, videoProvider);
429 }
Andrew Lee0f51da32015-04-16 13:11:55 -0700430
431 @Override
Andrew Leeedc625f2015-04-14 13:38:12 -0700432 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
433 String id = mIdByConference.get(conference);
434 mAdapter.setStatusHints(id, statusHints);
435 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700436 };
437
Ihab Awad542e0ea2014-05-16 10:22:16 -0700438 private final Connection.Listener mConnectionListener = new Connection.Listener() {
439 @Override
440 public void onStateChanged(Connection c, int state) {
441 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700442 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700443 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700444 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700445 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700446 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700447 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700448 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700449 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700450 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700451 // Handled in onDisconnected()
452 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700453 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700454 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700455 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700456 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700457 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700458 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700459 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700460 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700461 break;
462 }
463 }
464
465 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700466 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700467 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700468 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700469 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700470 }
471
472 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700473 public void onVideoStateChanged(Connection c, int videoState) {
474 String id = mIdByConnection.get(c);
475 Log.d(this, "Adapter set video state %d", videoState);
476 mAdapter.setVideoState(id, videoState);
477 }
478
479 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700480 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700481 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700482 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700483 }
484
485 @Override
486 public void onCallerDisplayNameChanged(
487 Connection c, String callerDisplayName, int presentation) {
488 String id = mIdByConnection.get(c);
489 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700490 }
491
492 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700493 public void onDestroyed(Connection c) {
494 removeConnection(c);
495 }
Ihab Awadf8358972014-05-28 16:46:42 -0700496
497 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700498 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700499 String id = mIdByConnection.get(c);
500 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700501 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700502 }
503
504 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800505 public void onPostDialChar(Connection c, char nextChar) {
506 String id = mIdByConnection.get(c);
507 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
508 mAdapter.onPostDialChar(id, nextChar);
509 }
510
511 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700512 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700513 String id = mIdByConnection.get(c);
514 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700515 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700516 }
Santos Cordonb6939982014-06-04 20:20:58 -0700517
518 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800519 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700520 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700521 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800522 Connection.capabilitiesToString(capabilities));
523 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700524 }
525
Santos Cordonb6939982014-06-04 20:20:58 -0700526 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700527 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700528 String id = mIdByConnection.get(c);
Rekha Kumar07366812015-03-24 16:42:31 -0700529 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c,
530 videoProvider);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700531 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700532 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700533
534 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700535 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700536 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700537 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700538 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700539
540 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700541 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700542 String id = mIdByConnection.get(c);
543 mAdapter.setStatusHints(id, statusHints);
544 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700545
546 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800547 public void onConferenceablesChanged(
548 Connection connection, List<IConferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700549 mAdapter.setConferenceableConnections(
550 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800551 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700552 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700553
554 @Override
555 public void onConferenceChanged(Connection connection, Conference conference) {
556 String id = mIdByConnection.get(connection);
557 if (id != null) {
558 String conferenceId = null;
559 if (conference != null) {
560 conferenceId = mIdByConference.get(conference);
561 }
562 mAdapter.setIsConferenced(id, conferenceId);
563 }
564 }
Rekha Kumar07366812015-03-24 16:42:31 -0700565
566 @Override
567 public void onCallSubstateChanged(Connection c, int callSubstate) {
568 String id = mIdByConnection.get(c);
569 Log.d(this, "Adapter set call substate %d", callSubstate);
570 mAdapter.setCallSubstate(id, callSubstate);
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(),
Rekha Kumar07366812015-03-24 16:42:31 -0700641 createIdList(connection.getConferenceables()),
642 connection.getCallSubstate()));
Evan Charltonbf11f982014-07-20 22:06:28 -0700643 }
644
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700645 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700646 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700647 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700648 }
649
Tyler Gunnbe74de02014-08-29 14:51:48 -0700650 private void answerVideo(String callId, int videoState) {
651 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700652 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700653 }
654
Tyler Gunnbe74de02014-08-29 14:51:48 -0700655 private void answer(String callId) {
656 Log.d(this, "answer %s", callId);
657 findConnectionForAction(callId, "answer").onAnswer();
658 }
659
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700660 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700661 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700662 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700663 }
664
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700665 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700666 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700667 if (mConnectionById.containsKey(callId)) {
668 findConnectionForAction(callId, "disconnect").onDisconnect();
669 } else {
670 findConferenceForAction(callId, "disconnect").onDisconnect();
671 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700672 }
673
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700674 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700675 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700676 if (mConnectionById.containsKey(callId)) {
677 findConnectionForAction(callId, "hold").onHold();
678 } else {
679 findConferenceForAction(callId, "hold").onHold();
680 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700681 }
682
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700683 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700684 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700685 if (mConnectionById.containsKey(callId)) {
686 findConnectionForAction(callId, "unhold").onUnhold();
687 } else {
688 findConferenceForAction(callId, "unhold").onUnhold();
689 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700690 }
691
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700692 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700693 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700694 if (mConnectionById.containsKey(callId)) {
695 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
696 } else {
697 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
698 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700699 }
700
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700701 private void playDtmfTone(String callId, char digit) {
702 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700703 if (mConnectionById.containsKey(callId)) {
704 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
705 } else {
706 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
707 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700708 }
709
710 private void stopDtmfTone(String callId) {
711 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700712 if (mConnectionById.containsKey(callId)) {
713 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
714 } else {
715 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
716 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700717 }
718
Santos Cordon823fd3c2014-08-07 18:35:18 -0700719 private void conference(String callId1, String callId2) {
720 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700721
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800722 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700723 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800724 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700725 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800726 conference2 = findConferenceForAction(callId2, "conference");
727 if (conference2 == getNullConference()) {
728 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
729 callId2);
730 return;
731 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700732 }
Santos Cordonb6939982014-06-04 20:20:58 -0700733
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800734 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700735 Connection connection1 = findConnectionForAction(callId1, "conference");
736 if (connection1 == getNullConnection()) {
737 Conference conference1 = findConferenceForAction(callId1, "addConnection");
738 if (conference1 == getNullConference()) {
739 Log.w(this,
740 "Connection1 or Conference1 missing in conference request %s.",
741 callId1);
742 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800743 // Call 1 is a conference.
744 if (connection2 != getNullConnection()) {
745 // Call 2 is a connection so merge via call 1 (conference).
746 conference1.onMerge(connection2);
747 } else {
748 // Call 2 is ALSO a conference; this should never happen.
749 Log.wtf(this, "There can only be one conference and an attempt was made to " +
750 "merge two conferences.");
751 return;
752 }
Ihab Awad50e35062014-09-30 09:17:03 -0700753 }
754 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800755 // Call 1 is a connection.
756 if (conference2 != getNullConference()) {
757 // Call 2 is a conference, so merge via call 2.
758 conference2.onMerge(connection1);
759 } else {
760 // Call 2 is a connection, so merge together.
761 onConference(connection1, connection2);
762 }
Ihab Awad50e35062014-09-30 09:17:03 -0700763 }
Santos Cordon980acb92014-05-31 10:31:19 -0700764 }
765
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700766 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700767 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700768
769 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700770 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700771 Log.w(this, "Connection missing in conference request %s.", callId);
772 return;
773 }
774
Santos Cordon0159ac02014-08-21 14:28:11 -0700775 Conference conference = connection.getConference();
776 if (conference != null) {
777 conference.onSeparate(connection);
778 }
Santos Cordon980acb92014-05-31 10:31:19 -0700779 }
780
Santos Cordona4868042014-09-04 17:39:22 -0700781 private void mergeConference(String callId) {
782 Log.d(this, "mergeConference(%s)", callId);
783 Conference conference = findConferenceForAction(callId, "mergeConference");
784 if (conference != null) {
785 conference.onMerge();
786 }
787 }
788
789 private void swapConference(String callId) {
790 Log.d(this, "swapConference(%s)", callId);
791 Conference conference = findConferenceForAction(callId, "swapConference");
792 if (conference != null) {
793 conference.onSwap();
794 }
795 }
796
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700797 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700798 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700799 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700800 }
801
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700802 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700803 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700804 // No need to query again if we already did it.
805 return;
806 }
807
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700808 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700809 @Override
810 public void onResult(
811 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700812 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700813 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700814 @Override
815 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700816 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700817 mRemoteConnectionManager.addConnectionService(
818 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700819 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700820 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700821 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700822 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700823 }
824 });
825 }
826
827 @Override
828 public void onError() {
829 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700830 @Override
831 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700832 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700833 }
834 });
835 }
836 });
837 }
838
Ihab Awadf8b69882014-07-25 15:14:01 -0700839 /**
840 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700841 * incoming request. This is used by {@code ConnectionService}s that are registered with
842 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
843 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700844 *
845 * @param connectionManagerPhoneAccount See description at
846 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
847 * @param request Details about the incoming call.
848 * @return The {@code Connection} object to satisfy this call, or {@code null} to
849 * not handle the call.
850 */
851 public final RemoteConnection createRemoteIncomingConnection(
852 PhoneAccountHandle connectionManagerPhoneAccount,
853 ConnectionRequest request) {
854 return mRemoteConnectionManager.createRemoteConnection(
855 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700856 }
857
858 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700859 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700860 * outgoing request. This is used by {@code ConnectionService}s that are registered with
861 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
862 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700863 *
864 * @param connectionManagerPhoneAccount See description at
865 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
866 * @param request Details about the incoming call.
867 * @return The {@code Connection} object to satisfy this call, or {@code null} to
868 * not handle the call.
869 */
870 public final RemoteConnection createRemoteOutgoingConnection(
871 PhoneAccountHandle connectionManagerPhoneAccount,
872 ConnectionRequest request) {
873 return mRemoteConnectionManager.createRemoteConnection(
874 connectionManagerPhoneAccount, request, false);
875 }
876
877 /**
Santos Cordona663f862014-10-29 13:49:58 -0700878 * Indicates to the relevant {@code RemoteConnectionService} that the specified
879 * {@link RemoteConnection}s should be merged into a conference call.
880 * <p>
881 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
882 * be invoked.
883 *
884 * @param remoteConnection1 The first of the remote connections to conference.
885 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700886 */
887 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700888 RemoteConnection remoteConnection1,
889 RemoteConnection remoteConnection2) {
890 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700891 }
892
893 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700894 * Adds a new conference call. When a conference call is created either as a result of an
895 * explicit request via {@link #onConference} or otherwise, the connection service should supply
896 * an instance of {@link Conference} by invoking this method. A conference call provided by this
897 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
898 *
899 * @param conference The new conference object.
900 */
901 public final void addConference(Conference conference) {
Rekha Kumar07366812015-03-24 16:42:31 -0700902 Log.d(this, "addConference: conference=%s", conference);
903
Santos Cordon823fd3c2014-08-07 18:35:18 -0700904 String id = addConferenceInternal(conference);
905 if (id != null) {
906 List<String> connectionIds = new ArrayList<>(2);
907 for (Connection connection : conference.getConnections()) {
908 if (mIdByConnection.containsKey(connection)) {
909 connectionIds.add(mIdByConnection.get(connection));
910 }
911 }
912 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700913 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700914 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800915 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800916 connectionIds,
Rekha Kumar07366812015-03-24 16:42:31 -0700917 conference.getVideoProvider() == null ?
918 null : conference.getVideoProvider().getInterface(),
919 conference.getVideoState(),
Andrew Lee3e3e2f22015-04-16 13:48:43 -0700920 conference.getConnectTimeMillis(),
Andrew Leeedc625f2015-04-14 13:38:12 -0700921 conference.getStatusHints());
Andrew Lee0f51da32015-04-16 13:11:55 -0700922
Santos Cordon823fd3c2014-08-07 18:35:18 -0700923 mAdapter.addConferenceCall(id, parcelableConference);
Rekha Kumar07366812015-03-24 16:42:31 -0700924 mAdapter.setVideoProvider(id, conference.getVideoProvider());
925 mAdapter.setVideoState(id, conference.getVideoState());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700926
927 // Go through any child calls and set the parent.
928 for (Connection connection : conference.getConnections()) {
929 String connectionId = mIdByConnection.get(connection);
930 if (connectionId != null) {
931 mAdapter.setIsConferenced(connectionId, id);
932 }
933 }
934 }
935 }
936
937 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700938 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
939 * connection.
940 *
941 * @param phoneAccountHandle The phone account handle for the connection.
942 * @param connection The connection to add.
943 */
944 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
945 Connection connection) {
946
947 String id = addExistingConnectionInternal(connection);
948 if (id != null) {
949 List<String> emptyList = new ArrayList<>(0);
950
951 ParcelableConnection parcelableConnection = new ParcelableConnection(
952 phoneAccountHandle,
953 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800954 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700955 connection.getAddress(),
956 connection.getAddressPresentation(),
957 connection.getCallerDisplayName(),
958 connection.getCallerDisplayNamePresentation(),
959 connection.getVideoProvider() == null ?
960 null : connection.getVideoProvider().getInterface(),
961 connection.getVideoState(),
962 connection.isRingbackRequested(),
963 connection.getAudioModeIsVoip(),
964 connection.getStatusHints(),
965 connection.getDisconnectCause(),
Rekha Kumar07366812015-03-24 16:42:31 -0700966 emptyList, connection.getCallSubstate());
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700967 mAdapter.addExistingConnection(id, parcelableConnection);
968 }
969 }
970
971 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700972 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
973 * has taken responsibility.
974 *
975 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700976 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700977 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700978 return mConnectionById.values();
979 }
980
981 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700982 * Create a {@code Connection} given an incoming request. This is used to attach to existing
983 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700984 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700985 * @param connectionManagerPhoneAccount See description at
986 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
987 * @param request Details about the incoming call.
988 * @return The {@code Connection} object to satisfy this call, or {@code null} to
989 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700990 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700991 public Connection onCreateIncomingConnection(
992 PhoneAccountHandle connectionManagerPhoneAccount,
993 ConnectionRequest request) {
994 return null;
995 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700996
997 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700998 * Create a {@code Connection} given an outgoing request. This is used to initiate new
999 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001000 *
Ihab Awadf8b69882014-07-25 15:14:01 -07001001 * @param connectionManagerPhoneAccount The connection manager account to use for managing
1002 * this call.
1003 * <p>
1004 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
1005 * has registered one or more {@code PhoneAccount}s having
1006 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
1007 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
1008 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
1009 * making the connection.
1010 * <p>
1011 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
1012 * being asked to make a direct connection. The
1013 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
1014 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
1015 * making the connection.
1016 * @param request Details about the outgoing call.
1017 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -07001018 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -07001019 */
Ihab Awadf8b69882014-07-25 15:14:01 -07001020 public Connection onCreateOutgoingConnection(
1021 PhoneAccountHandle connectionManagerPhoneAccount,
1022 ConnectionRequest request) {
1023 return null;
1024 }
Ihab Awad542e0ea2014-05-16 10:22:16 -07001025
1026 /**
Yorke Leec3cf9822014-10-02 09:38:39 -07001027 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
1028 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
1029 * call created using
1030 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1031 *
1032 * @param connectionManagerPhoneAccount
1033 * @param request
1034 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001035 *
1036 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001037 */
1038 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1039 ConnectionRequest request) {
1040 return null;
1041 }
1042
1043 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001044 * Conference two specified connections. Invoked when the user has made a request to merge the
1045 * specified connections into a conference call. In response, the connection service should
1046 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001047 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001048 * @param connection1 A connection to merge into a conference call.
1049 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001050 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001051 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001052
Santos Cordona663f862014-10-29 13:49:58 -07001053 /**
1054 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1055 * When this method is invoked, this {@link ConnectionService} should create its own
1056 * representation of the conference call and send it to telecom using {@link #addConference}.
1057 * <p>
1058 * This is only relevant to {@link ConnectionService}s which are registered with
1059 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1060 *
1061 * @param conference The remote conference call.
1062 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001063 public void onRemoteConferenceAdded(RemoteConference conference) {}
1064
Santos Cordon823fd3c2014-08-07 18:35:18 -07001065 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001066 * Called when an existing connection is added remotely.
1067 * @param connection The existing connection which was added.
1068 */
1069 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1070
1071 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001072 * @hide
1073 */
1074 public boolean containsConference(Conference conference) {
1075 return mIdByConference.containsKey(conference);
1076 }
1077
Ihab Awadb8e85c72014-08-23 20:34:57 -07001078 /** {@hide} */
1079 void addRemoteConference(RemoteConference remoteConference) {
1080 onRemoteConferenceAdded(remoteConference);
1081 }
1082
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001083 /** {@hide} */
1084 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1085 onRemoteExistingConnectionAdded(remoteConnection);
1086 }
1087
Ihab Awad5d0410f2014-07-30 10:07:40 -07001088 private void onAccountsInitialized() {
1089 mAreAccountsInitialized = true;
1090 for (Runnable r : mPreInitializationConnectionRequests) {
1091 r.run();
1092 }
1093 mPreInitializationConnectionRequests.clear();
1094 }
1095
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001096 /**
1097 * Adds an existing connection to the list of connections, identified by a new UUID.
1098 *
1099 * @param connection The connection.
1100 * @return The UUID of the connection (e.g. the call-id).
1101 */
1102 private String addExistingConnectionInternal(Connection connection) {
1103 String id = UUID.randomUUID().toString();
1104 addConnection(id, connection);
1105 return id;
1106 }
1107
Ihab Awad542e0ea2014-05-16 10:22:16 -07001108 private void addConnection(String callId, Connection connection) {
1109 mConnectionById.put(callId, connection);
1110 mIdByConnection.put(connection, callId);
1111 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001112 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001113 }
1114
Anthony Lee30e65842014-11-06 16:30:53 -08001115 /** {@hide} */
1116 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001117 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001118 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001119 connection.removeConnectionListener(mConnectionListener);
1120 mConnectionById.remove(mIdByConnection.get(connection));
1121 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001122 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001123 }
1124
Santos Cordon823fd3c2014-08-07 18:35:18 -07001125 private String addConferenceInternal(Conference conference) {
1126 if (mIdByConference.containsKey(conference)) {
1127 Log.w(this, "Re-adding an existing conference: %s.", conference);
1128 } else if (conference != null) {
1129 String id = UUID.randomUUID().toString();
1130 mConferenceById.put(id, conference);
1131 mIdByConference.put(conference, id);
1132 conference.addListener(mConferenceListener);
1133 return id;
1134 }
1135
1136 return null;
1137 }
1138
1139 private void removeConference(Conference conference) {
1140 if (mIdByConference.containsKey(conference)) {
1141 conference.removeListener(mConferenceListener);
1142
1143 String id = mIdByConference.get(conference);
1144 mConferenceById.remove(id);
1145 mIdByConference.remove(conference);
1146 mAdapter.removeCall(id);
1147 }
1148 }
1149
Ihab Awad542e0ea2014-05-16 10:22:16 -07001150 private Connection findConnectionForAction(String callId, String action) {
1151 if (mConnectionById.containsKey(callId)) {
1152 return mConnectionById.get(callId);
1153 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001154 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001155 return getNullConnection();
1156 }
1157
1158 static synchronized Connection getNullConnection() {
1159 if (sNullConnection == null) {
1160 sNullConnection = new Connection() {};
1161 }
1162 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001163 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001164
1165 private Conference findConferenceForAction(String conferenceId, String action) {
1166 if (mConferenceById.containsKey(conferenceId)) {
1167 return mConferenceById.get(conferenceId);
1168 }
1169 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1170 return getNullConference();
1171 }
1172
Ihab Awadb8e85c72014-08-23 20:34:57 -07001173 private List<String> createConnectionIdList(List<Connection> connections) {
1174 List<String> ids = new ArrayList<>();
1175 for (Connection c : connections) {
1176 if (mIdByConnection.containsKey(c)) {
1177 ids.add(mIdByConnection.get(c));
1178 }
1179 }
1180 Collections.sort(ids);
1181 return ids;
1182 }
1183
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001184 /**
1185 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
1186 * {@link IConferenceable}s passed in.
1187 *
1188 * @param conferenceables The {@link IConferenceable} connections and conferences.
1189 * @return List of string conference and call Ids.
1190 */
1191 private List<String> createIdList(List<IConferenceable> conferenceables) {
1192 List<String> ids = new ArrayList<>();
1193 for (IConferenceable c : conferenceables) {
1194 // Only allow Connection and Conference conferenceables.
1195 if (c instanceof Connection) {
1196 Connection connection = (Connection) c;
1197 if (mIdByConnection.containsKey(connection)) {
1198 ids.add(mIdByConnection.get(connection));
1199 }
1200 } else if (c instanceof Conference) {
1201 Conference conference = (Conference) c;
1202 if (mIdByConference.containsKey(conference)) {
1203 ids.add(mIdByConference.get(conference));
1204 }
1205 }
1206 }
1207 Collections.sort(ids);
1208 return ids;
1209 }
1210
Santos Cordon0159ac02014-08-21 14:28:11 -07001211 private Conference getNullConference() {
1212 if (sNullConference == null) {
1213 sNullConference = new Conference(null) {};
1214 }
1215 return sNullConference;
1216 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001217
1218 private void endAllConnections() {
1219 // Unbound from telecomm. We should end all connections and conferences.
1220 for (Connection connection : mIdByConnection.keySet()) {
1221 // only operate on top-level calls. Conference calls will be removed on their own.
1222 if (connection.getConference() == null) {
1223 connection.onDisconnect();
1224 }
1225 }
1226 for (Conference conference : mIdByConference.keySet()) {
1227 conference.onDisconnect();
1228 }
1229 }
Santos Cordon980acb92014-05-31 10:31:19 -07001230}