blob: 65d48f13f2a88845a68fe2e993cf5e323c45b3ba [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/**
Santos Cordona663f862014-10-29 13:49:58 -070043 * {@code ConnectionService} is an abstract service that should be implemented by any app which can
44 * make phone calls and want those calls to be integrated into the built-in phone app.
45 * Once implemented, the {@code ConnectionService} needs two additional steps before it will be
46 * integrated into the phone app:
47 * <p>
48 * 1. <i>Registration in AndroidManifest.xml</i>
49 * <br/>
50 * <pre>
51 * &lt;service android:name="com.example.package.MyConnectionService"
52 * android:label="@string/some_label_for_my_connection_service"
53 * android:permission="android.permission.BIND_CONNECTION_SERVICE"&gt;
54 * &lt;intent-filter&gt;
55 * &lt;action android:name="android.telecom.ConnectionService" /&gt;
56 * &lt;/intent-filter&gt;
57 * &lt;/service&gt;
58 * </pre>
59 * <p>
60 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i>
61 * <br/>
62 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information.
63 * <p>
64 * Once registered and enabled by the user in the dialer settings, telecom will bind to a
65 * {@code ConnectionService} implementation when it wants that {@code ConnectionService} to place
66 * a call or the service has indicated that is has an incoming call through
67 * {@link TelecomManager#addNewIncomingCall}. The {@code ConnectionService} can then expect a call
68 * to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection} wherein it
69 * should provide a new instance of a {@link Connection} object. It is through this
70 * {@link Connection} object that telecom receives state updates and the {@code ConnectionService}
71 * receives call-commands such as answer, reject, hold and disconnect.
72 * <p>
73 * When there are no more live calls, telecom will unbind from the {@code ConnectionService}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070074 */
Sailesh Nepal2a46b902014-07-04 17:21:07 -070075public abstract class ConnectionService extends Service {
Santos Cordon5c6fa952014-07-20 17:47:12 -070076 /**
77 * The {@link Intent} that must be declared as handled by the service.
78 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070079 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
Tyler Gunnef9f6f92014-09-12 22:16:17 -070080 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService";
Santos Cordon5c6fa952014-07-20 17:47:12 -070081
Ihab Awad542e0ea2014-05-16 10:22:16 -070082 // Flag controlling whether PII is emitted into the logs
Ihab Awad60ac30b2014-05-20 22:32:12 -070083 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
Ihab Awad542e0ea2014-05-16 10:22:16 -070084
Ihab Awad8aecfed2014-08-08 17:06:11 -070085 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070086 private static final int MSG_CREATE_CONNECTION = 2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -070087 private static final int MSG_ABORT = 3;
Sailesh Nepalc5b01572014-07-14 16:29:44 -070088 private static final int MSG_ANSWER = 4;
89 private static final int MSG_REJECT = 5;
90 private static final int MSG_DISCONNECT = 6;
91 private static final int MSG_HOLD = 7;
92 private static final int MSG_UNHOLD = 8;
93 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9;
94 private static final int MSG_PLAY_DTMF_TONE = 10;
95 private static final int MSG_STOP_DTMF_TONE = 11;
96 private static final int MSG_CONFERENCE = 12;
97 private static final int MSG_SPLIT_FROM_CONFERENCE = 13;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070098 private static final int MSG_ON_POST_DIAL_CONTINUE = 14;
Ihab Awadb19a0bc2014-08-07 19:46:01 -070099 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700100 private static final int MSG_ANSWER_VIDEO = 17;
Santos Cordona4868042014-09-04 17:39:22 -0700101 private static final int MSG_MERGE_CONFERENCE = 18;
102 private static final int MSG_SWAP_CONFERENCE = 19;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700103
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700104 private static Connection sNullConnection;
105
mike dooley95e80702014-09-18 14:07:52 -0700106 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>();
107 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>();
108 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>();
109 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>();
Ihab Awadb8e85c72014-08-23 20:34:57 -0700110 private final RemoteConnectionManager mRemoteConnectionManager =
111 new RemoteConnectionManager(this);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700112 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700113 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700114
Santos Cordon823fd3c2014-08-07 18:35:18 -0700115 private boolean mAreAccountsInitialized = false;
Santos Cordon0159ac02014-08-21 14:28:11 -0700116 private Conference sNullConference;
Santos Cordon823fd3c2014-08-07 18:35:18 -0700117
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700118 private final IBinder mBinder = new IConnectionService.Stub() {
119 @Override
120 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700121 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
122 }
123
124 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
125 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700126 }
127
128 @Override
Ihab Awadf8b69882014-07-25 15:14:01 -0700129 public void createConnection(
130 PhoneAccountHandle connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700131 String id,
Ihab Awadf8b69882014-07-25 15:14:01 -0700132 ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700133 boolean isIncoming,
134 boolean isUnknown) {
Ihab Awadf8b69882014-07-25 15:14:01 -0700135 SomeArgs args = SomeArgs.obtain();
136 args.arg1 = connectionManagerPhoneAccount;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700137 args.arg2 = id;
138 args.arg3 = request;
Ihab Awadf8b69882014-07-25 15:14:01 -0700139 args.argi1 = isIncoming ? 1 : 0;
Yorke Leec3cf9822014-10-02 09:38:39 -0700140 args.argi2 = isUnknown ? 1 : 0;
Ihab Awadf8b69882014-07-25 15:14:01 -0700141 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700142 }
143
144 @Override
145 public void abort(String callId) {
146 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget();
147 }
148
149 @Override
Tyler Gunnbe74de02014-08-29 14:51:48 -0700150 /** @hide */
151 public void answerVideo(String callId, int videoState) {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700152 SomeArgs args = SomeArgs.obtain();
153 args.arg1 = callId;
Evan Charltonbf11f982014-07-20 22:06:28 -0700154 args.argi1 = videoState;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700155 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget();
156 }
157
158 @Override
159 public void answer(String callId) {
160 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget();
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700161 }
162
163 @Override
164 public void reject(String callId) {
165 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget();
166 }
167
168 @Override
169 public void disconnect(String callId) {
170 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget();
171 }
172
173 @Override
174 public void hold(String callId) {
175 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget();
176 }
177
178 @Override
179 public void unhold(String callId) {
180 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget();
181 }
182
183 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700184 public void onAudioStateChanged(String callId, AudioState audioState) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700185 SomeArgs args = SomeArgs.obtain();
186 args.arg1 = callId;
187 args.arg2 = audioState;
188 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget();
189 }
190
191 @Override
192 public void playDtmfTone(String callId, char digit) {
193 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget();
194 }
195
196 @Override
197 public void stopDtmfTone(String callId) {
198 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
199 }
200
201 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700202 public void conference(String callId1, String callId2) {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700203 SomeArgs args = SomeArgs.obtain();
Santos Cordon823fd3c2014-08-07 18:35:18 -0700204 args.arg1 = callId1;
205 args.arg2 = callId2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700206 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
207 }
208
209 @Override
210 public void splitFromConference(String callId) {
211 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
212 }
213
214 @Override
Santos Cordona4868042014-09-04 17:39:22 -0700215 public void mergeConference(String callId) {
216 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
217 }
218
219 @Override
220 public void swapConference(String callId) {
221 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
222 }
223
224 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700225 public void onPostDialContinue(String callId, boolean proceed) {
226 SomeArgs args = SomeArgs.obtain();
227 args.arg1 = callId;
228 args.argi1 = proceed ? 1 : 0;
229 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget();
230 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700231 };
232
233 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
234 @Override
235 public void handleMessage(Message msg) {
236 switch (msg.what) {
Ihab Awad8aecfed2014-08-08 17:06:11 -0700237 case MSG_ADD_CONNECTION_SERVICE_ADAPTER:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700238 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj);
239 onAdapterAttached();
240 break;
Ihab Awad8aecfed2014-08-08 17:06:11 -0700241 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER:
242 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj);
243 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700244 case MSG_CREATE_CONNECTION: {
245 SomeArgs args = (SomeArgs) msg.obj;
246 try {
Ihab Awad5d0410f2014-07-30 10:07:40 -0700247 final PhoneAccountHandle connectionManagerPhoneAccount =
Ihab Awadf8b69882014-07-25 15:14:01 -0700248 (PhoneAccountHandle) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700249 final String id = (String) args.arg2;
250 final ConnectionRequest request = (ConnectionRequest) args.arg3;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700251 final boolean isIncoming = args.argi1 == 1;
Yorke Leec3cf9822014-10-02 09:38:39 -0700252 final boolean isUnknown = args.argi2 == 1;
Ihab Awad5d0410f2014-07-30 10:07:40 -0700253 if (!mAreAccountsInitialized) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700254 Log.d(this, "Enqueueing pre-init request %s", id);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700255 mPreInitializationConnectionRequests.add(new Runnable() {
256 @Override
257 public void run() {
258 createConnection(
259 connectionManagerPhoneAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700260 id,
Ihab Awad5d0410f2014-07-30 10:07:40 -0700261 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700262 isIncoming,
263 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700264 }
265 });
266 } else {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700267 createConnection(
268 connectionManagerPhoneAccount,
269 id,
270 request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700271 isIncoming,
272 isUnknown);
Ihab Awad5d0410f2014-07-30 10:07:40 -0700273 }
Ihab Awadf8b69882014-07-25 15:14:01 -0700274 } finally {
275 args.recycle();
276 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700277 break;
Ihab Awadf8b69882014-07-25 15:14:01 -0700278 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700279 case MSG_ABORT:
280 abort((String) msg.obj);
281 break;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700282 case MSG_ANSWER:
283 answer((String) msg.obj);
284 break;
285 case MSG_ANSWER_VIDEO: {
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700286 SomeArgs args = (SomeArgs) msg.obj;
287 try {
288 String callId = (String) args.arg1;
Evan Charltonbf11f982014-07-20 22:06:28 -0700289 int videoState = args.argi1;
Tyler Gunnbe74de02014-08-29 14:51:48 -0700290 answerVideo(callId, videoState);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700291 } finally {
292 args.recycle();
293 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700294 break;
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700295 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700296 case MSG_REJECT:
297 reject((String) msg.obj);
298 break;
299 case MSG_DISCONNECT:
300 disconnect((String) msg.obj);
301 break;
302 case MSG_HOLD:
303 hold((String) msg.obj);
304 break;
305 case MSG_UNHOLD:
306 unhold((String) msg.obj);
307 break;
308 case MSG_ON_AUDIO_STATE_CHANGED: {
309 SomeArgs args = (SomeArgs) msg.obj;
310 try {
311 String callId = (String) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700312 AudioState audioState = (AudioState) args.arg2;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700313 onAudioStateChanged(callId, audioState);
314 } finally {
315 args.recycle();
316 }
317 break;
318 }
319 case MSG_PLAY_DTMF_TONE:
320 playDtmfTone((String) msg.obj, (char) msg.arg1);
321 break;
322 case MSG_STOP_DTMF_TONE:
323 stopDtmfTone((String) msg.obj);
324 break;
325 case MSG_CONFERENCE: {
326 SomeArgs args = (SomeArgs) msg.obj;
327 try {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700328 String callId1 = (String) args.arg1;
329 String callId2 = (String) args.arg2;
330 conference(callId1, callId2);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700331 } finally {
332 args.recycle();
333 }
334 break;
335 }
336 case MSG_SPLIT_FROM_CONFERENCE:
337 splitFromConference((String) msg.obj);
338 break;
Santos Cordona4868042014-09-04 17:39:22 -0700339 case MSG_MERGE_CONFERENCE:
340 mergeConference((String) msg.obj);
341 break;
342 case MSG_SWAP_CONFERENCE:
343 swapConference((String) msg.obj);
344 break;
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700345 case MSG_ON_POST_DIAL_CONTINUE: {
346 SomeArgs args = (SomeArgs) msg.obj;
347 try {
348 String callId = (String) args.arg1;
349 boolean proceed = (args.argi1 == 1);
350 onPostDialContinue(callId, proceed);
351 } finally {
352 args.recycle();
353 }
354 break;
355 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700356 default:
357 break;
358 }
359 }
360 };
361
Santos Cordon823fd3c2014-08-07 18:35:18 -0700362 private final Conference.Listener mConferenceListener = new Conference.Listener() {
363 @Override
364 public void onStateChanged(Conference conference, int oldState, int newState) {
365 String id = mIdByConference.get(conference);
366 switch (newState) {
367 case Connection.STATE_ACTIVE:
368 mAdapter.setActive(id);
369 break;
370 case Connection.STATE_HOLDING:
371 mAdapter.setOnHold(id);
372 break;
373 case Connection.STATE_DISCONNECTED:
374 // handled by onDisconnected
375 break;
376 }
377 }
378
379 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700380 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700381 String id = mIdByConference.get(conference);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700382 mAdapter.setDisconnected(id, disconnectCause);
Santos Cordon823fd3c2014-08-07 18:35:18 -0700383 }
384
385 @Override
386 public void onConnectionAdded(Conference conference, Connection connection) {
387 }
388
389 @Override
390 public void onConnectionRemoved(Conference conference, Connection connection) {
391 }
392
393 @Override
Ihab Awad50e35062014-09-30 09:17:03 -0700394 public void onConferenceableConnectionsChanged(
395 Conference conference, List<Connection> conferenceableConnections) {
396 mAdapter.setConferenceableConnections(
397 mIdByConference.get(conference),
398 createConnectionIdList(conferenceableConnections));
399 }
400
401 @Override
Santos Cordon823fd3c2014-08-07 18:35:18 -0700402 public void onDestroyed(Conference conference) {
403 removeConference(conference);
404 }
405
406 @Override
407 public void onCapabilitiesChanged(Conference conference, int capabilities) {
408 String id = mIdByConference.get(conference);
409 Log.d(this, "call capabilities: conference: %s",
410 PhoneCapabilities.toString(capabilities));
411 mAdapter.setCallCapabilities(id, capabilities);
412 }
413 };
414
Ihab Awad542e0ea2014-05-16 10:22:16 -0700415 private final Connection.Listener mConnectionListener = new Connection.Listener() {
416 @Override
417 public void onStateChanged(Connection c, int state) {
418 String id = mIdByConnection.get(c);
Ihab Awad42b30e12014-05-22 09:49:34 -0700419 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state));
Ihab Awad542e0ea2014-05-16 10:22:16 -0700420 switch (state) {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700421 case Connection.STATE_ACTIVE:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700422 mAdapter.setActive(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700423 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700424 case Connection.STATE_DIALING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700425 mAdapter.setDialing(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700426 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700427 case Connection.STATE_DISCONNECTED:
Ihab Awad542e0ea2014-05-16 10:22:16 -0700428 // Handled in onDisconnected()
429 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700430 case Connection.STATE_HOLDING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700431 mAdapter.setOnHold(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700432 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700433 case Connection.STATE_NEW:
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700434 // Nothing to tell Telecom
Ihab Awad542e0ea2014-05-16 10:22:16 -0700435 break;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700436 case Connection.STATE_RINGING:
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700437 mAdapter.setRinging(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700438 break;
439 }
440 }
441
442 @Override
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700443 public void onDisconnected(Connection c, DisconnectCause disconnectCause) {
Ihab Awad542e0ea2014-05-16 10:22:16 -0700444 String id = mIdByConnection.get(c);
Andrew Lee26786392014-09-16 18:14:59 -0700445 Log.d(this, "Adapter set disconnected %s", disconnectCause);
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700446 mAdapter.setDisconnected(id, disconnectCause);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700447 }
448
449 @Override
Tyler Gunnaa07df82014-07-17 07:50:22 -0700450 public void onVideoStateChanged(Connection c, int videoState) {
451 String id = mIdByConnection.get(c);
452 Log.d(this, "Adapter set video state %d", videoState);
453 mAdapter.setVideoState(id, videoState);
454 }
455
456 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700457 public void onAddressChanged(Connection c, Uri address, int presentation) {
Sailesh Nepal61203862014-07-11 14:50:13 -0700458 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700459 mAdapter.setAddress(id, address, presentation);
Sailesh Nepal61203862014-07-11 14:50:13 -0700460 }
461
462 @Override
463 public void onCallerDisplayNameChanged(
464 Connection c, String callerDisplayName, int presentation) {
465 String id = mIdByConnection.get(c);
466 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700467 }
468
469 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700470 public void onDestroyed(Connection c) {
471 removeConnection(c);
472 }
Ihab Awadf8358972014-05-28 16:46:42 -0700473
474 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700475 public void onPostDialWait(Connection c, String remaining) {
Sailesh Nepal091768c2014-06-30 15:15:23 -0700476 String id = mIdByConnection.get(c);
477 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700478 mAdapter.onPostDialWait(id, remaining);
Sailesh Nepal091768c2014-06-30 15:15:23 -0700479 }
480
481 @Override
Andrew Lee100e2932014-09-08 15:34:24 -0700482 public void onRingbackRequested(Connection c, boolean ringback) {
Ihab Awadf8358972014-05-28 16:46:42 -0700483 String id = mIdByConnection.get(c);
484 Log.d(this, "Adapter onRingback %b", ringback);
Andrew Lee100e2932014-09-08 15:34:24 -0700485 mAdapter.setRingbackRequested(id, ringback);
Ihab Awadf8358972014-05-28 16:46:42 -0700486 }
Santos Cordonb6939982014-06-04 20:20:58 -0700487
488 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700489 public void onCallCapabilitiesChanged(Connection c, int capabilities) {
Santos Cordonb6939982014-06-04 20:20:58 -0700490 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700491 Log.d(this, "capabilities: parcelableconnection: %s",
492 PhoneCapabilities.toString(capabilities));
493 mAdapter.setCallCapabilities(id, capabilities);
Santos Cordonb6939982014-06-04 20:20:58 -0700494 }
495
Santos Cordonb6939982014-06-04 20:20:58 -0700496 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700497 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) {
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700498 String id = mIdByConnection.get(c);
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700499 mAdapter.setVideoProvider(id, videoProvider);
Andrew Lee5ffbe8b2014-06-20 16:29:33 -0700500 }
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700501
502 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700503 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) {
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700504 String id = mIdByConnection.get(c);
Andrew Lee100e2932014-09-08 15:34:24 -0700505 mAdapter.setIsVoipAudioMode(id, isVoip);
Sailesh Nepal33aaae42014-07-07 22:49:44 -0700506 }
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700507
508 @Override
Sailesh Nepal001bbbb2014-07-15 14:40:39 -0700509 public void onStatusHintsChanged(Connection c, StatusHints statusHints) {
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700510 String id = mIdByConnection.get(c);
511 mAdapter.setStatusHints(id, statusHints);
512 }
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700513
514 @Override
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700515 public void onConferenceableConnectionsChanged(
516 Connection connection, List<Connection> conferenceableConnections) {
Ihab Awadb8e85c72014-08-23 20:34:57 -0700517 mAdapter.setConferenceableConnections(
518 mIdByConnection.get(connection),
519 createConnectionIdList(conferenceableConnections));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700520 }
Santos Cordon823fd3c2014-08-07 18:35:18 -0700521
522 @Override
523 public void onConferenceChanged(Connection connection, Conference conference) {
524 String id = mIdByConnection.get(connection);
525 if (id != null) {
526 String conferenceId = null;
527 if (conference != null) {
528 conferenceId = mIdByConference.get(conference);
529 }
530 mAdapter.setIsConferenced(id, conferenceId);
531 }
532 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700533 };
534
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700535 /** {@inheritDoc} */
Ihab Awad542e0ea2014-05-16 10:22:16 -0700536 @Override
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700537 public final IBinder onBind(Intent intent) {
538 return mBinder;
539 }
540
Santos Cordon29f2f2e2014-09-11 19:50:24 -0700541 /** {@inheritDoc} */
542 @Override
543 public boolean onUnbind(Intent intent) {
544 endAllConnections();
545 return super.onUnbind(intent);
546 }
547
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700548 /**
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700549 * This can be used by telecom to either create a new outgoing call or attach to an existing
550 * incoming call. In either case, telecom will cycle through a set of services and call
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700551 * createConnection util a connection service cancels the process or completes it successfully.
552 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700553 private void createConnection(
554 final PhoneAccountHandle callManagerAccount,
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700555 final String callId,
Ihab Awadf8b69882014-07-25 15:14:01 -0700556 final ConnectionRequest request,
Yorke Leec3cf9822014-10-02 09:38:39 -0700557 boolean isIncoming,
558 boolean isUnknown) {
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700559 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " +
Yorke Leec3cf9822014-10-02 09:38:39 -0700560 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming,
561 isUnknown);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700562
Yorke Leec3cf9822014-10-02 09:38:39 -0700563 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
564 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
Ihab Awad6107bab2014-08-18 09:23:25 -0700565 : onCreateOutgoingConnection(callManagerAccount, request);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700566 Log.d(this, "createConnection, connection: %s", connection);
567 if (connection == null) {
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700568 connection = Connection.createFailedConnection(
569 new DisconnectCause(DisconnectCause.ERROR));
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700570 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700571
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700572 if (connection.getState() != Connection.STATE_DISCONNECTED) {
Ihab Awad6107bab2014-08-18 09:23:25 -0700573 addConnection(callId, connection);
574 }
575
Andrew Lee100e2932014-09-08 15:34:24 -0700576 Uri address = connection.getAddress();
577 String number = address == null ? "null" : address.getSchemeSpecificPart();
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700578 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s",
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700579 Connection.toLogSafePhoneNumber(number),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700580 Connection.stateToString(connection.getState()),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700581 PhoneCapabilities.toString(connection.getCallCapabilities()));
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700582
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700583 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId);
Ihab Awad6107bab2014-08-18 09:23:25 -0700584 mAdapter.handleCreateConnectionComplete(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700585 callId,
Evan Charltonbf11f982014-07-20 22:06:28 -0700586 request,
587 new ParcelableConnection(
588 request.getAccountHandle(),
589 connection.getState(),
590 connection.getCallCapabilities(),
Andrew Lee100e2932014-09-08 15:34:24 -0700591 connection.getAddress(),
592 connection.getAddressPresentation(),
Evan Charltonbf11f982014-07-20 22:06:28 -0700593 connection.getCallerDisplayName(),
594 connection.getCallerDisplayNamePresentation(),
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700595 connection.getVideoProvider() == null ?
596 null : connection.getVideoProvider().getInterface(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700597 connection.getVideoState(),
Andrew Lee100e2932014-09-08 15:34:24 -0700598 connection.isRingbackRequested(),
Sailesh Nepal8b9d3ca2014-08-14 17:39:34 -0700599 connection.getAudioModeIsVoip(),
Ihab Awad6107bab2014-08-18 09:23:25 -0700600 connection.getStatusHints(),
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700601 connection.getDisconnectCause(),
Ihab Awadb8e85c72014-08-23 20:34:57 -0700602 createConnectionIdList(connection.getConferenceableConnections())));
Evan Charltonbf11f982014-07-20 22:06:28 -0700603 }
604
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700605 private void abort(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700606 Log.d(this, "abort %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700607 findConnectionForAction(callId, "abort").onAbort();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700608 }
609
Tyler Gunnbe74de02014-08-29 14:51:48 -0700610 private void answerVideo(String callId, int videoState) {
611 Log.d(this, "answerVideo %s", callId);
Andrew Lee8da4c3c2014-07-16 10:11:42 -0700612 findConnectionForAction(callId, "answer").onAnswer(videoState);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700613 }
614
Tyler Gunnbe74de02014-08-29 14:51:48 -0700615 private void answer(String callId) {
616 Log.d(this, "answer %s", callId);
617 findConnectionForAction(callId, "answer").onAnswer();
618 }
619
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700620 private void reject(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700621 Log.d(this, "reject %s", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700622 findConnectionForAction(callId, "reject").onReject();
Ihab Awad542e0ea2014-05-16 10:22:16 -0700623 }
624
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700625 private void disconnect(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700626 Log.d(this, "disconnect %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700627 if (mConnectionById.containsKey(callId)) {
628 findConnectionForAction(callId, "disconnect").onDisconnect();
629 } else {
630 findConferenceForAction(callId, "disconnect").onDisconnect();
631 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700632 }
633
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700634 private void hold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700635 Log.d(this, "hold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700636 if (mConnectionById.containsKey(callId)) {
637 findConnectionForAction(callId, "hold").onHold();
638 } else {
639 findConferenceForAction(callId, "hold").onHold();
640 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700641 }
642
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700643 private void unhold(String callId) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700644 Log.d(this, "unhold %s", callId);
Santos Cordon0159ac02014-08-21 14:28:11 -0700645 if (mConnectionById.containsKey(callId)) {
646 findConnectionForAction(callId, "unhold").onUnhold();
647 } else {
648 findConferenceForAction(callId, "unhold").onUnhold();
649 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700650 }
651
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700652 private void onAudioStateChanged(String callId, AudioState audioState) {
Ihab Awad60ac30b2014-05-20 22:32:12 -0700653 Log.d(this, "onAudioStateChanged %s %s", callId, audioState);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700654 if (mConnectionById.containsKey(callId)) {
655 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState);
656 } else {
657 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState);
658 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700659 }
660
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700661 private void playDtmfTone(String callId, char digit) {
662 Log.d(this, "playDtmfTone %s %c", callId, digit);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700663 if (mConnectionById.containsKey(callId)) {
664 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
665 } else {
666 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit);
667 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700668 }
669
670 private void stopDtmfTone(String callId) {
671 Log.d(this, "stopDtmfTone %s", callId);
Yorke Leea0d3ca92014-09-15 19:18:13 -0700672 if (mConnectionById.containsKey(callId)) {
673 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone();
674 } else {
675 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone();
676 }
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700677 }
678
Santos Cordon823fd3c2014-08-07 18:35:18 -0700679 private void conference(String callId1, String callId2) {
680 Log.d(this, "conference %s, %s", callId1, callId2);
Santos Cordon980acb92014-05-31 10:31:19 -0700681
Santos Cordon823fd3c2014-08-07 18:35:18 -0700682 Connection connection2 = findConnectionForAction(callId2, "conference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700683 if (connection2 == getNullConnection()) {
Santos Cordon823fd3c2014-08-07 18:35:18 -0700684 Log.w(this, "Connection2 missing in conference request %s.", callId2);
685 return;
686 }
Santos Cordonb6939982014-06-04 20:20:58 -0700687
Ihab Awad50e35062014-09-30 09:17:03 -0700688 Connection connection1 = findConnectionForAction(callId1, "conference");
689 if (connection1 == getNullConnection()) {
690 Conference conference1 = findConferenceForAction(callId1, "addConnection");
691 if (conference1 == getNullConference()) {
692 Log.w(this,
693 "Connection1 or Conference1 missing in conference request %s.",
694 callId1);
695 } else {
696 conference1.onMerge(connection2);
697 }
698 } else {
699 onConference(connection1, connection2);
700 }
Santos Cordon980acb92014-05-31 10:31:19 -0700701 }
702
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700703 private void splitFromConference(String callId) {
Santos Cordonb6939982014-06-04 20:20:58 -0700704 Log.d(this, "splitFromConference(%s)", callId);
Santos Cordon980acb92014-05-31 10:31:19 -0700705
706 Connection connection = findConnectionForAction(callId, "splitFromConference");
Sailesh Nepalcf7020b2014-08-20 10:07:19 -0700707 if (connection == getNullConnection()) {
Santos Cordon980acb92014-05-31 10:31:19 -0700708 Log.w(this, "Connection missing in conference request %s.", callId);
709 return;
710 }
711
Santos Cordon0159ac02014-08-21 14:28:11 -0700712 Conference conference = connection.getConference();
713 if (conference != null) {
714 conference.onSeparate(connection);
715 }
Santos Cordon980acb92014-05-31 10:31:19 -0700716 }
717
Santos Cordona4868042014-09-04 17:39:22 -0700718 private void mergeConference(String callId) {
719 Log.d(this, "mergeConference(%s)", callId);
720 Conference conference = findConferenceForAction(callId, "mergeConference");
721 if (conference != null) {
722 conference.onMerge();
723 }
724 }
725
726 private void swapConference(String callId) {
727 Log.d(this, "swapConference(%s)", callId);
728 Conference conference = findConferenceForAction(callId, "swapConference");
729 if (conference != null) {
730 conference.onSwap();
731 }
732 }
733
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700734 private void onPostDialContinue(String callId, boolean proceed) {
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700735 Log.d(this, "onPostDialContinue(%s)", callId);
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700736 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed);
Evan Charlton6dea4ac2014-06-03 14:07:13 -0700737 }
738
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700739 private void onAdapterAttached() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700740 if (mAreAccountsInitialized) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700741 // No need to query again if we already did it.
742 return;
743 }
744
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700745 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() {
Santos Cordon52d8a152014-06-17 19:08:45 -0700746 @Override
747 public void onResult(
748 final List<ComponentName> componentNames,
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700749 final List<IBinder> services) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700750 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700751 @Override
752 public void run() {
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700753 for (int i = 0; i < componentNames.size() && i < services.size(); i++) {
Santos Cordon52d8a152014-06-17 19:08:45 -0700754 mRemoteConnectionManager.addConnectionService(
755 componentNames.get(i),
Sailesh Nepal2a46b902014-07-04 17:21:07 -0700756 IConnectionService.Stub.asInterface(services.get(i)));
Santos Cordon52d8a152014-06-17 19:08:45 -0700757 }
Ihab Awad5d0410f2014-07-30 10:07:40 -0700758 onAccountsInitialized();
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700759 Log.d(this, "remote connection services found: " + services);
Santos Cordon52d8a152014-06-17 19:08:45 -0700760 }
761 });
762 }
763
764 @Override
765 public void onError() {
766 mHandler.post(new Runnable() {
Ihab Awad6107bab2014-08-18 09:23:25 -0700767 @Override
768 public void run() {
Ihab Awad9c3f1882014-06-30 21:17:13 -0700769 mAreAccountsInitialized = true;
Santos Cordon52d8a152014-06-17 19:08:45 -0700770 }
771 });
772 }
773 });
774 }
775
Ihab Awadf8b69882014-07-25 15:14:01 -0700776 /**
777 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700778 * incoming request. This is used by {@code ConnectionService}s that are registered with
779 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage
780 * SIM-based incoming calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700781 *
782 * @param connectionManagerPhoneAccount See description at
783 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
784 * @param request Details about the incoming call.
785 * @return The {@code Connection} object to satisfy this call, or {@code null} to
786 * not handle the call.
787 */
788 public final RemoteConnection createRemoteIncomingConnection(
789 PhoneAccountHandle connectionManagerPhoneAccount,
790 ConnectionRequest request) {
791 return mRemoteConnectionManager.createRemoteConnection(
792 connectionManagerPhoneAccount, request, true);
Santos Cordon52d8a152014-06-17 19:08:45 -0700793 }
794
795 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700796 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an
Santos Cordona663f862014-10-29 13:49:58 -0700797 * outgoing request. This is used by {@code ConnectionService}s that are registered with
798 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the
799 * SIM-based {@code ConnectionService} to place its outgoing calls.
Ihab Awadf8b69882014-07-25 15:14:01 -0700800 *
801 * @param connectionManagerPhoneAccount See description at
802 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
803 * @param request Details about the incoming call.
804 * @return The {@code Connection} object to satisfy this call, or {@code null} to
805 * not handle the call.
806 */
807 public final RemoteConnection createRemoteOutgoingConnection(
808 PhoneAccountHandle connectionManagerPhoneAccount,
809 ConnectionRequest request) {
810 return mRemoteConnectionManager.createRemoteConnection(
811 connectionManagerPhoneAccount, request, false);
812 }
813
814 /**
Santos Cordona663f862014-10-29 13:49:58 -0700815 * Indicates to the relevant {@code RemoteConnectionService} that the specified
816 * {@link RemoteConnection}s should be merged into a conference call.
817 * <p>
818 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will
819 * be invoked.
820 *
821 * @param remoteConnection1 The first of the remote connections to conference.
822 * @param remoteConnection2 The second of the remote connections to conference.
Ihab Awadb8e85c72014-08-23 20:34:57 -0700823 */
824 public final void conferenceRemoteConnections(
Santos Cordona663f862014-10-29 13:49:58 -0700825 RemoteConnection remoteConnection1,
826 RemoteConnection remoteConnection2) {
827 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2);
Ihab Awadb8e85c72014-08-23 20:34:57 -0700828 }
829
830 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700831 * Adds a new conference call. When a conference call is created either as a result of an
832 * explicit request via {@link #onConference} or otherwise, the connection service should supply
833 * an instance of {@link Conference} by invoking this method. A conference call provided by this
834 * method will persist until {@link Conference#destroy} is invoked on the conference instance.
835 *
836 * @param conference The new conference object.
837 */
838 public final void addConference(Conference conference) {
839 String id = addConferenceInternal(conference);
840 if (id != null) {
841 List<String> connectionIds = new ArrayList<>(2);
842 for (Connection connection : conference.getConnections()) {
843 if (mIdByConnection.containsKey(connection)) {
844 connectionIds.add(mIdByConnection.get(connection));
845 }
846 }
847 ParcelableConference parcelableConference = new ParcelableConference(
Nancy Chenea38cca2014-09-05 16:38:49 -0700848 conference.getPhoneAccountHandle(),
Santos Cordon823fd3c2014-08-07 18:35:18 -0700849 conference.getState(),
850 conference.getCapabilities(),
851 connectionIds);
852 mAdapter.addConferenceCall(id, parcelableConference);
853
854 // Go through any child calls and set the parent.
855 for (Connection connection : conference.getConnections()) {
856 String connectionId = mIdByConnection.get(connection);
857 if (connectionId != null) {
858 mAdapter.setIsConferenced(connectionId, id);
859 }
860 }
861 }
862 }
863
864 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700865 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new
866 * connection.
867 *
868 * @param phoneAccountHandle The phone account handle for the connection.
869 * @param connection The connection to add.
870 */
871 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle,
872 Connection connection) {
873
874 String id = addExistingConnectionInternal(connection);
875 if (id != null) {
876 List<String> emptyList = new ArrayList<>(0);
877
878 ParcelableConnection parcelableConnection = new ParcelableConnection(
879 phoneAccountHandle,
880 connection.getState(),
881 connection.getCallCapabilities(),
882 connection.getAddress(),
883 connection.getAddressPresentation(),
884 connection.getCallerDisplayName(),
885 connection.getCallerDisplayNamePresentation(),
886 connection.getVideoProvider() == null ?
887 null : connection.getVideoProvider().getInterface(),
888 connection.getVideoState(),
889 connection.isRingbackRequested(),
890 connection.getAudioModeIsVoip(),
891 connection.getStatusHints(),
892 connection.getDisconnectCause(),
893 emptyList);
894 mAdapter.addExistingConnection(id, parcelableConnection);
895 }
896 }
897
898 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700899 * Returns all the active {@code Connection}s for which this {@code ConnectionService}
900 * has taken responsibility.
901 *
902 * @return A collection of {@code Connection}s created by this {@code ConnectionService}.
Santos Cordonb6939982014-06-04 20:20:58 -0700903 */
Sailesh Nepal091768c2014-06-30 15:15:23 -0700904 public final Collection<Connection> getAllConnections() {
Santos Cordonb6939982014-06-04 20:20:58 -0700905 return mConnectionById.values();
906 }
907
908 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700909 * Create a {@code Connection} given an incoming request. This is used to attach to existing
910 * incoming calls.
Evan Charltonbf11f982014-07-20 22:06:28 -0700911 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700912 * @param connectionManagerPhoneAccount See description at
913 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}.
914 * @param request Details about the incoming call.
915 * @return The {@code Connection} object to satisfy this call, or {@code null} to
916 * not handle the call.
Ihab Awad542e0ea2014-05-16 10:22:16 -0700917 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700918 public Connection onCreateIncomingConnection(
919 PhoneAccountHandle connectionManagerPhoneAccount,
920 ConnectionRequest request) {
921 return null;
922 }
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700923
924 /**
Ihab Awadf8b69882014-07-25 15:14:01 -0700925 * Create a {@code Connection} given an outgoing request. This is used to initiate new
926 * outgoing calls.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700927 *
Ihab Awadf8b69882014-07-25 15:14:01 -0700928 * @param connectionManagerPhoneAccount The connection manager account to use for managing
929 * this call.
930 * <p>
931 * If this parameter is not {@code null}, it means that this {@code ConnectionService}
932 * has registered one or more {@code PhoneAccount}s having
933 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain
934 * one of these {@code PhoneAccount}s, while the {@code request} will contain another
935 * (usually but not always distinct) {@code PhoneAccount} to be used for actually
936 * making the connection.
937 * <p>
938 * If this parameter is {@code null}, it means that this {@code ConnectionService} is
939 * being asked to make a direct connection. The
940 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be
941 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for
942 * making the connection.
943 * @param request Details about the outgoing call.
944 * @return The {@code Connection} object to satisfy this call, or the result of an invocation
Andrew Lee7f3d41f2014-09-11 17:33:16 -0700945 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call.
Sailesh Nepalc5b01572014-07-14 16:29:44 -0700946 */
Ihab Awadf8b69882014-07-25 15:14:01 -0700947 public Connection onCreateOutgoingConnection(
948 PhoneAccountHandle connectionManagerPhoneAccount,
949 ConnectionRequest request) {
950 return null;
951 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700952
953 /**
Yorke Leec3cf9822014-10-02 09:38:39 -0700954 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating
955 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming
956 * call created using
957 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
958 *
959 * @param connectionManagerPhoneAccount
960 * @param request
961 * @return
Yorke Lee770ed6e2014-10-06 18:58:52 -0700962 *
963 * @hide
Yorke Leec3cf9822014-10-02 09:38:39 -0700964 */
965 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
966 ConnectionRequest request) {
967 return null;
968 }
969
970 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700971 * Conference two specified connections. Invoked when the user has made a request to merge the
972 * specified connections into a conference call. In response, the connection service should
973 * create an instance of {@link Conference} and pass it into {@link #addConference}.
Santos Cordonb6939982014-06-04 20:20:58 -0700974 *
Santos Cordon823fd3c2014-08-07 18:35:18 -0700975 * @param connection1 A connection to merge into a conference call.
976 * @param connection2 A connection to merge into a conference call.
Santos Cordonb6939982014-06-04 20:20:58 -0700977 */
Santos Cordon823fd3c2014-08-07 18:35:18 -0700978 public void onConference(Connection connection1, Connection connection2) {}
Santos Cordonb6939982014-06-04 20:20:58 -0700979
Santos Cordona663f862014-10-29 13:49:58 -0700980 /**
981 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s.
982 * When this method is invoked, this {@link ConnectionService} should create its own
983 * representation of the conference call and send it to telecom using {@link #addConference}.
984 * <p>
985 * This is only relevant to {@link ConnectionService}s which are registered with
986 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}.
987 *
988 * @param conference The remote conference call.
989 */
Ihab Awadb8e85c72014-08-23 20:34:57 -0700990 public void onRemoteConferenceAdded(RemoteConference conference) {}
991
Santos Cordon823fd3c2014-08-07 18:35:18 -0700992 /**
Tyler Gunn4a57b9b2014-10-30 14:27:48 -0700993 * Called when an existing connection is added remotely.
994 * @param connection The existing connection which was added.
995 */
996 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {}
997
998 /**
Santos Cordon823fd3c2014-08-07 18:35:18 -0700999 * @hide
1000 */
1001 public boolean containsConference(Conference conference) {
1002 return mIdByConference.containsKey(conference);
1003 }
1004
Ihab Awadb8e85c72014-08-23 20:34:57 -07001005 /** {@hide} */
1006 void addRemoteConference(RemoteConference remoteConference) {
1007 onRemoteConferenceAdded(remoteConference);
1008 }
1009
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001010 /** {@hide} */
1011 void addRemoteExistingConnection(RemoteConnection remoteConnection) {
1012 onRemoteExistingConnectionAdded(remoteConnection);
1013 }
1014
Ihab Awad5d0410f2014-07-30 10:07:40 -07001015 private void onAccountsInitialized() {
1016 mAreAccountsInitialized = true;
1017 for (Runnable r : mPreInitializationConnectionRequests) {
1018 r.run();
1019 }
1020 mPreInitializationConnectionRequests.clear();
1021 }
1022
Tyler Gunn4a57b9b2014-10-30 14:27:48 -07001023 /**
1024 * Adds an existing connection to the list of connections, identified by a new UUID.
1025 *
1026 * @param connection The connection.
1027 * @return The UUID of the connection (e.g. the call-id).
1028 */
1029 private String addExistingConnectionInternal(Connection connection) {
1030 String id = UUID.randomUUID().toString();
1031 addConnection(id, connection);
1032 return id;
1033 }
1034
Ihab Awad542e0ea2014-05-16 10:22:16 -07001035 private void addConnection(String callId, Connection connection) {
1036 mConnectionById.put(callId, connection);
1037 mIdByConnection.put(connection, callId);
1038 connection.addConnectionListener(mConnectionListener);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001039 connection.setConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001040 }
1041
Anthony Lee30e65842014-11-06 16:30:53 -08001042 /** {@hide} */
1043 protected void removeConnection(Connection connection) {
Ihab Awad8aecfed2014-08-08 17:06:11 -07001044 String id = mIdByConnection.get(connection);
Santos Cordon823fd3c2014-08-07 18:35:18 -07001045 connection.unsetConnectionService(this);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001046 connection.removeConnectionListener(mConnectionListener);
1047 mConnectionById.remove(mIdByConnection.get(connection));
1048 mIdByConnection.remove(connection);
Ihab Awad8aecfed2014-08-08 17:06:11 -07001049 mAdapter.removeCall(id);
Ihab Awad542e0ea2014-05-16 10:22:16 -07001050 }
1051
Santos Cordon823fd3c2014-08-07 18:35:18 -07001052 private String addConferenceInternal(Conference conference) {
1053 if (mIdByConference.containsKey(conference)) {
1054 Log.w(this, "Re-adding an existing conference: %s.", conference);
1055 } else if (conference != null) {
1056 String id = UUID.randomUUID().toString();
1057 mConferenceById.put(id, conference);
1058 mIdByConference.put(conference, id);
1059 conference.addListener(mConferenceListener);
1060 return id;
1061 }
1062
1063 return null;
1064 }
1065
1066 private void removeConference(Conference conference) {
1067 if (mIdByConference.containsKey(conference)) {
1068 conference.removeListener(mConferenceListener);
1069
1070 String id = mIdByConference.get(conference);
1071 mConferenceById.remove(id);
1072 mIdByConference.remove(conference);
1073 mAdapter.removeCall(id);
1074 }
1075 }
1076
Ihab Awad542e0ea2014-05-16 10:22:16 -07001077 private Connection findConnectionForAction(String callId, String action) {
1078 if (mConnectionById.containsKey(callId)) {
1079 return mConnectionById.get(callId);
1080 }
Ihab Awad60ac30b2014-05-20 22:32:12 -07001081 Log.w(this, "%s - Cannot find Connection %s", action, callId);
Sailesh Nepalcf7020b2014-08-20 10:07:19 -07001082 return getNullConnection();
1083 }
1084
1085 static synchronized Connection getNullConnection() {
1086 if (sNullConnection == null) {
1087 sNullConnection = new Connection() {};
1088 }
1089 return sNullConnection;
Santos Cordon7c7bc7f2014-07-28 18:15:48 -07001090 }
Santos Cordon0159ac02014-08-21 14:28:11 -07001091
1092 private Conference findConferenceForAction(String conferenceId, String action) {
1093 if (mConferenceById.containsKey(conferenceId)) {
1094 return mConferenceById.get(conferenceId);
1095 }
1096 Log.w(this, "%s - Cannot find conference %s", action, conferenceId);
1097 return getNullConference();
1098 }
1099
Ihab Awadb8e85c72014-08-23 20:34:57 -07001100 private List<String> createConnectionIdList(List<Connection> connections) {
1101 List<String> ids = new ArrayList<>();
1102 for (Connection c : connections) {
1103 if (mIdByConnection.containsKey(c)) {
1104 ids.add(mIdByConnection.get(c));
1105 }
1106 }
1107 Collections.sort(ids);
1108 return ids;
1109 }
1110
Santos Cordon0159ac02014-08-21 14:28:11 -07001111 private Conference getNullConference() {
1112 if (sNullConference == null) {
1113 sNullConference = new Conference(null) {};
1114 }
1115 return sNullConference;
1116 }
Santos Cordon29f2f2e2014-09-11 19:50:24 -07001117
1118 private void endAllConnections() {
1119 // Unbound from telecomm. We should end all connections and conferences.
1120 for (Connection connection : mIdByConnection.keySet()) {
1121 // only operate on top-level calls. Conference calls will be removed on their own.
1122 if (connection.getConference() == null) {
1123 connection.onDisconnect();
1124 }
1125 }
1126 for (Conference conference : mIdByConference.keySet()) {
1127 conference.onDisconnect();
1128 }
1129 }
Santos Cordon980acb92014-05-31 10:31:19 -07001130}