Hide Security option if no biometric authentication available. Require biometric authentication when going into security settings.

This commit is contained in:
Alex Ning 2020-09-19 00:07:41 +08:00
parent 8a999a04c3
commit bf906b8a6c
8 changed files with 120 additions and 29 deletions

View File

@ -6,12 +6,12 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.slider.Slider;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.R;
@ -158,7 +158,7 @@ public class MarkdownBottomBarRecyclerViewAdapter extends RecyclerView.Adapter<R
}
case MarkdownBottomBarRecyclerViewAdapter.HEADER: {
View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_select_header, null);
SeekBar seekBar = dialogView.findViewById(R.id.seek_bar_dialog_select_header);
Slider seekBar = dialogView.findViewById(R.id.seek_bar_dialog_select_header);
new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.select_header_size)
.setView(dialogView)
@ -167,7 +167,7 @@ public class MarkdownBottomBarRecyclerViewAdapter extends RecyclerView.Adapter<R
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
String hashTags;
switch (seekBar.getProgress()) {
switch ((int) seekBar.getValue()) {
case 0:
hashTags = "###### ";
break;

View File

@ -66,6 +66,7 @@ import ml.docilealligator.infinityforreddit.Settings.DownloadLocationPreferenceF
import ml.docilealligator.infinityforreddit.Settings.GesturesAndButtonsPreferenceFragment;
import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment;
import ml.docilealligator.infinityforreddit.Settings.NotificationPreferenceFragment;
import ml.docilealligator.infinityforreddit.Settings.SecurityPreferenceFragment;
import ml.docilealligator.infinityforreddit.Settings.ThemePreferenceFragment;
import ml.docilealligator.infinityforreddit.Settings.VideoPreferenceFragment;
@ -203,4 +204,6 @@ public interface AppComponent {
void inject(CommentFullMarkdownActivity commentFullMarkdownActivity);
void inject(SelectUserFlairActivity selectUserFlairActivity);
void inject(SecurityPreferenceFragment securityPreferenceFragment);
}

View File

@ -1,12 +1,14 @@
package ml.docilealligator.infinityforreddit.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
@ -24,6 +26,9 @@ import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL;
public class MainPreferenceFragment extends PreferenceFragmentCompat {
@Inject
@ -32,13 +37,14 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat {
@Inject
@Named("post_feed_scrolled_position_cache")
SharedPreferences cache;
private Activity activity;
private AppCompatActivity activity;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.main_preferences, rootKey);
((Infinity) activity.getApplication()).getAppComponent().inject(this);
Preference securityPreference = findPreference(SharedPreferencesUtils.SECURITY);
SwitchPreference savePostFeedScrolledPositionSwitch = findPreference(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION);
SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT);
SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY);
@ -93,11 +99,18 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat {
return true;
});
}
BiometricManager biometricManager = BiometricManager.from(activity);
if (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) != BiometricManager.BIOMETRIC_SUCCESS) {
if (securityPreference != null) {
securityPreference.setVisible(false);
}
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
activity = (Activity) context;
activity = (AppCompatActivity) context;
}
}

View File

@ -1,22 +1,44 @@
package ml.docilealligator.infinityforreddit.Settings;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
import org.greenrobot.eventbus.EventBus;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Event.ChangeRequireAuthToAccountSectionEvent;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL;
public class SecurityPreferenceFragment extends PreferenceFragmentCompat {
private AppCompatActivity activity;
@Inject
@Named("default")
SharedPreferences sharedPreferences;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.security_preferences, rootKey);
((Infinity) activity.getApplication()).getAppComponent().inject(this);
SwitchPreference requireAuthToAccountSectionSwitch = findPreference(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER);
if (requireAuthToAccountSectionSwitch != null) {
@ -26,4 +48,44 @@ public class SecurityPreferenceFragment extends PreferenceFragmentCompat {
});
}
}
@Override
public void onResume() {
super.onResume();
Executor executor = ContextCompat.getMainExecutor(activity);
BiometricPrompt biometricPrompt = new BiometricPrompt(SecurityPreferenceFragment.this,
executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode,
@NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
activity.onBackPressed();
}
@Override
public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
activity.onBackPressed();
}
});
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(R.string.unlock))
.setAllowedAuthenticators(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)
.build();
biometricPrompt.authenticate(promptInfo);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
this.activity = (AppCompatActivity) context;
}
}

View File

@ -126,6 +126,7 @@ public class SharedPreferencesUtils {
public static final String REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER = "require_auth_to_account_section";
public static final String LONG_PRESS_TO_HIDE_TOOLBAR_IN_COMPACT_LAYOUT = "long_press_to_hide_toolbar_in_compact_layout";
public static final String POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT = "post_compact_layout_toolbar_hidden_by_default";
public static final String SECURITY = "security";
public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs";
public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title";

View File

@ -1,37 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/header6"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
<SeekBar
<com.google.android.material.slider.Slider
android:id="@+id/seek_bar_dialog_select_header"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:padding="16dp"
android:max="5"
android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />
android:valueFrom="1"
android:valueTo="6"
android:stepSize="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/header1"
android:layout_gravity="start"
android:text="@string/large"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
android:fontFamily="?attr/font_family"
app:layout_constraintTop_toBottomOf="@+id/seek_bar_dialog_select_header"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/small"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
app:layout_constraintTop_toBottomOf="@+id/seek_bar_dialog_select_header"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -859,17 +859,18 @@
<string name="view_full_comment_markdown">View Full Markdown</string>
<string name="unlock_account_section">Unlock Account Section</string>
<string name="select_user_flair_success">User flair selected</string>
<string name="select_user_flair_failed">Cannot select user flair</string>
<string name="select_this_user_flair">Select this user flair?</string>
<string name="select_header_size">Select Header Size</string>
<string name="header1">H1</string>
<string name="header6">H6</string>
<string name="large">Large</string>
<string name="small">Small</string>
<string name="insert_link">Insert Link</string>
<string name="text_hint">Text</string>
<string name="link_hint">Link</string>
<string name="unlock_account_section">Unlock Account Section</string>
<string name="unlock">Unlock</string>
</resources>

View File

@ -42,6 +42,7 @@
app:fragment="ml.docilealligator.infinityforreddit.Settings.DownloadLocationPreferenceFragment" />
<Preference
app:key="security"
app:title="@string/settings_security_title"
android:icon="@drawable/ic_security_24dp"
app:fragment="ml.docilealligator.infinityforreddit.Settings.SecurityPreferenceFragment" />