From 0153f72aaaefd70a3f87a65e5c6e7e4f631b948b Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Thu, 29 Jun 2023 22:30:49 +0200 Subject: [PATCH] Fix biometric lock on devices without biometric data --- .../cringe_authenticator/MainActivity.java | 19 +++++++++---------- ...ynamicFragment.java => GroupFragment.java} | 10 +++++----- .../fragment/MenuFragment.java | 4 ++-- .../fragment/SettingsFragment.java | 14 ++++++++++++-- ...ragment_dynamic.xml => fragment_group.xml} | 2 +- app/src/main/res/navigation/nav_graph.xml | 4 ++-- 6 files changed, 31 insertions(+), 22 deletions(-) rename app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/{DynamicFragment.java => GroupFragment.java} (91%) rename app/src/main/res/layout/{fragment_dynamic.xml => fragment_group.xml} (95%) diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java index 47ffdc3..c6fe614 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java @@ -4,7 +4,6 @@ import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRON import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL; import android.os.Bundle; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -19,6 +18,7 @@ import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.biometric.BiometricManager; import androidx.biometric.BiometricPrompt; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -27,7 +27,7 @@ import com.cringe_studios.cringe_authenticator.databinding.ActivityMainBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeChoiceBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeHotpBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeTotpBinding; -import com.cringe_studios.cringe_authenticator.fragment.DynamicFragment; +import com.cringe_studios.cringe_authenticator.fragment.GroupFragment; import com.cringe_studios.cringe_authenticator.fragment.HomeFragment; import com.cringe_studios.cringe_authenticator.fragment.MenuFragment; import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment; @@ -72,7 +72,7 @@ public class MainActivity extends AppCompatActivity { } }); - if(SettingsUtil.isBiometricLock(this)) { + if(SettingsUtil.isBiometricLock(this) && BiometricManager.from(this).canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) { BiometricPrompt.PromptInfo info = new BiometricPrompt.PromptInfo.Builder() .setTitle("Cringe Authenticator") .setSubtitle("Unlock the authenticator") @@ -93,11 +93,10 @@ public class MainActivity extends AppCompatActivity { } Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(fragment instanceof DynamicFragment) { - DynamicFragment frag = (DynamicFragment) fragment; + if(fragment instanceof GroupFragment) { + GroupFragment frag = (GroupFragment) fragment; frag.addOTP(obj.getData()); } - Log.i("AMOGUS", "Actually got something bruh" + obj); }); } @@ -198,7 +197,7 @@ public class MainActivity extends AppCompatActivity { binding.inputDigits.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new Integer[]{6, 7, 8, 9, 10, 11, 12})); showCodeDialog(binding.getRoot(), () -> { Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(!(fragment instanceof DynamicFragment)) return true; + if(!(fragment instanceof GroupFragment)) return true; try { String name = binding.inputName.getText().toString(); @@ -216,7 +215,7 @@ public class MainActivity extends AppCompatActivity { return false; } - ((DynamicFragment) fragment).addOTP(data); + ((GroupFragment) fragment).addOTP(data); return true; }catch(NumberFormatException e) { showErrorDialog("Invalid number entered"); @@ -231,7 +230,7 @@ public class MainActivity extends AppCompatActivity { binding.inputDigits.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new Integer[]{6, 7, 8, 9, 10, 11, 12})); showCodeDialog(binding.getRoot(), () -> { Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(!(fragment instanceof DynamicFragment)) return true; + if(!(fragment instanceof GroupFragment)) return true; try { String name = binding.inputName.getText().toString(); @@ -249,7 +248,7 @@ public class MainActivity extends AppCompatActivity { return false; } - ((DynamicFragment) fragment).addOTP(data); + ((GroupFragment) fragment).addOTP(data); return true; }catch(NumberFormatException e) { showErrorDialog("Invalid number entered"); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/GroupFragment.java similarity index 91% rename from app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java rename to app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/GroupFragment.java index af20563..8a7944f 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/DynamicFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/GroupFragment.java @@ -11,7 +11,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.cringe_studios.cringe_authenticator.OTPData; -import com.cringe_studios.cringe_authenticator.databinding.FragmentDynamicBinding; +import com.cringe_studios.cringe_authenticator.databinding.FragmentGroupBinding; import com.cringe_studios.cringe_authenticator.otplist.OTPListAdapter; import com.cringe_studios.cringe_authenticator.otplist.OTPListItem; import com.cringe_studios.cringe_authenticator.util.FabUtil; @@ -20,13 +20,13 @@ import com.cringe_studios.cringe_authenticator_library.OTPType; import java.util.List; -public class DynamicFragment extends NamedFragment { +public class GroupFragment extends NamedFragment { public static final String BUNDLE_GROUP = "group"; private String groupName; - private FragmentDynamicBinding binding; + private FragmentGroupBinding binding; private Handler handler; @@ -47,9 +47,9 @@ public class DynamicFragment extends NamedFragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - binding = FragmentDynamicBinding.inflate(inflater, container, false); + binding = FragmentGroupBinding.inflate(inflater, container, false); - groupName = requireArguments().getString(DynamicFragment.BUNDLE_GROUP); + groupName = requireArguments().getString(GroupFragment.BUNDLE_GROUP); FabUtil.showFabs(requireActivity()); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java index 938bec8..b022116 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java @@ -35,8 +35,8 @@ public class MenuFragment extends NamedFragment { groupListAdapter = new GroupListAdapter(requireContext(), group -> { Bundle bundle = new Bundle(); - bundle.putString(DynamicFragment.BUNDLE_GROUP, group); - NavigationUtil.navigate(this, DynamicFragment.class, bundle); + bundle.putString(GroupFragment.BUNDLE_GROUP, group); + NavigationUtil.navigate(this, GroupFragment.class, bundle); }, this::removeGroup); binding.menuItems.setAdapter(groupListAdapter); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java index 6821c61..09e1187 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java @@ -1,5 +1,8 @@ package com.cringe_studios.cringe_authenticator.fragment; +import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG; +import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL; + import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -9,6 +12,7 @@ import android.widget.ArrayAdapter; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.biometric.BiometricManager; import com.cringe_studios.cringe_authenticator.databinding.FragmentSettingsBinding; import com.cringe_studios.cringe_authenticator.util.FabUtil; @@ -31,8 +35,14 @@ public class SettingsFragment extends NamedFragment { binding.settingsEnableIntroVideo.setChecked(SettingsUtil.isIntroVideoEnabled(requireContext())); binding.settingsEnableIntroVideo.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setEnableIntroVideo(requireContext(), checked)); - binding.settingsBiometricLock.setChecked(SettingsUtil.isBiometricLock(requireContext())); - binding.settingsBiometricLock.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setBiometricLock(requireContext(), checked)); + if(BiometricManager.from(requireContext()).canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) { + binding.settingsBiometricLock.setChecked(SettingsUtil.isBiometricLock(requireContext())); + binding.settingsBiometricLock.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setBiometricLock(requireContext(), checked)); + }else { + binding.settingsBiometricLock.setChecked(false); + binding.settingsBiometricLock.setEnabled(false); + // TODO: inform user + } binding.settingsTheme.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, SettingsUtil.THEME_NAMES.toArray(new String[0]))); binding.settingsTheme.setSelection(SettingsUtil.THEME_NAMES.indexOf(SettingsUtil.getTheme(requireContext()))); diff --git a/app/src/main/res/layout/fragment_dynamic.xml b/app/src/main/res/layout/fragment_group.xml similarity index 95% rename from app/src/main/res/layout/fragment_dynamic.xml rename to app/src/main/res/layout/fragment_group.xml index a04d39c..00d94a9 100644 --- a/app/src/main/res/layout/fragment_dynamic.xml +++ b/app/src/main/res/layout/fragment_group.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".fragment.DynamicFragment"> + tools:context=".fragment.GroupFragment"> + tools:layout="@layout/fragment_group"> \ No newline at end of file