1 /* 2 * Copyright (C) 2025 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.settings.biometrics.face; 18 19 import static android.hardware.biometrics.BiometricFaceConstants.FEATURE_REQUIRE_ATTENTION; 20 21 import android.content.Context; 22 import android.hardware.face.FaceManager; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 27 import com.android.settings.Utils; 28 29 public class FaceAttentionController { 30 31 @Nullable private byte[] mToken; 32 private FaceManager mFaceManager; 33 34 public interface OnSetAttentionListener { 35 /** 36 * Calls when setting attention is completed from FaceManager. 37 */ onSetAttentionCompleted(boolean success)38 void onSetAttentionCompleted(boolean success); 39 } 40 41 public interface OnGetAttentionListener { 42 /** 43 * Calls when getting attention is completed from FaceManager. 44 */ onGetAttentionCompleted(boolean success, boolean enabled)45 void onGetAttentionCompleted(boolean success, boolean enabled); 46 } 47 48 @Nullable private OnSetAttentionListener mSetListener; 49 @Nullable private OnGetAttentionListener mGetListener; 50 51 private final FaceManager.SetFeatureCallback mSetFeatureCallback = 52 new FaceManager.SetFeatureCallback() { 53 @Override 54 public void onCompleted(boolean success, int feature) { 55 if (feature == FEATURE_REQUIRE_ATTENTION) { 56 if (mSetListener != null) { 57 mSetListener.onSetAttentionCompleted(success); 58 } 59 } 60 } 61 }; 62 63 private final FaceManager.GetFeatureCallback mGetFeatureCallback = 64 new FaceManager.GetFeatureCallback() { 65 @Override 66 public void onCompleted( 67 boolean success, @NonNull int[] features, @NonNull boolean[] featureState) { 68 boolean requireAttentionEnabled = false; 69 for (int i = 0; i < features.length; i++) { 70 if (features[i] == FEATURE_REQUIRE_ATTENTION) { 71 requireAttentionEnabled = featureState[i]; 72 } 73 } 74 if (mGetListener != null) { 75 mGetListener.onGetAttentionCompleted(success, requireAttentionEnabled); 76 } 77 } 78 }; 79 FaceAttentionController(@onNull Context context)80 public FaceAttentionController(@NonNull Context context) { 81 mFaceManager = Utils.getFaceManagerOrNull(context); 82 } 83 84 /** 85 * Set the challenge token 86 */ setToken(@ullable byte[] token)87 public void setToken(@Nullable byte[] token) { 88 mToken = token; 89 } 90 91 /** 92 * Get the gaze status 93 */ getAttentionStatus(int userId, @Nullable OnGetAttentionListener listener)94 public void getAttentionStatus(int userId, 95 @Nullable OnGetAttentionListener listener) { 96 mGetListener = listener; 97 mFaceManager.getFeature(userId, FEATURE_REQUIRE_ATTENTION, mGetFeatureCallback); 98 } 99 100 /** 101 * Set the gaze status 102 */ setAttentionStatus( int userId, boolean enabled, @Nullable OnSetAttentionListener listener)103 public void setAttentionStatus( 104 int userId, boolean enabled, @Nullable OnSetAttentionListener listener) { 105 mSetListener = listener; 106 mFaceManager.setFeature(userId, FEATURE_REQUIRE_ATTENTION, enabled, mToken, 107 mSetFeatureCallback); 108 } 109 } 110