Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.telecom; |
| 18 | |
| 19 | import static android.telecom.CallException.TRANSACTION_EXCEPTION_KEY; |
| 20 | |
| 21 | import android.annotation.CallbackExecutor; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Thomas Stuart | 73a8b87 | 2023-01-10 16:21:51 -0800 | [diff] [blame] | 24 | import android.annotation.SuppressLint; |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 25 | import android.os.Binder; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.OutcomeReceiver; |
| 28 | import android.os.ParcelUuid; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ResultReceiver; |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 31 | import android.text.TextUtils; |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 32 | |
| 33 | import com.android.internal.telecom.ClientTransactionalServiceRepository; |
| 34 | import com.android.internal.telecom.ICallControl; |
| 35 | |
Thomas Stuart | 7aeccde | 2022-12-21 14:47:21 -0800 | [diff] [blame] | 36 | import java.util.List; |
| 37 | import java.util.Objects; |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 38 | import 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 Stuart | 73a8b87 | 2023-01-10 16:21:51 -0800 | [diff] [blame] | 50 | @SuppressLint("NotCloseable") |
| 51 | public final class CallControl { |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 52 | 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 71 | * an individual call. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 72 | */ |
| 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 82 | * will be called on. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 83 | * @param callback that will be completed on the Telecom side that details success or failure |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 84 | * of the requested operation. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 85 | * |
| 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 113 | * will be called on. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 114 | * @param callback that will be completed on the Telecom side that details success or failure |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 115 | * of the requested operation. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 116 | * |
| 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 140 | * Request Telecom disconnect the call and remove the call from telecom tracking. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 141 | * |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 142 | * @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 Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 150 | * |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 151 | * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback |
| 152 | * will be called on. |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 153 | * |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 154 | * @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 Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 168 | */ |
| 169 | public void disconnect(@NonNull DisconnectCause disconnectCause, |
| 170 | @CallbackExecutor @NonNull Executor executor, |
| 171 | @NonNull OutcomeReceiver<Void, CallException> callback) { |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 172 | Objects.requireNonNull(disconnectCause); |
| 173 | Objects.requireNonNull(executor); |
| 174 | Objects.requireNonNull(callback); |
| 175 | validateDisconnectCause(disconnectCause); |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 176 | 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 Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 189 | * 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 |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 200 | * started the call streaming. |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 201 | * |
| 202 | * {@link OutcomeReceiver#onError} will be called if Telecom has failed to |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 203 | * start the call streaming. A {@link CallException} will be passed that |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 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 Stuart | 7aeccde | 2022-12-21 14:47:21 -0800 | [diff] [blame] | 221 | * 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 225 | * @param callEndpoint The {@link CallEndpoint} to change to. |
| 226 | * @param executor The {@link Executor} on which the {@link OutcomeReceiver} callback |
Thomas Stuart | 7aeccde | 2022-12-21 14:47:21 -0800 | [diff] [blame] | 227 | * will be called on. |
Thomas Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 228 | * @param callback The {@link OutcomeReceiver} that will be completed on the Telecom side |
Thomas Stuart | 7aeccde | 2022-12-21 14:47:21 -0800 | [diff] [blame] | 229 | * 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 Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 257 | * 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 260 | * |
| 261 | * @hide |
| 262 | */ |
Thomas Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 263 | 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 Stuart | c2427a7 | 2023-01-27 17:11:02 -0800 | [diff] [blame] | 304 | |
| 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 Stuart | 9bfb243 | 2022-09-27 15:02:07 -0700 | [diff] [blame] | 318 | } |