1

In Java, I am trying to implement a feature where only the admin (person who knows the device password) can access an information screen and when they click on a button within the app in the MainActivity, the lock screen will appear as a form of authentication and display another activity screen on success. Is this possible?

So far, I noticed that the authentication only displays when I open the app and not when I press the button in an onClickListener. Most of the solutions I've seen are doing it this way. I have the code in MainActivity within an onCreate() method.

MainActivity

ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                  // There are no request codes
                  Intent data = result.getData();
                } else {
                  finish();
                }
            }
        });

start_end_button_add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

      authScreen(activityResultLaunch);

    }
});

private void authScreen(ActivityResultLauncher<Intent> activityResultLaunch) {
    KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    if (!mKeyguardManager.isKeyguardSecure()) {
      // Show a message that the user hasn't set up a lock screen.
    } else {
      Intent intent = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);

      if (intent != null) {
        startActivityForResult.launch(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
      }
    }

    Intent intent = new Intent(DetectorActivity.this, SearchActivity.class);

    startActivity(intent);
    finish();

}

Currently, the app immediately goes to the SearchActivity class without having the lock screen displayed in between. Even if I get into the app after PIN code entered is a success, it still doesn't get into the activityResultLaunch success condition as per the new implementation referenced here by user Martin Zeitler.

Reference: https://mobile-security.gitbook.io/mobile-security-testing-guide/android-testing-guide/0x05f-testing-local-authentication#:~:text=In%20Android%2C%20there%20are%20two,and%20the%20Biometric%20Authentication%20flow.

HoRn
  • 1,458
  • 5
  • 20
  • 25
  • I didn't find a solution but I managed to find a way to do as I originally intended on an Android 11 device using a more recent approach with the Biometric Prompt instead. My original question was done on an Android 10 device. – Hilmi Marzuqi Sep 12 '22 at 01:40

0 Answers0