From 87d1c21f50b45b65875d1c50a3231003e4c30e9c Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Wed, 4 Oct 2023 15:31:44 +0200 Subject: [PATCH] Use EditOTPFragment for view and add code --- .../code_guard/MainActivity.java | 53 +----- .../code_guard/fragment/EditOTPFragment.java | 2 + .../code_guard/fragment/GroupFragment.java | 3 +- .../code_guard/util/DialogUtil.java | 153 ------------------ .../code_guard/util/NavigationUtil.java | 8 +- .../res/layout/dialog_input_code_choice.xml | 17 -- .../res/layout/dialog_input_code_hotp.xml | 64 -------- .../res/layout/dialog_input_code_totp.xml | 64 -------- .../main/res/layout/dialog_set_password.xml | 21 +-- 9 files changed, 21 insertions(+), 364 deletions(-) delete mode 100644 app/src/main/res/layout/dialog_input_code_choice.xml delete mode 100644 app/src/main/res/layout/dialog_input_code_hotp.xml delete mode 100644 app/src/main/res/layout/dialog_input_code_totp.xml diff --git a/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java b/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java index 48f7570..15d0387 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java @@ -4,8 +4,6 @@ import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Toast; @@ -20,7 +18,6 @@ import androidx.fragment.app.Fragment; import com.cringe_studios.code_guard.databinding.ActivityMainBinding; import com.cringe_studios.code_guard.databinding.DialogIconPackExistsBinding; -import com.cringe_studios.code_guard.databinding.DialogInputCodeChoiceBinding; import com.cringe_studios.code_guard.fragment.AboutFragment; import com.cringe_studios.code_guard.fragment.EditOTPFragment; import com.cringe_studios.code_guard.fragment.GroupFragment; @@ -39,7 +36,6 @@ import com.cringe_studios.code_guard.util.OTPDatabase; import com.cringe_studios.code_guard.util.SettingsUtil; import com.cringe_studios.code_guard.util.StyledDialogBuilder; import com.cringe_studios.code_guard.util.ThemeUtil; -import com.cringe_studios.cringe_authenticator_library.OTPType; import com.google.mlkit.vision.common.InputImage; import java.io.IOException; @@ -357,51 +353,14 @@ public class MainActivity extends BaseActivity { } public void inputCode(MenuItem item) { - DialogInputCodeChoiceBinding binding = DialogInputCodeChoiceBinding.inflate(getLayoutInflater()); + Fragment fragment = NavigationUtil.getCurrentFragment(this); + if(!(fragment instanceof GroupFragment)) return; - String[] options = new String[2]; - options[0] = OTPType.TOTP.getFriendlyName() + " (TOTP)"; - options[1] = OTPType.HOTP.getFriendlyName() + " (HOTP)"; + GroupFragment f = (GroupFragment) fragment; - AlertDialog dialog = new StyledDialogBuilder(this) - .setTitle(R.string.create_totp_title) - .setView(binding.getRoot()) - .setNegativeButton(R.string.cancel, (view, which) -> {}) - .create(); - - binding.codeTypes.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, options)); - binding.codeTypes.setOnItemClickListener((AdapterView parent, View view, int position, long id) -> { - switch(position) { - case 0: - showTOTPDialog(); - break; - case 1: - showHOTPDialog(); - break; - } - - dialog.dismiss(); - }); - - dialog.show(); - } - - private void showTOTPDialog() { - DialogUtil.showTOTPDialog(getLayoutInflater(), null, data -> { - Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(!(fragment instanceof GroupFragment)) return; - - ((GroupFragment) fragment).addOTP(data); - }, false); - } - - private void showHOTPDialog() { - DialogUtil.showHOTPDialog(getLayoutInflater(), null, data -> { - Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(!(fragment instanceof GroupFragment)) return; - - ((GroupFragment) fragment).addOTP(data); - }, false); + NavigationUtil.openOverlay(this, new EditOTPFragment(null, false, data -> { + f.addOTP(data); + })); } public void addOTP(MenuItem item) { diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/EditOTPFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/EditOTPFragment.java index adfc1ae..140041f 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/fragment/EditOTPFragment.java +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/EditOTPFragment.java @@ -77,6 +77,8 @@ public class EditOTPFragment extends NamedFragment { binding.inputImage.setVisibility(SettingsUtil.isShowImages(requireContext()) ? View.VISIBLE : View.GONE); binding.inputImage.setOnClickListener(v -> { + if(view) return; + new StyledDialogBuilder(requireContext()) .setTitle(R.string.edit_otp_choose_image) .setItems(R.array.edit_otp_choose_image_options, (d, which) -> { diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java index 85efb97..c4b59b1 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java @@ -124,7 +124,8 @@ public class GroupFragment extends NamedFragment { if(items.size() != 1) return; OTPData data = items.get(0).getOTPData(); - DialogUtil.showViewCodeDialog(getLayoutInflater(), data); + //DialogUtil.showViewCodeDialog(getLayoutInflater(), data); + NavigationUtil.openOverlay(this, new EditOTPFragment(data, true, null)); } public void editOTP() { diff --git a/app/src/main/java/com/cringe_studios/code_guard/util/DialogUtil.java b/app/src/main/java/com/cringe_studios/code_guard/util/DialogUtil.java index 27c77b2..fa2bef9 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/util/DialogUtil.java +++ b/app/src/main/java/com/cringe_studios/code_guard/util/DialogUtil.java @@ -3,10 +3,8 @@ package com.cringe_studios.code_guard.util; import android.content.Context; import android.view.LayoutInflater; import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; -import androidx.annotation.NonNull; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; import androidx.core.util.Consumer; @@ -15,15 +13,9 @@ import com.cringe_studios.code_guard.R; import com.cringe_studios.code_guard.backup.BackupData; import com.cringe_studios.code_guard.databinding.DialogCreateGroupBinding; import com.cringe_studios.code_guard.databinding.DialogErrorBinding; -import com.cringe_studios.code_guard.databinding.DialogInputCodeHotpBinding; -import com.cringe_studios.code_guard.databinding.DialogInputCodeTotpBinding; import com.cringe_studios.code_guard.databinding.DialogInputPasswordBinding; import com.cringe_studios.code_guard.databinding.DialogSetPasswordBinding; -import com.cringe_studios.code_guard.model.OTPData; -import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; -import com.cringe_studios.cringe_authenticator_library.OTPType; -import java.util.Arrays; import java.util.List; import java.util.UUID; @@ -108,151 +100,6 @@ public class DialogUtil { return b.toString().trim(); } - public static void showTOTPDialog(LayoutInflater inflater, OTPData initialData, Consumer callback, boolean view) { - Context context = inflater.getContext(); - DialogInputCodeTotpBinding binding = DialogInputCodeTotpBinding.inflate(inflater); - - binding.inputAlgorithm.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, OTPAlgorithm.values())); - binding.inputAlgorithm.setEnabled(!view); - - binding.inputDigits.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, DIGITS)); - binding.inputDigits.setEnabled(!view); - - binding.inputName.setEnabled(!view); - binding.inputIssuer.setEnabled(!view); - binding.inputSecret.setEnabled(!view); - binding.inputPeriod.setEnabled(!view); - binding.inputChecksum.setEnabled(!view); - - if(initialData != null) { - binding.inputName.setText(initialData.getName()); - binding.inputIssuer.setText(initialData.getIssuer()); - binding.inputSecret.setText(initialData.getSecret()); - binding.inputAlgorithm.setSelection(initialData.getAlgorithm().ordinal()); - - int index = Arrays.asList(DIGITS).indexOf(initialData.getDigits()); - if(index != -1) binding.inputDigits.setSelection(index); - - binding.inputPeriod.setText(String.valueOf(initialData.getPeriod())); - binding.inputChecksum.setChecked(initialData.hasChecksum()); - } - - showCodeDialog(context, binding.getRoot(), () -> { - try { - String name = binding.inputName.getText().toString(); - if(name.trim().isEmpty()) { - showErrorDialog(context, context.getString(R.string.otp_add_missing_name)); - return false; - } - - String issuer = binding.inputIssuer.getText().toString(); - if(issuer.trim().isEmpty()) { - issuer = null; - } - - String secret = binding.inputSecret.getText().toString(); - OTPAlgorithm algorithm = (OTPAlgorithm) binding.inputAlgorithm.getSelectedItem(); - int digits = (int) binding.inputDigits.getSelectedItem(); - int period = Integer.parseInt(binding.inputPeriod.getText().toString()); - boolean checksum = binding.inputChecksum.isChecked(); - - OTPData data = new OTPData(name, issuer, OTPType.TOTP, secret, algorithm, digits, period, 0, checksum); - - String errorMessage = data.validate(); - if(errorMessage != null) { - showErrorDialog(context, errorMessage); - return false; - } - - callback.accept(data); - return true; - }catch(NumberFormatException e) { - showErrorDialog(context, context.getString(R.string.input_code_invalid_number)); - return false; - } - }); - } - - public static void showHOTPDialog(LayoutInflater inflater, OTPData initialData, Consumer callback, boolean view) { - Context context = inflater.getContext(); - DialogInputCodeHotpBinding binding = DialogInputCodeHotpBinding.inflate(inflater); - - binding.inputAlgorithm.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, OTPAlgorithm.values())); - binding.inputAlgorithm.setEnabled(!view); - - binding.inputDigits.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, DIGITS)); - binding.inputDigits.setEnabled(!view); - - binding.inputName.setEnabled(!view); - binding.inputIssuer.setEnabled(!view); - binding.inputSecret.setEnabled(!view); - binding.inputCounter.setEnabled(!view); - binding.inputChecksum.setEnabled(!view); - - if(initialData != null) { - binding.inputName.setText(initialData.getName()); - binding.inputIssuer.setText(initialData.getIssuer()); - binding.inputSecret.setText(initialData.getSecret()); - binding.inputAlgorithm.setSelection(initialData.getAlgorithm().ordinal()); - - int index = Arrays.asList(DIGITS).indexOf(initialData.getDigits()); - if(index != -1) binding.inputDigits.setSelection(index); - - binding.inputCounter.setText(String.valueOf(initialData.getCounter())); - binding.inputChecksum.setChecked(initialData.hasChecksum()); - } - - showCodeDialog(context, binding.getRoot(), () -> { - try { - String name = binding.inputName.getText().toString(); - if(name.trim().isEmpty()) { - showErrorDialog(context, context.getString(R.string.otp_add_missing_name)); - return false; - } - - String issuer = binding.inputIssuer.getText().toString(); - if(issuer.trim().isEmpty()) { - issuer = null; - } - - String secret = binding.inputSecret.getText().toString(); - OTPAlgorithm algorithm = (OTPAlgorithm) binding.inputAlgorithm.getSelectedItem(); - int digits = (int) binding.inputDigits.getSelectedItem(); - int counter = Integer.parseInt(binding.inputCounter.getText().toString()); - boolean checksum = binding.inputChecksum.isChecked(); - - OTPData data = new OTPData(name, issuer, OTPType.HOTP, secret, algorithm, digits, 0, counter, checksum); - - String errorMessage = data.validate(); - if(errorMessage != null) { - showErrorDialog(context, errorMessage); - return false; - } - - callback.accept(data); - return true; - }catch(NumberFormatException e) { - showErrorDialog(context, context.getString(R.string.input_code_invalid_number)); - return false; - } - }); - } - - public static void showViewCodeDialog(LayoutInflater inflater, @NonNull OTPData initialData) { - // TODO: use better dialogs - switch(initialData.getType()) { - case HOTP: showHOTPDialog(inflater, initialData, d -> {}, true); break; - case TOTP: showTOTPDialog(inflater, initialData, d -> {}, true); break; - } - } - - public static void showEditCodeDialog(LayoutInflater inflater, @NonNull OTPData initialData, Consumer callback) { - switch(initialData.getType()) { - case HOTP: showHOTPDialog(inflater, initialData, callback, false); break; - case TOTP: showTOTPDialog(inflater, initialData, callback, false); break; - } - } - public static void showCreateGroupDialog(LayoutInflater inflater, String initialName, Consumer callback, Runnable onDismiss) { Context context = inflater.getContext(); diff --git a/app/src/main/java/com/cringe_studios/code_guard/util/NavigationUtil.java b/app/src/main/java/com/cringe_studios/code_guard/util/NavigationUtil.java index d31b445..8e44ba1 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/util/NavigationUtil.java +++ b/app/src/main/java/com/cringe_studios/code_guard/util/NavigationUtil.java @@ -42,8 +42,7 @@ public class NavigationUtil { navigate((AppCompatActivity) currentFragment.requireActivity(), fragmentClass, args); } - public static void openOverlay(Fragment currentFragment, NamedFragment overlay) { - AppCompatActivity activity = (AppCompatActivity) currentFragment.requireActivity(); + public static void openOverlay(AppCompatActivity activity, NamedFragment overlay) { FragmentManager manager = activity.getSupportFragmentManager(); manager.beginTransaction() .setReorderingAllowed(true) @@ -52,6 +51,11 @@ public class NavigationUtil { .commit(); } + public static void openOverlay(Fragment currentFragment, NamedFragment overlay) { + AppCompatActivity activity = (AppCompatActivity) currentFragment.requireActivity(); + openOverlay(activity, overlay); + } + public static void closeOverlay(Fragment currentFragment) { AppCompatActivity activity = (AppCompatActivity) currentFragment.requireActivity(); FragmentManager manager = activity.getSupportFragmentManager(); diff --git a/app/src/main/res/layout/dialog_input_code_choice.xml b/app/src/main/res/layout/dialog_input_code_choice.xml deleted file mode 100644 index f3fd0a0..0000000 --- a/app/src/main/res/layout/dialog_input_code_choice.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_input_code_hotp.xml b/app/src/main/res/layout/dialog_input_code_hotp.xml deleted file mode 100644 index 060cc21..0000000 --- a/app/src/main/res/layout/dialog_input_code_hotp.xml +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_input_code_totp.xml b/app/src/main/res/layout/dialog_input_code_totp.xml deleted file mode 100644 index 9784894..0000000 --- a/app/src/main/res/layout/dialog_input_code_totp.xml +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_set_password.xml b/app/src/main/res/layout/dialog_set_password.xml index 13f14b5..1f33821 100644 --- a/app/src/main/res/layout/dialog_set_password.xml +++ b/app/src/main/res/layout/dialog_set_password.xml @@ -7,12 +7,6 @@ android:padding="16dp" android:orientation="vertical"> - - - - + android:layout_marginTop="10dp" + app:passwordToggleEnabled="true"> - + android:inputType="textPassword" + app:boxStrokeWidth="0dp"/>