blob: 8e4b17cb92434db3178c48ee5351bf67b4b64b29 [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Santos Cordon5c6fa952014-07-20 17:47:12 -070019import android.annotation.SdkConstant;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070020import android.app.Service;
Santos Cordon52d8a152014-06-17 19:08:45 -070021import android.content.ComponentName;
Santos Cordon5c6fa952014-07-20 17:47:12 -070022import android.content.Intent;
Ihab Awad542e0ea2014-05-16 10:22:16 -070023import android.net.Uri;
Santos Cordon52d8a152014-06-17 19:08:45 -070024import android.os.Handler;
25import android.os.IBinder;
26import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070027import android.os.Message;
Andrew Lee14185762014-07-25 09:41:56 -070028
Sailesh Nepal2a46b902014-07-04 17:21:07 -070029import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070030import com.android.internal.telecom.IConnectionService;
31import com.android.internal.telecom.IConnectionServiceAdapter;
32import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070033
Ihab Awad5d0410f2014-07-30 10:07:40 -070034import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070035import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070036import java.util.Collections;
Ihab Awad542e0ea2014-05-16 10:22:16 -070037import java.util.HashMap;
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;
Ihab Awad542e0ea2014-05-16 10:22:16 -070041
42/**
Sailesh Nepal2a46b902014-07-04 17:21:07 -070043 * A {@link android.app.Service} that provides telephone connections to processes running on an
44 * Android device.
Ihab Awad542e0ea2014-05-16 10:22:16 -070045 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070046public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070047 /**
48 * The {@link Intent} that must be declared as handled by the service.
49 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070050 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070051 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070052
Ihab Awad542e0ea2014-05-16 10:22:16 -070053 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070054 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070055
Ihab Awad8aecfed2014-08-08 17:06:11 -070056 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070057 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070058 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070059 private static final int MSG_ANSWER = 4;
60 private static final int MSG_REJECT = 5;
61 private static final int MSG_DISCONNECT = 6;
62 private static final int MSG_HOLD = 7;
63 private static final int MSG_UNHOLD = 8;
64 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
65 private static final int MSG_PLAY_DTMF_TONE = 10;
66 private static final int MSG_STOP_DTMF_TONE = 11;
67 private static final int MSG_CONFERENCE = 12;
68 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070069 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070070 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -070071 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -070072 private static final int MSG_MERGE_CONFERENCE = 18;
73 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070074
Sailesh Nepalcf7020b2014-08-20 10:07:19 -070075 private static Connection sNullConnection;
76
Ihab Awad542e0ea2014-05-16 10:22:16 -070077 private final Map<String, Connection> mConnectionById = new HashMap<>();
78 private final Map<Connection, String> mIdByConnection = new HashMap<>();
Santos Cordon823fd3c2014-08-07 18:35:18 -070079 private final Map<String, Conference> mConferenceById = new HashMap<>();
80 private final Map<Conference, String> mIdByConference = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070081 private final RemoteConnectionManager mRemoteConnectionManager =
82 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -070083 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070084 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -070085
Santos Cordon823fd3c2014-08-07 18:35:18 -070086 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -070087 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -070088
Sailesh Nepal2a46b902014-07-04 17:21:07 -070089 private final IBinder mBinder = new IConnectionService.Stub() {
90 @Override
91 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -070092 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
93 }
94
95 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
96 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070097 }
98
99 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700100 public void createConnection(
101 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700102 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700103 ConnectionRequest request,
104 boolean isIncoming) {
105 SomeArgs args = SomeArgs.obtain();
106 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700107 args.arg2 = id;
108 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700109 args.argi1 = isIncoming ? 1 : 0;
110 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700111 }
112
113 @Override
114 public void abort(String callId) {
115 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
116 }
117
118 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700119 /** @hide */
120 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700121 SomeArgs args = SomeArgs.obtain();
122 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700123 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700124 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
125 }
126
127 @Override
128 public void answer(String callId) {
129 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700130 }
131
132 @Override
133 public void reject(String callId) {
134 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
135 }
136
137 @Override
138 public void disconnect(String callId) {
139 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
140 }
141
142 @Override
143 public void hold(String callId) {
144 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
145 }
146
147 @Override
148 public void unhold(String callId) {
149 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
150 }
151
152 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700153 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700154 SomeArgs args = SomeArgs.obtain();
155 args.arg1 = callId;
156 args.arg2 = audioState;
157 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
158 }
159
160 @Override
161 public void playDtmfTone(String callId, char digit) {
162 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
163 }
164
165 @Override
166 public void stopDtmfTone(String callId) {
167 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
168 }
169
170 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700171 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700172 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700173 args.arg1 = callId1;
174 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700175 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
176 }
177
178 @Override
179 public void splitFromConference(String callId) {
180 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
181 }
182
183 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700184 public void mergeConference(String callId) {
185 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
186 }
187
188 @Override
189 public void swapConference(String callId) {
190 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
191 }
192
193 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700194 public void onPostDialContinue(String callId, boolean proceed) {
195 SomeArgs args = SomeArgs.obtain();
196 args.arg1 = callId;
197 args.argi1 = proceed ? 1 : 0;
198 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
199 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700200 };
201
202 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
203 @Override
204 public void handleMessage(Message msg) {
205 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700206 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700207 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
208 onAdapterAttached();
209 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700210 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
211 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
212 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700213 case MSG_CREATE_CONNECTION: {
214 SomeArgs args = (SomeArgs) msg.obj;
215 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700216 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700217 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700218 final String id = (String) args.arg2;
219 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700220 final boolean isIncoming = args.argi1 == 1;
221 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700222 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700223 mPreInitializationConnectionRequests.add(new Runnable() {
224 @Override
225 public void run() {
226 createConnection(
227 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700228 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700229 request,
230 isIncoming);
231 }
232 });
233 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700234 createConnection(
235 connectionManagerPhoneAccount,
236 id,
237 request,
238 isIncoming);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700239 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700240 } finally {
241 args.recycle();
242 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700243 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700244 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700245 case MSG_ABORT:
246 abort((String) msg.obj);
247 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700248 case MSG_ANSWER:
249 answer((String) msg.obj);
250 break;
251 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700252 SomeArgs args = (SomeArgs) msg.obj;
253 try {
254 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700255 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700256 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700257 } finally {
258 args.recycle();
259 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700260 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700261 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700262 case MSG_REJECT:
263 reject((String) msg.obj);
264 break;
265 case MSG_DISCONNECT:
266 disconnect((String) msg.obj);
267 break;
268 case MSG_HOLD:
269 hold((String) msg.obj);
270 break;
271 case MSG_UNHOLD:
272 unhold((String) msg.obj);
273 break;
274 case MSG_ON_AUDIO_STATE_CHANGED: {
275 SomeArgs args = (SomeArgs) msg.obj;
276 try {
277 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700278 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700279 onAudioStateChanged(callId, audioState);
280 } finally {
281 args.recycle();
282 }
283 break;
284 }
285 case MSG_PLAY_DTMF_TONE:
286 playDtmfTone((String) msg.obj, (char) msg.arg1);
287 break;
288 case MSG_STOP_DTMF_TONE:
289 stopDtmfTone((String) msg.obj);
290 break;
291 case MSG_CONFERENCE: {
292 SomeArgs args = (SomeArgs) msg.obj;
293 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700294 String callId1 = (String) args.arg1;
295 String callId2 = (String) args.arg2;
296 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700297 } finally {
298 args.recycle();
299 }
300 break;
301 }
302 case MSG_SPLIT_FROM_CONFERENCE:
303 splitFromConference((String) msg.obj);
304 break;
Santos Cordona4868042014-09-04 17:39:22 -0700305 case MSG_MERGE_CONFERENCE:
306 mergeConference((String) msg.obj);
307 break;
308 case MSG_SWAP_CONFERENCE:
309 swapConference((String) msg.obj);
310 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700311 case MSG_ON_POST_DIAL_CONTINUE: {
312 SomeArgs args = (SomeArgs) msg.obj;
313 try {
314 String callId = (String) args.arg1;
315 boolean proceed = (args.argi1 == 1);
316 onPostDialContinue(callId, proceed);
317 } finally {
318 args.recycle();
319 }
320 break;
321 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700322 default:
323 break;
324 }
325 }
326 };
327
Santos Cordon823fd3c2014-08-07 18:35:18 -0700328 private final Conference.Listener mConferenceListener = new Conference.Listener() {
329 @Override
330 public void onStateChanged(Conference conference, int oldState, int newState) {
331 String id = mIdByConference.get(conference);
332 switch (newState) {
333 case Connection.STATE_ACTIVE:
334 mAdapter.setActive(id);
335 break;
336 case Connection.STATE_HOLDING:
337 mAdapter.setOnHold(id);
338 break;
339 case Connection.STATE_DISCONNECTED:
340 // handled by onDisconnected
341 break;
342 }
343 }
344
345 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700346 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700347 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700348 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700349 }
350
351 @Override
352 public void onConnectionAdded(Conference conference, Connection connection) {
353 }
354
355 @Override
356 public void onConnectionRemoved(Conference conference, Connection connection) {
357 }
358
359 @Override
360 public void onDestroyed(Conference conference) {
361 removeConference(conference);
362 }
363
364 @Override
365 public void onCapabilitiesChanged(Conference conference, int capabilities) {
366 String id = mIdByConference.get(conference);
367 Log.d(this, "call capabilities: conference: %s",
368 PhoneCapabilities.toString(capabilities));
369 mAdapter.setCallCapabilities(id, capabilities);
370 }
371 };
372
Ihab Awad542e0ea2014-05-16 10:22:16 -0700373 private final Connection.Listener mConnectionListener = new Connection.Listener() {
374 @Override
375 public void onStateChanged(Connection c, int state) {
376 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700377 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700378 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700379 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700380 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700381 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700382 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700383 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700384 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700385 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700386 // Handled in onDisconnected()
387 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700388 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700389 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700390 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700391 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700392 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700393 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700394 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700395 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700396 break;
397 }
398 }
399
400 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700401 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700402 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700403 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700404 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700405 }
406
407 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700408 public void onVideoStateChanged(Connection c, int videoState) {
409 String id = mIdByConnection.get(c);
410 Log.d(this, "Adapter set video state %d", videoState);
411 mAdapter.setVideoState(id, videoState);
412 }
413
414 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700415 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700416 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700417 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700418 }
419
420 @Override
421 public void onCallerDisplayNameChanged(
422 Connection c, String callerDisplayName, int presentation) {
423 String id = mIdByConnection.get(c);
424 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700425 }
426
427 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700428 public void onDestroyed(Connection c) {
429 removeConnection(c);
430 }
Ihab Awadf8358972014-05-28 16:46:42 -0700431
432 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700433 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700434 String id = mIdByConnection.get(c);
435 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700436 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700437 }
438
439 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700440 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700441 String id = mIdByConnection.get(c);
442 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700443 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700444 }
Santos Cordonb6939982014-06-04 20:20:58 -0700445
446 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700447 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700448 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700449 Log.d(this, "capabilities: parcelableconnection: %s",
450 PhoneCapabilities.toString(capabilities));
451 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700452 }
453
Santos Cordonb6939982014-06-04 20:20:58 -0700454 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700455 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700456 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700457 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700458 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700459
460 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700461 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700462 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700463 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700464 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700465
466 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700467 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700468 String id = mIdByConnection.get(c);
469 mAdapter.setStatusHints(id, statusHints);
470 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700471
472 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700473 public void onConferenceableConnectionsChanged(
474 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700475 mAdapter.setConferenceableConnections(
476 mIdByConnection.get(connection),
477 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700478 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700479
480 @Override
481 public void onConferenceChanged(Connection connection, Conference conference) {
482 String id = mIdByConnection.get(connection);
483 if (id != null) {
484 String conferenceId = null;
485 if (conference != null) {
486 conferenceId = mIdByConference.get(conference);
487 }
488 mAdapter.setIsConferenced(id, conferenceId);
489 }
490 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700491 };
492
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700493 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700494 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700495 public final IBinder onBind(Intent intent) {
496 return mBinder;
497 }
498
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700499 /** {@inheritDoc} */
500 @Override
501 public boolean onUnbind(Intent intent) {
502 endAllConnections();
503 return super.onUnbind(intent);
504 }
505
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700506 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700507 * This can be used by telecom to either create a new outgoing call or attach to an existing
508 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700509 * createConnection util a connection service cancels the process or completes it successfully.
510 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700511 private void createConnection(
512 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700513 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700514 final ConnectionRequest request,
515 boolean isIncoming) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700516 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
517 "isIncoming: %b", callManagerAccount, callId, request, isIncoming);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700518
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700519 Connection connection = isIncoming
Ihab Awad6107bab2014-08-18 09:23:25 -0700520 ? onCreateIncomingConnection(callManagerAccount, request)
521 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700522 Log.d(this, "createConnection, connection: %s", connection);
523 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700524 connection = Connection.createFailedConnection(
525 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700526 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700527
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700528 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700529 addConnection(callId, connection);
530 }
531
Andrew Lee100e2932014-09-08 15:34:24 -0700532 Uri address = connection.getAddress();
533 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700534 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700535 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700536 Connection.stateToString(connection.getState()),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700537 PhoneCapabilities.toString(connection.getCallCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700538
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700539 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700540 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700541 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700542 request,
543 new ParcelableConnection(
544 request.getAccountHandle(),
545 connection.getState(),
546 connection.getCallCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700547 connection.getAddress(),
548 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700549 connection.getCallerDisplayName(),
550 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700551 connection.getVideoProvider() == null ?
552 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700553 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700554 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700555 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700556 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700557 connection.getDisconnectCause(),
Ihab Awadb8e85c72014-08-23 20:34:57 -0700558 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700559 }
560
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700561 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700562 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700563 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700564 }
565
Tyler Gunnbe74de02014-08-29 14:51:48 -0700566 private void answerVideo(String callId, int videoState) {
567 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700568 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700569 }
570
Tyler Gunnbe74de02014-08-29 14:51:48 -0700571 private void answer(String callId) {
572 Log.d(this, "answer %s", callId);
573 findConnectionForAction(callId, "answer").onAnswer();
574 }
575
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700576 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700577 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700578 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700579 }
580
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700581 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700582 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700583 if (mConnectionById.containsKey(callId)) {
584 findConnectionForAction(callId, "disconnect").onDisconnect();
585 } else {
586 findConferenceForAction(callId, "disconnect").onDisconnect();
587 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700588 }
589
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700590 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700591 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700592 if (mConnectionById.containsKey(callId)) {
593 findConnectionForAction(callId, "hold").onHold();
594 } else {
595 findConferenceForAction(callId, "hold").onHold();
596 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700597 }
598
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700599 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700600 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700601 if (mConnectionById.containsKey(callId)) {
602 findConnectionForAction(callId, "unhold").onUnhold();
603 } else {
604 findConferenceForAction(callId, "unhold").onUnhold();
605 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700606 }
607
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700608 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700609 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700610 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
611 }
612
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700613 private void playDtmfTone(String callId, char digit) {
614 Log.d(this, "playDtmfTone %s %c", callId, digit);
615 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
616 }
617
618 private void stopDtmfTone(String callId) {
619 Log.d(this, "stopDtmfTone %s", callId);
620 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
621 }
622
Santos Cordon823fd3c2014-08-07 18:35:18 -0700623 private void conference(String callId1, String callId2) {
624 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700625
Santos Cordon823fd3c2014-08-07 18:35:18 -0700626 Connection connection1 = findConnectionForAction(callId1, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700627 if (connection1 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700628 Log.w(this, "Connection1 missing in conference request %s.", callId1);
Santos Cordonb6939982014-06-04 20:20:58 -0700629 return;
Santos Cordon980acb92014-05-31 10:31:19 -0700630 }
631
Santos Cordon823fd3c2014-08-07 18:35:18 -0700632 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700633 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700634 Log.w(this, "Connection2 missing in conference request %s.", callId2);
635 return;
636 }
Santos Cordonb6939982014-06-04 20:20:58 -0700637
Santos Cordon823fd3c2014-08-07 18:35:18 -0700638 onConference(connection1, connection2);
Santos Cordon980acb92014-05-31 10:31:19 -0700639 }
640
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700641 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700642 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700643
644 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700645 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700646 Log.w(this, "Connection missing in conference request %s.", callId);
647 return;
648 }
649
Santos Cordon0159ac02014-08-21 14:28:11 -0700650 Conference conference = connection.getConference();
651 if (conference != null) {
652 conference.onSeparate(connection);
653 }
Santos Cordon980acb92014-05-31 10:31:19 -0700654 }
655
Santos Cordona4868042014-09-04 17:39:22 -0700656 private void mergeConference(String callId) {
657 Log.d(this, "mergeConference(%s)", callId);
658 Conference conference = findConferenceForAction(callId, "mergeConference");
659 if (conference != null) {
660 conference.onMerge();
661 }
662 }
663
664 private void swapConference(String callId) {
665 Log.d(this, "swapConference(%s)", callId);
666 Conference conference = findConferenceForAction(callId, "swapConference");
667 if (conference != null) {
668 conference.onSwap();
669 }
670 }
671
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700672 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700673 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700674 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700675 }
676
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700677 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700678 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700679 // No need to query again if we already did it.
680 return;
681 }
682
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700683 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700684 @Override
685 public void onResult(
686 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700687 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700688 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700689 @Override
690 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700691 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700692 mRemoteConnectionManager.addConnectionService(
693 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700694 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700695 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700696 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700697 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700698 }
699 });
700 }
701
702 @Override
703 public void onError() {
704 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700705 @Override
706 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700707 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700708 }
709 });
710 }
711 });
712 }
713
Ihab Awadf8b69882014-07-25 15:14:01 -0700714 /**
715 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
716 * incoming request. This is used to attach to existing incoming calls.
717 *
718 * @param connectionManagerPhoneAccount See description at
719 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
720 * @param request Details about the incoming call.
721 * @return The {@code Connection} object to satisfy this call, or {@code null} to
722 * not handle the call.
723 */
724 public final RemoteConnection createRemoteIncomingConnection(
725 PhoneAccountHandle connectionManagerPhoneAccount,
726 ConnectionRequest request) {
727 return mRemoteConnectionManager.createRemoteConnection(
728 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700729 }
730
731 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700732 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
733 * outgoing request. This is used to initiate new outgoing calls.
734 *
735 * @param connectionManagerPhoneAccount See description at
736 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
737 * @param request Details about the incoming call.
738 * @return The {@code Connection} object to satisfy this call, or {@code null} to
739 * not handle the call.
740 */
741 public final RemoteConnection createRemoteOutgoingConnection(
742 PhoneAccountHandle connectionManagerPhoneAccount,
743 ConnectionRequest request) {
744 return mRemoteConnectionManager.createRemoteConnection(
745 connectionManagerPhoneAccount, request, false);
746 }
747
748 /**
Ihab Awadb8e85c72014-08-23 20:34:57 -0700749 * Adds two {@code RemoteConnection}s to some {@code RemoteConference}.
750 */
751 public final void conferenceRemoteConnections(
752 RemoteConnection a,
753 RemoteConnection b) {
754 mRemoteConnectionManager.conferenceRemoteConnections(a, b);
755 }
756
757 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700758 * Adds a new conference call. When a conference call is created either as a result of an
759 * explicit request via {@link #onConference} or otherwise, the connection service should supply
760 * an instance of {@link Conference} by invoking this method. A conference call provided by this
761 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
762 *
763 * @param conference The new conference object.
764 */
765 public final void addConference(Conference conference) {
766 String id = addConferenceInternal(conference);
767 if (id != null) {
768 List<String> connectionIds = new ArrayList<>(2);
769 for (Connection connection : conference.getConnections()) {
770 if (mIdByConnection.containsKey(connection)) {
771 connectionIds.add(mIdByConnection.get(connection));
772 }
773 }
774 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700775 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700776 conference.getState(),
777 conference.getCapabilities(),
778 connectionIds);
779 mAdapter.addConferenceCall(id, parcelableConference);
780
781 // Go through any child calls and set the parent.
782 for (Connection connection : conference.getConnections()) {
783 String connectionId = mIdByConnection.get(connection);
784 if (connectionId != null) {
785 mAdapter.setIsConferenced(connectionId, id);
786 }
787 }
788 }
789 }
790
791 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700792 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
793 * has taken responsibility.
794 *
795 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700796 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700797 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700798 return mConnectionById.values();
799 }
800
801 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700802 * Create a {@code Connection} given an incoming request. This is used to attach to existing
803 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700804 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700805 * @param connectionManagerPhoneAccount See description at
806 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
807 * @param request Details about the incoming call.
808 * @return The {@code Connection} object to satisfy this call, or {@code null} to
809 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700810 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700811 public Connection onCreateIncomingConnection(
812 PhoneAccountHandle connectionManagerPhoneAccount,
813 ConnectionRequest request) {
814 return null;
815 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700816
817 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700818 * Create a {@code Connection} given an outgoing request. This is used to initiate new
819 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700820 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700821 * @param connectionManagerPhoneAccount The connection manager account to use for managing
822 * this call.
823 * <p>
824 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
825 * has registered one or more {@code PhoneAccount}s having
826 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
827 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
828 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
829 * making the connection.
830 * <p>
831 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
832 * being asked to make a direct connection. The
833 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
834 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
835 * making the connection.
836 * @param request Details about the outgoing call.
837 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700838 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700839 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700840 public Connection onCreateOutgoingConnection(
841 PhoneAccountHandle connectionManagerPhoneAccount,
842 ConnectionRequest request) {
843 return null;
844 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700845
846 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700847 * Conference two specified connections. Invoked when the user has made a request to merge the
848 * specified connections into a conference call. In response, the connection service should
849 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700850 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700851 * @param connection1 A connection to merge into a conference call.
852 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700853 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700854 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700855
Ihab Awadb8e85c72014-08-23 20:34:57 -0700856 public void onRemoteConferenceAdded(RemoteConference conference) {}
857
Santos Cordon823fd3c2014-08-07 18:35:18 -0700858 /**
859 * @hide
860 */
861 public boolean containsConference(Conference conference) {
862 return mIdByConference.containsKey(conference);
863 }
864
Ihab Awadb8e85c72014-08-23 20:34:57 -0700865 /** {@hide} */
866 void addRemoteConference(RemoteConference remoteConference) {
867 onRemoteConferenceAdded(remoteConference);
868 }
869
Ihab Awad5d0410f2014-07-30 10:07:40 -0700870 private void onAccountsInitialized() {
871 mAreAccountsInitialized = true;
872 for (Runnable r : mPreInitializationConnectionRequests) {
873 r.run();
874 }
875 mPreInitializationConnectionRequests.clear();
876 }
877
Ihab Awad542e0ea2014-05-16 10:22:16 -0700878 private void addConnection(String callId, Connection connection) {
879 mConnectionById.put(callId, connection);
880 mIdByConnection.put(connection, callId);
881 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700882 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700883 }
884
885 private void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700886 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700887 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700888 connection.removeConnectionListener(mConnectionListener);
889 mConnectionById.remove(mIdByConnection.get(connection));
890 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700891 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700892 }
893
Santos Cordon823fd3c2014-08-07 18:35:18 -0700894 private String addConferenceInternal(Conference conference) {
895 if (mIdByConference.containsKey(conference)) {
896 Log.w(this, "Re-adding an existing conference: %s.", conference);
897 } else if (conference != null) {
898 String id = UUID.randomUUID().toString();
899 mConferenceById.put(id, conference);
900 mIdByConference.put(conference, id);
901 conference.addListener(mConferenceListener);
902 return id;
903 }
904
905 return null;
906 }
907
908 private void removeConference(Conference conference) {
909 if (mIdByConference.containsKey(conference)) {
910 conference.removeListener(mConferenceListener);
911
912 String id = mIdByConference.get(conference);
913 mConferenceById.remove(id);
914 mIdByConference.remove(conference);
915 mAdapter.removeCall(id);
916 }
917 }
918
Ihab Awad542e0ea2014-05-16 10:22:16 -0700919 private Connection findConnectionForAction(String callId, String action) {
920 if (mConnectionById.containsKey(callId)) {
921 return mConnectionById.get(callId);
922 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700923 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700924 return getNullConnection();
925 }
926
927 static synchronized Connection getNullConnection() {
928 if (sNullConnection == null) {
929 sNullConnection = new Connection() {};
930 }
931 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700932 }
Santos Cordon0159ac02014-08-21 14:28:11 -0700933
934 private Conference findConferenceForAction(String conferenceId, String action) {
935 if (mConferenceById.containsKey(conferenceId)) {
936 return mConferenceById.get(conferenceId);
937 }
938 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
939 return getNullConference();
940 }
941
Ihab Awadb8e85c72014-08-23 20:34:57 -0700942 private List<String> createConnectionIdList(List<Connection> connections) {
943 List<String> ids = new ArrayList<>();
944 for (Connection c : connections) {
945 if (mIdByConnection.containsKey(c)) {
946 ids.add(mIdByConnection.get(c));
947 }
948 }
949 Collections.sort(ids);
950 return ids;
951 }
952
Santos Cordon0159ac02014-08-21 14:28:11 -0700953 private Conference getNullConference() {
954 if (sNullConference == null) {
955 sNullConference = new Conference(null) {};
956 }
957 return sNullConference;
958 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700959
960 private void endAllConnections() {
961 // Unbound from telecomm. We should end all connections and conferences.
962 for (Connection connection : mIdByConnection.keySet()) {
963 // only operate on top-level calls. Conference calls will be removed on their own.
964 if (connection.getConference() == null) {
965 connection.onDisconnect();
966 }
967 }
968 for (Conference conference : mIdByConference.keySet()) {
969 conference.onDisconnect();
970 }
971 }
Santos Cordon980acb92014-05-31 10:31:19 -0700972}