blob: f3c91f664dee23b4154b348461ac7b7a72964b01 [file] [log] [blame]
Thomas Stuart9bfb2432022-09-27 15:02:07 -07001/*
2 * Copyright (C) 2022 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
17package android.telecom;
18
19import static android.telecom.CallException.TRANSACTION_EXCEPTION_KEY;
20
21import android.annotation.CallbackExecutor;
22import android.annotation.NonNull;
23import android.annotation.Nullable;
Thomas Stuart73a8b872023-01-10 16:21:51 -080024import android.annotation.SuppressLint;
Thomas Stuart9bfb2432022-09-27 15:02:07 -070025import android.os.Binder;
26import android.os.Bundle;
27import android.os.OutcomeReceiver;
28import android.os.ParcelUuid;
29import android.os.RemoteException;
30import android.os.ResultReceiver;
Thomas Stuartc2427a72023-01-27 17:11:02 -080031import android.text.TextUtils;
Thomas Stuart9bfb2432022-09-27 15:02:07 -070032
33import com.android.internal.telecom.ClientTransactionalServiceRepository;
34import com.android.internal.telecom.ICallControl;
35
Thomas Stuart7aeccde2022-12-21 14:47:21 -080036import java.util.List;
37import java.util.Objects;
Thomas Stuart9bfb2432022-09-27 15:02:07 -070038import java.util.concurrent.Executor;
39
40/**
41 * CallControl provides client side control of a call. Each Call will get an individual CallControl
42 * instance in which the client can alter the state of the associated call.
43 *
44 * <p>
45 * Each method is Transactional meaning that it can succeed or fail. If a transaction succeeds,
46 * the {@link OutcomeReceiver#onResult} will be called by Telecom. Otherwise, the
47 * {@link OutcomeReceiver#onError} is called and provides a {@link CallException} that details why
48 * the operation failed.
49 */
Thomas Stuart73a8b872023-01-10 16:21:51 -080050@SuppressLint("NotCloseable")
51public final class CallControl {
Thomas Stuart9bfb2432022-09-27 15:02:07 -070052 private static final String TAG = CallControl.class.getSimpleName();
53 private static final String INTERFACE_ERROR_MSG = "Call Control is not available";
54 private final String mCallId;
55 private final ICallControl mServerInterface;
56 private final PhoneAccountHandle mPhoneAccountHandle;
57 private final ClientTransactionalServiceRepository mRepository;
58
59 /** @hide */
60 public CallControl(@NonNull String callId, @Nullable ICallControl serverInterface,
61 @NonNull ClientTransactionalServiceRepository repository,
62 @NonNull PhoneAccountHandle pah) {
63 mCallId = callId;
64 mServerInterface = serverInterface;
65 mRepository = repository;
66 mPhoneAccountHandle = pah;
67 }
68
69 /**
70 * @return the callId Telecom assigned to this CallControl object which should be attached to
Thomas Stuartc2427a72023-01-27 17:11:02 -080071 * an individual call.
Thomas Stuart9bfb2432022-09-27 15:02:07 -070072 */
73 @NonNull
74 public ParcelUuid getCallId() {
75 return ParcelUuid.fromString(mCallId);
76 }
77
78 /**
79 * Request Telecom set the call state to active.
80 *
81 * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback
Thomas Stuartc2427a72023-01-27 17:11:02 -080082 * will be called on.
Thomas Stuart9bfb2432022-09-27 15:02:07 -070083 * @param callback that will be completed on the Telecom side that details success or failure
Thomas Stuartc2427a72023-01-27 17:11:02 -080084 * of the requested operation.
Thomas Stuart9bfb2432022-09-27 15:02:07 -070085 *
86 * {@link OutcomeReceiver#onResult} will be called if Telecom has successfully
87 * switched the call state to active
88 *
89 * {@link OutcomeReceiver#onError} will be called if Telecom has failed to set
90 * the call state to active. A {@link CallException} will be passed
91 * that details why the operation failed.
92 */
93 public void setActive(@CallbackExecutor @NonNull Executor executor,
94 @NonNull OutcomeReceiver<Void, CallException> callback) {
95 if (mServerInterface != null) {
96 try {
97 mServerInterface.setActive(mCallId,
98 new CallControlResultReceiver("setActive", executor, callback));
99
100 } catch (RemoteException e) {
101 throw e.rethrowAsRuntimeException();
102 }
103 } else {
104 throw new IllegalStateException(INTERFACE_ERROR_MSG);
105 }
106 }
107
108 /**
109 * Request Telecom set the call state to inactive. This the same as hold for two call endpoints
110 * but can be extended to setting a meeting to inactive.
111 *
112 * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback
Thomas Stuartc2427a72023-01-27 17:11:02 -0800113 * will be called on.
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700114 * @param callback that will be completed on the Telecom side that details success or failure
Thomas Stuartc2427a72023-01-27 17:11:02 -0800115 * of the requested operation.
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700116 *
117 * {@link OutcomeReceiver#onResult} will be called if Telecom has successfully
118 * switched the call state to inactive
119 *
120 * {@link OutcomeReceiver#onError} will be called if Telecom has failed to set
121 * the call state to inactive. A {@link CallException} will be passed
122 * that details why the operation failed.
123 */
124 public void setInactive(@CallbackExecutor @NonNull Executor executor,
125 @NonNull OutcomeReceiver<Void, CallException> callback) {
126 if (mServerInterface != null) {
127 try {
128 mServerInterface.setInactive(mCallId,
129 new CallControlResultReceiver("setInactive", executor, callback));
130
131 } catch (RemoteException e) {
132 throw e.rethrowAsRuntimeException();
133 }
134 } else {
135 throw new IllegalStateException(INTERFACE_ERROR_MSG);
136 }
137 }
138
139 /**
Thomas Stuartc2427a72023-01-27 17:11:02 -0800140 * Request Telecom disconnect the call and remove the call from telecom tracking.
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700141 *
Thomas Stuartc2427a72023-01-27 17:11:02 -0800142 * @param disconnectCause represents the cause for disconnecting the call. The only valid
143 * codes for the {@link android.telecom.DisconnectCause} passed in are:
144 * <ul>
145 * <li>{@link DisconnectCause#LOCAL}</li>
146 * <li>{@link DisconnectCause#REMOTE}</li>
147 * <li>{@link DisconnectCause#REJECTED}</li>
148 * <li>{@link DisconnectCause#MISSED}</li>
149 * </ul>
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700150 *
Thomas Stuartc2427a72023-01-27 17:11:02 -0800151 * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback
152 * will be called on.
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700153 *
Thomas Stuartc2427a72023-01-27 17:11:02 -0800154 * @param callback That will be completed on the Telecom side that details success or
155 * failure of the requested operation.
156 *
157 * {@link OutcomeReceiver#onResult} will be called if Telecom has
158 * successfully disconnected the call.
159 *
160 * {@link OutcomeReceiver#onError} will be called if Telecom has failed
161 * to disconnect the call. A {@link CallException} will be passed
162 * that details why the operation failed.
163 *
164 * <p>
165 * Note: After the call has been successfully disconnected, calling any CallControl API will
166 * result in the {@link OutcomeReceiver#onError} with
167 * {@link CallException#CODE_CALL_IS_NOT_BEING_TRACKED}.
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700168 */
169 public void disconnect(@NonNull DisconnectCause disconnectCause,
170 @CallbackExecutor @NonNull Executor executor,
171 @NonNull OutcomeReceiver<Void, CallException> callback) {
Thomas Stuartc2427a72023-01-27 17:11:02 -0800172 Objects.requireNonNull(disconnectCause);
173 Objects.requireNonNull(executor);
174 Objects.requireNonNull(callback);
175 validateDisconnectCause(disconnectCause);
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700176 if (mServerInterface != null) {
177 try {
178 mServerInterface.disconnect(mCallId, disconnectCause,
179 new CallControlResultReceiver("disconnect", executor, callback));
180 } catch (RemoteException e) {
181 throw e.rethrowAsRuntimeException();
182 }
183 } else {
184 throw new IllegalStateException(INTERFACE_ERROR_MSG);
185 }
186 }
187
188 /**
Grace Jiaef5a4cc2022-12-13 11:08:55 -0800189 * Request start a call streaming session. On receiving valid request, telecom will bind to
190 * the {@link CallStreamingService} implemented by a general call streaming sender. So that the
191 * call streaming sender can perform streaming local device audio to another remote device and
192 * control the call during streaming.
193 *
194 * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback
195 * will be called on.
196 * @param callback that will be completed on the Telecom side that details success or failure
197 * of the requested operation.
198 *
199 * {@link OutcomeReceiver#onResult} will be called if Telecom has successfully
200 * rejected the incoming call.
201 *
202 * {@link OutcomeReceiver#onError} will be called if Telecom has failed to
203 * reject the incoming call. A {@link CallException} will be passed that
204 * details why the operation failed.
205 */
206 public void startCallStreaming(@CallbackExecutor @NonNull Executor executor,
207 @NonNull OutcomeReceiver<Void, CallException> callback) {
208 if (mServerInterface != null) {
209 try {
210 mServerInterface.startCallStreaming(mCallId,
211 new CallControlResultReceiver("startCallStreaming", executor, callback));
212 } catch (RemoteException e) {
213 throw e.rethrowAsRuntimeException();
214 }
215 } else {
216 throw new IllegalStateException(INTERFACE_ERROR_MSG);
217 }
218 }
219
220 /**
Thomas Stuart7aeccde2022-12-21 14:47:21 -0800221 * Request a CallEndpoint change. Clients should not define their own CallEndpoint when
222 * requesting a change. Instead, the new endpoint should be one of the valid endpoints provided
223 * by {@link CallEventCallback#onAvailableCallEndpointsChanged(List)}.
224 *
Thomas Stuartc2427a72023-01-27 17:11:02 -0800225 * @param callEndpoint The {@link CallEndpoint} to change to.
226 * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback
Thomas Stuart7aeccde2022-12-21 14:47:21 -0800227 * will be called on.
Thomas Stuartc2427a72023-01-27 17:11:02 -0800228 * @param callback The {@link OutcomeReceiver} that will be completed on the Telecom side
Thomas Stuart7aeccde2022-12-21 14:47:21 -0800229 * that details success or failure of the requested operation.
230 *
231 * {@link OutcomeReceiver#onResult} will be called if Telecom has
232 * successfully changed the CallEndpoint that was requested.
233 *
234 * {@link OutcomeReceiver#onError} will be called if Telecom has failed to
235 * switch to the requested CallEndpoint. A {@link CallException} will be
236 * passed that details why the operation failed.
237 */
238 public void requestCallEndpointChange(@NonNull CallEndpoint callEndpoint,
239 @CallbackExecutor @NonNull Executor executor,
240 @NonNull OutcomeReceiver<Void, CallException> callback) {
241 Objects.requireNonNull(callEndpoint);
242 Objects.requireNonNull(executor);
243 Objects.requireNonNull(callback);
244 if (mServerInterface != null) {
245 try {
246 mServerInterface.requestCallEndpointChange(callEndpoint,
247 new CallControlResultReceiver("endpointChange", executor, callback));
248 } catch (RemoteException e) {
249 throw e.rethrowAsRuntimeException();
250 }
251 } else {
252 throw new IllegalStateException(INTERFACE_ERROR_MSG);
253 }
254 }
255
256 /**
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700257 * Since {@link OutcomeReceiver}s cannot be passed via AIDL, a ResultReceiver (which can) must
258 * wrap the Clients {@link OutcomeReceiver} passed in and await for the Telecom Server side
259 * response in {@link ResultReceiver#onReceiveResult(int, Bundle)}.
Thomas Stuartc2427a72023-01-27 17:11:02 -0800260 *
261 * @hide
262 */
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700263 private class CallControlResultReceiver extends ResultReceiver {
264 private final String mCallingMethod;
265 private final Executor mExecutor;
266 private final OutcomeReceiver<Void, CallException> mClientCallback;
267
268 CallControlResultReceiver(String method, Executor executor,
269 OutcomeReceiver<Void, CallException> clientCallback) {
270 super(null);
271 mCallingMethod = method;
272 mExecutor = executor;
273 mClientCallback = clientCallback;
274 }
275
276 @Override
277 protected void onReceiveResult(int resultCode, Bundle resultData) {
278 Log.d(CallControl.TAG, "%s: oRR: resultCode=[%s]", mCallingMethod, resultCode);
279 super.onReceiveResult(resultCode, resultData);
280 final long identity = Binder.clearCallingIdentity();
281 try {
282 if (resultCode == TelecomManager.TELECOM_TRANSACTION_SUCCESS) {
283 mExecutor.execute(() -> mClientCallback.onResult(null));
284 } else {
285 mExecutor.execute(() ->
286 mClientCallback.onError(getTransactionException(resultData)));
287 }
288 } finally {
289 Binder.restoreCallingIdentity(identity);
290 }
291 }
292
293 }
294
295 /** @hide */
296 private CallException getTransactionException(Bundle resultData) {
297 String message = "unknown error";
298 if (resultData != null && resultData.containsKey(TRANSACTION_EXCEPTION_KEY)) {
299 return resultData.getParcelable(TRANSACTION_EXCEPTION_KEY,
300 CallException.class);
301 }
302 return new CallException(message, CallException.CODE_ERROR_UNKNOWN);
303 }
Thomas Stuartc2427a72023-01-27 17:11:02 -0800304
305 /** @hide */
306 private void validateDisconnectCause(DisconnectCause disconnectCause) {
307 final int code = disconnectCause.getCode();
308 if (code != DisconnectCause.LOCAL && code != DisconnectCause.REMOTE
309 && code != DisconnectCause.MISSED && code != DisconnectCause.REJECTED) {
310 throw new IllegalArgumentException(TextUtils.formatSimple(
311 "The DisconnectCause code provided, %d , is not a valid Disconnect code. Valid "
312 + "DisconnectCause codes are limited to [DisconnectCause.LOCAL, "
313 + "DisconnectCause.REMOTE, DisconnectCause.MISSED, or "
314 + "DisconnectCause.REJECTED]", disconnectCause.getCode()));
315 }
316 }
317
Thomas Stuart9bfb2432022-09-27 15:02:07 -0700318}