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.server.biometrics; 18 19 20 import android.hardware.biometrics.BiometricTestSession; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 25 import java.util.ArrayList; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * List of sessions that will be closed after a test. 32 * 33 * Prefer to simply use a try block with a single session when possible. 34 */ 35 public class TestSessionList implements AutoCloseable { 36 private final Idler mIdler; 37 private final List<BiometricTestSession> mSessions = new ArrayList<>(); 38 private final Map<Integer, BiometricTestSession> mSessionMap = new HashMap<>(); 39 40 public interface Idler { 41 /** Wait for all sensor to be idle. */ waitForIdleSensors()42 void waitForIdleSensors(); 43 } 44 45 /** Create a list with the given idler. */ TestSessionList(@onNull Idler idler)46 public TestSessionList(@NonNull Idler idler) { 47 mIdler = idler; 48 } 49 50 /** Add a session. */ add(@onNull BiometricTestSession session)51 public void add(@NonNull BiometricTestSession session) { 52 mSessions.add(session); 53 } 54 55 /** Add a session associated with a sensor id. */ put(int sensorId, @NonNull BiometricTestSession session)56 public void put(int sensorId, @NonNull BiometricTestSession session) { 57 mSessions.add(session); 58 mSessionMap.put(sensorId, session); 59 } 60 61 /** The first session. */ 62 @Nullable first()63 public BiometricTestSession first() { 64 return mSessions.get(0); 65 } 66 67 /** The session associated with the id added with {@link #put(int, BiometricTestSession)}. */ 68 @Nullable find(int sensorId)69 public BiometricTestSession find(int sensorId) { 70 return mSessionMap.get(sensorId); 71 } 72 73 @Override close()74 public void close() throws Exception { 75 for (BiometricTestSession session : mSessions) { 76 session.close(); 77 } 78 mIdler.waitForIdleSensors(); 79 } 80 } 81