blob: 6eee99d0cebfaf372a129e7b3fb566f59644cb14 [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Tyler Gunn711d876fd2014-09-19 11:17:02 -070019import android.annotation.SystemApi;
Santos Cordon5c6fa952014-07-20 17:47:12 -070020import android.annotation.SdkConstant;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070021import android.app.Service;
Santos Cordon52d8a152014-06-17 19:08:45 -070022import android.content.ComponentName;
Santos Cordon5c6fa952014-07-20 17:47:12 -070023import android.content.Intent;
Ihab Awad542e0ea2014-05-16 10:22:16 -070024import android.net.Uri;
Santos Cordon52d8a152014-06-17 19:08:45 -070025import android.os.Handler;
26import android.os.IBinder;
27import android.os.Looper;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070028import android.os.Message;
Andrew Lee14185762014-07-25 09:41:56 -070029
Sailesh Nepal2a46b902014-07-04 17:21:07 -070030import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070031import com.android.internal.telecom.IConnectionService;
32import com.android.internal.telecom.IConnectionServiceAdapter;
33import com.android.internal.telecom.RemoteServiceCallback;
Santos Cordon52d8a152014-06-17 19:08:45 -070034
Ihab Awad5d0410f2014-07-30 10:07:40 -070035import java.util.ArrayList;
Santos Cordonb6939982014-06-04 20:20:58 -070036import java.util.Collection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070037import java.util.Collections;
Santos Cordon52d8a152014-06-17 19:08:45 -070038import java.util.List;
Ihab Awad542e0ea2014-05-16 10:22:16 -070039import java.util.Map;
Santos Cordon823fd3c2014-08-07 18:35:18 -070040import java.util.UUID;
mike dooley95e80702014-09-18 14:07:52 -070041import java.util.concurrent.ConcurrentHashMap;
Ihab Awad542e0ea2014-05-16 10:22:16 -070042
43/**
Sailesh Nepal2a46b902014-07-04 17:21:07 -070044 * A {@link android.app.Service} that provides telephone connections to processes running on an
45 * Android device.
Tyler Gunn711d876fd2014-09-19 11:17:02 -070046 * @hide
Ihab Awad542e0ea2014-05-16 10:22:16 -070047 */
Tyler Gunn711d876fd2014-09-19 11:17:02 -070048@SystemApi
Sailesh Nepal2a46b902014-07-04 17:21:07 -070049public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070050 /**
51 * The {@link Intent} that must be declared as handled by the service.
52 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070053 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070054 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070055
Ihab Awad542e0ea2014-05-16 10:22:16 -070056 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070057 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070058
Ihab Awad8aecfed2014-08-08 17:06:11 -070059 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070060 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070061 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070062 private static final int MSG_ANSWER = 4;
63 private static final int MSG_REJECT = 5;
64 private static final int MSG_DISCONNECT = 6;
65 private static final int MSG_HOLD = 7;
66 private static final int MSG_UNHOLD = 8;
67 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
68 private static final int MSG_PLAY_DTMF_TONE = 10;
69 private static final int MSG_STOP_DTMF_TONE = 11;
70 private static final int MSG_CONFERENCE = 12;
71 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070072 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070073 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -070074 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -070075 private static final int MSG_MERGE_CONFERENCE = 18;
76 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -070077
Sailesh Nepalcf7020b2014-08-20 10:07:19 -070078 private static Connection sNullConnection;
79
mike dooley95e80702014-09-18 14:07:52 -070080 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
81 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
82 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
83 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -070084 private final RemoteConnectionManager mRemoteConnectionManager =
85 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -070086 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -070087 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -070088
Santos Cordon823fd3c2014-08-07 18:35:18 -070089 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -070090 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -070091
Sailesh Nepal2a46b902014-07-04 17:21:07 -070092 private final IBinder mBinder = new IConnectionService.Stub() {
93 @Override
94 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -070095 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
96 }
97
98 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
99 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700100 }
101
102 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700103 public void createConnection(
104 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700105 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700106 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700107 boolean isIncoming,
108 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700109 SomeArgs args = SomeArgs.obtain();
110 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700111 args.arg2 = id;
112 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700113 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700114 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700115 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700116 }
117
118 @Override
119 public void abort(String callId) {
120 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
121 }
122
123 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700124 /** @hide */
125 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700126 SomeArgs args = SomeArgs.obtain();
127 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700128 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700129 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
130 }
131
132 @Override
133 public void answer(String callId) {
134 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700135 }
136
137 @Override
138 public void reject(String callId) {
139 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
140 }
141
142 @Override
143 public void disconnect(String callId) {
144 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
145 }
146
147 @Override
148 public void hold(String callId) {
149 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
150 }
151
152 @Override
153 public void unhold(String callId) {
154 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
155 }
156
157 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700158 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700159 SomeArgs args = SomeArgs.obtain();
160 args.arg1 = callId;
161 args.arg2 = audioState;
162 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
163 }
164
165 @Override
166 public void playDtmfTone(String callId, char digit) {
167 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
168 }
169
170 @Override
171 public void stopDtmfTone(String callId) {
172 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
173 }
174
175 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700176 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700177 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700178 args.arg1 = callId1;
179 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700180 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
181 }
182
183 @Override
184 public void splitFromConference(String callId) {
185 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
186 }
187
188 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700189 public void mergeConference(String callId) {
190 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
191 }
192
193 @Override
194 public void swapConference(String callId) {
195 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
196 }
197
198 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700199 public void onPostDialContinue(String callId, boolean proceed) {
200 SomeArgs args = SomeArgs.obtain();
201 args.arg1 = callId;
202 args.argi1 = proceed ? 1 : 0;
203 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
204 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700205 };
206
207 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
208 @Override
209 public void handleMessage(Message msg) {
210 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700211 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700212 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
213 onAdapterAttached();
214 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700215 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
216 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
217 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700218 case MSG_CREATE_CONNECTION: {
219 SomeArgs args = (SomeArgs) msg.obj;
220 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700221 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700222 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700223 final String id = (String) args.arg2;
224 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700225 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700226 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700227 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700228 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700229 mPreInitializationConnectionRequests.add(new Runnable() {
230 @Override
231 public void run() {
232 createConnection(
233 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700234 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700235 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700236 isIncoming,
237 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700238 }
239 });
240 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700241 createConnection(
242 connectionManagerPhoneAccount,
243 id,
244 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700245 isIncoming,
246 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700247 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700248 } finally {
249 args.recycle();
250 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700251 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700252 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700253 case MSG_ABORT:
254 abort((String) msg.obj);
255 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700256 case MSG_ANSWER:
257 answer((String) msg.obj);
258 break;
259 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700260 SomeArgs args = (SomeArgs) msg.obj;
261 try {
262 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700263 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700264 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700265 } finally {
266 args.recycle();
267 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700268 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700269 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700270 case MSG_REJECT:
271 reject((String) msg.obj);
272 break;
273 case MSG_DISCONNECT:
274 disconnect((String) msg.obj);
275 break;
276 case MSG_HOLD:
277 hold((String) msg.obj);
278 break;
279 case MSG_UNHOLD:
280 unhold((String) msg.obj);
281 break;
282 case MSG_ON_AUDIO_STATE_CHANGED: {
283 SomeArgs args = (SomeArgs) msg.obj;
284 try {
285 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700286 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700287 onAudioStateChanged(callId, audioState);
288 } finally {
289 args.recycle();
290 }
291 break;
292 }
293 case MSG_PLAY_DTMF_TONE:
294 playDtmfTone((String) msg.obj, (char) msg.arg1);
295 break;
296 case MSG_STOP_DTMF_TONE:
297 stopDtmfTone((String) msg.obj);
298 break;
299 case MSG_CONFERENCE: {
300 SomeArgs args = (SomeArgs) msg.obj;
301 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700302 String callId1 = (String) args.arg1;
303 String callId2 = (String) args.arg2;
304 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700305 } finally {
306 args.recycle();
307 }
308 break;
309 }
310 case MSG_SPLIT_FROM_CONFERENCE:
311 splitFromConference((String) msg.obj);
312 break;
Santos Cordona4868042014-09-04 17:39:22 -0700313 case MSG_MERGE_CONFERENCE:
314 mergeConference((String) msg.obj);
315 break;
316 case MSG_SWAP_CONFERENCE:
317 swapConference((String) msg.obj);
318 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700319 case MSG_ON_POST_DIAL_CONTINUE: {
320 SomeArgs args = (SomeArgs) msg.obj;
321 try {
322 String callId = (String) args.arg1;
323 boolean proceed = (args.argi1 == 1);
324 onPostDialContinue(callId, proceed);
325 } finally {
326 args.recycle();
327 }
328 break;
329 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700330 default:
331 break;
332 }
333 }
334 };
335
Santos Cordon823fd3c2014-08-07 18:35:18 -0700336 private final Conference.Listener mConferenceListener = new Conference.Listener() {
337 @Override
338 public void onStateChanged(Conference conference, int oldState, int newState) {
339 String id = mIdByConference.get(conference);
340 switch (newState) {
341 case Connection.STATE_ACTIVE:
342 mAdapter.setActive(id);
343 break;
344 case Connection.STATE_HOLDING:
345 mAdapter.setOnHold(id);
346 break;
347 case Connection.STATE_DISCONNECTED:
348 // handled by onDisconnected
349 break;
350 }
351 }
352
353 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700354 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700355 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700356 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700357 }
358
359 @Override
360 public void onConnectionAdded(Conference conference, Connection connection) {
361 }
362
363 @Override
364 public void onConnectionRemoved(Conference conference, Connection connection) {
365 }
366
367 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700368 public void onConferenceableConnectionsChanged(
369 Conference conference, List<Connection> conferenceableConnections) {
370 mAdapter.setConferenceableConnections(
371 mIdByConference.get(conference),
372 createConnectionIdList(conferenceableConnections));
373 }
374
375 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700376 public void onDestroyed(Conference conference) {
377 removeConference(conference);
378 }
379
380 @Override
381 public void onCapabilitiesChanged(Conference conference, int capabilities) {
382 String id = mIdByConference.get(conference);
383 Log.d(this, "call capabilities: conference: %s",
384 PhoneCapabilities.toString(capabilities));
385 mAdapter.setCallCapabilities(id, capabilities);
386 }
387 };
388
Ihab Awad542e0ea2014-05-16 10:22:16 -0700389 private final Connection.Listener mConnectionListener = new Connection.Listener() {
390 @Override
391 public void onStateChanged(Connection c, int state) {
392 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700393 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700394 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700395 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700396 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700397 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700398 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700399 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700400 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700401 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700402 // Handled in onDisconnected()
403 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700404 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700405 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700406 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700407 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700408 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700409 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700410 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700411 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700412 break;
413 }
414 }
415
416 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700417 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700418 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700419 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700420 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700421 }
422
423 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700424 public void onVideoStateChanged(Connection c, int videoState) {
425 String id = mIdByConnection.get(c);
426 Log.d(this, "Adapter set video state %d", videoState);
427 mAdapter.setVideoState(id, videoState);
428 }
429
430 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700431 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700432 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700433 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700434 }
435
436 @Override
437 public void onCallerDisplayNameChanged(
438 Connection c, String callerDisplayName, int presentation) {
439 String id = mIdByConnection.get(c);
440 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700441 }
442
443 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700444 public void onDestroyed(Connection c) {
445 removeConnection(c);
446 }
Ihab Awadf8358972014-05-28 16:46:42 -0700447
448 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700449 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700450 String id = mIdByConnection.get(c);
451 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700452 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700453 }
454
455 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700456 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700457 String id = mIdByConnection.get(c);
458 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700459 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700460 }
Santos Cordonb6939982014-06-04 20:20:58 -0700461
462 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700463 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700464 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700465 Log.d(this, "capabilities: parcelableconnection: %s",
466 PhoneCapabilities.toString(capabilities));
467 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700468 }
469
Santos Cordonb6939982014-06-04 20:20:58 -0700470 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700471 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700472 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700473 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700474 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700475
476 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700477 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700478 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700479 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700480 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700481
482 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700483 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700484 String id = mIdByConnection.get(c);
485 mAdapter.setStatusHints(id, statusHints);
486 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700487
488 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700489 public void onConferenceableConnectionsChanged(
490 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700491 mAdapter.setConferenceableConnections(
492 mIdByConnection.get(connection),
493 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700494 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700495
496 @Override
497 public void onConferenceChanged(Connection connection, Conference conference) {
498 String id = mIdByConnection.get(connection);
499 if (id != null) {
500 String conferenceId = null;
501 if (conference != null) {
502 conferenceId = mIdByConference.get(conference);
503 }
504 mAdapter.setIsConferenced(id, conferenceId);
505 }
506 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700507 };
508
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700509 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700510 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700511 public final IBinder onBind(Intent intent) {
512 return mBinder;
513 }
514
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700515 /** {@inheritDoc} */
516 @Override
517 public boolean onUnbind(Intent intent) {
518 endAllConnections();
519 return super.onUnbind(intent);
520 }
521
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700522 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700523 * This can be used by telecom to either create a new outgoing call or attach to an existing
524 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700525 * createConnection util a connection service cancels the process or completes it successfully.
526 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700527 private void createConnection(
528 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700529 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700530 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700531 boolean isIncoming,
532 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700533 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700534 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
535 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700536
Yorke Leec3cf9822014-10-02 09:38:39 -0700537 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
538 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700539 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700540 Log.d(this, "createConnection, connection: %s", connection);
541 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700542 connection = Connection.createFailedConnection(
543 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700544 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700545
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700546 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700547 addConnection(callId, connection);
548 }
549
Andrew Lee100e2932014-09-08 15:34:24 -0700550 Uri address = connection.getAddress();
551 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700552 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700553 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700554 Connection.stateToString(connection.getState()),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700555 PhoneCapabilities.toString(connection.getCallCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700556
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700557 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700558 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700559 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700560 request,
561 new ParcelableConnection(
562 request.getAccountHandle(),
563 connection.getState(),
564 connection.getCallCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700565 connection.getAddress(),
566 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700567 connection.getCallerDisplayName(),
568 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700569 connection.getVideoProvider() == null ?
570 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700571 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700572 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700573 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700574 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700575 connection.getDisconnectCause(),
Ihab Awadb8e85c72014-08-23 20:34:57 -0700576 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700577 }
578
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700579 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700580 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700581 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700582 }
583
Tyler Gunnbe74de02014-08-29 14:51:48 -0700584 private void answerVideo(String callId, int videoState) {
585 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700586 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700587 }
588
Tyler Gunnbe74de02014-08-29 14:51:48 -0700589 private void answer(String callId) {
590 Log.d(this, "answer %s", callId);
591 findConnectionForAction(callId, "answer").onAnswer();
592 }
593
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700594 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700595 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700596 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700597 }
598
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700599 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700600 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700601 if (mConnectionById.containsKey(callId)) {
602 findConnectionForAction(callId, "disconnect").onDisconnect();
603 } else {
604 findConferenceForAction(callId, "disconnect").onDisconnect();
605 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700606 }
607
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700608 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700609 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700610 if (mConnectionById.containsKey(callId)) {
611 findConnectionForAction(callId, "hold").onHold();
612 } else {
613 findConferenceForAction(callId, "hold").onHold();
614 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700615 }
616
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700617 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700618 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700619 if (mConnectionById.containsKey(callId)) {
620 findConnectionForAction(callId, "unhold").onUnhold();
621 } else {
622 findConferenceForAction(callId, "unhold").onUnhold();
623 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700624 }
625
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700626 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700627 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700628 if (mConnectionById.containsKey(callId)) {
629 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
630 } else {
631 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
632 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700633 }
634
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700635 private void playDtmfTone(String callId, char digit) {
636 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700637 if (mConnectionById.containsKey(callId)) {
638 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
639 } else {
640 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
641 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700642 }
643
644 private void stopDtmfTone(String callId) {
645 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700646 if (mConnectionById.containsKey(callId)) {
647 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
648 } else {
649 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
650 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700651 }
652
Santos Cordon823fd3c2014-08-07 18:35:18 -0700653 private void conference(String callId1, String callId2) {
654 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700655
Santos Cordon823fd3c2014-08-07 18:35:18 -0700656 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700657 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700658 Log.w(this, "Connection2 missing in conference request %s.", callId2);
659 return;
660 }
Santos Cordonb6939982014-06-04 20:20:58 -0700661
Ihab Awad50e35062014-09-30 09:17:03 -0700662 Connection connection1 = findConnectionForAction(callId1, "conference");
663 if (connection1 == getNullConnection()) {
664 Conference conference1 = findConferenceForAction(callId1, "addConnection");
665 if (conference1 == getNullConference()) {
666 Log.w(this,
667 "Connection1 or Conference1 missing in conference request %s.",
668 callId1);
669 } else {
670 conference1.onMerge(connection2);
671 }
672 } else {
673 onConference(connection1, connection2);
674 }
Santos Cordon980acb92014-05-31 10:31:19 -0700675 }
676
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700677 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700678 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700679
680 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700681 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700682 Log.w(this, "Connection missing in conference request %s.", callId);
683 return;
684 }
685
Santos Cordon0159ac02014-08-21 14:28:11 -0700686 Conference conference = connection.getConference();
687 if (conference != null) {
688 conference.onSeparate(connection);
689 }
Santos Cordon980acb92014-05-31 10:31:19 -0700690 }
691
Santos Cordona4868042014-09-04 17:39:22 -0700692 private void mergeConference(String callId) {
693 Log.d(this, "mergeConference(%s)", callId);
694 Conference conference = findConferenceForAction(callId, "mergeConference");
695 if (conference != null) {
696 conference.onMerge();
697 }
698 }
699
700 private void swapConference(String callId) {
701 Log.d(this, "swapConference(%s)", callId);
702 Conference conference = findConferenceForAction(callId, "swapConference");
703 if (conference != null) {
704 conference.onSwap();
705 }
706 }
707
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700708 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700709 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700710 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700711 }
712
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700713 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700714 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700715 // No need to query again if we already did it.
716 return;
717 }
718
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700719 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700720 @Override
721 public void onResult(
722 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700723 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700724 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700725 @Override
726 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700727 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700728 mRemoteConnectionManager.addConnectionService(
729 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700730 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700731 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700732 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700733 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700734 }
735 });
736 }
737
738 @Override
739 public void onError() {
740 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700741 @Override
742 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700743 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700744 }
745 });
746 }
747 });
748 }
749
Ihab Awadf8b69882014-07-25 15:14:01 -0700750 /**
751 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
752 * incoming request. This is used to attach to existing incoming calls.
753 *
754 * @param connectionManagerPhoneAccount See description at
755 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
756 * @param request Details about the incoming call.
757 * @return The {@code Connection} object to satisfy this call, or {@code null} to
758 * not handle the call.
759 */
760 public final RemoteConnection createRemoteIncomingConnection(
761 PhoneAccountHandle connectionManagerPhoneAccount,
762 ConnectionRequest request) {
763 return mRemoteConnectionManager.createRemoteConnection(
764 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700765 }
766
767 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700768 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
769 * outgoing request. This is used to initiate new outgoing calls.
770 *
771 * @param connectionManagerPhoneAccount See description at
772 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
773 * @param request Details about the incoming call.
774 * @return The {@code Connection} object to satisfy this call, or {@code null} to
775 * not handle the call.
776 */
777 public final RemoteConnection createRemoteOutgoingConnection(
778 PhoneAccountHandle connectionManagerPhoneAccount,
779 ConnectionRequest request) {
780 return mRemoteConnectionManager.createRemoteConnection(
781 connectionManagerPhoneAccount, request, false);
782 }
783
784 /**
Ihab Awadb8e85c72014-08-23 20:34:57 -0700785 * Adds two {@code RemoteConnection}s to some {@code RemoteConference}.
786 */
787 public final void conferenceRemoteConnections(
788 RemoteConnection a,
789 RemoteConnection b) {
790 mRemoteConnectionManager.conferenceRemoteConnections(a, b);
791 }
792
793 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700794 * Adds a new conference call. When a conference call is created either as a result of an
795 * explicit request via {@link #onConference} or otherwise, the connection service should supply
796 * an instance of {@link Conference} by invoking this method. A conference call provided by this
797 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
798 *
799 * @param conference The new conference object.
800 */
801 public final void addConference(Conference conference) {
802 String id = addConferenceInternal(conference);
803 if (id != null) {
804 List<String> connectionIds = new ArrayList<>(2);
805 for (Connection connection : conference.getConnections()) {
806 if (mIdByConnection.containsKey(connection)) {
807 connectionIds.add(mIdByConnection.get(connection));
808 }
809 }
810 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700811 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700812 conference.getState(),
813 conference.getCapabilities(),
814 connectionIds);
815 mAdapter.addConferenceCall(id, parcelableConference);
816
817 // Go through any child calls and set the parent.
818 for (Connection connection : conference.getConnections()) {
819 String connectionId = mIdByConnection.get(connection);
820 if (connectionId != null) {
821 mAdapter.setIsConferenced(connectionId, id);
822 }
823 }
824 }
825 }
826
827 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700828 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
829 * has taken responsibility.
830 *
831 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700832 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700833 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700834 return mConnectionById.values();
835 }
836
837 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700838 * Create a {@code Connection} given an incoming request. This is used to attach to existing
839 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700840 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700841 * @param connectionManagerPhoneAccount See description at
842 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
843 * @param request Details about the incoming call.
844 * @return The {@code Connection} object to satisfy this call, or {@code null} to
845 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700846 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700847 public Connection onCreateIncomingConnection(
848 PhoneAccountHandle connectionManagerPhoneAccount,
849 ConnectionRequest request) {
850 return null;
851 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700852
853 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700854 * Create a {@code Connection} given an outgoing request. This is used to initiate new
855 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700856 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700857 * @param connectionManagerPhoneAccount The connection manager account to use for managing
858 * this call.
859 * <p>
860 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
861 * has registered one or more {@code PhoneAccount}s having
862 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
863 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
864 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
865 * making the connection.
866 * <p>
867 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
868 * being asked to make a direct connection. The
869 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
870 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
871 * making the connection.
872 * @param request Details about the outgoing call.
873 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700874 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700875 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700876 public Connection onCreateOutgoingConnection(
877 PhoneAccountHandle connectionManagerPhoneAccount,
878 ConnectionRequest request) {
879 return null;
880 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700881
882 /**
Yorke Leec3cf9822014-10-02 09:38:39 -0700883 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
884 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
885 * call created using
886 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
887 *
888 * @param connectionManagerPhoneAccount
889 * @param request
890 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -0700891 *
892 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -0700893 */
894 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
895 ConnectionRequest request) {
896 return null;
897 }
898
899 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700900 * Conference two specified connections. Invoked when the user has made a request to merge the
901 * specified connections into a conference call. In response, the connection service should
902 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700903 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700904 * @param connection1 A connection to merge into a conference call.
905 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700906 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700907 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700908
Ihab Awadb8e85c72014-08-23 20:34:57 -0700909 public void onRemoteConferenceAdded(RemoteConference conference) {}
910
Santos Cordon823fd3c2014-08-07 18:35:18 -0700911 /**
912 * @hide
913 */
914 public boolean containsConference(Conference conference) {
915 return mIdByConference.containsKey(conference);
916 }
917
Ihab Awadb8e85c72014-08-23 20:34:57 -0700918 /** {@hide} */
919 void addRemoteConference(RemoteConference remoteConference) {
920 onRemoteConferenceAdded(remoteConference);
921 }
922
Ihab Awad5d0410f2014-07-30 10:07:40 -0700923 private void onAccountsInitialized() {
924 mAreAccountsInitialized = true;
925 for (Runnable r : mPreInitializationConnectionRequests) {
926 r.run();
927 }
928 mPreInitializationConnectionRequests.clear();
929 }
930
Ihab Awad542e0ea2014-05-16 10:22:16 -0700931 private void addConnection(String callId, Connection connection) {
932 mConnectionById.put(callId, connection);
933 mIdByConnection.put(connection, callId);
934 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700935 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700936 }
937
938 private void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700939 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700940 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700941 connection.removeConnectionListener(mConnectionListener);
942 mConnectionById.remove(mIdByConnection.get(connection));
943 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -0700944 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700945 }
946
Santos Cordon823fd3c2014-08-07 18:35:18 -0700947 private String addConferenceInternal(Conference conference) {
948 if (mIdByConference.containsKey(conference)) {
949 Log.w(this, "Re-adding an existing conference: %s.", conference);
950 } else if (conference != null) {
951 String id = UUID.randomUUID().toString();
952 mConferenceById.put(id, conference);
953 mIdByConference.put(conference, id);
954 conference.addListener(mConferenceListener);
955 return id;
956 }
957
958 return null;
959 }
960
961 private void removeConference(Conference conference) {
962 if (mIdByConference.containsKey(conference)) {
963 conference.removeListener(mConferenceListener);
964
965 String id = mIdByConference.get(conference);
966 mConferenceById.remove(id);
967 mIdByConference.remove(conference);
968 mAdapter.removeCall(id);
969 }
970 }
971
Ihab Awad542e0ea2014-05-16 10:22:16 -0700972 private Connection findConnectionForAction(String callId, String action) {
973 if (mConnectionById.containsKey(callId)) {
974 return mConnectionById.get(callId);
975 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700976 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700977 return getNullConnection();
978 }
979
980 static synchronized Connection getNullConnection() {
981 if (sNullConnection == null) {
982 sNullConnection = new Connection() {};
983 }
984 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700985 }
Santos Cordon0159ac02014-08-21 14:28:11 -0700986
987 private Conference findConferenceForAction(String conferenceId, String action) {
988 if (mConferenceById.containsKey(conferenceId)) {
989 return mConferenceById.get(conferenceId);
990 }
991 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
992 return getNullConference();
993 }
994
Ihab Awadb8e85c72014-08-23 20:34:57 -0700995 private List<String> createConnectionIdList(List<Connection> connections) {
996 List<String> ids = new ArrayList<>();
997 for (Connection c : connections) {
998 if (mIdByConnection.containsKey(c)) {
999 ids.add(mIdByConnection.get(c));
1000 }
1001 }
1002 Collections.sort(ids);
1003 return ids;
1004 }
1005
Santos Cordon0159ac02014-08-21 14:28:11 -07001006 private Conference getNullConference() {
1007 if (sNullConference == null) {
1008 sNullConference = new Conference(null) {};
1009 }
1010 return sNullConference;
1011 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001012
1013 private void endAllConnections() {
1014 // Unbound from telecomm. We should end all connections and conferences.
1015 for (Connection connection : mIdByConnection.keySet()) {
1016 // only operate on top-level calls. Conference calls will be removed on their own.
1017 if (connection.getConference() == null) {
1018 connection.onDisconnect();
1019 }
1020 }
1021 for (Conference conference : mIdByConference.keySet()) {
1022 conference.onDisconnect();
1023 }
1024 }
Santos Cordon980acb92014-05-31 10:31:19 -07001025}