From 22cf0a9c20b41176e9e2216d79934028c570b0c3 Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Sat, 14 May 2022 12:08:33 +0800 Subject: Fix biometric authentication prompt not shown when opening the account section in the navigation drawer. Require biometric authentication in AccountChooserBottomSheetFragment. --- .../infinityforreddit/activities/MainActivity.java | 7 ++- .../HeaderSectionRecyclerViewAdapter.java | 3 +- .../NavigationDrawerRecyclerViewMergedAdapter.java | 24 +++++---- .../AccountChooserBottomSheetFragment.java | 59 ++++++++++++++++++++-- 4 files changed, 74 insertions(+), 19 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 561acf46..02bff5d4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -213,6 +213,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Named("navigation_drawer") SharedPreferences mNavigationDrawerSharedPreferences; @Inject + @Named("security") + SharedPreferences mSecuritySharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; @@ -724,8 +727,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb fab.setVisibility(View.VISIBLE); adapter = new NavigationDrawerRecyclerViewMergedAdapter(this, mSharedPreferences, - mNsfwAndSpoilerSharedPreferences, mNavigationDrawerSharedPreferences, mCustomThemeWrapper, mAccountName, - new NavigationDrawerRecyclerViewMergedAdapter.ItemClickListener() { + mNsfwAndSpoilerSharedPreferences, mNavigationDrawerSharedPreferences, mSecuritySharedPreferences, + mCustomThemeWrapper, mAccountName, new NavigationDrawerRecyclerViewMergedAdapter.ItemClickListener() { @Override public void onMenuClick(int stringId) { Intent intent = null; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/HeaderSectionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/HeaderSectionRecyclerViewAdapter.java index 6533f610..efc4ecc0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/HeaderSectionRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/HeaderSectionRecyclerViewAdapter.java @@ -49,6 +49,7 @@ public class HeaderSectionRecyclerViewAdapter extends RecyclerView.Adapter { - adapter.changeAccountsDataset(accounts); - }); + if (sharedPreferences.getBoolean(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER, false)) { + BiometricManager biometricManager = BiometricManager.from(activity); + if (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) { + Executor executor = ContextCompat.getMainExecutor(activity); + BiometricPrompt biometricPrompt = new BiometricPrompt(this, + executor, new BiometricPrompt.AuthenticationCallback() { + @Override + public void onAuthenticationSucceeded( + @NonNull BiometricPrompt.AuthenticationResult result) { + super.onAuthenticationSucceeded(result); + accountViewModel = new ViewModelProvider(AccountChooserBottomSheetFragment.this, + new AccountViewModel.Factory(redditDataRoomDatabase)).get(AccountViewModel.class); + accountViewModel.getAllAccountsLiveData().observe(getViewLifecycleOwner(), accounts -> { + adapter.changeAccountsDataset(accounts); + }); + } + + @Override + public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { + super.onAuthenticationError(errorCode, errString); + dismiss(); + } + }); + + BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() + .setTitle(getString(R.string.unlock)) + .setAllowedAuthenticators(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) + .build(); + + biometricPrompt.authenticate(promptInfo); + } else { + dismiss(); + } + } else { + accountViewModel = new ViewModelProvider(this, + new AccountViewModel.Factory(redditDataRoomDatabase)).get(AccountViewModel.class); + accountViewModel.getAllAccountsLiveData().observe(getViewLifecycleOwner(), accounts -> { + adapter.changeAccountsDataset(accounts); + }); + } return rootView; } -- cgit v1.2.3