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 com.android.server.telecom.voip;
18 
19 import android.os.OutcomeReceiver;
20 import android.telecom.CallException;
21 import android.util.Log;
22 
23 import com.android.server.telecom.Call;
24 import com.android.server.telecom.CallsManager;
25 
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.CompletionStage;
28 
29 /**
30  * This VoipCallTransaction is responsible for holding any active call in favor of a new call
31  * request. If the active call cannot be held or disconnected, the transaction will fail.
32  */
33 public class MaybeHoldCallForNewCallTransaction extends VoipCallTransaction {
34 
35     private static final String TAG = MaybeHoldCallForNewCallTransaction.class.getSimpleName();
36     private final CallsManager mCallsManager;
37     private final Call mCall;
38     private final boolean mIsCallControlRequest;
39 
MaybeHoldCallForNewCallTransaction(CallsManager callsManager, Call call, boolean isCallControlRequest)40     public MaybeHoldCallForNewCallTransaction(CallsManager callsManager, Call call,
41             boolean isCallControlRequest) {
42         super(callsManager.getLock());
43         mCallsManager = callsManager;
44         mCall = call;
45         mIsCallControlRequest = isCallControlRequest;
46     }
47 
48     @Override
processTransaction(Void v)49     public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) {
50         Log.d(TAG, "processTransaction");
51         CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();
52 
53         mCallsManager.transactionHoldPotentialActiveCallForNewCall(mCall, mIsCallControlRequest,
54                 new OutcomeReceiver<>() {
55             @Override
56             public void onResult(Boolean result) {
57                 Log.d(TAG, "processTransaction: onResult");
58                 future.complete(new VoipCallTransactionResult(
59                         VoipCallTransactionResult.RESULT_SUCCEED, null));
60             }
61 
62             @Override
63             public void onError(CallException exception) {
64                 Log.d(TAG, "processTransaction: onError");
65                 future.complete(new VoipCallTransactionResult(
66                        exception.getCode(), exception.getMessage()));
67             }
68         });
69 
70         return future;
71     }
72 }
73