blob: 6e41c33b7b6f3a19b67ca51abea12936e633741f [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Santos Cordon5c6fa952014-07-20 17:47:12 -070019import android.annotation.SdkConstant;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070020import android.app.Service;
Santos Cordon52d8a152014-06-17 19:08:45 -070021import android.content.ComponentName;
Santos Cordon5c6fa952014-07-20 17:47:12 -070022import android.content.Intent;
Ihab Awad542e0ea2014-05-16 10:22:16 -070023import android.net.Uri;
Santos Cordon52d8a152014-06-17 19:08:45 -070024import android.os.Handler;
25import android.os.IBinder;
26import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070027import android.os.Message;
Andrew Lee14185762014-07-25 09:41:56 -070028
Sailesh Nepal2a46b902014-07-04 17:21:07 -070029import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070030import com.android.internal.telecom.IConnectionService;
31import com.android.internal.telecom.IConnectionServiceAdapter;
32import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070033
Ihab Awad5d0410f2014-07-30 10:07:40 -070034import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070035import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070036import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070037import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070038import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070039import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070040import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070041
42/**
Sailesh Nepal2a46b902014-07-04 17:21:07 -070043 * A {@link android.app.Service} that provides telephone connections to processes running on an
44 * Android device.
Ihab Awad542e0ea2014-05-16 10:22:16 -070045 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070046public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070047 /**
48 * The {@link Intent} that must be declared as handled by the service.
49 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070050 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070051 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070052
Ihab Awad542e0ea2014-05-16 10:22:16 -070053 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070054 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070055
Ihab Awad8aecfed2014-08-08 17:06:11 -070056 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070057 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070058 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070059 private static final int MSG_ANSWER = 4;
60 private static final int MSG_REJECT = 5;
61 private static final int MSG_DISCONNECT = 6;
62 private static final int MSG_HOLD = 7;
63 private static final int MSG_UNHOLD = 8;
64 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
65 private static final int MSG_PLAY_DTMF_TONE = 10;
66 private static final int MSG_STOP_DTMF_TONE = 11;
67 private static final int MSG_CONFERENCE = 12;
68 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070069 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070070 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -070071 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -070072 private static final int MSG_MERGE_CONFERENCE = 18;
73 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070074
Sailesh Nepalcf7020b2014-08-20 10:07:19 -070075 private static Connection sNullConnection;
76
mike dooley95e80702014-09-18 14:07:52 -070077 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
78 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
79 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
80 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070081 private final RemoteConnectionManager mRemoteConnectionManager =
82 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -070083 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070084 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -070085
Santos Cordon823fd3c2014-08-07 18:35:18 -070086 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -070087 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -070088
Sailesh Nepal2a46b902014-07-04 17:21:07 -070089 private final IBinder mBinder = new IConnectionService.Stub() {
90 @Override
91 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -070092 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
93 }
94
95 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
96 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070097 }
98
99 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700100 public void createConnection(
101 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700102 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700103 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700104 boolean isIncoming,
105 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700106 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;
Yorke Leec3cf9822014-10-02 09:38:39 -0700111 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700112 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700113 }
114
115 @Override
116 public void abort(String callId) {
117 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
118 }
119
120 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700121 /** @hide */
122 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700123 SomeArgs args = SomeArgs.obtain();
124 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700125 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700126 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
127 }
128
129 @Override
130 public void answer(String callId) {
131 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700132 }
133
134 @Override
135 public void reject(String callId) {
136 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
137 }
138
139 @Override
140 public void disconnect(String callId) {
141 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
142 }
143
144 @Override
145 public void hold(String callId) {
146 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
147 }
148
149 @Override
150 public void unhold(String callId) {
151 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
152 }
153
154 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700155 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700156 SomeArgs args = SomeArgs.obtain();
157 args.arg1 = callId;
158 args.arg2 = audioState;
159 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
160 }
161
162 @Override
163 public void playDtmfTone(String callId, char digit) {
164 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
165 }
166
167 @Override
168 public void stopDtmfTone(String callId) {
169 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
170 }
171
172 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700173 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700174 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700175 args.arg1 = callId1;
176 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700177 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
178 }
179
180 @Override
181 public void splitFromConference(String callId) {
182 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
183 }
184
185 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700186 public void mergeConference(String callId) {
187 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
188 }
189
190 @Override
191 public void swapConference(String callId) {
192 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
193 }
194
195 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700196 public void onPostDialContinue(String callId, boolean proceed) {
197 SomeArgs args = SomeArgs.obtain();
198 args.arg1 = callId;
199 args.argi1 = proceed ? 1 : 0;
200 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
201 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700202 };
203
204 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
205 @Override
206 public void handleMessage(Message msg) {
207 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700208 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700209 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
210 onAdapterAttached();
211 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700212 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
213 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
214 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700215 case MSG_CREATE_CONNECTION: {
216 SomeArgs args = (SomeArgs) msg.obj;
217 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700218 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700219 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700220 final String id = (String) args.arg2;
221 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700222 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700223 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700224 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,
Yorke Leec3cf9822014-10-02 09:38:39 -0700233 isIncoming,
234 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700235 }
236 });
237 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700238 createConnection(
239 connectionManagerPhoneAccount,
240 id,
241 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700242 isIncoming,
243 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700244 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700245 } finally {
246 args.recycle();
247 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700248 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700249 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700250 case MSG_ABORT:
251 abort((String) msg.obj);
252 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700253 case MSG_ANSWER:
254 answer((String) msg.obj);
255 break;
256 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700257 SomeArgs args = (SomeArgs) msg.obj;
258 try {
259 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700260 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700261 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700262 } finally {
263 args.recycle();
264 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700265 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700266 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700267 case MSG_REJECT:
268 reject((String) msg.obj);
269 break;
270 case MSG_DISCONNECT:
271 disconnect((String) msg.obj);
272 break;
273 case MSG_HOLD:
274 hold((String) msg.obj);
275 break;
276 case MSG_UNHOLD:
277 unhold((String) msg.obj);
278 break;
279 case MSG_ON_AUDIO_STATE_CHANGED: {
280 SomeArgs args = (SomeArgs) msg.obj;
281 try {
282 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700283 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700284 onAudioStateChanged(callId, audioState);
285 } finally {
286 args.recycle();
287 }
288 break;
289 }
290 case MSG_PLAY_DTMF_TONE:
291 playDtmfTone((String) msg.obj, (char) msg.arg1);
292 break;
293 case MSG_STOP_DTMF_TONE:
294 stopDtmfTone((String) msg.obj);
295 break;
296 case MSG_CONFERENCE: {
297 SomeArgs args = (SomeArgs) msg.obj;
298 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700299 String callId1 = (String) args.arg1;
300 String callId2 = (String) args.arg2;
301 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700302 } finally {
303 args.recycle();
304 }
305 break;
306 }
307 case MSG_SPLIT_FROM_CONFERENCE:
308 splitFromConference((String) msg.obj);
309 break;
Santos Cordona4868042014-09-04 17:39:22 -0700310 case MSG_MERGE_CONFERENCE:
311 mergeConference((String) msg.obj);
312 break;
313 case MSG_SWAP_CONFERENCE:
314 swapConference((String) msg.obj);
315 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700316 case MSG_ON_POST_DIAL_CONTINUE: {
317 SomeArgs args = (SomeArgs) msg.obj;
318 try {
319 String callId = (String) args.arg1;
320 boolean proceed = (args.argi1 == 1);
321 onPostDialContinue(callId, proceed);
322 } finally {
323 args.recycle();
324 }
325 break;
326 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700327 default:
328 break;
329 }
330 }
331 };
332
Santos Cordon823fd3c2014-08-07 18:35:18 -0700333 private final Conference.Listener mConferenceListener = new Conference.Listener() {
334 @Override
335 public void onStateChanged(Conference conference, int oldState, int newState) {
336 String id = mIdByConference.get(conference);
337 switch (newState) {
338 case Connection.STATE_ACTIVE:
339 mAdapter.setActive(id);
340 break;
341 case Connection.STATE_HOLDING:
342 mAdapter.setOnHold(id);
343 break;
344 case Connection.STATE_DISCONNECTED:
345 // handled by onDisconnected
346 break;
347 }
348 }
349
350 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700351 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700352 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700353 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700354 }
355
356 @Override
357 public void onConnectionAdded(Conference conference, Connection connection) {
358 }
359
360 @Override
361 public void onConnectionRemoved(Conference conference, Connection connection) {
362 }
363
364 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700365 public void onConferenceableConnectionsChanged(
366 Conference conference, List<Connection> conferenceableConnections) {
367 mAdapter.setConferenceableConnections(
368 mIdByConference.get(conference),
369 createConnectionIdList(conferenceableConnections));
370 }
371
372 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700373 public void onDestroyed(Conference conference) {
374 removeConference(conference);
375 }
376
377 @Override
378 public void onCapabilitiesChanged(Conference conference, int capabilities) {
379 String id = mIdByConference.get(conference);
380 Log.d(this, "call capabilities: conference: %s",
381 PhoneCapabilities.toString(capabilities));
382 mAdapter.setCallCapabilities(id, capabilities);
383 }
384 };
385
Ihab Awad542e0ea2014-05-16 10:22:16 -0700386 private final Connection.Listener mConnectionListener = new Connection.Listener() {
387 @Override
388 public void onStateChanged(Connection c, int state) {
389 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700390 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700391 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700392 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700393 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700394 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700395 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700396 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700397 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700398 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700399 // Handled in onDisconnected()
400 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700401 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700402 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700403 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700404 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700405 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700406 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700407 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700408 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700409 break;
410 }
411 }
412
413 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700414 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700415 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700416 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700417 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700418 }
419
420 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700421 public void onVideoStateChanged(Connection c, int videoState) {
422 String id = mIdByConnection.get(c);
423 Log.d(this, "Adapter set video state %d", videoState);
424 mAdapter.setVideoState(id, videoState);
425 }
426
427 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700428 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700429 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700430 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700431 }
432
433 @Override
434 public void onCallerDisplayNameChanged(
435 Connection c, String callerDisplayName, int presentation) {
436 String id = mIdByConnection.get(c);
437 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700438 }
439
440 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700441 public void onDestroyed(Connection c) {
442 removeConnection(c);
443 }
Ihab Awadf8358972014-05-28 16:46:42 -0700444
445 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700446 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700447 String id = mIdByConnection.get(c);
448 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700449 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700450 }
451
452 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700453 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700454 String id = mIdByConnection.get(c);
455 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700456 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700457 }
Santos Cordonb6939982014-06-04 20:20:58 -0700458
459 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700460 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700461 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700462 Log.d(this, "capabilities: parcelableconnection: %s",
463 PhoneCapabilities.toString(capabilities));
464 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700465 }
466
Santos Cordonb6939982014-06-04 20:20:58 -0700467 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700468 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700469 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700470 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700471 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700472
473 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700474 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700475 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700476 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700477 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700478
479 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700480 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700481 String id = mIdByConnection.get(c);
482 mAdapter.setStatusHints(id, statusHints);
483 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700484
485 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700486 public void onConferenceableConnectionsChanged(
487 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700488 mAdapter.setConferenceableConnections(
489 mIdByConnection.get(connection),
490 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700491 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700492
493 @Override
494 public void onConferenceChanged(Connection connection, Conference conference) {
495 String id = mIdByConnection.get(connection);
496 if (id != null) {
497 String conferenceId = null;
498 if (conference != null) {
499 conferenceId = mIdByConference.get(conference);
500 }
501 mAdapter.setIsConferenced(id, conferenceId);
502 }
503 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700504 };
505
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700506 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700507 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700508 public final IBinder onBind(Intent intent) {
509 return mBinder;
510 }
511
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700512 /** {@inheritDoc} */
513 @Override
514 public boolean onUnbind(Intent intent) {
515 endAllConnections();
516 return super.onUnbind(intent);
517 }
518
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700519 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700520 * This can be used by telecom to either create a new outgoing call or attach to an existing
521 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700522 * createConnection util a connection service cancels the process or completes it successfully.
523 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700524 private void createConnection(
525 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700526 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700527 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700528 boolean isIncoming,
529 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700530 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700531 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
532 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700533
Yorke Leec3cf9822014-10-02 09:38:39 -0700534 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
535 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700536 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700537 Log.d(this, "createConnection, connection: %s", connection);
538 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700539 connection = Connection.createFailedConnection(
540 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700541 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700542
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700543 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700544 addConnection(callId, connection);
545 }
546
Andrew Lee100e2932014-09-08 15:34:24 -0700547 Uri address = connection.getAddress();
548 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700549 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700550 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700551 Connection.stateToString(connection.getState()),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700552 PhoneCapabilities.toString(connection.getCallCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700553
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700554 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700555 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700556 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700557 request,
558 new ParcelableConnection(
559 request.getAccountHandle(),
560 connection.getState(),
561 connection.getCallCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700562 connection.getAddress(),
563 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700564 connection.getCallerDisplayName(),
565 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700566 connection.getVideoProvider() == null ?
567 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700568 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700569 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700570 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700571 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700572 connection.getDisconnectCause(),
Ihab Awadb8e85c72014-08-23 20:34:57 -0700573 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700574 }
575
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700576 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700577 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700578 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700579 }
580
Tyler Gunnbe74de02014-08-29 14:51:48 -0700581 private void answerVideo(String callId, int videoState) {
582 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700583 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700584 }
585
Tyler Gunnbe74de02014-08-29 14:51:48 -0700586 private void answer(String callId) {
587 Log.d(this, "answer %s", callId);
588 findConnectionForAction(callId, "answer").onAnswer();
589 }
590
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700591 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700592 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700593 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700594 }
595
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700596 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700597 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700598 if (mConnectionById.containsKey(callId)) {
599 findConnectionForAction(callId, "disconnect").onDisconnect();
600 } else {
601 findConferenceForAction(callId, "disconnect").onDisconnect();
602 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700603 }
604
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700605 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700606 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700607 if (mConnectionById.containsKey(callId)) {
608 findConnectionForAction(callId, "hold").onHold();
609 } else {
610 findConferenceForAction(callId, "hold").onHold();
611 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700612 }
613
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700614 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700615 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700616 if (mConnectionById.containsKey(callId)) {
617 findConnectionForAction(callId, "unhold").onUnhold();
618 } else {
619 findConferenceForAction(callId, "unhold").onUnhold();
620 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700621 }
622
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700623 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700624 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700625 if (mConnectionById.containsKey(callId)) {
626 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
627 } else {
628 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
629 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700630 }
631
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700632 private void playDtmfTone(String callId, char digit) {
633 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700634 if (mConnectionById.containsKey(callId)) {
635 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
636 } else {
637 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
638 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700639 }
640
641 private void stopDtmfTone(String callId) {
642 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700643 if (mConnectionById.containsKey(callId)) {
644 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
645 } else {
646 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
647 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700648 }
649
Santos Cordon823fd3c2014-08-07 18:35:18 -0700650 private void conference(String callId1, String callId2) {
651 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700652
Santos Cordon823fd3c2014-08-07 18:35:18 -0700653 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700654 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700655 Log.w(this, "Connection2 missing in conference request %s.", callId2);
656 return;
657 }
Santos Cordonb6939982014-06-04 20:20:58 -0700658
Ihab Awad50e35062014-09-30 09:17:03 -0700659 Connection connection1 = findConnectionForAction(callId1, "conference");
660 if (connection1 == getNullConnection()) {
661 Conference conference1 = findConferenceForAction(callId1, "addConnection");
662 if (conference1 == getNullConference()) {
663 Log.w(this,
664 "Connection1 or Conference1 missing in conference request %s.",
665 callId1);
666 } else {
667 conference1.onMerge(connection2);
668 }
669 } else {
670 onConference(connection1, connection2);
671 }
Santos Cordon980acb92014-05-31 10:31:19 -0700672 }
673
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700674 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700675 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700676
677 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700678 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700679 Log.w(this, "Connection missing in conference request %s.", callId);
680 return;
681 }
682
Santos Cordon0159ac02014-08-21 14:28:11 -0700683 Conference conference = connection.getConference();
684 if (conference != null) {
685 conference.onSeparate(connection);
686 }
Santos Cordon980acb92014-05-31 10:31:19 -0700687 }
688
Santos Cordona4868042014-09-04 17:39:22 -0700689 private void mergeConference(String callId) {
690 Log.d(this, "mergeConference(%s)", callId);
691 Conference conference = findConferenceForAction(callId, "mergeConference");
692 if (conference != null) {
693 conference.onMerge();
694 }
695 }
696
697 private void swapConference(String callId) {
698 Log.d(this, "swapConference(%s)", callId);
699 Conference conference = findConferenceForAction(callId, "swapConference");
700 if (conference != null) {
701 conference.onSwap();
702 }
703 }
704
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700705 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700706 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700707 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700708 }
709
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700710 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700711 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700712 // No need to query again if we already did it.
713 return;
714 }
715
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700716 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700717 @Override
718 public void onResult(
719 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700720 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700721 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700722 @Override
723 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700724 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700725 mRemoteConnectionManager.addConnectionService(
726 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700727 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700728 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700729 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700730 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700731 }
732 });
733 }
734
735 @Override
736 public void onError() {
737 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700738 @Override
739 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700740 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700741 }
742 });
743 }
744 });
745 }
746
Ihab Awadf8b69882014-07-25 15:14:01 -0700747 /**
748 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
749 * incoming request. This is used to attach to existing incoming calls.
750 *
751 * @param connectionManagerPhoneAccount See description at
752 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
753 * @param request Details about the incoming call.
754 * @return The {@code Connection} object to satisfy this call, or {@code null} to
755 * not handle the call.
756 */
757 public final RemoteConnection createRemoteIncomingConnection(
758 PhoneAccountHandle connectionManagerPhoneAccount,
759 ConnectionRequest request) {
760 return mRemoteConnectionManager.createRemoteConnection(
761 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700762 }
763
764 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700765 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
766 * outgoing request. This is used to initiate new outgoing calls.
767 *
768 * @param connectionManagerPhoneAccount See description at
769 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
770 * @param request Details about the incoming call.
771 * @return The {@code Connection} object to satisfy this call, or {@code null} to
772 * not handle the call.
773 */
774 public final RemoteConnection createRemoteOutgoingConnection(
775 PhoneAccountHandle connectionManagerPhoneAccount,
776 ConnectionRequest request) {
777 return mRemoteConnectionManager.createRemoteConnection(
778 connectionManagerPhoneAccount, request, false);
779 }
780
781 /**
Ihab Awadb8e85c72014-08-23 20:34:57 -0700782 * Adds two {@code RemoteConnection}s to some {@code RemoteConference}.
783 */
784 public final void conferenceRemoteConnections(
785 RemoteConnection a,
786 RemoteConnection b) {
787 mRemoteConnectionManager.conferenceRemoteConnections(a, b);
788 }
789
790 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700791 * Adds a new conference call. When a conference call is created either as a result of an
792 * explicit request via {@link #onConference} or otherwise, the connection service should supply
793 * an instance of {@link Conference} by invoking this method. A conference call provided by this
794 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
795 *
796 * @param conference The new conference object.
797 */
798 public final void addConference(Conference conference) {
799 String id = addConferenceInternal(conference);
800 if (id != null) {
801 List<String> connectionIds = new ArrayList<>(2);
802 for (Connection connection : conference.getConnections()) {
803 if (mIdByConnection.containsKey(connection)) {
804 connectionIds.add(mIdByConnection.get(connection));
805 }
806 }
807 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700808 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700809 conference.getState(),
810 conference.getCapabilities(),
811 connectionIds);
812 mAdapter.addConferenceCall(id, parcelableConference);
813
814 // Go through any child calls and set the parent.
815 for (Connection connection : conference.getConnections()) {
816 String connectionId = mIdByConnection.get(connection);
817 if (connectionId != null) {
818 mAdapter.setIsConferenced(connectionId, id);
819 }
820 }
821 }
822 }
823
824 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700825 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
826 * connection.
827 *
828 * @param phoneAccountHandle The phone account handle for the connection.
829 * @param connection The connection to add.
830 */
831 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
832 Connection connection) {
833
834 String id = addExistingConnectionInternal(connection);
835 if (id != null) {
836 List<String> emptyList = new ArrayList<>(0);
837
838 ParcelableConnection parcelableConnection = new ParcelableConnection(
839 phoneAccountHandle,
840 connection.getState(),
841 connection.getCallCapabilities(),
842 connection.getAddress(),
843 connection.getAddressPresentation(),
844 connection.getCallerDisplayName(),
845 connection.getCallerDisplayNamePresentation(),
846 connection.getVideoProvider() == null ?
847 null : connection.getVideoProvider().getInterface(),
848 connection.getVideoState(),
849 connection.isRingbackRequested(),
850 connection.getAudioModeIsVoip(),
851 connection.getStatusHints(),
852 connection.getDisconnectCause(),
853 emptyList);
854 mAdapter.addExistingConnection(id, parcelableConnection);
855 }
856 }
857
858 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700859 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
860 * has taken responsibility.
861 *
862 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700863 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700864 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700865 return mConnectionById.values();
866 }
867
868 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700869 * Create a {@code Connection} given an incoming request. This is used to attach to existing
870 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700871 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700872 * @param connectionManagerPhoneAccount See description at
873 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
874 * @param request Details about the incoming call.
875 * @return The {@code Connection} object to satisfy this call, or {@code null} to
876 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700877 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700878 public Connection onCreateIncomingConnection(
879 PhoneAccountHandle connectionManagerPhoneAccount,
880 ConnectionRequest request) {
881 return null;
882 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700883
884 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700885 * Create a {@code Connection} given an outgoing request. This is used to initiate new
886 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700887 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700888 * @param connectionManagerPhoneAccount The connection manager account to use for managing
889 * this call.
890 * <p>
891 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
892 * has registered one or more {@code PhoneAccount}s having
893 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
894 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
895 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
896 * making the connection.
897 * <p>
898 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
899 * being asked to make a direct connection. The
900 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
901 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
902 * making the connection.
903 * @param request Details about the outgoing call.
904 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700905 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700906 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700907 public Connection onCreateOutgoingConnection(
908 PhoneAccountHandle connectionManagerPhoneAccount,
909 ConnectionRequest request) {
910 return null;
911 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700912
913 /**
Yorke Leec3cf9822014-10-02 09:38:39 -0700914 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
915 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
916 * call created using
917 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
918 *
919 * @param connectionManagerPhoneAccount
920 * @param request
921 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -0700922 *
923 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -0700924 */
925 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
926 ConnectionRequest request) {
927 return null;
928 }
929
930 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700931 * Conference two specified connections. Invoked when the user has made a request to merge the
932 * specified connections into a conference call. In response, the connection service should
933 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700934 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700935 * @param connection1 A connection to merge into a conference call.
936 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700937 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700938 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700939
Ihab Awadb8e85c72014-08-23 20:34:57 -0700940 public void onRemoteConferenceAdded(RemoteConference conference) {}
941
Santos Cordon823fd3c2014-08-07 18:35:18 -0700942 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700943 * Called when an existing connection is added remotely.
944 * @param connection The existing connection which was added.
945 */
946 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
947
948 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700949 * @hide
950 */
951 public boolean containsConference(Conference conference) {
952 return mIdByConference.containsKey(conference);
953 }
954
Ihab Awadb8e85c72014-08-23 20:34:57 -0700955 /** {@hide} */
956 void addRemoteConference(RemoteConference remoteConference) {
957 onRemoteConferenceAdded(remoteConference);
958 }
959
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700960 /** {@hide} */
961 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
962 onRemoteExistingConnectionAdded(remoteConnection);
963 }
964
Ihab Awad5d0410f2014-07-30 10:07:40 -0700965 private void onAccountsInitialized() {
966 mAreAccountsInitialized = true;
967 for (Runnable r : mPreInitializationConnectionRequests) {
968 r.run();
969 }
970 mPreInitializationConnectionRequests.clear();
971 }
972
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700973 /**
974 * Adds an existing connection to the list of connections, identified by a new UUID.
975 *
976 * @param connection The connection.
977 * @return The UUID of the connection (e.g. the call-id).
978 */
979 private String addExistingConnectionInternal(Connection connection) {
980 String id = UUID.randomUUID().toString();
981 addConnection(id, connection);
982 return id;
983 }
984
Ihab Awad542e0ea2014-05-16 10:22:16 -0700985 private void addConnection(String callId, Connection connection) {
986 mConnectionById.put(callId, connection);
987 mIdByConnection.put(connection, callId);
988 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700989 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700990 }
991
992 private void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700993 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700994 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700995 connection.removeConnectionListener(mConnectionListener);
996 mConnectionById.remove(mIdByConnection.get(connection));
997 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700998 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700999 }
1000
Santos Cordon823fd3c2014-08-07 18:35:18 -07001001 private String addConferenceInternal(Conference conference) {
1002 if (mIdByConference.containsKey(conference)) {
1003 Log.w(this, "Re-adding an existing conference: %s.", conference);
1004 } else if (conference != null) {
1005 String id = UUID.randomUUID().toString();
1006 mConferenceById.put(id, conference);
1007 mIdByConference.put(conference, id);
1008 conference.addListener(mConferenceListener);
1009 return id;
1010 }
1011
1012 return null;
1013 }
1014
1015 private void removeConference(Conference conference) {
1016 if (mIdByConference.containsKey(conference)) {
1017 conference.removeListener(mConferenceListener);
1018
1019 String id = mIdByConference.get(conference);
1020 mConferenceById.remove(id);
1021 mIdByConference.remove(conference);
1022 mAdapter.removeCall(id);
1023 }
1024 }
1025
Ihab Awad542e0ea2014-05-16 10:22:16 -07001026 private Connection findConnectionForAction(String callId, String action) {
1027 if (mConnectionById.containsKey(callId)) {
1028 return mConnectionById.get(callId);
1029 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001030 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001031 return getNullConnection();
1032 }
1033
1034 static synchronized Connection getNullConnection() {
1035 if (sNullConnection == null) {
1036 sNullConnection = new Connection() {};
1037 }
1038 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001039 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001040
1041 private Conference findConferenceForAction(String conferenceId, String action) {
1042 if (mConferenceById.containsKey(conferenceId)) {
1043 return mConferenceById.get(conferenceId);
1044 }
1045 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1046 return getNullConference();
1047 }
1048
Ihab Awadb8e85c72014-08-23 20:34:57 -07001049 private List<String> createConnectionIdList(List<Connection> connections) {
1050 List<String> ids = new ArrayList<>();
1051 for (Connection c : connections) {
1052 if (mIdByConnection.containsKey(c)) {
1053 ids.add(mIdByConnection.get(c));
1054 }
1055 }
1056 Collections.sort(ids);
1057 return ids;
1058 }
1059
Santos Cordon0159ac02014-08-21 14:28:11 -07001060 private Conference getNullConference() {
1061 if (sNullConference == null) {
1062 sNullConference = new Conference(null) {};
1063 }
1064 return sNullConference;
1065 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001066
1067 private void endAllConnections() {
1068 // Unbound from telecomm. We should end all connections and conferences.
1069 for (Connection connection : mIdByConnection.keySet()) {
1070 // only operate on top-level calls. Conference calls will be removed on their own.
1071 if (connection.getConference() == null) {
1072 connection.onDisconnect();
1073 }
1074 }
1075 for (Conference conference : mIdByConference.keySet()) {
1076 conference.onDisconnect();
1077 }
1078 }
Santos Cordon980acb92014-05-31 10:31:19 -07001079}