1 /* 2 * Copyright (C) 2023 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 com.android.internal.annotations.VisibleForTesting; 20 import com.android.server.telecom.Call; 21 import com.android.server.telecom.TelecomSystem; 22 23 import android.telecom.Log; 24 25 import java.util.concurrent.CompletableFuture; 26 import java.util.concurrent.CompletionStage; 27 28 /** 29 * VerifyCallStateChangeTransaction is a transaction that verifies a CallState change and has 30 * the ability to disconnect if the CallState is not changed within the timeout window. 31 * <p> 32 * Note: This transaction has a timeout of 2 seconds. 33 */ 34 public class VerifyCallStateChangeTransaction extends VoipCallTransaction { 35 private static final String TAG = VerifyCallStateChangeTransaction.class.getSimpleName(); 36 private static final long CALL_STATE_TIMEOUT_MILLISECONDS = 2000L; 37 private final Call mCall; 38 private final int mTargetCallState; 39 private final CompletableFuture<VoipCallTransactionResult> mTransactionResult = 40 new CompletableFuture<>(); 41 42 private final Call.CallStateListener mCallStateListenerImpl = new Call.CallStateListener() { 43 @Override 44 public void onCallStateChanged(int newCallState) { 45 Log.d(TAG, "newState=[%d], expectedState=[%d]", newCallState, mTargetCallState); 46 if (newCallState == mTargetCallState) { 47 mTransactionResult.complete(new VoipCallTransactionResult( 48 VoipCallTransactionResult.RESULT_SUCCEED, TAG)); 49 } 50 // NOTE:: keep listening to the call state until the timeout is reached. It's possible 51 // another call state is reached in between... 52 } 53 }; 54 VerifyCallStateChangeTransaction(TelecomSystem.SyncRoot lock, Call call, int targetCallState)55 public VerifyCallStateChangeTransaction(TelecomSystem.SyncRoot lock, Call call, 56 int targetCallState) { 57 super(lock, CALL_STATE_TIMEOUT_MILLISECONDS); 58 mCall = call; 59 mTargetCallState = targetCallState; 60 } 61 62 @Override processTransaction(Void v)63 public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) { 64 Log.d(TAG, "processTransaction:"); 65 // It's possible the Call is already in the expected call state 66 if (isNewCallStateTargetCallState()) { 67 mTransactionResult.complete(new VoipCallTransactionResult( 68 VoipCallTransactionResult.RESULT_SUCCEED, TAG)); 69 return mTransactionResult; 70 } 71 mCall.addCallStateListener(mCallStateListenerImpl); 72 return mTransactionResult; 73 } 74 75 @Override finishTransaction()76 public void finishTransaction() { 77 mCall.removeCallStateListener(mCallStateListenerImpl); 78 } 79 isNewCallStateTargetCallState()80 private boolean isNewCallStateTargetCallState() { 81 return mCall.getState() == mTargetCallState; 82 } 83 84 @VisibleForTesting getTransactionResult()85 public CompletableFuture<VoipCallTransactionResult> getTransactionResult() { 86 return mTransactionResult; 87 } 88 89 @VisibleForTesting getCallStateListenerImpl()90 public Call.CallStateListener getCallStateListenerImpl() { 91 return mCallStateListenerImpl; 92 } 93 } 94