Avoid using MANDATORY_BIOMETRICS bit if flag is not enabled

Flag: android.hardware.biometrics.flags.mandatory_biometrics
Bug: 370757426
Test: N/A
Change-Id: I4e2a72a80f68579d60466e859370b6fd93a8cd23
This commit is contained in:
Diya Bera
2024-10-03 23:08:45 +00:00
parent 78153998c7
commit aa322bc4f7
2 changed files with 29 additions and 11 deletions

View File

@@ -1505,17 +1505,18 @@ public final class Utils extends com.android.settingslib.Utils {
* @return biometric status when mandatory biometrics authentication is requested * @return biometric status when mandatory biometrics authentication is requested
*/ */
public static BiometricStatus requestBiometricAuthenticationForMandatoryBiometrics( public static BiometricStatus requestBiometricAuthenticationForMandatoryBiometrics(
@NonNull Context context, @NonNull Context context, boolean biometricsAuthenticationRequested, int userId) {
boolean biometricsAuthenticationRequested, int userId) {
final BiometricManager biometricManager = context.getSystemService(BiometricManager.class); final BiometricManager biometricManager = context.getSystemService(BiometricManager.class);
if (biometricManager == null) { if (biometricManager == null) {
Log.e(TAG, "Biometric Manager is null."); Log.e(TAG, "Biometric Manager is null.");
return BiometricStatus.NOT_ACTIVE; return BiometricStatus.NOT_ACTIVE;
} }
final int status = biometricManager.canAuthenticate(userId,
BiometricManager.Authenticators.MANDATORY_BIOMETRICS);
if (android.hardware.biometrics.Flags.mandatoryBiometrics() if (android.hardware.biometrics.Flags.mandatoryBiometrics()
&& !biometricsAuthenticationRequested) { && !biometricsAuthenticationRequested) {
final UserManager userManager = context.getSystemService(
UserManager.class);
final int status = biometricManager.canAuthenticate(getEffectiveUserId(
userManager, userId), BiometricManager.Authenticators.MANDATORY_BIOMETRICS);
switch(status) { switch(status) {
case BiometricManager.BIOMETRIC_SUCCESS: case BiometricManager.BIOMETRIC_SUCCESS:
return BiometricStatus.OK; return BiometricStatus.OK;
@@ -1544,8 +1545,10 @@ public final class Utils extends com.android.settingslib.Utils {
*/ */
public static void launchBiometricPromptForMandatoryBiometrics(@NonNull Fragment fragment, public static void launchBiometricPromptForMandatoryBiometrics(@NonNull Fragment fragment,
int requestCode, int userId, boolean hideBackground) { int requestCode, int userId, boolean hideBackground) {
final UserManager userManager = (UserManager) fragment.getContext().getSystemService(
UserManager.class);
fragment.startActivityForResult(getIntentForBiometricAuthentication(fragment.getResources(), fragment.startActivityForResult(getIntentForBiometricAuthentication(fragment.getResources(),
userId, hideBackground), requestCode); getEffectiveUserId(userManager, userId), hideBackground), requestCode);
} }
/** /**
@@ -1561,21 +1564,32 @@ public final class Utils extends com.android.settingslib.Utils {
*/ */
public static void launchBiometricPromptForMandatoryBiometrics(@NonNull Activity activity, public static void launchBiometricPromptForMandatoryBiometrics(@NonNull Activity activity,
int requestCode, int userId, boolean hideBackground) { int requestCode, int userId, boolean hideBackground) {
final UserManager userManager = activity.getSystemService(UserManager.class);
activity.startActivityForResult(getIntentForBiometricAuthentication( activity.startActivityForResult(getIntentForBiometricAuthentication(
activity.getResources(), userId, hideBackground), requestCode); activity.getResources(), getEffectiveUserId(userManager, userId),
hideBackground), requestCode);
} }
private static Intent getIntentForBiometricAuthentication(Resources resources, int userId, private static int getEffectiveUserId(UserManager userManager, int userId) {
boolean hideBackground) { if (userManager != null) {
return userManager.getCredentialOwnerProfile(userId);
}
return userId;
}
private static Intent getIntentForBiometricAuthentication(Resources resources,
int effectiveUserId, boolean hideBackground) {
final Intent intent = new Intent(); final Intent intent = new Intent();
if (android.hardware.biometrics.Flags.mandatoryBiometrics()) {
intent.putExtra(BIOMETRIC_PROMPT_AUTHENTICATORS, intent.putExtra(BIOMETRIC_PROMPT_AUTHENTICATORS,
BiometricManager.Authenticators.MANDATORY_BIOMETRICS); BiometricManager.Authenticators.MANDATORY_BIOMETRICS);
}
intent.putExtra(BIOMETRIC_PROMPT_NEGATIVE_BUTTON_TEXT, intent.putExtra(BIOMETRIC_PROMPT_NEGATIVE_BUTTON_TEXT,
resources.getString(R.string.cancel)); resources.getString(R.string.cancel));
intent.putExtra(KeyguardManager.EXTRA_DESCRIPTION, intent.putExtra(KeyguardManager.EXTRA_DESCRIPTION,
resources.getString(R.string.mandatory_biometrics_prompt_description)); resources.getString(R.string.mandatory_biometrics_prompt_description));
intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_ALLOW_ANY_USER, true); intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_ALLOW_ANY_USER, true);
intent.putExtra(EXTRA_USER_ID, userId); intent.putExtra(EXTRA_USER_ID, effectiveUserId);
intent.putExtra(BIOMETRIC_PROMPT_HIDE_BACKGROUND, hideBackground); intent.putExtra(BIOMETRIC_PROMPT_HIDE_BACKGROUND, hideBackground);
intent.setClassName(SETTINGS_PACKAGE_NAME, intent.setClassName(SETTINGS_PACKAGE_NAME,
ConfirmDeviceCredentialActivity.InternalActivity.class.getName()); ConfirmDeviceCredentialActivity.InternalActivity.class.getName());

View File

@@ -564,6 +564,8 @@ public class UtilsTest {
@Test @Test
@EnableFlags(Flags.FLAG_MANDATORY_BIOMETRICS) @EnableFlags(Flags.FLAG_MANDATORY_BIOMETRICS)
public void testRequestBiometricAuthentication_biometricManagerReturnsSuccessForDifferentUser_shouldReturnError() { public void testRequestBiometricAuthentication_biometricManagerReturnsSuccessForDifferentUser_shouldReturnError() {
when(mContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
when(mMockUserManager.getCredentialOwnerProfile(USER_ID)).thenReturn(USER_ID);
when(mBiometricManager.canAuthenticate(anyInt(), when(mBiometricManager.canAuthenticate(anyInt(),
eq(BiometricManager.Authenticators.MANDATORY_BIOMETRICS))) eq(BiometricManager.Authenticators.MANDATORY_BIOMETRICS)))
.thenReturn(BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE); .thenReturn(BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE);
@@ -579,6 +581,8 @@ public class UtilsTest {
@EnableFlags(Flags.FLAG_MANDATORY_BIOMETRICS) @EnableFlags(Flags.FLAG_MANDATORY_BIOMETRICS)
public void testLaunchBiometricPrompt_checkIntentValues() { public void testLaunchBiometricPrompt_checkIntentValues() {
when(mFragment.getContext()).thenReturn(mContext); when(mFragment.getContext()).thenReturn(mContext);
when(mContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
when(mMockUserManager.getCredentialOwnerProfile(USER_ID)).thenReturn(USER_ID);
final int requestCode = 1; final int requestCode = 1;
final ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); final ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);