From 825335ea5f7ed13f660ff76381319455888dc0df Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Sat, 27 Nov 2021 21:16:50 +0200 Subject: [PATCH] MOB-40 added save can to home fragment, fixed language resources --- .../mobileauthapp/AuthFragment.kt | 25 +++--- .../mobileauthapp/CanFragment.kt | 10 ++- .../mobileauthapp/HomeFragment.kt | 19 +++-- .../mobileauthapp/MainActivity.kt | 2 +- .../mobileauthapp/PinFragment.kt | 12 +-- .../mobileauthapp/ResultFragment.kt | 4 - .../mobileauthapp/menu/SettingsFragment.kt | 2 +- .../app/src/main/res/layout/fragment_auth.xml | 2 +- .../app/src/main/res/layout/fragment_can.xml | 6 +- .../app/src/main/res/layout/fragment_home.xml | 4 +- .../app/src/main/res/layout/fragment_pin.xml | 4 +- .../app/src/main/res/layout/fragment_pin2.xml | 2 +- .../app/src/main/res/layout/fragment_user.xml | 2 +- .../app/src/main/res/navigation/nav_graph.xml | 16 +--- .../app/src/main/res/values-en/strings.xml | 85 +++++++------------ .../app/src/main/res/values-et/strings.xml | 76 ++++++----------- .../app/src/main/res/values/strings.xml | 79 ++++++----------- 17 files changed, 134 insertions(+), 216 deletions(-) diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt index 8dfdfd7..2837671 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt @@ -34,7 +34,7 @@ class AuthFragment : Fragment() { private val viewModel: SmartCardViewModel by activityViewModels() - private val intentParameters: ParametersViewModel by activityViewModels() + private val paramsModel: ParametersViewModel by activityViewModels() private var binding: FragmentAuthBinding? = null @@ -67,17 +67,18 @@ class AuthFragment : Fragment() { override fun onFinish() { Thread.sleep(750) - goToTheStart() + cancelAuth() } }.start() + // The button exists in code for testing reasons, but not visible to the user anymore unless visibility is changed in the code. binding!!.nextButton.visibility = View.GONE binding!!.nextButton.setOnClickListener { goToNextFragment() } - binding!!.cancelButton.setOnClickListener { goToTheStart() } + binding!!.cancelButton.setOnClickListener { cancelAuth() } val adapter = NfcAdapter.getDefaultAdapter(activity) if (adapter != null) getInfoFromIdCard(adapter) - else { // If we don't have access to an NFC adapter then we can't detect an ID card, maybe should tell the user reason as well - goToTheStart() + else { // If NFC adapter can not be detected then end the auth process as it is not possible to read an ID card + cancelAuth() } } @@ -87,7 +88,7 @@ class AuthFragment : Fragment() { findNavController().navigate(action) } - private fun goToTheStart() { + private fun cancelAuth() { viewModel.clearUserInfo() timer.cancel() if (args.mobile) { @@ -111,11 +112,11 @@ class AuthFragment : Fragment() { try { val comms = Comms(it, viewModel.userCan) val jws = Authenticator(comms).authenticate( - intentParameters.challenge, - intentParameters.origin, + paramsModel.challenge, + paramsModel.origin, viewModel.userPin ) - intentParameters.setToken(jws) + paramsModel.setToken(jws) requireActivity().runOnUiThread { goToNextFragment() } @@ -129,8 +130,8 @@ class AuthFragment : Fragment() { binding!!.timeCounter.text = getString(R.string.wrong_pin, messagePieces[messagePieces.size - 1]) viewModel.deletePin(requireContext()) } - else -> { - binding!!.timeCounter.text = getString(R.string.no_success) + else -> requireActivity().runOnUiThread { + binding!!.timeCounter.text = getString(R.string.wrong_can_text) viewModel.deleteCan(requireContext()) } } @@ -138,7 +139,7 @@ class AuthFragment : Fragment() { } // Give user some time to read the error message Thread.sleep(2000) - goToTheStart() + cancelAuth() } finally { adapter.disableReaderMode(activity) } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt index e7a478f..c6a791d 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt @@ -29,8 +29,6 @@ class CanFragment : Fragment() { // Navigation arguments: // saving = true means that we are navigating here from the settings menu and must return to the settings menu. - // reading = true means that we are only reading the information from the ID card that does not need PIN 1, - // this information is passed on to the next PinFragment. private val args: CanFragmentArgs by navArgs() override fun onCreateView( @@ -65,7 +63,7 @@ class CanFragment : Fragment() { * Takes user to the next fragment, which is PinFragment. */ private fun goToTheNextFragment() { - val action = CanFragmentDirections.actionCanFragmentToPinFragment(reading = args.reading, auth = args.auth, mobile = args.mobile) + val action = CanFragmentDirections.actionCanFragmentToPinFragment(auth = args.auth, mobile = args.mobile) findNavController().navigate(action) } @@ -76,7 +74,11 @@ class CanFragment : Fragment() { */ private fun goToTheStart() { if (args.saving) { - findNavController().navigate(R.id.action_canFragment_to_settingsFragment) + if (args.fromhome) { + findNavController().navigate(R.id.action_canFragment_to_homeFragment) + } else { + findNavController().navigate(R.id.action_canFragment_to_settingsFragment) + } } else if (args.auth || args.mobile) { if (args.mobile) { val resultIntent = Intent() diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt index b63a06f..bf83c2c 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt @@ -1,11 +1,12 @@ package com.tarkvaraprojekt.mobileauthapp +import android.content.BroadcastReceiver import android.content.Intent +import android.content.IntentFilter import android.nfc.NfcAdapter import android.nfc.TagLostException import android.nfc.tech.IsoDep import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,7 +19,6 @@ import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentHomeBinding import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel import java.lang.Exception -import java.net.URLDecoder /** * HomeFragment is only shown to the user when then the user launches the application. When the application @@ -99,7 +99,7 @@ class HomeFragment : Fragment() { } catch (e: Exception) { // There was a problem with parameters, which means that authentication is not possible. // In that case we will cancel the authentication immediately as it would be waste of the user's time to carry on - // before eventual error. + // before getting an inevitable error. val resultIntent = Intent() requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) requireActivity().finish() @@ -161,8 +161,15 @@ class HomeFragment : Fragment() { if (canIsSaved) { binding!!.detectionActionText.text = getString(R.string.action_detect) enableReaderMode() + binding!!.homeActionButton.visibility = View.GONE } else { binding!!.detectionActionText.text = getString(R.string.action_detect_unavailable) + binding!!.homeActionButton.text = getString(R.string.add_can_text) + binding!!.homeActionButton.setOnClickListener { + val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(saving = true, fromhome = true) + findNavController().navigate(action) + } + binding!!.homeActionButton.visibility = View.VISIBLE } } @@ -170,11 +177,11 @@ class HomeFragment : Fragment() { * Resets the error message and allows the user to try again */ private fun reset() { - binding!!.buttonAgain.setOnClickListener { + binding!!.homeActionButton.text = getString(R.string.try_again_text) + binding!!.homeActionButton.setOnClickListener { updateAction(canSaved) - binding!!.buttonAgain.visibility = View.GONE } - binding!!.buttonAgain.visibility = View.VISIBLE + binding!!.homeActionButton.visibility = View.VISIBLE } /** diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt index b95948e..8ec228c 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt @@ -47,7 +47,7 @@ class MainActivity : AppCompatActivity() { true } else { if (!inMenu) { - Toast.makeText(this, getString(R.string.unavailable), Toast.LENGTH_SHORT).show() + Toast.makeText(this, getString(R.string.menu_unavailable_message), Toast.LENGTH_SHORT).show() } false } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt index a0df1f3..4c536d4 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt @@ -28,8 +28,6 @@ class PinFragment : Fragment() { // Navigation arguments: // saving = true means that the user must be returned to the settings menu - // reading = true means that we are reading information from the ID card that does - // not require PIN 1 so it is not necessary to ask it. private val args: PinFragmentArgs by navArgs() // TODO: Should be persistent and read when launching the app @@ -69,7 +67,7 @@ class PinFragment : Fragment() { * Takes user to the next fragment, which is AuthFragment. */ private fun goToTheNextFragment() { - val action = PinFragmentDirections.actionPinFragmentToAuthFragment(reading = args.reading, auth = args.auth, mobile = args.mobile) + val action = PinFragmentDirections.actionPinFragmentToAuthFragment(auth = args.auth, mobile = args.mobile) findNavController().navigate(action) } @@ -97,13 +95,9 @@ class PinFragment : Fragment() { * Checks if the current fragment can be skipped or not. * If the user has PIN 1 saved on the device or PIN 1 is not required * then the PIN 1 won't be asked. - * - * NOTE: maybe args.reading can be removed after changing the nav_graph */ private fun checkIfSkip() { - if (args.reading) { - goToTheNextFragment() - } else if (viewModel.userPin.length in 4..12) { + if (viewModel.userPin.length in 4..12) { goToTheNextFragment() } } @@ -127,7 +121,7 @@ class PinFragment : Fragment() { goToTheNextFragment() } } else { - Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT) + Toast.makeText(requireContext(), getString(R.string.pin_helper_text), Toast.LENGTH_SHORT) .show() } } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt index b891bc5..b397820 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt @@ -68,17 +68,13 @@ class ResultFragment : Fragment() { .setJsonObjectBody(json) .asJsonObject() .setCallback { e, result -> - // do stuff with the result or error if (result == null) { - // TODO: Set auth message failed and close the app - Log.i("Log thingy fail", "result was null") if (args.mobile) { createResponse(false) } else { requireActivity().finishAndRemoveTask() } } else { - Log.i("POST request response", result.toString()) if (args.mobile) { createResponse(true, result.toString(), paramsModel.token) } else { diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt index 6a3cedb..b966714 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt @@ -55,7 +55,7 @@ class SettingsFragment : Fragment() { binding!!.canMenuAction.text = getString(R.string.can_delete) } else { binding!!.canSaved.text = getString(R.string.saved_can, getString(R.string.missing)) - binding!!.canMenuAction.text = getString(R.string.can_add) + binding!!.canMenuAction.text = getString(R.string.add_can_text) } } diff --git a/MobileAuthApp/app/src/main/res/layout/fragment_auth.xml b/MobileAuthApp/app/src/main/res/layout/fragment_auth.xml index 7730d12..a0f4aa7 100644 --- a/MobileAuthApp/app/src/main/res/layout/fragment_auth.xml +++ b/MobileAuthApp/app/src/main/res/layout/fragment_auth.xml @@ -62,7 +62,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_big" - android:text="@string/next_text" + android:text="@string/continue_button" android:textSize="@dimen/regular_text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/cancel_button" diff --git a/MobileAuthApp/app/src/main/res/layout/fragment_can.xml b/MobileAuthApp/app/src/main/res/layout/fragment_can.xml index 1fe0ebd..c4cff63 100644 --- a/MobileAuthApp/app/src/main/res/layout/fragment_can.xml +++ b/MobileAuthApp/app/src/main/res/layout/fragment_can.xml @@ -23,11 +23,11 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/margin_big" - android:hint="@string/hint" + android:hint="@string/can_text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/title_text" app:helperTextEnabled="true" - app:helperText="@string/helper" + app:helperText="@string/can_helper_text" app:helperTextTextAppearance="@style/helper" app:counterEnabled="true" app:counterMaxLength="6" @@ -48,7 +48,7 @@