Fix biometric authentication prompt not shown when opening the account section in the navigation drawer. Require biometric authentication in AccountChooserBottomSheetFragment.

This commit is contained in:
Docile-Alligator 2022-05-14 12:08:33 +08:00
parent 345392a833
commit 22cf0a9c20
6 changed files with 75 additions and 20 deletions

View File

@ -213,6 +213,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Named("navigation_drawer") @Named("navigation_drawer")
SharedPreferences mNavigationDrawerSharedPreferences; SharedPreferences mNavigationDrawerSharedPreferences;
@Inject @Inject
@Named("security")
SharedPreferences mSecuritySharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@Inject @Inject
Executor mExecutor; Executor mExecutor;
@ -724,8 +727,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
fab.setVisibility(View.VISIBLE); fab.setVisibility(View.VISIBLE);
adapter = new NavigationDrawerRecyclerViewMergedAdapter(this, mSharedPreferences, adapter = new NavigationDrawerRecyclerViewMergedAdapter(this, mSharedPreferences,
mNsfwAndSpoilerSharedPreferences, mNavigationDrawerSharedPreferences, mCustomThemeWrapper, mAccountName, mNsfwAndSpoilerSharedPreferences, mNavigationDrawerSharedPreferences, mSecuritySharedPreferences,
new NavigationDrawerRecyclerViewMergedAdapter.ItemClickListener() { mCustomThemeWrapper, mAccountName, new NavigationDrawerRecyclerViewMergedAdapter.ItemClickListener() {
@Override @Override
public void onMenuClick(int stringId) { public void onMenuClick(int stringId) {
Intent intent = null; Intent intent = null;

View File

@ -49,6 +49,7 @@ public class HeaderSectionRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
public HeaderSectionRecyclerViewAdapter(BaseActivity baseActivity, RequestManager glide, String accountName, public HeaderSectionRecyclerViewAdapter(BaseActivity baseActivity, RequestManager glide, String accountName,
SharedPreferences sharedPreferences, SharedPreferences sharedPreferences,
SharedPreferences navigationDrawerSharedPreferences, SharedPreferences navigationDrawerSharedPreferences,
SharedPreferences securitySharedPreferences,
PageToggle pageToggle) { PageToggle pageToggle) {
this.baseActivity = baseActivity; this.baseActivity = baseActivity;
resources = baseActivity.getResources(); resources = baseActivity.getResources();
@ -56,7 +57,7 @@ public class HeaderSectionRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
this.accountName = accountName; this.accountName = accountName;
isLoggedIn = accountName != null; isLoggedIn = accountName != null;
this.pageToggle = pageToggle; this.pageToggle = pageToggle;
requireAuthToAccountSection = sharedPreferences.getBoolean(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER, false); requireAuthToAccountSection = securitySharedPreferences.getBoolean(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER, false);
showAvatarOnTheRightInTheNavigationDrawer = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_AVATAR_ON_THE_RIGHT, false); showAvatarOnTheRightInTheNavigationDrawer = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_AVATAR_ON_THE_RIGHT, false);
showAvatarOnTheRightInTheNavigationDrawer = navigationDrawerSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_AVATAR_ON_THE_RIGHT, false); showAvatarOnTheRightInTheNavigationDrawer = navigationDrawerSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_AVATAR_ON_THE_RIGHT, false);
} }

View File

@ -35,23 +35,25 @@ public class NavigationDrawerRecyclerViewMergedAdapter {
public NavigationDrawerRecyclerViewMergedAdapter(BaseActivity baseActivity, SharedPreferences sharedPreferences, public NavigationDrawerRecyclerViewMergedAdapter(BaseActivity baseActivity, SharedPreferences sharedPreferences,
SharedPreferences nsfwAndSpoilerSharedPreferences, SharedPreferences nsfwAndSpoilerSharedPreferences,
SharedPreferences navigationDrawerSharedPreferences, SharedPreferences navigationDrawerSharedPreferences,
SharedPreferences securitySharedPreferences,
CustomThemeWrapper customThemeWrapper, CustomThemeWrapper customThemeWrapper,
String accountName, String accountName,
ItemClickListener itemClickListener) { ItemClickListener itemClickListener) {
RequestManager glide = Glide.with(baseActivity); RequestManager glide = Glide.with(baseActivity);
headerSectionRecyclerViewAdapter = new HeaderSectionRecyclerViewAdapter(baseActivity, glide, accountName, headerSectionRecyclerViewAdapter = new HeaderSectionRecyclerViewAdapter(baseActivity, glide, accountName,
sharedPreferences, navigationDrawerSharedPreferences, new HeaderSectionRecyclerViewAdapter.PageToggle() { sharedPreferences, navigationDrawerSharedPreferences, securitySharedPreferences,
@Override new HeaderSectionRecyclerViewAdapter.PageToggle() {
public void openAccountSection() { @Override
NavigationDrawerRecyclerViewMergedAdapter.this.openAccountSection(); public void openAccountSection() {
} NavigationDrawerRecyclerViewMergedAdapter.this.openAccountSection();
}
@Override @Override
public void closeAccountSectionWithoutChangeIconResource() { public void closeAccountSectionWithoutChangeIconResource() {
NavigationDrawerRecyclerViewMergedAdapter.this.closeAccountSectionWithoutChangeIconResource(); NavigationDrawerRecyclerViewMergedAdapter.this.closeAccountSectionWithoutChangeIconResource();
} }
}); });
accountSectionRecyclerViewAdapter = new AccountSectionRecyclerViewAdapter(baseActivity, customThemeWrapper, accountSectionRecyclerViewAdapter = new AccountSectionRecyclerViewAdapter(baseActivity, customThemeWrapper,
navigationDrawerSharedPreferences, accountName != null, itemClickListener); navigationDrawerSharedPreferences, accountName != null, itemClickListener);
redditSectionRecyclerViewAdapter = new RedditSectionRecyclerViewAdapter(baseActivity, customThemeWrapper, redditSectionRecyclerViewAdapter = new RedditSectionRecyclerViewAdapter(baseActivity, customThemeWrapper,

View File

@ -1,18 +1,28 @@
package ml.docilealligator.infinityforreddit.bottomsheetfragments; package ml.docilealligator.infinityforreddit.bottomsheetfragments;
import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import java.util.concurrent.Executor;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
@ -23,6 +33,7 @@ import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.adapters.AccountChooserRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.AccountChooserRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedBottomSheetDialogFragment; import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedBottomSheetDialogFragment;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class AccountChooserBottomSheetFragment extends LandscapeExpandedBottomSheetDialogFragment { public class AccountChooserBottomSheetFragment extends LandscapeExpandedBottomSheetDialogFragment {
@ -30,6 +41,9 @@ public class AccountChooserBottomSheetFragment extends LandscapeExpandedBottomSh
RedditDataRoomDatabase redditDataRoomDatabase; RedditDataRoomDatabase redditDataRoomDatabase;
@Inject @Inject
CustomThemeWrapper customThemeWrapper; CustomThemeWrapper customThemeWrapper;
@Inject
@Named("security")
SharedPreferences sharedPreferences;
BaseActivity activity; BaseActivity activity;
RecyclerView recyclerView; RecyclerView recyclerView;
AccountChooserRecyclerViewAdapter adapter; AccountChooserRecyclerViewAdapter adapter;
@ -57,11 +71,46 @@ public class AccountChooserBottomSheetFragment extends LandscapeExpandedBottomSh
}); });
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
accountViewModel = new ViewModelProvider(this, if (sharedPreferences.getBoolean(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER, false)) {
new AccountViewModel.Factory(redditDataRoomDatabase)).get(AccountViewModel.class); BiometricManager biometricManager = BiometricManager.from(activity);
accountViewModel.getAllAccountsLiveData().observe(getViewLifecycleOwner(), accounts -> { if (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) {
adapter.changeAccountsDataset(accounts); 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; return rootView;
} }

View File

@ -532,7 +532,7 @@
<string name="settings_swipe_action_threshold">Threshold</string> <string name="settings_swipe_action_threshold">Threshold</string>
<string name="settings_pull_to_refresh_title">Pull to Refresh</string> <string name="settings_pull_to_refresh_title">Pull to Refresh</string>
<string name="settings_security_title">Security</string> <string name="settings_security_title">Security</string>
<string name="settings_require_authentication_to_go_to_account_section_in_navigation_drawer_title">Require Authentication to Go to Account Section in Navigation Drawer</string> <string name="settings_require_authentication_to_show_accounts">Require Authentication to Show Accounts</string>
<string name="settings_long_press_to_hide_toolbar_in_compact_layout_title">Long Press to Hide Toolbar</string> <string name="settings_long_press_to_hide_toolbar_in_compact_layout_title">Long Press to Hide Toolbar</string>
<string name="settings_post_compact_layout_toolbar_hidden_by_default_title">Hide Toolbar by Default</string> <string name="settings_post_compact_layout_toolbar_hidden_by_default_title">Hide Toolbar by Default</string>
<string name="settings_customize_bottom_app_bar_title">Customize Bottom Navigation Bar</string> <string name="settings_customize_bottom_app_bar_title">Customize Bottom Navigation Bar</string>

View File

@ -3,7 +3,7 @@
<ml.docilealligator.infinityforreddit.customviews.CustomFontSwitchPreference <ml.docilealligator.infinityforreddit.customviews.CustomFontSwitchPreference
app:defaultValue="false" app:defaultValue="false"
app:key="require_auth_to_account_section" app:key="require_auth_to_account_section"
app:title="@string/settings_require_authentication_to_go_to_account_section_in_navigation_drawer_title" /> app:title="@string/settings_require_authentication_to_show_accounts" />
<ml.docilealligator.infinityforreddit.customviews.CustomFontSwitchPreference <ml.docilealligator.infinityforreddit.customviews.CustomFontSwitchPreference
app:defaultValue="false" app:defaultValue="false"