blob: 9f2689017e5c5896bb03e96ecc9097ac34123b2d [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;
Evan Charltonbf11f982014-07-20 22:06:28 -070028import android.telephony.DisconnectCause;
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;
Ihab Awad542e0ea2014-05-16 10:22:16 -070038import java.util.HashMap;
Santos Cordon52d8a152014-06-17 19:08:45 -070039import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070040import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070041import java.util.UUID;
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.
Ihab Awad542e0ea2014-05-16 10:22:16 -070046 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070047public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070048 /**
49 * The {@link Intent} that must be declared as handled by the service.
50 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070051 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070052 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070053
Ihab Awad542e0ea2014-05-16 10:22:16 -070054 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070055 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070056
Ihab Awad8aecfed2014-08-08 17:06:11 -070057 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070058 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070059 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070060 private static final int MSG_ANSWER = 4;
61 private static final int MSG_REJECT = 5;
62 private static final int MSG_DISCONNECT = 6;
63 private static final int MSG_HOLD = 7;
64 private static final int MSG_UNHOLD = 8;
65 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
66 private static final int MSG_PLAY_DTMF_TONE = 10;
67 private static final int MSG_STOP_DTMF_TONE = 11;
68 private static final int MSG_CONFERENCE = 12;
69 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070070 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070071 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -070072 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -070073 private static final int MSG_MERGE_CONFERENCE = 18;
74 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070075
Sailesh Nepalcf7020b2014-08-20 10:07:19 -070076 private static Connection sNullConnection;
77
Ihab Awad542e0ea2014-05-16 10:22:16 -070078 private final Map<String, Connection> mConnectionById = new HashMap<>();
79 private final Map<Connection, String> mIdByConnection = new HashMap<>();
Santos Cordon823fd3c2014-08-07 18:35:18 -070080 private final Map<String, Conference> mConferenceById = new HashMap<>();
81 private final Map<Conference, String> mIdByConference = new HashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070082 private final RemoteConnectionManager mRemoteConnectionManager =
83 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -070084 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070085 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -070086
Santos Cordon823fd3c2014-08-07 18:35:18 -070087 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -070088 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -070089
Sailesh Nepal2a46b902014-07-04 17:21:07 -070090 private final IBinder mBinder = new IConnectionService.Stub() {
91 @Override
92 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -070093 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
94 }
95
96 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
97 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070098 }
99
100 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700101 public void createConnection(
102 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700103 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700104 ConnectionRequest request,
105 boolean isIncoming) {
106 SomeArgs args = SomeArgs.obtain();
107 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700108 args.arg2 = id;
109 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700110 args.argi1 = isIncoming ? 1 : 0;
111 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700112 }
113
114 @Override
115 public void abort(String callId) {
116 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
117 }
118
119 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700120 /** @hide */
121 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700122 SomeArgs args = SomeArgs.obtain();
123 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700124 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700125 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
126 }
127
128 @Override
129 public void answer(String callId) {
130 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700131 }
132
133 @Override
134 public void reject(String callId) {
135 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
136 }
137
138 @Override
139 public void disconnect(String callId) {
140 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
141 }
142
143 @Override
144 public void hold(String callId) {
145 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
146 }
147
148 @Override
149 public void unhold(String callId) {
150 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
151 }
152
153 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700154 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700155 SomeArgs args = SomeArgs.obtain();
156 args.arg1 = callId;
157 args.arg2 = audioState;
158 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
159 }
160
161 @Override
162 public void playDtmfTone(String callId, char digit) {
163 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
164 }
165
166 @Override
167 public void stopDtmfTone(String callId) {
168 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
169 }
170
171 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700172 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700173 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700174 args.arg1 = callId1;
175 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700176 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
177 }
178
179 @Override
180 public void splitFromConference(String callId) {
181 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
182 }
183
184 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700185 public void mergeConference(String callId) {
186 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
187 }
188
189 @Override
190 public void swapConference(String callId) {
191 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
192 }
193
194 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700195 public void onPostDialContinue(String callId, boolean proceed) {
196 SomeArgs args = SomeArgs.obtain();
197 args.arg1 = callId;
198 args.argi1 = proceed ? 1 : 0;
199 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
200 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700201 };
202
203 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
204 @Override
205 public void handleMessage(Message msg) {
206 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700207 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700208 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
209 onAdapterAttached();
210 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700211 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
212 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
213 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700214 case MSG_CREATE_CONNECTION: {
215 SomeArgs args = (SomeArgs) msg.obj;
216 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700217 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700218 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700219 final String id = (String) args.arg2;
220 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700221 final boolean isIncoming = args.argi1 == 1;
222 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700223 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700224 mPreInitializationConnectionRequests.add(new Runnable() {
225 @Override
226 public void run() {
227 createConnection(
228 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700229 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700230 request,
231 isIncoming);
232 }
233 });
234 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700235 createConnection(
236 connectionManagerPhoneAccount,
237 id,
238 request,
239 isIncoming);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700240 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700241 } finally {
242 args.recycle();
243 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700244 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700245 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700246 case MSG_ABORT:
247 abort((String) msg.obj);
248 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700249 case MSG_ANSWER:
250 answer((String) msg.obj);
251 break;
252 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700253 SomeArgs args = (SomeArgs) msg.obj;
254 try {
255 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700256 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700257 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700258 } finally {
259 args.recycle();
260 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700261 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700262 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700263 case MSG_REJECT:
264 reject((String) msg.obj);
265 break;
266 case MSG_DISCONNECT:
267 disconnect((String) msg.obj);
268 break;
269 case MSG_HOLD:
270 hold((String) msg.obj);
271 break;
272 case MSG_UNHOLD:
273 unhold((String) msg.obj);
274 break;
275 case MSG_ON_AUDIO_STATE_CHANGED: {
276 SomeArgs args = (SomeArgs) msg.obj;
277 try {
278 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700279 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700280 onAudioStateChanged(callId, audioState);
281 } finally {
282 args.recycle();
283 }
284 break;
285 }
286 case MSG_PLAY_DTMF_TONE:
287 playDtmfTone((String) msg.obj, (char) msg.arg1);
288 break;
289 case MSG_STOP_DTMF_TONE:
290 stopDtmfTone((String) msg.obj);
291 break;
292 case MSG_CONFERENCE: {
293 SomeArgs args = (SomeArgs) msg.obj;
294 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700295 String callId1 = (String) args.arg1;
296 String callId2 = (String) args.arg2;
297 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700298 } finally {
299 args.recycle();
300 }
301 break;
302 }
303 case MSG_SPLIT_FROM_CONFERENCE:
304 splitFromConference((String) msg.obj);
305 break;
Santos Cordona4868042014-09-04 17:39:22 -0700306 case MSG_MERGE_CONFERENCE:
307 mergeConference((String) msg.obj);
308 break;
309 case MSG_SWAP_CONFERENCE:
310 swapConference((String) msg.obj);
311 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700312 case MSG_ON_POST_DIAL_CONTINUE: {
313 SomeArgs args = (SomeArgs) msg.obj;
314 try {
315 String callId = (String) args.arg1;
316 boolean proceed = (args.argi1 == 1);
317 onPostDialContinue(callId, proceed);
318 } finally {
319 args.recycle();
320 }
321 break;
322 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700323 default:
324 break;
325 }
326 }
327 };
328
Santos Cordon823fd3c2014-08-07 18:35:18 -0700329 private final Conference.Listener mConferenceListener = new Conference.Listener() {
330 @Override
331 public void onStateChanged(Conference conference, int oldState, int newState) {
332 String id = mIdByConference.get(conference);
333 switch (newState) {
334 case Connection.STATE_ACTIVE:
335 mAdapter.setActive(id);
336 break;
337 case Connection.STATE_HOLDING:
338 mAdapter.setOnHold(id);
339 break;
340 case Connection.STATE_DISCONNECTED:
341 // handled by onDisconnected
342 break;
343 }
344 }
345
346 @Override
347 public void onDisconnected(Conference conference, int cause, String message) {
348 String id = mIdByConference.get(conference);
349 mAdapter.setDisconnected(id, cause, message);
350 }
351
352 @Override
353 public void onConnectionAdded(Conference conference, Connection connection) {
354 }
355
356 @Override
357 public void onConnectionRemoved(Conference conference, Connection connection) {
358 }
359
360 @Override
361 public void onDestroyed(Conference conference) {
362 removeConference(conference);
363 }
364
365 @Override
366 public void onCapabilitiesChanged(Conference conference, int capabilities) {
367 String id = mIdByConference.get(conference);
368 Log.d(this, "call capabilities: conference: %s",
369 PhoneCapabilities.toString(capabilities));
370 mAdapter.setCallCapabilities(id, capabilities);
371 }
372 };
373
Ihab Awad542e0ea2014-05-16 10:22:16 -0700374 private final Connection.Listener mConnectionListener = new Connection.Listener() {
375 @Override
376 public void onStateChanged(Connection c, int state) {
377 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700378 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700379 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700380 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700381 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700382 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700383 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700384 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700385 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700386 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700387 // Handled in onDisconnected()
388 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700389 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700390 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700391 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700392 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700393 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700394 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700395 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700396 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700397 break;
398 }
399 }
400
401 @Override
402 public void onDisconnected(Connection c, int cause, String message) {
403 String id = mIdByConnection.get(c);
Ihab Awad60ac30b2014-05-20 22:32:12 -0700404 Log.d(this, "Adapter set disconnected %d %s", cause, message);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700405 mAdapter.setDisconnected(id, cause, message);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700406 }
407
408 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700409 public void onVideoStateChanged(Connection c, int videoState) {
410 String id = mIdByConnection.get(c);
411 Log.d(this, "Adapter set video state %d", videoState);
412 mAdapter.setVideoState(id, videoState);
413 }
414
415 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700416 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700417 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700418 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700419 }
420
421 @Override
422 public void onCallerDisplayNameChanged(
423 Connection c, String callerDisplayName, int presentation) {
424 String id = mIdByConnection.get(c);
425 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700426 }
427
428 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700429 public void onDestroyed(Connection c) {
430 removeConnection(c);
431 }
Ihab Awadf8358972014-05-28 16:46:42 -0700432
433 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700434 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700435 String id = mIdByConnection.get(c);
436 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700437 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700438 }
439
440 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700441 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700442 String id = mIdByConnection.get(c);
443 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700444 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700445 }
Santos Cordonb6939982014-06-04 20:20:58 -0700446
447 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700448 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700449 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700450 Log.d(this, "capabilities: parcelableconnection: %s",
451 PhoneCapabilities.toString(capabilities));
452 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700453 }
454
Santos Cordonb6939982014-06-04 20:20:58 -0700455 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700456 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700457 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700458 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700459 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700460
461 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700462 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700463 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700464 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700465 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700466
467 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700468 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700469 String id = mIdByConnection.get(c);
470 mAdapter.setStatusHints(id, statusHints);
471 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700472
473 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700474 public void onConferenceableConnectionsChanged(
475 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700476 mAdapter.setConferenceableConnections(
477 mIdByConnection.get(connection),
478 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700479 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700480
481 @Override
482 public void onConferenceChanged(Connection connection, Conference conference) {
483 String id = mIdByConnection.get(connection);
484 if (id != null) {
485 String conferenceId = null;
486 if (conference != null) {
487 conferenceId = mIdByConference.get(conference);
488 }
489 mAdapter.setIsConferenced(id, conferenceId);
490 }
491 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700492 };
493
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700494 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700495 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700496 public final IBinder onBind(Intent intent) {
497 return mBinder;
498 }
499
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700500 /** {@inheritDoc} */
501 @Override
502 public boolean onUnbind(Intent intent) {
503 endAllConnections();
504 return super.onUnbind(intent);
505 }
506
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700507 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700508 * This can be used by telecom to either create a new outgoing call or attach to an existing
509 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700510 * createConnection util a connection service cancels the process or completes it successfully.
511 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700512 private void createConnection(
513 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700514 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700515 final ConnectionRequest request,
516 boolean isIncoming) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700517 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
518 "isIncoming: %b", callManagerAccount, callId, request, isIncoming);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700519
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700520 Connection connection = isIncoming
Ihab Awad6107bab2014-08-18 09:23:25 -0700521 ? onCreateIncomingConnection(callManagerAccount, request)
522 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700523 Log.d(this, "createConnection, connection: %s", connection);
524 if (connection == null) {
525 connection = Connection.createFailedConnection(DisconnectCause.OUTGOING_FAILURE, null);
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 connection.getDisconnectMessage(),
559 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700560 }
561
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700562 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700563 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700564 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700565 }
566
Tyler Gunnbe74de02014-08-29 14:51:48 -0700567 private void answerVideo(String callId, int videoState) {
568 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700569 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700570 }
571
Tyler Gunnbe74de02014-08-29 14:51:48 -0700572 private void answer(String callId) {
573 Log.d(this, "answer %s", callId);
574 findConnectionForAction(callId, "answer").onAnswer();
575 }
576
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700577 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700578 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700579 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700580 }
581
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700582 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700583 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700584 if (mConnectionById.containsKey(callId)) {
585 findConnectionForAction(callId, "disconnect").onDisconnect();
586 } else {
587 findConferenceForAction(callId, "disconnect").onDisconnect();
588 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700589 }
590
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700591 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700592 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700593 if (mConnectionById.containsKey(callId)) {
594 findConnectionForAction(callId, "hold").onHold();
595 } else {
596 findConferenceForAction(callId, "hold").onHold();
597 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700598 }
599
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700600 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700601 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700602 if (mConnectionById.containsKey(callId)) {
603 findConnectionForAction(callId, "unhold").onUnhold();
604 } else {
605 findConferenceForAction(callId, "unhold").onUnhold();
606 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700607 }
608
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700609 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700610 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700611 if (mConnectionById.containsKey(callId)) {
612 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
613 } else {
614 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
615 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700616 }
617
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700618 private void playDtmfTone(String callId, char digit) {
619 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700620 if (mConnectionById.containsKey(callId)) {
621 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
622 } else {
623 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
624 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700625 }
626
627 private void stopDtmfTone(String callId) {
628 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700629 if (mConnectionById.containsKey(callId)) {
630 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
631 } else {
632 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
633 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700634 }
635
Santos Cordon823fd3c2014-08-07 18:35:18 -0700636 private void conference(String callId1, String callId2) {
637 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700638
Santos Cordon823fd3c2014-08-07 18:35:18 -0700639 Connection connection1 = findConnectionForAction(callId1, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700640 if (connection1 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700641 Log.w(this, "Connection1 missing in conference request %s.", callId1);
Santos Cordonb6939982014-06-04 20:20:58 -0700642 return;
Santos Cordon980acb92014-05-31 10:31:19 -0700643 }
644
Santos Cordon823fd3c2014-08-07 18:35:18 -0700645 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700646 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700647 Log.w(this, "Connection2 missing in conference request %s.", callId2);
648 return;
649 }
Santos Cordonb6939982014-06-04 20:20:58 -0700650
Santos Cordon823fd3c2014-08-07 18:35:18 -0700651 onConference(connection1, connection2);
Santos Cordon980acb92014-05-31 10:31:19 -0700652 }
653
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700654 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700655 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700656
657 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700658 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700659 Log.w(this, "Connection missing in conference request %s.", callId);
660 return;
661 }
662
Santos Cordon0159ac02014-08-21 14:28:11 -0700663 Conference conference = connection.getConference();
664 if (conference != null) {
665 conference.onSeparate(connection);
666 }
Santos Cordon980acb92014-05-31 10:31:19 -0700667 }
668
Santos Cordona4868042014-09-04 17:39:22 -0700669 private void mergeConference(String callId) {
670 Log.d(this, "mergeConference(%s)", callId);
671 Conference conference = findConferenceForAction(callId, "mergeConference");
672 if (conference != null) {
673 conference.onMerge();
674 }
675 }
676
677 private void swapConference(String callId) {
678 Log.d(this, "swapConference(%s)", callId);
679 Conference conference = findConferenceForAction(callId, "swapConference");
680 if (conference != null) {
681 conference.onSwap();
682 }
683 }
684
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700685 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700686 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700687 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700688 }
689
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700690 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700691 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700692 // No need to query again if we already did it.
693 return;
694 }
695
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700696 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700697 @Override
698 public void onResult(
699 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700700 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700701 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700702 @Override
703 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700704 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700705 mRemoteConnectionManager.addConnectionService(
706 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700707 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700708 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700709 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700710 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700711 }
712 });
713 }
714
715 @Override
716 public void onError() {
717 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700718 @Override
719 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700720 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700721 }
722 });
723 }
724 });
725 }
726
Ihab Awadf8b69882014-07-25 15:14:01 -0700727 /**
728 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
729 * incoming request. This is used to attach to existing incoming calls.
730 *
731 * @param connectionManagerPhoneAccount See description at
732 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
733 * @param request Details about the incoming call.
734 * @return The {@code Connection} object to satisfy this call, or {@code null} to
735 * not handle the call.
736 */
737 public final RemoteConnection createRemoteIncomingConnection(
738 PhoneAccountHandle connectionManagerPhoneAccount,
739 ConnectionRequest request) {
740 return mRemoteConnectionManager.createRemoteConnection(
741 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700742 }
743
744 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700745 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
746 * outgoing request. This is used to initiate new outgoing calls.
747 *
748 * @param connectionManagerPhoneAccount See description at
749 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
750 * @param request Details about the incoming call.
751 * @return The {@code Connection} object to satisfy this call, or {@code null} to
752 * not handle the call.
753 */
754 public final RemoteConnection createRemoteOutgoingConnection(
755 PhoneAccountHandle connectionManagerPhoneAccount,
756 ConnectionRequest request) {
757 return mRemoteConnectionManager.createRemoteConnection(
758 connectionManagerPhoneAccount, request, false);
759 }
760
761 /**
Ihab Awadb8e85c72014-08-23 20:34:57 -0700762 * Adds two {@code RemoteConnection}s to some {@code RemoteConference}.
763 */
764 public final void conferenceRemoteConnections(
765 RemoteConnection a,
766 RemoteConnection b) {
767 mRemoteConnectionManager.conferenceRemoteConnections(a, b);
768 }
769
770 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700771 * Adds a new conference call. When a conference call is created either as a result of an
772 * explicit request via {@link #onConference} or otherwise, the connection service should supply
773 * an instance of {@link Conference} by invoking this method. A conference call provided by this
774 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
775 *
776 * @param conference The new conference object.
777 */
778 public final void addConference(Conference conference) {
779 String id = addConferenceInternal(conference);
780 if (id != null) {
781 List<String> connectionIds = new ArrayList<>(2);
782 for (Connection connection : conference.getConnections()) {
783 if (mIdByConnection.containsKey(connection)) {
784 connectionIds.add(mIdByConnection.get(connection));
785 }
786 }
787 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700788 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700789 conference.getState(),
790 conference.getCapabilities(),
791 connectionIds);
792 mAdapter.addConferenceCall(id, parcelableConference);
793
794 // Go through any child calls and set the parent.
795 for (Connection connection : conference.getConnections()) {
796 String connectionId = mIdByConnection.get(connection);
797 if (connectionId != null) {
798 mAdapter.setIsConferenced(connectionId, id);
799 }
800 }
801 }
802 }
803
804 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700805 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
806 * has taken responsibility.
807 *
808 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700809 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700810 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700811 return mConnectionById.values();
812 }
813
814 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700815 * Create a {@code Connection} given an incoming request. This is used to attach to existing
816 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700817 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700818 * @param connectionManagerPhoneAccount See description at
819 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
820 * @param request Details about the incoming call.
821 * @return The {@code Connection} object to satisfy this call, or {@code null} to
822 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700823 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700824 public Connection onCreateIncomingConnection(
825 PhoneAccountHandle connectionManagerPhoneAccount,
826 ConnectionRequest request) {
827 return null;
828 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700829
830 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700831 * Create a {@code Connection} given an outgoing request. This is used to initiate new
832 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700833 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700834 * @param connectionManagerPhoneAccount The connection manager account to use for managing
835 * this call.
836 * <p>
837 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
838 * has registered one or more {@code PhoneAccount}s having
839 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
840 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
841 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
842 * making the connection.
843 * <p>
844 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
845 * being asked to make a direct connection. The
846 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
847 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
848 * making the connection.
849 * @param request Details about the outgoing call.
850 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700851 * of {@link Connection#createFailedConnection(int, String)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700852 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700853 public Connection onCreateOutgoingConnection(
854 PhoneAccountHandle connectionManagerPhoneAccount,
855 ConnectionRequest request) {
856 return null;
857 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700858
859 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700860 * Conference two specified connections. Invoked when the user has made a request to merge the
861 * specified connections into a conference call. In response, the connection service should
862 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700863 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700864 * @param connection1 A connection to merge into a conference call.
865 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700866 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700867 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700868
Ihab Awadb8e85c72014-08-23 20:34:57 -0700869 public void onRemoteConferenceAdded(RemoteConference conference) {}
870
Santos Cordon823fd3c2014-08-07 18:35:18 -0700871 /**
872 * @hide
873 */
874 public boolean containsConference(Conference conference) {
875 return mIdByConference.containsKey(conference);
876 }
877
Ihab Awadb8e85c72014-08-23 20:34:57 -0700878 /** {@hide} */
879 void addRemoteConference(RemoteConference remoteConference) {
880 onRemoteConferenceAdded(remoteConference);
881 }
882
Ihab Awad5d0410f2014-07-30 10:07:40 -0700883 private void onAccountsInitialized() {
884 mAreAccountsInitialized = true;
885 for (Runnable r : mPreInitializationConnectionRequests) {
886 r.run();
887 }
888 mPreInitializationConnectionRequests.clear();
889 }
890
Ihab Awad542e0ea2014-05-16 10:22:16 -0700891 private void addConnection(String callId, Connection connection) {
892 mConnectionById.put(callId, connection);
893 mIdByConnection.put(connection, callId);
894 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700895 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700896 }
897
898 private void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700899 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700900 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700901 connection.removeConnectionListener(mConnectionListener);
902 mConnectionById.remove(mIdByConnection.get(connection));
903 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700904 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700905 }
906
Santos Cordon823fd3c2014-08-07 18:35:18 -0700907 private String addConferenceInternal(Conference conference) {
908 if (mIdByConference.containsKey(conference)) {
909 Log.w(this, "Re-adding an existing conference: %s.", conference);
910 } else if (conference != null) {
911 String id = UUID.randomUUID().toString();
912 mConferenceById.put(id, conference);
913 mIdByConference.put(conference, id);
914 conference.addListener(mConferenceListener);
915 return id;
916 }
917
918 return null;
919 }
920
921 private void removeConference(Conference conference) {
922 if (mIdByConference.containsKey(conference)) {
923 conference.removeListener(mConferenceListener);
924
925 String id = mIdByConference.get(conference);
926 mConferenceById.remove(id);
927 mIdByConference.remove(conference);
928 mAdapter.removeCall(id);
929 }
930 }
931
Ihab Awad542e0ea2014-05-16 10:22:16 -0700932 private Connection findConnectionForAction(String callId, String action) {
933 if (mConnectionById.containsKey(callId)) {
934 return mConnectionById.get(callId);
935 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700936 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700937 return getNullConnection();
938 }
939
940 static synchronized Connection getNullConnection() {
941 if (sNullConnection == null) {
942 sNullConnection = new Connection() {};
943 }
944 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700945 }
Santos Cordon0159ac02014-08-21 14:28:11 -0700946
947 private Conference findConferenceForAction(String conferenceId, String action) {
948 if (mConferenceById.containsKey(conferenceId)) {
949 return mConferenceById.get(conferenceId);
950 }
951 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
952 return getNullConference();
953 }
954
Ihab Awadb8e85c72014-08-23 20:34:57 -0700955 private List<String> createConnectionIdList(List<Connection> connections) {
956 List<String> ids = new ArrayList<>();
957 for (Connection c : connections) {
958 if (mIdByConnection.containsKey(c)) {
959 ids.add(mIdByConnection.get(c));
960 }
961 }
962 Collections.sort(ids);
963 return ids;
964 }
965
Santos Cordon0159ac02014-08-21 14:28:11 -0700966 private Conference getNullConference() {
967 if (sNullConference == null) {
968 sNullConference = new Conference(null) {};
969 }
970 return sNullConference;
971 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700972
973 private void endAllConnections() {
974 // Unbound from telecomm. We should end all connections and conferences.
975 for (Connection connection : mIdByConnection.keySet()) {
976 // only operate on top-level calls. Conference calls will be removed on their own.
977 if (connection.getConference() == null) {
978 connection.onDisconnect();
979 }
980 }
981 for (Conference conference : mIdByConference.keySet()) {
982 conference.onDisconnect();
983 }
984 }
Santos Cordon980acb92014-05-31 10:31:19 -0700985}