diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-09-18 16:07:41 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-09-18 16:07:41 +0000 |
commit | bf906b8a6cc81423edf681efe2218e1689816855 (patch) | |
tree | ea989a4b35ce71e325be8fbb34deea1e1fdf7f58 /app/src | |
parent | 8a999a04c372311bed0f3d890a42e387969e2954 (diff) | |
download | infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.tar infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.tar.gz infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.tar.bz2 infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.tar.lz infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.tar.xz infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.tar.zst infinity-for-reddit-bf906b8a6cc81423edf681efe2218e1689816855.zip |
Hide Security option if no biometric authentication available. Require biometric authentication when going into security settings.
Diffstat (limited to 'app/src')
8 files changed, 120 insertions, 29 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java index be458bab..3d4b1788 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 328b3714..8a8905a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -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); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java index 3a11855a..ce601cf7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java @@ -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; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SecurityPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SecurityPreferenceFragment.java index 882fe17d..4a5df68b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SecurityPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SecurityPreferenceFragment.java @@ -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; + } }
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index 87b70f3e..351088c3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -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"; diff --git a/app/src/main/res/layout/dialog_select_header.xml b/app/src/main/res/layout/dialog_select_header.xml index 14c68249..31d49a9f 100644 --- a/app/src/main/res/layout/dialog_select_header.xml +++ b/app/src/main/res/layout/dialog_select_header.xml @@ -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" + app:layout_constraintTop_toBottomOf="@+id/seek_bar_dialog_select_header" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintBottom_toBottomOf="parent" /> + + <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" /> + 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" /> -</LinearLayout>
\ No newline at end of file +</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 337f759d..6020561e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -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> diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 48bf5697..f2990908 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -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" /> |