blob: efd311e8ca3cd1e45914101c4dd8f5addc479f0b [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
Ihab Awad50e35062014-09-30 09:17:03 -0700363 public void onConferenceableConnectionsChanged(
364 Conference conference, List<Connection> conferenceableConnections) {
365 mAdapter.setConferenceableConnections(
366 mIdByConference.get(conference),
367 createConnectionIdList(conferenceableConnections));
368 }
369
370 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700371 public void onDestroyed(Conference conference) {
372 removeConference(conference);
373 }
374
375 @Override
376 public void onCapabilitiesChanged(Conference conference, int capabilities) {
377 String id = mIdByConference.get(conference);
378 Log.d(this, "call capabilities: conference: %s",
379 PhoneCapabilities.toString(capabilities));
380 mAdapter.setCallCapabilities(id, capabilities);
381 }
382 };
383
Ihab Awad542e0ea2014-05-16 10:22:16 -0700384 private final Connection.Listener mConnectionListener = new Connection.Listener() {
385 @Override
386 public void onStateChanged(Connection c, int state) {
387 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700388 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700389 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700390 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700391 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700392 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700393 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700394 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700395 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700396 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700397 // Handled in onDisconnected()
398 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700399 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700400 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700401 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700402 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700403 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700404 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700405 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700406 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700407 break;
408 }
409 }
410
411 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700412 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700413 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700414 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700415 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700416 }
417
418 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700419 public void onVideoStateChanged(Connection c, int videoState) {
420 String id = mIdByConnection.get(c);
421 Log.d(this, "Adapter set video state %d", videoState);
422 mAdapter.setVideoState(id, videoState);
423 }
424
425 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700426 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700427 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700428 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700429 }
430
431 @Override
432 public void onCallerDisplayNameChanged(
433 Connection c, String callerDisplayName, int presentation) {
434 String id = mIdByConnection.get(c);
435 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700436 }
437
438 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700439 public void onDestroyed(Connection c) {
440 removeConnection(c);
441 }
Ihab Awadf8358972014-05-28 16:46:42 -0700442
443 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700444 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700445 String id = mIdByConnection.get(c);
446 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700447 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700448 }
449
450 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700451 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700452 String id = mIdByConnection.get(c);
453 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700454 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700455 }
Santos Cordonb6939982014-06-04 20:20:58 -0700456
457 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700458 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700459 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700460 Log.d(this, "capabilities: parcelableconnection: %s",
461 PhoneCapabilities.toString(capabilities));
462 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700463 }
464
Santos Cordonb6939982014-06-04 20:20:58 -0700465 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700466 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700467 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700468 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700469 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700470
471 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700472 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700473 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700474 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700475 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700476
477 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700478 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700479 String id = mIdByConnection.get(c);
480 mAdapter.setStatusHints(id, statusHints);
481 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700482
483 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700484 public void onConferenceableConnectionsChanged(
485 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700486 mAdapter.setConferenceableConnections(
487 mIdByConnection.get(connection),
488 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700489 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700490
491 @Override
492 public void onConferenceChanged(Connection connection, Conference conference) {
493 String id = mIdByConnection.get(connection);
494 if (id != null) {
495 String conferenceId = null;
496 if (conference != null) {
497 conferenceId = mIdByConference.get(conference);
498 }
499 mAdapter.setIsConferenced(id, conferenceId);
500 }
501 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700502 };
503
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700504 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700505 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700506 public final IBinder onBind(Intent intent) {
507 return mBinder;
508 }
509
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700510 /** {@inheritDoc} */
511 @Override
512 public boolean onUnbind(Intent intent) {
513 endAllConnections();
514 return super.onUnbind(intent);
515 }
516
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700517 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700518 * This can be used by telecom to either create a new outgoing call or attach to an existing
519 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700520 * createConnection util a connection service cancels the process or completes it successfully.
521 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700522 private void createConnection(
523 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700524 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700525 final ConnectionRequest request,
526 boolean isIncoming) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700527 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
528 "isIncoming: %b", callManagerAccount, callId, request, isIncoming);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700529
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700530 Connection connection = isIncoming
Ihab Awad6107bab2014-08-18 09:23:25 -0700531 ? onCreateIncomingConnection(callManagerAccount, request)
532 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700533 Log.d(this, "createConnection, connection: %s", connection);
534 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700535 connection = Connection.createFailedConnection(
536 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700537 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700538
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700539 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700540 addConnection(callId, connection);
541 }
542
Andrew Lee100e2932014-09-08 15:34:24 -0700543 Uri address = connection.getAddress();
544 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700545 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700546 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700547 Connection.stateToString(connection.getState()),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700548 PhoneCapabilities.toString(connection.getCallCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700549
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700550 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700551 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700552 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700553 request,
554 new ParcelableConnection(
555 request.getAccountHandle(),
556 connection.getState(),
557 connection.getCallCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700558 connection.getAddress(),
559 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700560 connection.getCallerDisplayName(),
561 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700562 connection.getVideoProvider() == null ?
563 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700564 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700565 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700566 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700567 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700568 connection.getDisconnectCause(),
Ihab Awadb8e85c72014-08-23 20:34:57 -0700569 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700570 }
571
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700572 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700573 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700574 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700575 }
576
Tyler Gunnbe74de02014-08-29 14:51:48 -0700577 private void answerVideo(String callId, int videoState) {
578 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700579 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700580 }
581
Tyler Gunnbe74de02014-08-29 14:51:48 -0700582 private void answer(String callId) {
583 Log.d(this, "answer %s", callId);
584 findConnectionForAction(callId, "answer").onAnswer();
585 }
586
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700587 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700588 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700589 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700590 }
591
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700592 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700593 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700594 if (mConnectionById.containsKey(callId)) {
595 findConnectionForAction(callId, "disconnect").onDisconnect();
596 } else {
597 findConferenceForAction(callId, "disconnect").onDisconnect();
598 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700599 }
600
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700601 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700602 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700603 if (mConnectionById.containsKey(callId)) {
604 findConnectionForAction(callId, "hold").onHold();
605 } else {
606 findConferenceForAction(callId, "hold").onHold();
607 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700608 }
609
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700610 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700611 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700612 if (mConnectionById.containsKey(callId)) {
613 findConnectionForAction(callId, "unhold").onUnhold();
614 } else {
615 findConferenceForAction(callId, "unhold").onUnhold();
616 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700617 }
618
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700619 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700620 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700621 if (mConnectionById.containsKey(callId)) {
622 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
623 } else {
624 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
625 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700626 }
627
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700628 private void playDtmfTone(String callId, char digit) {
629 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700630 if (mConnectionById.containsKey(callId)) {
631 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
632 } else {
633 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
634 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700635 }
636
637 private void stopDtmfTone(String callId) {
638 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700639 if (mConnectionById.containsKey(callId)) {
640 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
641 } else {
642 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
643 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700644 }
645
Santos Cordon823fd3c2014-08-07 18:35:18 -0700646 private void conference(String callId1, String callId2) {
647 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700648
Santos Cordon823fd3c2014-08-07 18:35:18 -0700649 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700650 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700651 Log.w(this, "Connection2 missing in conference request %s.", callId2);
652 return;
653 }
Santos Cordonb6939982014-06-04 20:20:58 -0700654
Ihab Awad50e35062014-09-30 09:17:03 -0700655 Connection connection1 = findConnectionForAction(callId1, "conference");
656 if (connection1 == getNullConnection()) {
657 Conference conference1 = findConferenceForAction(callId1, "addConnection");
658 if (conference1 == getNullConference()) {
659 Log.w(this,
660 "Connection1 or Conference1 missing in conference request %s.",
661 callId1);
662 } else {
663 conference1.onMerge(connection2);
664 }
665 } else {
666 onConference(connection1, connection2);
667 }
Santos Cordon980acb92014-05-31 10:31:19 -0700668 }
669
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700670 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700671 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700672
673 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700674 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700675 Log.w(this, "Connection missing in conference request %s.", callId);
676 return;
677 }
678
Santos Cordon0159ac02014-08-21 14:28:11 -0700679 Conference conference = connection.getConference();
680 if (conference != null) {
681 conference.onSeparate(connection);
682 }
Santos Cordon980acb92014-05-31 10:31:19 -0700683 }
684
Santos Cordona4868042014-09-04 17:39:22 -0700685 private void mergeConference(String callId) {
686 Log.d(this, "mergeConference(%s)", callId);
687 Conference conference = findConferenceForAction(callId, "mergeConference");
688 if (conference != null) {
689 conference.onMerge();
690 }
691 }
692
693 private void swapConference(String callId) {
694 Log.d(this, "swapConference(%s)", callId);
695 Conference conference = findConferenceForAction(callId, "swapConference");
696 if (conference != null) {
697 conference.onSwap();
698 }
699 }
700
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700701 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700702 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700703 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700704 }
705
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700706 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700707 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700708 // No need to query again if we already did it.
709 return;
710 }
711
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700712 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700713 @Override
714 public void onResult(
715 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700716 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700717 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700718 @Override
719 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700720 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700721 mRemoteConnectionManager.addConnectionService(
722 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700723 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700724 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700725 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700726 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700727 }
728 });
729 }
730
731 @Override
732 public void onError() {
733 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700734 @Override
735 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700736 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700737 }
738 });
739 }
740 });
741 }
742
Ihab Awadf8b69882014-07-25 15:14:01 -0700743 /**
744 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
745 * incoming request. This is used to attach to existing incoming calls.
746 *
747 * @param connectionManagerPhoneAccount See description at
748 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
749 * @param request Details about the incoming call.
750 * @return The {@code Connection} object to satisfy this call, or {@code null} to
751 * not handle the call.
752 */
753 public final RemoteConnection createRemoteIncomingConnection(
754 PhoneAccountHandle connectionManagerPhoneAccount,
755 ConnectionRequest request) {
756 return mRemoteConnectionManager.createRemoteConnection(
757 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700758 }
759
760 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700761 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
762 * outgoing request. This is used to initiate new outgoing calls.
763 *
764 * @param connectionManagerPhoneAccount See description at
765 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
766 * @param request Details about the incoming call.
767 * @return The {@code Connection} object to satisfy this call, or {@code null} to
768 * not handle the call.
769 */
770 public final RemoteConnection createRemoteOutgoingConnection(
771 PhoneAccountHandle connectionManagerPhoneAccount,
772 ConnectionRequest request) {
773 return mRemoteConnectionManager.createRemoteConnection(
774 connectionManagerPhoneAccount, request, false);
775 }
776
777 /**
Ihab Awadb8e85c72014-08-23 20:34:57 -0700778 * Adds two {@code RemoteConnection}s to some {@code RemoteConference}.
779 */
780 public final void conferenceRemoteConnections(
781 RemoteConnection a,
782 RemoteConnection b) {
783 mRemoteConnectionManager.conferenceRemoteConnections(a, b);
784 }
785
786 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700787 * Adds a new conference call. When a conference call is created either as a result of an
788 * explicit request via {@link #onConference} or otherwise, the connection service should supply
789 * an instance of {@link Conference} by invoking this method. A conference call provided by this
790 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
791 *
792 * @param conference The new conference object.
793 */
794 public final void addConference(Conference conference) {
795 String id = addConferenceInternal(conference);
796 if (id != null) {
797 List<String> connectionIds = new ArrayList<>(2);
798 for (Connection connection : conference.getConnections()) {
799 if (mIdByConnection.containsKey(connection)) {
800 connectionIds.add(mIdByConnection.get(connection));
801 }
802 }
803 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700804 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700805 conference.getState(),
806 conference.getCapabilities(),
807 connectionIds);
808 mAdapter.addConferenceCall(id, parcelableConference);
809
810 // Go through any child calls and set the parent.
811 for (Connection connection : conference.getConnections()) {
812 String connectionId = mIdByConnection.get(connection);
813 if (connectionId != null) {
814 mAdapter.setIsConferenced(connectionId, id);
815 }
816 }
817 }
818 }
819
820 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700821 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
822 * has taken responsibility.
823 *
824 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700825 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700826 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700827 return mConnectionById.values();
828 }
829
830 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700831 * Create a {@code Connection} given an incoming request. This is used to attach to existing
832 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700833 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700834 * @param connectionManagerPhoneAccount See description at
835 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
836 * @param request Details about the incoming call.
837 * @return The {@code Connection} object to satisfy this call, or {@code null} to
838 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700839 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700840 public Connection onCreateIncomingConnection(
841 PhoneAccountHandle connectionManagerPhoneAccount,
842 ConnectionRequest request) {
843 return null;
844 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700845
846 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700847 * Create a {@code Connection} given an outgoing request. This is used to initiate new
848 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700849 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700850 * @param connectionManagerPhoneAccount The connection manager account to use for managing
851 * this call.
852 * <p>
853 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
854 * has registered one or more {@code PhoneAccount}s having
855 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
856 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
857 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
858 * making the connection.
859 * <p>
860 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
861 * being asked to make a direct connection. The
862 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
863 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
864 * making the connection.
865 * @param request Details about the outgoing call.
866 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700867 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700868 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700869 public Connection onCreateOutgoingConnection(
870 PhoneAccountHandle connectionManagerPhoneAccount,
871 ConnectionRequest request) {
872 return null;
873 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700874
875 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700876 * Conference two specified connections. Invoked when the user has made a request to merge the
877 * specified connections into a conference call. In response, the connection service should
878 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700879 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700880 * @param connection1 A connection to merge into a conference call.
881 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700882 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700883 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700884
Ihab Awadb8e85c72014-08-23 20:34:57 -0700885 public void onRemoteConferenceAdded(RemoteConference conference) {}
886
Santos Cordon823fd3c2014-08-07 18:35:18 -0700887 /**
888 * @hide
889 */
890 public boolean containsConference(Conference conference) {
891 return mIdByConference.containsKey(conference);
892 }
893
Ihab Awadb8e85c72014-08-23 20:34:57 -0700894 /** {@hide} */
895 void addRemoteConference(RemoteConference remoteConference) {
896 onRemoteConferenceAdded(remoteConference);
897 }
898
Ihab Awad5d0410f2014-07-30 10:07:40 -0700899 private void onAccountsInitialized() {
900 mAreAccountsInitialized = true;
901 for (Runnable r : mPreInitializationConnectionRequests) {
902 r.run();
903 }
904 mPreInitializationConnectionRequests.clear();
905 }
906
Ihab Awad542e0ea2014-05-16 10:22:16 -0700907 private void addConnection(String callId, Connection connection) {
908 mConnectionById.put(callId, connection);
909 mIdByConnection.put(connection, callId);
910 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700911 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700912 }
913
914 private void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700915 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700916 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700917 connection.removeConnectionListener(mConnectionListener);
918 mConnectionById.remove(mIdByConnection.get(connection));
919 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700920 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700921 }
922
Santos Cordon823fd3c2014-08-07 18:35:18 -0700923 private String addConferenceInternal(Conference conference) {
924 if (mIdByConference.containsKey(conference)) {
925 Log.w(this, "Re-adding an existing conference: %s.", conference);
926 } else if (conference != null) {
927 String id = UUID.randomUUID().toString();
928 mConferenceById.put(id, conference);
929 mIdByConference.put(conference, id);
930 conference.addListener(mConferenceListener);
931 return id;
932 }
933
934 return null;
935 }
936
937 private void removeConference(Conference conference) {
938 if (mIdByConference.containsKey(conference)) {
939 conference.removeListener(mConferenceListener);
940
941 String id = mIdByConference.get(conference);
942 mConferenceById.remove(id);
943 mIdByConference.remove(conference);
944 mAdapter.removeCall(id);
945 }
946 }
947
Ihab Awad542e0ea2014-05-16 10:22:16 -0700948 private Connection findConnectionForAction(String callId, String action) {
949 if (mConnectionById.containsKey(callId)) {
950 return mConnectionById.get(callId);
951 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700952 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700953 return getNullConnection();
954 }
955
956 static synchronized Connection getNullConnection() {
957 if (sNullConnection == null) {
958 sNullConnection = new Connection() {};
959 }
960 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700961 }
Santos Cordon0159ac02014-08-21 14:28:11 -0700962
963 private Conference findConferenceForAction(String conferenceId, String action) {
964 if (mConferenceById.containsKey(conferenceId)) {
965 return mConferenceById.get(conferenceId);
966 }
967 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
968 return getNullConference();
969 }
970
Ihab Awadb8e85c72014-08-23 20:34:57 -0700971 private List<String> createConnectionIdList(List<Connection> connections) {
972 List<String> ids = new ArrayList<>();
973 for (Connection c : connections) {
974 if (mIdByConnection.containsKey(c)) {
975 ids.add(mIdByConnection.get(c));
976 }
977 }
978 Collections.sort(ids);
979 return ids;
980 }
981
Santos Cordon0159ac02014-08-21 14:28:11 -0700982 private Conference getNullConference() {
983 if (sNullConference == null) {
984 sNullConference = new Conference(null) {};
985 }
986 return sNullConference;
987 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700988
989 private void endAllConnections() {
990 // Unbound from telecomm. We should end all connections and conferences.
991 for (Connection connection : mIdByConnection.keySet()) {
992 // only operate on top-level calls. Conference calls will be removed on their own.
993 if (connection.getConference() == null) {
994 connection.onDisconnect();
995 }
996 }
997 for (Conference conference : mIdByConference.keySet()) {
998 conference.onDisconnect();
999 }
1000 }
Santos Cordon980acb92014-05-31 10:31:19 -07001001}