blob: 6ec3fa356d73400606aeb25ab00cef01b23df692 [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;
Evan Charlton0e094d92014-11-08 15:49:16 -080020import android.annotation.SystemApi;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070021import android.app.Service;
Santos Cordon52d8a152014-06-17 19:08:45 -070022import android.content.ComponentName;
Santos Cordon5c6fa952014-07-20 17:47:12 -070023import android.content.Intent;
Ihab Awad542e0ea2014-05-16 10:22:16 -070024import android.net.Uri;
Santos Cordon52d8a152014-06-17 19:08:45 -070025import android.os.Handler;
26import android.os.IBinder;
27import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070028import android.os.Message;
Andrew Lee14185762014-07-25 09:41:56 -070029
Sailesh Nepal2a46b902014-07-04 17:21:07 -070030import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070031import com.android.internal.telecom.IConnectionService;
32import com.android.internal.telecom.IConnectionServiceAdapter;
33import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070034
Ihab Awad5d0410f2014-07-30 10:07:40 -070035import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070036import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070037import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070038import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070039import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070040import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070041import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070042
43/**
Santos Cordona663f862014-10-29 13:49:58 -070044 * {@code ConnectionService} is an abstract service that should be implemented by any app which can
45 * make phone calls and want those calls to be integrated into the built-in phone app.
46 * Once implemented, the {@code ConnectionService} needs two additional steps before it will be
47 * integrated into the phone app:
48 * <p>
49 * 1. <i>Registration in AndroidManifest.xml</i>
50 * <br/>
51 * <pre>
52 * &lt;service android:name="com.example.package.MyConnectionService"
53 * android:label="@string/some_label_for_my_connection_service"
54 * android:permission="android.permission.BIND_CONNECTION_SERVICE"&gt;
55 * &lt;intent-filter&gt;
56 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
57 * &lt;/intent-filter&gt;
58 * &lt;/service&gt;
59 * </pre>
60 * <p>
61 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
62 * <br/>
63 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
64 * <p>
65 * Once registered and enabled by the user in the dialer settings, telecom will bind to a
66 * {@code ConnectionService} implementation when it wants that {@code ConnectionService} to place
67 * a call or the service has indicated that is has an incoming call through
68 * {@link TelecomManager#addNewIncomingCall}. The {@code ConnectionService} can then expect a call
69 * to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection} wherein it
70 * should provide a new instance of a {@link Connection} object. It is through this
71 * {@link Connection} object that telecom receives state updates and the {@code ConnectionService}
72 * receives call-commands such as answer, reject, hold and disconnect.
73 * <p>
74 * When there are no more live calls, telecom will unbind from the {@code ConnectionService}.
Evan Charlton0e094d92014-11-08 15:49:16 -080075 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -070076 */
Evan Charlton0e094d92014-11-08 15:49:16 -080077@SystemApi
Sailesh Nepal2a46b902014-07-04 17:21:07 -070078public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070079 /**
80 * The {@link Intent} that must be declared as handled by the service.
81 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070082 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070083 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070084
Ihab Awad542e0ea2014-05-16 10:22:16 -070085 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070086 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070087
Ihab Awad8aecfed2014-08-08 17:06:11 -070088 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070089 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070090 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070091 private static final int MSG_ANSWER = 4;
92 private static final int MSG_REJECT = 5;
93 private static final int MSG_DISCONNECT = 6;
94 private static final int MSG_HOLD = 7;
95 private static final int MSG_UNHOLD = 8;
96 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
97 private static final int MSG_PLAY_DTMF_TONE = 10;
98 private static final int MSG_STOP_DTMF_TONE = 11;
99 private static final int MSG_CONFERENCE = 12;
100 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700101 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700102 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700103 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700104 private static final int MSG_MERGE_CONFERENCE = 18;
105 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700106
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700107 private static Connection sNullConnection;
108
mike dooley95e80702014-09-18 14:07:52 -0700109 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
110 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
111 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
112 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700113 private final RemoteConnectionManager mRemoteConnectionManager =
114 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700115 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700116 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700117
Santos Cordon823fd3c2014-08-07 18:35:18 -0700118 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700119 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700120
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700121 private final IBinder mBinder = new IConnectionService.Stub() {
122 @Override
123 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700124 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
125 }
126
127 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
128 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700129 }
130
131 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700132 public void createConnection(
133 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700134 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700135 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700136 boolean isIncoming,
137 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700138 SomeArgs args = SomeArgs.obtain();
139 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700140 args.arg2 = id;
141 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700142 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700143 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700144 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700145 }
146
147 @Override
148 public void abort(String callId) {
149 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
150 }
151
152 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700153 /** @hide */
154 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700155 SomeArgs args = SomeArgs.obtain();
156 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700157 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700158 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
159 }
160
161 @Override
162 public void answer(String callId) {
163 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700164 }
165
166 @Override
167 public void reject(String callId) {
168 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
169 }
170
171 @Override
172 public void disconnect(String callId) {
173 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
174 }
175
176 @Override
177 public void hold(String callId) {
178 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
179 }
180
181 @Override
182 public void unhold(String callId) {
183 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
184 }
185
186 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700187 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700188 SomeArgs args = SomeArgs.obtain();
189 args.arg1 = callId;
190 args.arg2 = audioState;
191 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
192 }
193
194 @Override
195 public void playDtmfTone(String callId, char digit) {
196 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
197 }
198
199 @Override
200 public void stopDtmfTone(String callId) {
201 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
202 }
203
204 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700205 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700206 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700207 args.arg1 = callId1;
208 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700209 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
210 }
211
212 @Override
213 public void splitFromConference(String callId) {
214 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
215 }
216
217 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700218 public void mergeConference(String callId) {
219 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
220 }
221
222 @Override
223 public void swapConference(String callId) {
224 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
225 }
226
227 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700228 public void onPostDialContinue(String callId, boolean proceed) {
229 SomeArgs args = SomeArgs.obtain();
230 args.arg1 = callId;
231 args.argi1 = proceed ? 1 : 0;
232 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
233 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700234 };
235
236 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
237 @Override
238 public void handleMessage(Message msg) {
239 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700240 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700241 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
242 onAdapterAttached();
243 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700244 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
245 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
246 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700247 case MSG_CREATE_CONNECTION: {
248 SomeArgs args = (SomeArgs) msg.obj;
249 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700250 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700251 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700252 final String id = (String) args.arg2;
253 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700254 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700255 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700256 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700257 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700258 mPreInitializationConnectionRequests.add(new Runnable() {
259 @Override
260 public void run() {
261 createConnection(
262 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700263 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700264 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700265 isIncoming,
266 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700267 }
268 });
269 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700270 createConnection(
271 connectionManagerPhoneAccount,
272 id,
273 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700274 isIncoming,
275 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700276 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700277 } finally {
278 args.recycle();
279 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700280 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700281 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700282 case MSG_ABORT:
283 abort((String) msg.obj);
284 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700285 case MSG_ANSWER:
286 answer((String) msg.obj);
287 break;
288 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700289 SomeArgs args = (SomeArgs) msg.obj;
290 try {
291 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700292 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700293 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700294 } finally {
295 args.recycle();
296 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700297 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700298 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700299 case MSG_REJECT:
300 reject((String) msg.obj);
301 break;
302 case MSG_DISCONNECT:
303 disconnect((String) msg.obj);
304 break;
305 case MSG_HOLD:
306 hold((String) msg.obj);
307 break;
308 case MSG_UNHOLD:
309 unhold((String) msg.obj);
310 break;
311 case MSG_ON_AUDIO_STATE_CHANGED: {
312 SomeArgs args = (SomeArgs) msg.obj;
313 try {
314 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700315 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700316 onAudioStateChanged(callId, audioState);
317 } finally {
318 args.recycle();
319 }
320 break;
321 }
322 case MSG_PLAY_DTMF_TONE:
323 playDtmfTone((String) msg.obj, (char) msg.arg1);
324 break;
325 case MSG_STOP_DTMF_TONE:
326 stopDtmfTone((String) msg.obj);
327 break;
328 case MSG_CONFERENCE: {
329 SomeArgs args = (SomeArgs) msg.obj;
330 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700331 String callId1 = (String) args.arg1;
332 String callId2 = (String) args.arg2;
333 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700334 } finally {
335 args.recycle();
336 }
337 break;
338 }
339 case MSG_SPLIT_FROM_CONFERENCE:
340 splitFromConference((String) msg.obj);
341 break;
Santos Cordona4868042014-09-04 17:39:22 -0700342 case MSG_MERGE_CONFERENCE:
343 mergeConference((String) msg.obj);
344 break;
345 case MSG_SWAP_CONFERENCE:
346 swapConference((String) msg.obj);
347 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700348 case MSG_ON_POST_DIAL_CONTINUE: {
349 SomeArgs args = (SomeArgs) msg.obj;
350 try {
351 String callId = (String) args.arg1;
352 boolean proceed = (args.argi1 == 1);
353 onPostDialContinue(callId, proceed);
354 } finally {
355 args.recycle();
356 }
357 break;
358 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700359 default:
360 break;
361 }
362 }
363 };
364
Santos Cordon823fd3c2014-08-07 18:35:18 -0700365 private final Conference.Listener mConferenceListener = new Conference.Listener() {
366 @Override
367 public void onStateChanged(Conference conference, int oldState, int newState) {
368 String id = mIdByConference.get(conference);
369 switch (newState) {
370 case Connection.STATE_ACTIVE:
371 mAdapter.setActive(id);
372 break;
373 case Connection.STATE_HOLDING:
374 mAdapter.setOnHold(id);
375 break;
376 case Connection.STATE_DISCONNECTED:
377 // handled by onDisconnected
378 break;
379 }
380 }
381
382 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700383 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700384 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700385 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700386 }
387
388 @Override
389 public void onConnectionAdded(Conference conference, Connection connection) {
390 }
391
392 @Override
393 public void onConnectionRemoved(Conference conference, Connection connection) {
394 }
395
396 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700397 public void onConferenceableConnectionsChanged(
398 Conference conference, List<Connection> conferenceableConnections) {
399 mAdapter.setConferenceableConnections(
400 mIdByConference.get(conference),
401 createConnectionIdList(conferenceableConnections));
402 }
403
404 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700405 public void onDestroyed(Conference conference) {
406 removeConference(conference);
407 }
408
409 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800410 public void onConnectionCapabilitiesChanged(
411 Conference conference,
412 int connectionCapabilities) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700413 String id = mIdByConference.get(conference);
414 Log.d(this, "call capabilities: conference: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800415 Connection.capabilitiesToString(connectionCapabilities));
416 mAdapter.setConnectionCapabilities(id, connectionCapabilities);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700417 }
Andrew Leeedc625f2015-04-14 13:38:12 -0700418
419 @Override
420 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {
421 String id = mIdByConference.get(conference);
422 mAdapter.setStatusHints(id, statusHints);
423 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700424 };
425
Ihab Awad542e0ea2014-05-16 10:22:16 -0700426 private final Connection.Listener mConnectionListener = new Connection.Listener() {
427 @Override
428 public void onStateChanged(Connection c, int state) {
429 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700430 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700431 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700432 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700433 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700434 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700435 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700436 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700437 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700438 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700439 // Handled in onDisconnected()
440 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700441 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700442 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700443 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700444 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700445 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700446 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700447 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700448 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700449 break;
450 }
451 }
452
453 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700454 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700455 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700456 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700457 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700458 }
459
460 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700461 public void onVideoStateChanged(Connection c, int videoState) {
462 String id = mIdByConnection.get(c);
463 Log.d(this, "Adapter set video state %d", videoState);
464 mAdapter.setVideoState(id, videoState);
465 }
466
467 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700468 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700469 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700470 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700471 }
472
473 @Override
474 public void onCallerDisplayNameChanged(
475 Connection c, String callerDisplayName, int presentation) {
476 String id = mIdByConnection.get(c);
477 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700478 }
479
480 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700481 public void onDestroyed(Connection c) {
482 removeConnection(c);
483 }
Ihab Awadf8358972014-05-28 16:46:42 -0700484
485 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700486 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700487 String id = mIdByConnection.get(c);
488 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700489 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700490 }
491
492 @Override
Nancy Chen27d1c2d2014-12-15 16:12:50 -0800493 public void onPostDialChar(Connection c, char nextChar) {
494 String id = mIdByConnection.get(c);
495 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar);
496 mAdapter.onPostDialChar(id, nextChar);
497 }
498
499 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700500 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700501 String id = mIdByConnection.get(c);
502 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700503 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700504 }
Santos Cordonb6939982014-06-04 20:20:58 -0700505
506 @Override
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800507 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700508 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700509 Log.d(this, "capabilities: parcelableconnection: %s",
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800510 Connection.capabilitiesToString(capabilities));
511 mAdapter.setConnectionCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700512 }
513
Santos Cordonb6939982014-06-04 20:20:58 -0700514 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700515 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700516 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700517 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700518 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700519
520 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700521 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700522 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700523 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700524 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700525
526 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700527 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700528 String id = mIdByConnection.get(c);
529 mAdapter.setStatusHints(id, statusHints);
530 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700531
532 @Override
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800533 public void onConferenceablesChanged(
534 Connection connection, List<IConferenceable> conferenceables) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700535 mAdapter.setConferenceableConnections(
536 mIdByConnection.get(connection),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800537 createIdList(conferenceables));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700538 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700539
540 @Override
541 public void onConferenceChanged(Connection connection, Conference conference) {
542 String id = mIdByConnection.get(connection);
543 if (id != null) {
544 String conferenceId = null;
545 if (conference != null) {
546 conferenceId = mIdByConference.get(conference);
547 }
548 mAdapter.setIsConferenced(id, conferenceId);
549 }
550 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700551 };
552
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700553 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700554 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700555 public final IBinder onBind(Intent intent) {
556 return mBinder;
557 }
558
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700559 /** {@inheritDoc} */
560 @Override
561 public boolean onUnbind(Intent intent) {
562 endAllConnections();
563 return super.onUnbind(intent);
564 }
565
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700566 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700567 * This can be used by telecom to either create a new outgoing call or attach to an existing
568 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700569 * createConnection util a connection service cancels the process or completes it successfully.
570 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700571 private void createConnection(
572 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700573 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700574 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700575 boolean isIncoming,
576 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700577 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700578 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
579 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700580
Yorke Leec3cf9822014-10-02 09:38:39 -0700581 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
582 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700583 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700584 Log.d(this, "createConnection, connection: %s", connection);
585 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700586 connection = Connection.createFailedConnection(
587 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700588 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700589
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700590 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700591 addConnection(callId, connection);
592 }
593
Andrew Lee100e2932014-09-08 15:34:24 -0700594 Uri address = connection.getAddress();
595 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700596 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700597 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700598 Connection.stateToString(connection.getState()),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800599 Connection.capabilitiesToString(connection.getConnectionCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700600
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700601 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700602 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700603 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700604 request,
605 new ParcelableConnection(
606 request.getAccountHandle(),
607 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800608 connection.getConnectionCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700609 connection.getAddress(),
610 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700611 connection.getCallerDisplayName(),
612 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700613 connection.getVideoProvider() == null ?
614 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700615 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700616 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700617 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700618 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700619 connection.getDisconnectCause(),
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800620 createIdList(connection.getConferenceables())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700621 }
622
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700623 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700624 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700625 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700626 }
627
Tyler Gunnbe74de02014-08-29 14:51:48 -0700628 private void answerVideo(String callId, int videoState) {
629 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700630 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700631 }
632
Tyler Gunnbe74de02014-08-29 14:51:48 -0700633 private void answer(String callId) {
634 Log.d(this, "answer %s", callId);
635 findConnectionForAction(callId, "answer").onAnswer();
636 }
637
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700638 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700639 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700640 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700641 }
642
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700643 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700644 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700645 if (mConnectionById.containsKey(callId)) {
646 findConnectionForAction(callId, "disconnect").onDisconnect();
647 } else {
648 findConferenceForAction(callId, "disconnect").onDisconnect();
649 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700650 }
651
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700652 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700653 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700654 if (mConnectionById.containsKey(callId)) {
655 findConnectionForAction(callId, "hold").onHold();
656 } else {
657 findConferenceForAction(callId, "hold").onHold();
658 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700659 }
660
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700661 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700662 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700663 if (mConnectionById.containsKey(callId)) {
664 findConnectionForAction(callId, "unhold").onUnhold();
665 } else {
666 findConferenceForAction(callId, "unhold").onUnhold();
667 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700668 }
669
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700670 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700671 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700672 if (mConnectionById.containsKey(callId)) {
673 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
674 } else {
675 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
676 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700677 }
678
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700679 private void playDtmfTone(String callId, char digit) {
680 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700681 if (mConnectionById.containsKey(callId)) {
682 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
683 } else {
684 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
685 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700686 }
687
688 private void stopDtmfTone(String callId) {
689 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700690 if (mConnectionById.containsKey(callId)) {
691 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
692 } else {
693 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
694 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700695 }
696
Santos Cordon823fd3c2014-08-07 18:35:18 -0700697 private void conference(String callId1, String callId2) {
698 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700699
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800700 // Attempt to get second connection or conference.
Santos Cordon823fd3c2014-08-07 18:35:18 -0700701 Connection connection2 = findConnectionForAction(callId2, "conference");
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800702 Conference conference2 = getNullConference();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700703 if (connection2 == getNullConnection()) {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800704 conference2 = findConferenceForAction(callId2, "conference");
705 if (conference2 == getNullConference()) {
706 Log.w(this, "Connection2 or Conference2 missing in conference request %s.",
707 callId2);
708 return;
709 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700710 }
Santos Cordonb6939982014-06-04 20:20:58 -0700711
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800712 // Attempt to get first connection or conference and perform merge.
Ihab Awad50e35062014-09-30 09:17:03 -0700713 Connection connection1 = findConnectionForAction(callId1, "conference");
714 if (connection1 == getNullConnection()) {
715 Conference conference1 = findConferenceForAction(callId1, "addConnection");
716 if (conference1 == getNullConference()) {
717 Log.w(this,
718 "Connection1 or Conference1 missing in conference request %s.",
719 callId1);
720 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800721 // Call 1 is a conference.
722 if (connection2 != getNullConnection()) {
723 // Call 2 is a connection so merge via call 1 (conference).
724 conference1.onMerge(connection2);
725 } else {
726 // Call 2 is ALSO a conference; this should never happen.
727 Log.wtf(this, "There can only be one conference and an attempt was made to " +
728 "merge two conferences.");
729 return;
730 }
Ihab Awad50e35062014-09-30 09:17:03 -0700731 }
732 } else {
Tyler Gunn6d76ca02014-11-17 15:49:51 -0800733 // Call 1 is a connection.
734 if (conference2 != getNullConference()) {
735 // Call 2 is a conference, so merge via call 2.
736 conference2.onMerge(connection1);
737 } else {
738 // Call 2 is a connection, so merge together.
739 onConference(connection1, connection2);
740 }
Ihab Awad50e35062014-09-30 09:17:03 -0700741 }
Santos Cordon980acb92014-05-31 10:31:19 -0700742 }
743
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700744 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700745 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700746
747 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700748 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700749 Log.w(this, "Connection missing in conference request %s.", callId);
750 return;
751 }
752
Santos Cordon0159ac02014-08-21 14:28:11 -0700753 Conference conference = connection.getConference();
754 if (conference != null) {
755 conference.onSeparate(connection);
756 }
Santos Cordon980acb92014-05-31 10:31:19 -0700757 }
758
Santos Cordona4868042014-09-04 17:39:22 -0700759 private void mergeConference(String callId) {
760 Log.d(this, "mergeConference(%s)", callId);
761 Conference conference = findConferenceForAction(callId, "mergeConference");
762 if (conference != null) {
763 conference.onMerge();
764 }
765 }
766
767 private void swapConference(String callId) {
768 Log.d(this, "swapConference(%s)", callId);
769 Conference conference = findConferenceForAction(callId, "swapConference");
770 if (conference != null) {
771 conference.onSwap();
772 }
773 }
774
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700775 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700776 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700777 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700778 }
779
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700780 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700781 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700782 // No need to query again if we already did it.
783 return;
784 }
785
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700786 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700787 @Override
788 public void onResult(
789 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700790 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700791 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700792 @Override
793 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700794 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700795 mRemoteConnectionManager.addConnectionService(
796 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700797 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700798 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700799 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700800 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700801 }
802 });
803 }
804
805 @Override
806 public void onError() {
807 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700808 @Override
809 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700810 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700811 }
812 });
813 }
814 });
815 }
816
Ihab Awadf8b69882014-07-25 15:14:01 -0700817 /**
818 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700819 * incoming request. This is used by {@code ConnectionService}s that are registered with
820 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
821 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700822 *
823 * @param connectionManagerPhoneAccount See description at
824 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
825 * @param request Details about the incoming call.
826 * @return The {@code Connection} object to satisfy this call, or {@code null} to
827 * not handle the call.
828 */
829 public final RemoteConnection createRemoteIncomingConnection(
830 PhoneAccountHandle connectionManagerPhoneAccount,
831 ConnectionRequest request) {
832 return mRemoteConnectionManager.createRemoteConnection(
833 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700834 }
835
836 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700837 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700838 * outgoing request. This is used by {@code ConnectionService}s that are registered with
839 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
840 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700841 *
842 * @param connectionManagerPhoneAccount See description at
843 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
844 * @param request Details about the incoming call.
845 * @return The {@code Connection} object to satisfy this call, or {@code null} to
846 * not handle the call.
847 */
848 public final RemoteConnection createRemoteOutgoingConnection(
849 PhoneAccountHandle connectionManagerPhoneAccount,
850 ConnectionRequest request) {
851 return mRemoteConnectionManager.createRemoteConnection(
852 connectionManagerPhoneAccount, request, false);
853 }
854
855 /**
Santos Cordona663f862014-10-29 13:49:58 -0700856 * Indicates to the relevant {@code RemoteConnectionService} that the specified
857 * {@link RemoteConnection}s should be merged into a conference call.
858 * <p>
859 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
860 * be invoked.
861 *
862 * @param remoteConnection1 The first of the remote connections to conference.
863 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700864 */
865 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700866 RemoteConnection remoteConnection1,
867 RemoteConnection remoteConnection2) {
868 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700869 }
870
871 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700872 * Adds a new conference call. When a conference call is created either as a result of an
873 * explicit request via {@link #onConference} or otherwise, the connection service should supply
874 * an instance of {@link Conference} by invoking this method. A conference call provided by this
875 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
876 *
877 * @param conference The new conference object.
878 */
879 public final void addConference(Conference conference) {
880 String id = addConferenceInternal(conference);
881 if (id != null) {
882 List<String> connectionIds = new ArrayList<>(2);
883 for (Connection connection : conference.getConnections()) {
884 if (mIdByConnection.containsKey(connection)) {
885 connectionIds.add(mIdByConnection.get(connection));
886 }
887 }
888 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700889 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700890 conference.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800891 conference.getConnectionCapabilities(),
Tyler Gunncd5d33c2015-01-12 09:02:01 -0800892 connectionIds,
Andrew Leeedc625f2015-04-14 13:38:12 -0700893 conference.getConnectTimeMillis(),
894 conference.getStatusHints());
Santos Cordon823fd3c2014-08-07 18:35:18 -0700895 mAdapter.addConferenceCall(id, parcelableConference);
896
897 // Go through any child calls and set the parent.
898 for (Connection connection : conference.getConnections()) {
899 String connectionId = mIdByConnection.get(connection);
900 if (connectionId != null) {
901 mAdapter.setIsConferenced(connectionId, id);
902 }
903 }
904 }
905 }
906
907 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700908 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
909 * connection.
910 *
911 * @param phoneAccountHandle The phone account handle for the connection.
912 * @param connection The connection to add.
913 */
914 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
915 Connection connection) {
916
917 String id = addExistingConnectionInternal(connection);
918 if (id != null) {
919 List<String> emptyList = new ArrayList<>(0);
920
921 ParcelableConnection parcelableConnection = new ParcelableConnection(
922 phoneAccountHandle,
923 connection.getState(),
Ihab Awad5c9c86e2014-11-12 13:41:16 -0800924 connection.getConnectionCapabilities(),
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700925 connection.getAddress(),
926 connection.getAddressPresentation(),
927 connection.getCallerDisplayName(),
928 connection.getCallerDisplayNamePresentation(),
929 connection.getVideoProvider() == null ?
930 null : connection.getVideoProvider().getInterface(),
931 connection.getVideoState(),
932 connection.isRingbackRequested(),
933 connection.getAudioModeIsVoip(),
934 connection.getStatusHints(),
935 connection.getDisconnectCause(),
936 emptyList);
937 mAdapter.addExistingConnection(id, parcelableConnection);
938 }
939 }
940
941 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700942 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
943 * has taken responsibility.
944 *
945 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700946 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700947 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700948 return mConnectionById.values();
949 }
950
951 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700952 * Create a {@code Connection} given an incoming request. This is used to attach to existing
953 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700954 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700955 * @param connectionManagerPhoneAccount See description at
956 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
957 * @param request Details about the incoming call.
958 * @return The {@code Connection} object to satisfy this call, or {@code null} to
959 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700960 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700961 public Connection onCreateIncomingConnection(
962 PhoneAccountHandle connectionManagerPhoneAccount,
963 ConnectionRequest request) {
964 return null;
965 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700966
967 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700968 * Create a {@code Connection} given an outgoing request. This is used to initiate new
969 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700970 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700971 * @param connectionManagerPhoneAccount The connection manager account to use for managing
972 * this call.
973 * <p>
974 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
975 * has registered one or more {@code PhoneAccount}s having
976 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
977 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
978 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
979 * making the connection.
980 * <p>
981 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
982 * being asked to make a direct connection. The
983 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
984 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
985 * making the connection.
986 * @param request Details about the outgoing call.
987 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700988 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700989 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700990 public Connection onCreateOutgoingConnection(
991 PhoneAccountHandle connectionManagerPhoneAccount,
992 ConnectionRequest request) {
993 return null;
994 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700995
996 /**
Yorke Leec3cf9822014-10-02 09:38:39 -0700997 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
998 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
999 * call created using
1000 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
1001 *
1002 * @param connectionManagerPhoneAccount
1003 * @param request
1004 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -07001005 *
1006 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -07001007 */
1008 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
1009 ConnectionRequest request) {
1010 return null;
1011 }
1012
1013 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001014 * Conference two specified connections. Invoked when the user has made a request to merge the
1015 * specified connections into a conference call. In response, the connection service should
1016 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -07001017 *
Santos Cordon823fd3c2014-08-07 18:35:18 -07001018 * @param connection1 A connection to merge into a conference call.
1019 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -07001020 */
Santos Cordon823fd3c2014-08-07 18:35:18 -07001021 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -07001022
Santos Cordona663f862014-10-29 13:49:58 -07001023 /**
1024 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
1025 * When this method is invoked, this {@link ConnectionService} should create its own
1026 * representation of the conference call and send it to telecom using {@link #addConference}.
1027 * <p>
1028 * This is only relevant to {@link ConnectionService}s which are registered with
1029 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
1030 *
1031 * @param conference The remote conference call.
1032 */
Ihab Awadb8e85c72014-08-23 20:34:57 -07001033 public void onRemoteConferenceAdded(RemoteConference conference) {}
1034
Santos Cordon823fd3c2014-08-07 18:35:18 -07001035 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001036 * Called when an existing connection is added remotely.
1037 * @param connection The existing connection which was added.
1038 */
1039 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
1040
1041 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -07001042 * @hide
1043 */
1044 public boolean containsConference(Conference conference) {
1045 return mIdByConference.containsKey(conference);
1046 }
1047
Ihab Awadb8e85c72014-08-23 20:34:57 -07001048 /** {@hide} */
1049 void addRemoteConference(RemoteConference remoteConference) {
1050 onRemoteConferenceAdded(remoteConference);
1051 }
1052
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001053 /** {@hide} */
1054 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1055 onRemoteExistingConnectionAdded(remoteConnection);
1056 }
1057
Ihab Awad5d0410f2014-07-30 10:07:40 -07001058 private void onAccountsInitialized() {
1059 mAreAccountsInitialized = true;
1060 for (Runnable r : mPreInitializationConnectionRequests) {
1061 r.run();
1062 }
1063 mPreInitializationConnectionRequests.clear();
1064 }
1065
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001066 /**
1067 * Adds an existing connection to the list of connections, identified by a new UUID.
1068 *
1069 * @param connection The connection.
1070 * @return The UUID of the connection (e.g. the call-id).
1071 */
1072 private String addExistingConnectionInternal(Connection connection) {
1073 String id = UUID.randomUUID().toString();
1074 addConnection(id, connection);
1075 return id;
1076 }
1077
Ihab Awad542e0ea2014-05-16 10:22:16 -07001078 private void addConnection(String callId, Connection connection) {
1079 mConnectionById.put(callId, connection);
1080 mIdByConnection.put(connection, callId);
1081 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001082 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001083 }
1084
Anthony Lee30e65842014-11-06 16:30:53 -08001085 /** {@hide} */
1086 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001087 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001088 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001089 connection.removeConnectionListener(mConnectionListener);
1090 mConnectionById.remove(mIdByConnection.get(connection));
1091 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001092 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001093 }
1094
Santos Cordon823fd3c2014-08-07 18:35:18 -07001095 private String addConferenceInternal(Conference conference) {
1096 if (mIdByConference.containsKey(conference)) {
1097 Log.w(this, "Re-adding an existing conference: %s.", conference);
1098 } else if (conference != null) {
1099 String id = UUID.randomUUID().toString();
1100 mConferenceById.put(id, conference);
1101 mIdByConference.put(conference, id);
1102 conference.addListener(mConferenceListener);
1103 return id;
1104 }
1105
1106 return null;
1107 }
1108
1109 private void removeConference(Conference conference) {
1110 if (mIdByConference.containsKey(conference)) {
1111 conference.removeListener(mConferenceListener);
1112
1113 String id = mIdByConference.get(conference);
1114 mConferenceById.remove(id);
1115 mIdByConference.remove(conference);
1116 mAdapter.removeCall(id);
1117 }
1118 }
1119
Ihab Awad542e0ea2014-05-16 10:22:16 -07001120 private Connection findConnectionForAction(String callId, String action) {
1121 if (mConnectionById.containsKey(callId)) {
1122 return mConnectionById.get(callId);
1123 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001124 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001125 return getNullConnection();
1126 }
1127
1128 static synchronized Connection getNullConnection() {
1129 if (sNullConnection == null) {
1130 sNullConnection = new Connection() {};
1131 }
1132 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001133 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001134
1135 private Conference findConferenceForAction(String conferenceId, String action) {
1136 if (mConferenceById.containsKey(conferenceId)) {
1137 return mConferenceById.get(conferenceId);
1138 }
1139 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1140 return getNullConference();
1141 }
1142
Ihab Awadb8e85c72014-08-23 20:34:57 -07001143 private List<String> createConnectionIdList(List<Connection> connections) {
1144 List<String> ids = new ArrayList<>();
1145 for (Connection c : connections) {
1146 if (mIdByConnection.containsKey(c)) {
1147 ids.add(mIdByConnection.get(c));
1148 }
1149 }
1150 Collections.sort(ids);
1151 return ids;
1152 }
1153
Tyler Gunn6d76ca02014-11-17 15:49:51 -08001154 /**
1155 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of
1156 * {@link IConferenceable}s passed in.
1157 *
1158 * @param conferenceables The {@link IConferenceable} connections and conferences.
1159 * @return List of string conference and call Ids.
1160 */
1161 private List<String> createIdList(List<IConferenceable> conferenceables) {
1162 List<String> ids = new ArrayList<>();
1163 for (IConferenceable c : conferenceables) {
1164 // Only allow Connection and Conference conferenceables.
1165 if (c instanceof Connection) {
1166 Connection connection = (Connection) c;
1167 if (mIdByConnection.containsKey(connection)) {
1168 ids.add(mIdByConnection.get(connection));
1169 }
1170 } else if (c instanceof Conference) {
1171 Conference conference = (Conference) c;
1172 if (mIdByConference.containsKey(conference)) {
1173 ids.add(mIdByConference.get(conference));
1174 }
1175 }
1176 }
1177 Collections.sort(ids);
1178 return ids;
1179 }
1180
Santos Cordon0159ac02014-08-21 14:28:11 -07001181 private Conference getNullConference() {
1182 if (sNullConference == null) {
1183 sNullConference = new Conference(null) {};
1184 }
1185 return sNullConference;
1186 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001187
1188 private void endAllConnections() {
1189 // Unbound from telecomm. We should end all connections and conferences.
1190 for (Connection connection : mIdByConnection.keySet()) {
1191 // only operate on top-level calls. Conference calls will be removed on their own.
1192 if (connection.getConference() == null) {
1193 connection.onDisconnect();
1194 }
1195 }
1196 for (Conference conference : mIdByConference.keySet()) {
1197 conference.onDisconnect();
1198 }
1199 }
Santos Cordon980acb92014-05-31 10:31:19 -07001200}