androidauthenticationandroid-biometric-prompt

Open Android credentials Setup programmatically


To display sensible data users can enable authentication in my app. I am using the android in-build authentication.

However, if the user did not secure his device using any pattern, pin, password or biometric authentication, I would like to open the android settings, where he can setup his authentication. Is there any Intent/ way to go there? I did not find it.

Some code so far: To determine, if the user did not setup any authentication method:

androidx.biometric.BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) activity, executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);

            // Determine, if the user has no device password set.
            boolean errorCodeIsBeingHandledSeparately = false;
    // HERE WE DETERMINE THAT CREDENTIALS HAVE NOT BEEN SETUP
            if (errorCode == BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL) {
                if (authenticationInterface != null) {
                    errorCodeIsBeingHandledSeparately = true;
                    authenticationInterface.onUserHasNoDevicePassWordSet();
                }
            }

            // Display error message, only if the error code is not being handled seperately.
            if (!errorCodeIsBeingHandledSeparately) {
                Toast.makeText(activity, "Authentication error\n" + errString, Toast.LENGTH_LONG).show();
            }


        }

        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);

            if (authenticationInterface == null) {
                Toast.makeText(activity, "Success", Toast.LENGTH_LONG).show();
            }
            else {
                authenticationInterface.onUserSuccessfullyAuthenticated();
            }
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Toast.makeText(activity, "Authentication failed", Toast.LENGTH_LONG).show();
        }
    });

The interface to receive authentication return.

/**
 * Interface to receive authentication return.
 */
private AuthenticationUtils.AuthenticationInterface authenticationInterface;
public interface AuthenticationInterface {
    public void onUserSuccessfullyAuthenticated();
    public void onUserHasNoDevicePassWordSet();
}

The dialog where I want to lead the user to go to the device setup credentials.

public void displayNoDeviceCredentialsSetDialog() {
    MaterialAlertDialogBuilder noDeviceCredentialsDialog = new MaterialAlertDialogBuilder(activity, R.style.AlertDialogTheme);
    String noDeviceCredentials_goToSettings_dialogMessage = activity.getString(R.string.authentication_noDeviceCredentials_goToSettings_dialogMessage);
    noDeviceCredentialsDialog.setMessage(noDeviceCredentials_goToSettings_dialogMessage);
    noDeviceCredentialsDialog.setPositiveButton(
            R.string.DialogConfirmationOK,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
         // HERE I WOULD LIKE TO OPEN THE ANDROID SETTING WHERE HE CAN SETUP HIS CREDENTIALS
                }
            }
    );
    noDeviceCredentialsDialog.setNegativeButton(
            R.string.DialogConfirmationNegativeAnswerText,
            null
    );
    noDeviceCredentialsDialog.show();
}

I would like to go here:

device security

You can get there from settings here:

security

What i am looking for is something like this: Here we navigate the user to some other android settings.

Intent intent2 = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  intent2.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
  intent2.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, getPackageName());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
  intent2.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
  intent2.putExtra("app_package", getPackageName());
  intent2.putExtra("app_uid", getApplicationInfo().uid);
} else {
  intent2.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
  intent2.addCategory(Intent.CATEGORY_DEFAULT);
  intent2.setData(Uri.parse("package:" + getPackageName()));
}
startActivity(intent2);

Solution

  • Just figured it out.

    There are 3 viable options to use: Settings.ACTION_BIOMETRIC_ENROLL, Settings.ACTION_FINGERPRINT_ENROLL and Settings.ACTION_SECURITY_SETTINGS.

    Final implementation I use is:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        activity.startActivity(new Intent(Settings.ACTION_BIOMETRIC_ENROLL));
    }
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            activity.startActivity(new Intent(Settings.ACTION_FINGERPRINT_ENROLL));
        }
        else {
            activity.startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
        }
    }
    

    Settings.ACTION_FINGERPRINT_ENROLL opens this: After chosing backup lock screen method and setup the chosen method, the device will ask you to register a fingerprint. ACTION_FINGERPRINT_ENROLL

    Settings.ACTION_SECURITY_SETTINGS opens this: ACTION_SECURITY_SETTINGS

    In lack of a device higher than Android Build "R" I could not test ACTION_BIOMETRIC_ENROLL, but I presume it will be similar to ACTION_FINGERPRINT_ENROLL.

    If you want to see what options there are to open android settings. You can just use "CTRL" + "mouse click" on any Settings.XXX (ACTION_SECURITY_SETTINGS, ACTION_FINGERPRINT_ENROLL, ...) in Android Studio. You will then see "..\android\platforms\android-31\android.jar!\android\provider\Settings.class" Settings options

    In case you struggle to figure out which API version is described with "Build.VERSION_CODES.P" you can also click "CTRL" + "Mose Click" on the Build version (P, O, ...). You will then see this: Build versions