first working implementation of the mensa card reader
* based on farebot's desfire protocol implementation * see #21
This commit is contained in:
@ -22,9 +22,12 @@
|
||||
|
||||
package org.mosad.seil0.projectlaogai
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.nfc.NdefMessage
|
||||
import android.nfc.NfcAdapter
|
||||
import android.nfc.Tag
|
||||
import android.nfc.tech.IsoDep
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
@ -35,6 +38,7 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import com.afollestad.aesthetic.Aesthetic
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.codebutler.farebot.card.desfire.DesfireProtocol
|
||||
import com.google.android.material.navigation.NavigationView
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.app_bar_main.*
|
||||
@ -44,6 +48,10 @@ import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.
|
||||
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorPrimary
|
||||
import org.mosad.seil0.projectlaogai.fragments.*
|
||||
import kotlin.system.measureTimeMillis
|
||||
import com.codebutler.farebot.Utils.selectAppFile
|
||||
import com.codebutler.farebot.card.desfire.DesfireFileSettings
|
||||
import java.lang.Exception
|
||||
|
||||
|
||||
// TODO save the current fragment to show it when the app is restarted
|
||||
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
|
||||
@ -74,16 +82,50 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
nav_view.setNavigationItemSelectedListener(this)
|
||||
|
||||
// TODO nfc stuff, needs to move to it's own function
|
||||
if (intent.action == NfcAdapter.ACTION_TECH_DISCOVERED) {
|
||||
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
|
||||
val appId = 0x5F8415
|
||||
val fileId = 1
|
||||
|
||||
val tag: Tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
||||
MaterialDialog(this)
|
||||
.title(text = "nfc tag detected")
|
||||
.message(text = tag.toString())
|
||||
.show()
|
||||
|
||||
val isoDep = IsoDep.get(tag)
|
||||
isoDep.connect()
|
||||
|
||||
val card = DesfireProtocol(isoDep)
|
||||
val settings = selectAppFile(card, appId, fileId)
|
||||
|
||||
if (settings is DesfireFileSettings.ValueDesfireFileSettings) {
|
||||
val data = try {
|
||||
card.readValue(fileId)
|
||||
} catch (ex: Exception) { 0 }
|
||||
|
||||
MaterialDialog(this)
|
||||
.title(text = "Mensa balance")
|
||||
.message(text = "current: ${data / 1000}.${(data % 1000) / 10}€\n" +
|
||||
"latest: ${settings.value / 1000}.${(settings.value % 1000) / 10}€")
|
||||
.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
|
||||
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
|
||||
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_TAG)?.also { rawMessages ->
|
||||
val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }
|
||||
// Process the messages array.
|
||||
|
||||
MaterialDialog(this)
|
||||
.title(text = "nfc tag detected (onNewIntent)")
|
||||
.message(text = messages[0].toString())
|
||||
.show()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
Aesthetic.resume(this)
|
||||
|
Reference in New Issue
Block a user