clean up the layout & polished Mensa credit dialog
* don't crash if the nfc tag has been removed between triggering intent and connecting to the tag * closes #21
This commit is contained in:
@ -4,6 +4,6 @@ package com.codebutler.farebot.card.desfire
|
||||
* Created by Jakob Wenzel on 16.11.13.
|
||||
*/
|
||||
class DesfireException : Exception {
|
||||
constructor(message: String) : super(message) {}
|
||||
constructor(cause: Throwable) : super(cause) {}
|
||||
constructor(message: String) : super(message)
|
||||
constructor(cause: Throwable) : super(cause)
|
||||
}
|
||||
|
@ -27,16 +27,20 @@ import android.content.Intent
|
||||
import android.nfc.NfcAdapter
|
||||
import android.nfc.Tag
|
||||
import android.nfc.tech.IsoDep
|
||||
import android.util.Log
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.customview.customView
|
||||
import com.codebutler.farebot.Utils
|
||||
import com.codebutler.farebot.card.desfire.DesfireFileSettings
|
||||
import com.codebutler.farebot.card.desfire.DesfireProtocol
|
||||
import kotlinx.android.synthetic.main.dialog_mensa_credit.*
|
||||
import org.mosad.seil0.projectlaogai.R
|
||||
import java.lang.Exception
|
||||
|
||||
class NFCMensaCard {
|
||||
|
||||
companion object {
|
||||
private const val className = "NFCMensaCard"
|
||||
private const val appId = 0x5F8415
|
||||
private const val fileId = 1
|
||||
|
||||
@ -48,21 +52,23 @@ class NFCMensaCard {
|
||||
fun readBalance(intent: Intent, context: Context) {
|
||||
val tag: Tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
||||
val isoDep = IsoDep.get(tag)
|
||||
isoDep.connect()
|
||||
try {
|
||||
isoDep.connect()
|
||||
|
||||
val card = DesfireProtocol(isoDep)
|
||||
val settings = Utils.selectAppFile(card, appId, fileId)
|
||||
val card = DesfireProtocol(isoDep)
|
||||
val settings = Utils.selectAppFile(card, appId, fileId)
|
||||
|
||||
if (settings is DesfireFileSettings.ValueDesfireFileSettings) {
|
||||
val data = try {
|
||||
card.readValue(fileId)
|
||||
} catch (ex: Exception) { 0 }
|
||||
if (settings is DesfireFileSettings.ValueDesfireFileSettings) {
|
||||
val data = try {
|
||||
card.readValue(fileId)
|
||||
} catch (ex: Exception) { 0 }
|
||||
|
||||
MaterialDialog(context)
|
||||
.title(R.string.mensa_credit)
|
||||
.message(text = lookAtMe(context, data, settings.value))
|
||||
.show()
|
||||
lookAtMe(context, data, settings.value).show()
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
Log.i(className,"could not connect to tag", ex)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,7 +80,10 @@ class NFCMensaCard {
|
||||
* @param lastRaw the raw card value of the last payment
|
||||
* @return the message containing all values
|
||||
*/
|
||||
private fun lookAtMe(context: Context, currentRaw: Int, lastRaw: Int): String {
|
||||
private fun lookAtMe(context: Context, currentRaw: Int, lastRaw: Int): MaterialDialog {
|
||||
val dialog = MaterialDialog(context)
|
||||
.customView(R.layout.dialog_mensa_credit)
|
||||
|
||||
val current = if (!PreferencesController.oGiants) {
|
||||
String.format("%.2f €", (currentRaw.toFloat() / 1000))
|
||||
} else {
|
||||
@ -87,7 +96,10 @@ class NFCMensaCard {
|
||||
String.format("%.4f shm", (lastRaw.toFloat() * 0.0000075))
|
||||
}
|
||||
|
||||
return context.resources.getString(R.string.mensa_current, current) + context.resources.getString(R.string.mensa_last, last)
|
||||
dialog.txtView_current.text = current
|
||||
dialog.txtView_last.text = context.resources.getString(R.string.mensa_last, last)
|
||||
|
||||
return dialog
|
||||
}
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ package org.mosad.seil0.projectlaogai.fragments
|
||||
|
||||
import android.graphics.Typeface
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -52,6 +53,7 @@ import java.util.*
|
||||
*/
|
||||
class HomeFragment : Fragment() {
|
||||
|
||||
private val className = "MyActivity"
|
||||
private val formatter = SimpleDateFormat("E dd.MM", Locale.getDefault())
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
@ -146,7 +148,7 @@ class HomeFragment : Fragment() {
|
||||
.title(R.string.error)
|
||||
.message(R.string.timetable_error)
|
||||
.show()
|
||||
// TODO log the error and send feedback
|
||||
Log.e(className, "could not load timetable") // TODO send feedback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user