blob: ed9251b7afdaaaf69d067954db710f1bde2d42fb [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
Tyler Gunn711d876fd2014-09-19 11:17:02 -070019import android.annotation.SystemApi;
Santos Cordon5c6fa952014-07-20 17:47:12 -070020import android.annotation.SdkConstant;
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/**
Sailesh Nepal2a46b902014-07-04 17:21:07 -070044 * A {@link android.app.Service} that provides telephone connections to processes running on an
45 * Android device.
Tyler Gunn711d876fd2014-09-19 11:17:02 -070046 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -070047 */
Tyler Gunn711d876fd2014-09-19 11:17:02 -070048@SystemApi
Sailesh Nepal2a46b902014-07-04 17:21:07 -070049public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070050 /**
51 * The {@link Intent} that must be declared as handled by the service.
52 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070053 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070054 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070055
Ihab Awad542e0ea2014-05-16 10:22:16 -070056 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070057 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070058
Ihab Awad8aecfed2014-08-08 17:06:11 -070059 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070060 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070061 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070062 private static final int MSG_ANSWER = 4;
63 private static final int MSG_REJECT = 5;
64 private static final int MSG_DISCONNECT = 6;
65 private static final int MSG_HOLD = 7;
66 private static final int MSG_UNHOLD = 8;
67 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
68 private static final int MSG_PLAY_DTMF_TONE = 10;
69 private static final int MSG_STOP_DTMF_TONE = 11;
70 private static final int MSG_CONFERENCE = 12;
71 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070072 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070073 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -070074 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -070075 private static final int MSG_MERGE_CONFERENCE = 18;
76 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070077
Sailesh Nepalcf7020b2014-08-20 10:07:19 -070078 private static Connection sNullConnection;
79
mike dooley95e80702014-09-18 14:07:52 -070080 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
81 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
82 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
83 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070084 private final RemoteConnectionManager mRemoteConnectionManager =
85 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -070086 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070087 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -070088
Santos Cordon823fd3c2014-08-07 18:35:18 -070089 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -070090 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -070091
Sailesh Nepal2a46b902014-07-04 17:21:07 -070092 private final IBinder mBinder = new IConnectionService.Stub() {
93 @Override
94 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -070095 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
96 }
97
98 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
99 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700100 }
101
102 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700103 public void createConnection(
104 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700105 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700106 ConnectionRequest request,
107 boolean isIncoming) {
108 SomeArgs args = SomeArgs.obtain();
109 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700110 args.arg2 = id;
111 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700112 args.argi1 = isIncoming ? 1 : 0;
113 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700114 }
115
116 @Override
117 public void abort(String callId) {
118 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
119 }
120
121 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700122 /** @hide */
123 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700124 SomeArgs args = SomeArgs.obtain();
125 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700126 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700127 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
128 }
129
130 @Override
131 public void answer(String callId) {
132 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700133 }
134
135 @Override
136 public void reject(String callId) {
137 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
138 }
139
140 @Override
141 public void disconnect(String callId) {
142 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
143 }
144
145 @Override
146 public void hold(String callId) {
147 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
148 }
149
150 @Override
151 public void unhold(String callId) {
152 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
153 }
154
155 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700156 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700157 SomeArgs args = SomeArgs.obtain();
158 args.arg1 = callId;
159 args.arg2 = audioState;
160 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
161 }
162
163 @Override
164 public void playDtmfTone(String callId, char digit) {
165 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
166 }
167
168 @Override
169 public void stopDtmfTone(String callId) {
170 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
171 }
172
173 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700174 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700175 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700176 args.arg1 = callId1;
177 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700178 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
179 }
180
181 @Override
182 public void splitFromConference(String callId) {
183 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
184 }
185
186 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700187 public void mergeConference(String callId) {
188 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
189 }
190
191 @Override
192 public void swapConference(String callId) {
193 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
194 }
195
196 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700197 public void onPostDialContinue(String callId, boolean proceed) {
198 SomeArgs args = SomeArgs.obtain();
199 args.arg1 = callId;
200 args.argi1 = proceed ? 1 : 0;
201 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
202 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700203 };
204
205 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
206 @Override
207 public void handleMessage(Message msg) {
208 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700209 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700210 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
211 onAdapterAttached();
212 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700213 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
214 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
215 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700216 case MSG_CREATE_CONNECTION: {
217 SomeArgs args = (SomeArgs) msg.obj;
218 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700219 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700220 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700221 final String id = (String) args.arg2;
222 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700223 final boolean isIncoming = args.argi1 == 1;
224 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700225 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700226 mPreInitializationConnectionRequests.add(new Runnable() {
227 @Override
228 public void run() {
229 createConnection(
230 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700231 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700232 request,
233 isIncoming);
234 }
235 });
236 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700237 createConnection(
238 connectionManagerPhoneAccount,
239 id,
240 request,
241 isIncoming);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700242 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700243 } finally {
244 args.recycle();
245 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700246 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700247 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700248 case MSG_ABORT:
249 abort((String) msg.obj);
250 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700251 case MSG_ANSWER:
252 answer((String) msg.obj);
253 break;
254 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700255 SomeArgs args = (SomeArgs) msg.obj;
256 try {
257 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700258 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700259 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700260 } finally {
261 args.recycle();
262 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700263 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700264 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700265 case MSG_REJECT:
266 reject((String) msg.obj);
267 break;
268 case MSG_DISCONNECT:
269 disconnect((String) msg.obj);
270 break;
271 case MSG_HOLD:
272 hold((String) msg.obj);
273 break;
274 case MSG_UNHOLD:
275 unhold((String) msg.obj);
276 break;
277 case MSG_ON_AUDIO_STATE_CHANGED: {
278 SomeArgs args = (SomeArgs) msg.obj;
279 try {
280 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700281 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700282 onAudioStateChanged(callId, audioState);
283 } finally {
284 args.recycle();
285 }
286 break;
287 }
288 case MSG_PLAY_DTMF_TONE:
289 playDtmfTone((String) msg.obj, (char) msg.arg1);
290 break;
291 case MSG_STOP_DTMF_TONE:
292 stopDtmfTone((String) msg.obj);
293 break;
294 case MSG_CONFERENCE: {
295 SomeArgs args = (SomeArgs) msg.obj;
296 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700297 String callId1 = (String) args.arg1;
298 String callId2 = (String) args.arg2;
299 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700300 } finally {
301 args.recycle();
302 }
303 break;
304 }
305 case MSG_SPLIT_FROM_CONFERENCE:
306 splitFromConference((String) msg.obj);
307 break;
Santos Cordona4868042014-09-04 17:39:22 -0700308 case MSG_MERGE_CONFERENCE:
309 mergeConference((String) msg.obj);
310 break;
311 case MSG_SWAP_CONFERENCE:
312 swapConference((String) msg.obj);
313 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700314 case MSG_ON_POST_DIAL_CONTINUE: {
315 SomeArgs args = (SomeArgs) msg.obj;
316 try {
317 String callId = (String) args.arg1;
318 boolean proceed = (args.argi1 == 1);
319 onPostDialContinue(callId, proceed);
320 } finally {
321 args.recycle();
322 }
323 break;
324 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700325 default:
326 break;
327 }
328 }
329 };
330
Santos Cordon823fd3c2014-08-07 18:35:18 -0700331 private final Conference.Listener mConferenceListener = new Conference.Listener() {
332 @Override
333 public void onStateChanged(Conference conference, int oldState, int newState) {
334 String id = mIdByConference.get(conference);
335 switch (newState) {
336 case Connection.STATE_ACTIVE:
337 mAdapter.setActive(id);
338 break;
339 case Connection.STATE_HOLDING:
340 mAdapter.setOnHold(id);
341 break;
342 case Connection.STATE_DISCONNECTED:
343 // handled by onDisconnected
344 break;
345 }
346 }
347
348 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700349 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700350 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700351 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700352 }
353
354 @Override
355 public void onConnectionAdded(Conference conference, Connection connection) {
356 }
357
358 @Override
359 public void onConnectionRemoved(Conference conference, Connection connection) {
360 }
361
362 @Override
363 public void onDestroyed(Conference conference) {
364 removeConference(conference);
365 }
366
367 @Override
368 public void onCapabilitiesChanged(Conference conference, int capabilities) {
369 String id = mIdByConference.get(conference);
370 Log.d(this, "call capabilities: conference: %s",
371 PhoneCapabilities.toString(capabilities));
372 mAdapter.setCallCapabilities(id, capabilities);
373 }
374 };
375
Ihab Awad542e0ea2014-05-16 10:22:16 -0700376 private final Connection.Listener mConnectionListener = new Connection.Listener() {
377 @Override
378 public void onStateChanged(Connection c, int state) {
379 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700380 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700381 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700382 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700383 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700384 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700385 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700386 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700387 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700388 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700389 // Handled in onDisconnected()
390 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700391 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700392 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700393 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700394 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700395 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700396 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700397 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700398 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700399 break;
400 }
401 }
402
403 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700404 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700405 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700406 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700407 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700408 }
409
410 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700411 public void onVideoStateChanged(Connection c, int videoState) {
412 String id = mIdByConnection.get(c);
413 Log.d(this, "Adapter set video state %d", videoState);
414 mAdapter.setVideoState(id, videoState);
415 }
416
417 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700418 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700419 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700420 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700421 }
422
423 @Override
424 public void onCallerDisplayNameChanged(
425 Connection c, String callerDisplayName, int presentation) {
426 String id = mIdByConnection.get(c);
427 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700428 }
429
430 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700431 public void onDestroyed(Connection c) {
432 removeConnection(c);
433 }
Ihab Awadf8358972014-05-28 16:46:42 -0700434
435 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700436 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700437 String id = mIdByConnection.get(c);
438 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700439 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700440 }
441
442 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700443 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700444 String id = mIdByConnection.get(c);
445 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700446 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700447 }
Santos Cordonb6939982014-06-04 20:20:58 -0700448
449 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700450 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700451 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700452 Log.d(this, "capabilities: parcelableconnection: %s",
453 PhoneCapabilities.toString(capabilities));
454 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700455 }
456
Santos Cordonb6939982014-06-04 20:20:58 -0700457 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700458 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700459 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700460 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700461 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700462
463 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700464 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700465 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700466 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700467 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700468
469 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700470 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700471 String id = mIdByConnection.get(c);
472 mAdapter.setStatusHints(id, statusHints);
473 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700474
475 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700476 public void onConferenceableConnectionsChanged(
477 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700478 mAdapter.setConferenceableConnections(
479 mIdByConnection.get(connection),
480 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700481 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700482
483 @Override
484 public void onConferenceChanged(Connection connection, Conference conference) {
485 String id = mIdByConnection.get(connection);
486 if (id != null) {
487 String conferenceId = null;
488 if (conference != null) {
489 conferenceId = mIdByConference.get(conference);
490 }
491 mAdapter.setIsConferenced(id, conferenceId);
492 }
493 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700494 };
495
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700496 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700497 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700498 public final IBinder onBind(Intent intent) {
499 return mBinder;
500 }
501
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700502 /** {@inheritDoc} */
503 @Override
504 public boolean onUnbind(Intent intent) {
505 endAllConnections();
506 return super.onUnbind(intent);
507 }
508
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700509 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700510 * This can be used by telecom to either create a new outgoing call or attach to an existing
511 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700512 * createConnection util a connection service cancels the process or completes it successfully.
513 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700514 private void createConnection(
515 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700516 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700517 final ConnectionRequest request,
518 boolean isIncoming) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700519 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
520 "isIncoming: %b", callManagerAccount, callId, request, isIncoming);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700521
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700522 Connection connection = isIncoming
Ihab Awad6107bab2014-08-18 09:23:25 -0700523 ? onCreateIncomingConnection(callManagerAccount, request)
524 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700525 Log.d(this, "createConnection, connection: %s", connection);
526 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700527 connection = Connection.createFailedConnection(
528 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700529 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700530
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700531 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700532 addConnection(callId, connection);
533 }
534
Andrew Lee100e2932014-09-08 15:34:24 -0700535 Uri address = connection.getAddress();
536 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700537 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700538 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700539 Connection.stateToString(connection.getState()),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700540 PhoneCapabilities.toString(connection.getCallCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700541
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700542 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700543 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700544 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700545 request,
546 new ParcelableConnection(
547 request.getAccountHandle(),
548 connection.getState(),
549 connection.getCallCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700550 connection.getAddress(),
551 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700552 connection.getCallerDisplayName(),
553 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700554 connection.getVideoProvider() == null ?
555 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700556 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700557 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700558 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700559 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700560 connection.getDisconnectCause(),
Ihab Awadb8e85c72014-08-23 20:34:57 -0700561 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700562 }
563
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700564 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700565 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700566 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700567 }
568
Tyler Gunnbe74de02014-08-29 14:51:48 -0700569 private void answerVideo(String callId, int videoState) {
570 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700571 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700572 }
573
Tyler Gunnbe74de02014-08-29 14:51:48 -0700574 private void answer(String callId) {
575 Log.d(this, "answer %s", callId);
576 findConnectionForAction(callId, "answer").onAnswer();
577 }
578
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700579 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700580 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700581 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700582 }
583
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700584 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700585 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700586 if (mConnectionById.containsKey(callId)) {
587 findConnectionForAction(callId, "disconnect").onDisconnect();
588 } else {
589 findConferenceForAction(callId, "disconnect").onDisconnect();
590 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700591 }
592
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700593 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700594 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700595 if (mConnectionById.containsKey(callId)) {
596 findConnectionForAction(callId, "hold").onHold();
597 } else {
598 findConferenceForAction(callId, "hold").onHold();
599 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700600 }
601
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700602 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700603 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700604 if (mConnectionById.containsKey(callId)) {
605 findConnectionForAction(callId, "unhold").onUnhold();
606 } else {
607 findConferenceForAction(callId, "unhold").onUnhold();
608 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700609 }
610
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700611 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700612 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700613 if (mConnectionById.containsKey(callId)) {
614 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
615 } else {
616 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
617 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700618 }
619
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700620 private void playDtmfTone(String callId, char digit) {
621 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700622 if (mConnectionById.containsKey(callId)) {
623 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
624 } else {
625 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
626 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700627 }
628
629 private void stopDtmfTone(String callId) {
630 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700631 if (mConnectionById.containsKey(callId)) {
632 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
633 } else {
634 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
635 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700636 }
637
Santos Cordon823fd3c2014-08-07 18:35:18 -0700638 private void conference(String callId1, String callId2) {
639 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700640
Santos Cordon823fd3c2014-08-07 18:35:18 -0700641 Connection connection1 = findConnectionForAction(callId1, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700642 if (connection1 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700643 Log.w(this, "Connection1 missing in conference request %s.", callId1);
Santos Cordonb6939982014-06-04 20:20:58 -0700644 return;
Santos Cordon980acb92014-05-31 10:31:19 -0700645 }
646
Santos Cordon823fd3c2014-08-07 18:35:18 -0700647 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700648 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700649 Log.w(this, "Connection2 missing in conference request %s.", callId2);
650 return;
651 }
Santos Cordonb6939982014-06-04 20:20:58 -0700652
Santos Cordon823fd3c2014-08-07 18:35:18 -0700653 onConference(connection1, connection2);
Santos Cordon980acb92014-05-31 10:31:19 -0700654 }
655
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700656 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700657 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700658
659 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700660 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700661 Log.w(this, "Connection missing in conference request %s.", callId);
662 return;
663 }
664
Santos Cordon0159ac02014-08-21 14:28:11 -0700665 Conference conference = connection.getConference();
666 if (conference != null) {
667 conference.onSeparate(connection);
668 }
Santos Cordon980acb92014-05-31 10:31:19 -0700669 }
670
Santos Cordona4868042014-09-04 17:39:22 -0700671 private void mergeConference(String callId) {
672 Log.d(this, "mergeConference(%s)", callId);
673 Conference conference = findConferenceForAction(callId, "mergeConference");
674 if (conference != null) {
675 conference.onMerge();
676 }
677 }
678
679 private void swapConference(String callId) {
680 Log.d(this, "swapConference(%s)", callId);
681 Conference conference = findConferenceForAction(callId, "swapConference");
682 if (conference != null) {
683 conference.onSwap();
684 }
685 }
686
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700687 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700688 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700689 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700690 }
691
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700692 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700693 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700694 // No need to query again if we already did it.
695 return;
696 }
697
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700698 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700699 @Override
700 public void onResult(
701 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700702 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700703 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700704 @Override
705 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700706 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700707 mRemoteConnectionManager.addConnectionService(
708 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700709 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700710 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700711 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700712 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700713 }
714 });
715 }
716
717 @Override
718 public void onError() {
719 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700720 @Override
721 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700722 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700723 }
724 });
725 }
726 });
727 }
728
Ihab Awadf8b69882014-07-25 15:14:01 -0700729 /**
730 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
731 * incoming request. This is used to attach to existing incoming calls.
732 *
733 * @param connectionManagerPhoneAccount See description at
734 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
735 * @param request Details about the incoming call.
736 * @return The {@code Connection} object to satisfy this call, or {@code null} to
737 * not handle the call.
738 */
739 public final RemoteConnection createRemoteIncomingConnection(
740 PhoneAccountHandle connectionManagerPhoneAccount,
741 ConnectionRequest request) {
742 return mRemoteConnectionManager.createRemoteConnection(
743 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700744 }
745
746 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700747 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
748 * outgoing request. This is used to initiate new outgoing calls.
749 *
750 * @param connectionManagerPhoneAccount See description at
751 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
752 * @param request Details about the incoming call.
753 * @return The {@code Connection} object to satisfy this call, or {@code null} to
754 * not handle the call.
755 */
756 public final RemoteConnection createRemoteOutgoingConnection(
757 PhoneAccountHandle connectionManagerPhoneAccount,
758 ConnectionRequest request) {
759 return mRemoteConnectionManager.createRemoteConnection(
760 connectionManagerPhoneAccount, request, false);
761 }
762
763 /**
Ihab Awadb8e85c72014-08-23 20:34:57 -0700764 * Adds two {@code RemoteConnection}s to some {@code RemoteConference}.
765 */
766 public final void conferenceRemoteConnections(
767 RemoteConnection a,
768 RemoteConnection b) {
769 mRemoteConnectionManager.conferenceRemoteConnections(a, b);
770 }
771
772 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700773 * Adds a new conference call. When a conference call is created either as a result of an
774 * explicit request via {@link #onConference} or otherwise, the connection service should supply
775 * an instance of {@link Conference} by invoking this method. A conference call provided by this
776 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
777 *
778 * @param conference The new conference object.
779 */
780 public final void addConference(Conference conference) {
781 String id = addConferenceInternal(conference);
782 if (id != null) {
783 List<String> connectionIds = new ArrayList<>(2);
784 for (Connection connection : conference.getConnections()) {
785 if (mIdByConnection.containsKey(connection)) {
786 connectionIds.add(mIdByConnection.get(connection));
787 }
788 }
789 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700790 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700791 conference.getState(),
792 conference.getCapabilities(),
793 connectionIds);
794 mAdapter.addConferenceCall(id, parcelableConference);
795
796 // Go through any child calls and set the parent.
797 for (Connection connection : conference.getConnections()) {
798 String connectionId = mIdByConnection.get(connection);
799 if (connectionId != null) {
800 mAdapter.setIsConferenced(connectionId, id);
801 }
802 }
803 }
804 }
805
806 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700807 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
808 * has taken responsibility.
809 *
810 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700811 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700812 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700813 return mConnectionById.values();
814 }
815
816 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700817 * Create a {@code Connection} given an incoming request. This is used to attach to existing
818 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700819 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700820 * @param connectionManagerPhoneAccount See description at
821 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
822 * @param request Details about the incoming call.
823 * @return The {@code Connection} object to satisfy this call, or {@code null} to
824 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700825 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700826 public Connection onCreateIncomingConnection(
827 PhoneAccountHandle connectionManagerPhoneAccount,
828 ConnectionRequest request) {
829 return null;
830 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700831
832 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700833 * Create a {@code Connection} given an outgoing request. This is used to initiate new
834 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700835 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700836 * @param connectionManagerPhoneAccount The connection manager account to use for managing
837 * this call.
838 * <p>
839 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
840 * has registered one or more {@code PhoneAccount}s having
841 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
842 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
843 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
844 * making the connection.
845 * <p>
846 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
847 * being asked to make a direct connection. The
848 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
849 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
850 * making the connection.
851 * @param request Details about the outgoing call.
852 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700853 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700854 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700855 public Connection onCreateOutgoingConnection(
856 PhoneAccountHandle connectionManagerPhoneAccount,
857 ConnectionRequest request) {
858 return null;
859 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700860
861 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700862 * Conference two specified connections. Invoked when the user has made a request to merge the
863 * specified connections into a conference call. In response, the connection service should
864 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700865 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700866 * @param connection1 A connection to merge into a conference call.
867 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700868 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700869 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700870
Ihab Awadb8e85c72014-08-23 20:34:57 -0700871 public void onRemoteConferenceAdded(RemoteConference conference) {}
872
Santos Cordon823fd3c2014-08-07 18:35:18 -0700873 /**
874 * @hide
875 */
876 public boolean containsConference(Conference conference) {
877 return mIdByConference.containsKey(conference);
878 }
879
Ihab Awadb8e85c72014-08-23 20:34:57 -0700880 /** {@hide} */
881 void addRemoteConference(RemoteConference remoteConference) {
882 onRemoteConferenceAdded(remoteConference);
883 }
884
Ihab Awad5d0410f2014-07-30 10:07:40 -0700885 private void onAccountsInitialized() {
886 mAreAccountsInitialized = true;
887 for (Runnable r : mPreInitializationConnectionRequests) {
888 r.run();
889 }
890 mPreInitializationConnectionRequests.clear();
891 }
892
Ihab Awad542e0ea2014-05-16 10:22:16 -0700893 private void addConnection(String callId, Connection connection) {
894 mConnectionById.put(callId, connection);
895 mIdByConnection.put(connection, callId);
896 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700897 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700898 }
899
900 private void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700901 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700902 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700903 connection.removeConnectionListener(mConnectionListener);
904 mConnectionById.remove(mIdByConnection.get(connection));
905 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700906 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700907 }
908
Santos Cordon823fd3c2014-08-07 18:35:18 -0700909 private String addConferenceInternal(Conference conference) {
910 if (mIdByConference.containsKey(conference)) {
911 Log.w(this, "Re-adding an existing conference: %s.", conference);
912 } else if (conference != null) {
913 String id = UUID.randomUUID().toString();
914 mConferenceById.put(id, conference);
915 mIdByConference.put(conference, id);
916 conference.addListener(mConferenceListener);
917 return id;
918 }
919
920 return null;
921 }
922
923 private void removeConference(Conference conference) {
924 if (mIdByConference.containsKey(conference)) {
925 conference.removeListener(mConferenceListener);
926
927 String id = mIdByConference.get(conference);
928 mConferenceById.remove(id);
929 mIdByConference.remove(conference);
930 mAdapter.removeCall(id);
931 }
932 }
933
Ihab Awad542e0ea2014-05-16 10:22:16 -0700934 private Connection findConnectionForAction(String callId, String action) {
935 if (mConnectionById.containsKey(callId)) {
936 return mConnectionById.get(callId);
937 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700938 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700939 return getNullConnection();
940 }
941
942 static synchronized Connection getNullConnection() {
943 if (sNullConnection == null) {
944 sNullConnection = new Connection() {};
945 }
946 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700947 }
Santos Cordon0159ac02014-08-21 14:28:11 -0700948
949 private Conference findConferenceForAction(String conferenceId, String action) {
950 if (mConferenceById.containsKey(conferenceId)) {
951 return mConferenceById.get(conferenceId);
952 }
953 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
954 return getNullConference();
955 }
956
Ihab Awadb8e85c72014-08-23 20:34:57 -0700957 private List<String> createConnectionIdList(List<Connection> connections) {
958 List<String> ids = new ArrayList<>();
959 for (Connection c : connections) {
960 if (mIdByConnection.containsKey(c)) {
961 ids.add(mIdByConnection.get(c));
962 }
963 }
964 Collections.sort(ids);
965 return ids;
966 }
967
Santos Cordon0159ac02014-08-21 14:28:11 -0700968 private Conference getNullConference() {
969 if (sNullConference == null) {
970 sNullConference = new Conference(null) {};
971 }
972 return sNullConference;
973 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700974
975 private void endAllConnections() {
976 // Unbound from telecomm. We should end all connections and conferences.
977 for (Connection connection : mIdByConnection.keySet()) {
978 // only operate on top-level calls. Conference calls will be removed on their own.
979 if (connection.getConference() == null) {
980 connection.onDisconnect();
981 }
982 }
983 for (Conference conference : mIdByConference.keySet()) {
984 conference.onDisconnect();
985 }
986 }
Santos Cordon980acb92014-05-31 10:31:19 -0700987}