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 318f25d..dc94333 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt @@ -40,7 +40,7 @@ class HomeFragment : Fragment() { initialChecks() // TODO: If app launched with intent then go to the CanFragment immediately. val auth = requireActivity().intent.getBooleanExtra("auth", false) - if (auth){ // Currently true for testing purposes + if (auth){ goToTheNextFragment(true) } binding!!.beginButton.setOnClickListener { goToTheNextFragment() } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/Pin2Fragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/Pin2Fragment.kt new file mode 100644 index 0000000..189c440 --- /dev/null +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/Pin2Fragment.kt @@ -0,0 +1,72 @@ +package com.tarkvaraprojekt.mobileauthapp + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentPin2Binding +import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel + +/** + * Fragment that deals with asking PIN 2 from the user. Basically the same as PIN 1 fragment. + */ +class Pin2Fragment : Fragment() { + + private val viewModel: SmartCardViewModel by activityViewModels() + + private var binding: FragmentPin2Binding? = null + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + binding = FragmentPin2Binding.inflate(inflater, container, false) + return binding!!.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding!!.nextButton.setOnClickListener { + checkPin2Length() + } + binding!!.cancelButton.setOnClickListener { + cancel() + } + } + + /** + * Checks if the length of the entered PIN 2 is in range 5..12 and if it is + * then it is saved to the viewModel. + */ + private fun checkPin2Length() { + val enteredPin2 = binding!!.pin2EditText.editText?.text.toString() + if (enteredPin2.length in 5..12) { + viewModel.setUserPin2(enteredPin2) + } else { + Toast.makeText(requireContext(), getString(R.string.length_pin2), Toast.LENGTH_SHORT) + .show() + } + } + + /** + * Authentication process is cancelled when cancel button is clicked and the application + * will be closed. + */ + private fun cancel() { + val resultIntent = Intent() + requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) + requireActivity().finish() + } + + override fun onDestroy() { + super.onDestroy() + binding = null + } + +} \ No newline at end of file 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 c510048..82c4d33 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt @@ -13,7 +13,8 @@ import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel /** * ResultFragment is used to create a JWT and to send response to the website/application - * that launched the MobileAuthApp. + * that launched the MobileAuthApp. If the mobile auth app was started by a website + * the result is sent to a server with a POST request. */ class ResultFragment : Fragment() { diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt index 1b66115..8780736 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt @@ -11,6 +11,9 @@ class SmartCardViewModel: ViewModel() { private var _userPin: String = "" val userPin get() = _userPin + private var _userPin2: String = "" + val userPin2 get() = _userPin2 + private var _userCan: String = "" val userCan get() = _userCan @@ -34,6 +37,7 @@ class SmartCardViewModel: ViewModel() { fun clearUserInfo() { _userPin = "" + _userPin2 = "" _userCan = "" _userFirstName = "" _userLastName = "" @@ -47,6 +51,10 @@ class SmartCardViewModel: ViewModel() { _userPin = newUserPin } + fun setUserPin2(newUserPin2: String) { + _userPin2 = newUserPin2 + } + fun setUserCan(newUserCan: String) { _userCan = newUserCan } diff --git a/MobileAuthApp/app/src/main/res/layout/fragment_pin2.xml b/MobileAuthApp/app/src/main/res/layout/fragment_pin2.xml new file mode 100644 index 0000000..8929989 --- /dev/null +++ b/MobileAuthApp/app/src/main/res/layout/fragment_pin2.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + +