1 /* 2 * Copyright (C) 2024 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.cts.apps; 18 19 import java.util.concurrent.LinkedBlockingQueue; 20 import java.util.concurrent.TimeUnit; 21 import java.util.function.Consumer; 22 23 public class CallSequencingValidator implements Consumer<CallStateTransitionOperation> { 24 private final LinkedBlockingQueue<CallStateTransitionOperation> mQueue = 25 new LinkedBlockingQueue<>(); 26 private final Object mLock = new Object(); 27 private boolean mIsCleanedUp = false; 28 completePendingOperationOrTimeout(int operationType)29 public CallStateTransitionOperation completePendingOperationOrTimeout(int operationType) { 30 CallStateTransitionOperation op; 31 try { 32 op = mQueue.poll(WaitUntil.DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS); 33 if (op == null) return null; 34 if (op.operationType != operationType) { 35 return null; 36 } 37 } catch (InterruptedException e) { 38 return null; 39 } 40 return op; 41 } 42 cleanup()43 public void cleanup() { 44 synchronized (mLock) { 45 mIsCleanedUp = true; 46 mQueue.clear(); 47 } 48 } 49 50 @Override accept(CallStateTransitionOperation op)51 public void accept(CallStateTransitionOperation op) { 52 synchronized (mLock) { 53 if (mIsCleanedUp) { 54 // Automatically complete pending operations after clean up 55 return; 56 } 57 mQueue.add(op); 58 } 59 } 60 } 61