From 553d04df8e040928de5faee9535c3c42abf83fde Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Fri, 23 Jun 2023 17:11:04 +0200 Subject: [PATCH] Add code input dialog --- .idea/deploymentTargetDropDown.xml | 17 +---- .../cringe_authenticator/IntroActivity.java | 7 ++ .../cringe_authenticator/MainActivity.java | 70 +++++++++++++++++-- app/src/main/res/layout/activity_main.xml | 3 +- .../res/layout/dialog_input_code_choice.xml | 17 +++++ .../res/layout/dialog_input_code_totp.xml | 47 +++++++++++++ 6 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 app/src/main/res/layout/dialog_input_code_choice.xml create mode 100644 app/src/main/res/layout/dialog_input_code_totp.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 5f6f784..bc1ce66 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -6,23 +6,12 @@ - - + + - - - - - - - - - - - - + \ No newline at end of file diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java index 0dcd3bc..f238eaa 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java @@ -7,6 +7,7 @@ import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.view.ViewGroup; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -42,6 +43,12 @@ public class IntroActivity extends AppCompatActivity { binding.videoView.setOnCompletionListener(mp -> openMainActivity()); + binding.videoView.setOnErrorListener((MediaPlayer mp, int what, int extra) -> { + Toast.makeText(this, "Failed to play video", Toast.LENGTH_LONG).show(); + openMainActivity(); + return true; + }); + setContentView(binding.getRoot()); } 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 12f08c2..3564f39 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 @@ -2,11 +2,16 @@ package com.cringe_studios.cringe_authenticator; import android.content.Intent; import android.os.Bundle; +import android.renderscript.ScriptGroup; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.EditText; +import android.widget.ListAdapter; +import android.widget.SimpleAdapter; import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; @@ -22,6 +27,8 @@ import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.cringe_studios.cringe_authenticator.databinding.ActivityMainBinding; +import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeChoiceBinding; +import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeTotpBinding; import com.cringe_studios.cringe_authenticator.fragment.DynamicFragment; import com.cringe_studios.cringe_authenticator.fragment.HomeFragment; import com.cringe_studios.cringe_authenticator.fragment.MenuFragment; @@ -29,6 +36,8 @@ import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment; import com.cringe_studios.cringe_authenticator.scanner.QRScannerActivity; import com.cringe_studios.cringe_authenticator.scanner.QRScannerContract; import com.cringe_studios.cringe_authenticator.util.NavigationUtil; +import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; +import com.cringe_studios.cringe_authenticator_library.OTPType; import java.util.concurrent.Executor; @@ -91,7 +100,9 @@ public class MainActivity extends AppCompatActivity { appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build(); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); - binding.fab.setOnClickListener(view -> NavigationUtil.navigate(this, MenuFragment.class, null)); + binding.fabMenu.setOnClickListener(view -> NavigationUtil.navigate(this, MenuFragment.class, null)); + binding.fabScan.setOnClickListener(view -> scanCode()); + binding.fabInput.setOnClickListener(view -> inputCode()); } @Override @@ -146,12 +157,63 @@ public class MainActivity extends AppCompatActivity { NavigationUtil.navigate(this, SettingsFragment.class, null); } - public void scanCode(View view) { - Log.i("AMOGUS", "Scan"); - Intent intent = new Intent(this, QRScannerActivity.class); + public void scanCode() { startQRCodeScan.launch(null); } + public void inputCode() { + DialogInputCodeChoiceBinding binding = DialogInputCodeChoiceBinding.inflate(getLayoutInflater()); + + String[] options = new String[2]; + options[0] = OTPType.TOTP.getFriendlyName() + " (TOTP)"; + options[1] = OTPType.HOTP.getFriendlyName() + " (HOTP)"; + + AlertDialog dialog = new AlertDialog.Builder(this) + .setTitle("Select Code Type") + .setView(binding.getRoot()) + .setNegativeButton("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; + } + showTOTPDialog(); + dialog.dismiss(); + }); + + dialog.show(); + } + + private void showTOTPDialog() { + DialogInputCodeTotpBinding binding = DialogInputCodeTotpBinding.inflate(getLayoutInflater()); + binding.inputAlgorithm.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, OTPAlgorithm.values())); + 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(), () -> { + // TODO: handle input + }); + } + + private void showHOTPDialog() { + DialogInputCodeTotpBinding binding = DialogInputCodeTotpBinding.inflate(getLayoutInflater()); + showCodeDialog(binding.getRoot(), () -> {}); + } + + private void showCodeDialog(View view, Runnable ok) { + new AlertDialog.Builder(this) + .setTitle("Input Code") + .setView(view) + .setPositiveButton("Ok", (btnView, which) -> ok.run()) + .setNegativeButton("Cancel", (btnView, which) -> {}) + .show(); + } + public void addGroup(MenuItem item) { EditText t = new EditText(this); new AlertDialog.Builder(this) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 38069df..85c6894 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -38,12 +38,11 @@ android:layout_gravity="bottom|end" android:layout_marginEnd="@dimen/fab_margin" android:layout_marginBottom="16dp" - android:onClick="scanCode" app:srcCompat="@drawable/baseline_qr_code_scanner_24" android:visibility="gone" /> + + + + \ 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 new file mode 100644 index 0000000..eccbf65 --- /dev/null +++ b/app/src/main/res/layout/dialog_input_code_totp.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + \ No newline at end of file