MOB-35 started integrating the NFC class with the UI

This commit is contained in:
Henrik Lepson 2021-10-06 21:02:33 +03:00
parent f93b37c535
commit fc56825ed0
6 changed files with 47 additions and 8 deletions

View File

@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tarkvaraprojekt.mobileauthapp">
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@ -1,5 +1,9 @@
package com.tarkvaraprojekt.mobileauthapp
import android.app.Activity
import android.content.Context
import android.nfc.NfcAdapter
import android.nfc.tech.IsoDep
import android.os.Bundle
import android.os.CountDownTimer
import android.view.LayoutInflater
@ -8,8 +12,10 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.tarkvaraprojekt.mobileauthapp.NFC.Comms
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentAuthBinding
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
import kotlin.concurrent.thread
/**
* Fragment that asks the user to detect the ID card with mobile NFC chip.
@ -49,13 +55,37 @@ class AuthFragment : Fragment() {
}.start()
binding!!.nextButton.setOnClickListener { goToNextFragment() }
binding!!.cancelButton.setOnClickListener { goToTheStart() }
val adapter = NfcAdapter.getDefaultAdapter(activity)
if (adapter != null)
getInfoFromIdCard(adapter)
}
private fun getInfoFromIdCard(adapter: NfcAdapter) {
adapter.enableReaderMode(activity, { tag ->
timer.cancel()
requireActivity().runOnUiThread {
binding!!.timeCounter.text = getString(R.string.card_detected)
}
val card = IsoDep.get(tag)
card.timeout = 32768
card.use {
val comms = Comms(it, viewModel.userCan)
val response = comms.readPersonalData(byteArrayOf(1, 2, 6))
if (response != null) {
viewModel.setUserFirstName(response[1])
viewModel.setUserLastName(response[0])
viewModel.setUserIdentificationNumber(response[2])
requireActivity().runOnUiThread{
binding!!.timeCounter.text = getString(R.string.data_read)
}
}
it.close()
adapter.disableReaderMode(activity)
}
}, NfcAdapter.FLAG_READER_NFC_A, null)
}
private fun goToNextFragment() {
//Dummy data for now
viewModel.setUserFirstName("John")
viewModel.setUserLastName("Doe")
viewModel.setUserIdentificationNumber("012345678910")
timer.cancel()
findNavController().navigate(R.id.action_authFragment_to_userFragment)
}

View File

@ -1,7 +1,9 @@
package com.tarkvaraprojekt.mobileauthapp
import android.nfc.NfcAdapter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.tarkvaraprojekt.mobileauthapp.databinding.ActivityMainBinding

View File

@ -44,8 +44,11 @@ class PinFragment : Fragment() {
)
findNavController().navigate(R.id.action_pinFragment_to_canFragment)
} else {
Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT)
.show()
// Currently it is not important to enter PIN1 so we will allow the user to leave this field empty
//Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT)
// .show()
viewModel.setUserPin("1234")
findNavController().navigate(R.id.action_pinFragment_to_canFragment)
}
}

View File

@ -13,8 +13,8 @@ import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
/**
* Fragment that is used to display the persons name and national identification number.
* Currently needed in order to test that the app is working because the results at the moment
* are not sent to some other website or app.
* Currently needed in order to test that the app is working and information is read
* from the ID card via NFC.
*/
class UserFragment : Fragment() {

View File

@ -17,6 +17,8 @@
<string name="text_can">CAN</string>
<string name="enter_can">Sisesta ID kaardi CAN (Card Access Number)</string>
<string name="length_can">CANi pikkus on vale</string>
<string name="card_detected">Kaart on tuvastatud. Hoia kaarti vastu telefoni.</string>
<string name="data_read">Andmed loetud.</string>
<!-- string resources for AuthFragment layout -->
<string name="auth_instruction_text">ID kaardiga ühenduse loomiseks pane kaart vastu telefoni</string>