cleand up the nfc part, added foreground-dispatch
* cleaned up the strings.xml files * fixed license dialog
This commit is contained in:
@ -22,12 +22,12 @@
|
||||
|
||||
package org.mosad.seil0.projectlaogai
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.graphics.Color
|
||||
import android.nfc.NdefMessage
|
||||
import android.nfc.NfcAdapter
|
||||
import android.nfc.Tag
|
||||
import android.nfc.tech.IsoDep
|
||||
import android.nfc.tech.NfcA
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
@ -37,20 +37,16 @@ import androidx.core.view.GravityCompat
|
||||
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.*
|
||||
import org.mosad.seil0.projectlaogai.controller.CacheController
|
||||
import org.mosad.seil0.projectlaogai.controller.NFCMensaCard
|
||||
import org.mosad.seil0.projectlaogai.controller.PreferencesController
|
||||
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorAccent
|
||||
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
|
||||
@ -58,6 +54,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
|
||||
private var activeFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
|
||||
|
||||
private lateinit var adapter: NfcAdapter
|
||||
private lateinit var pendingIntent: PendingIntent
|
||||
private lateinit var intentFiltersArray: Array<IntentFilter>
|
||||
private lateinit var techListsArray: Array<Array<String>>
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
Aesthetic.attach(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -67,6 +68,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
// load mensa, timetable and color
|
||||
load()
|
||||
initAesthetic()
|
||||
initForegroundDispatch()
|
||||
|
||||
//init home fragment
|
||||
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
|
||||
@ -81,65 +83,37 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
|
||||
nav_view.setNavigationItemSelectedListener(this)
|
||||
|
||||
// TODO nfc stuff, needs to move to it's own function
|
||||
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
|
||||
val appId = 0x5F8415
|
||||
val fileId = 1
|
||||
|
||||
val tag: Tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
// if we get an NFC read intent while the app is closed call readBalance
|
||||
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action)
|
||||
NFCMensaCard.readBalance(intent, this)
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
}
|
||||
}
|
||||
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action)
|
||||
NFCMensaCard.readBalance(intent, this)
|
||||
}
|
||||
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
Aesthetic.resume(this)
|
||||
adapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray)
|
||||
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
Aesthetic.pause(this)
|
||||
adapter.disableForegroundDispatch(this)
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
|
||||
drawer_layout.closeDrawer(GravityCompat.START)
|
||||
} else {
|
||||
// TODO only call on double tap
|
||||
super.onBackPressed()
|
||||
}
|
||||
}
|
||||
@ -214,4 +188,15 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
}
|
||||
}
|
||||
|
||||
private fun initForegroundDispatch() {
|
||||
intentFiltersArray = arrayOf(IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED).apply { addDataType("*/*") })
|
||||
techListsArray = arrayOf(arrayOf(NfcA::class.java.name))
|
||||
adapter = NfcAdapter.getDefaultAdapter(this)
|
||||
pendingIntent = PendingIntent.getActivity(
|
||||
this, 0,
|
||||
Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,93 @@
|
||||
/**
|
||||
* ProjectLaogai
|
||||
*
|
||||
* Copyright 2019 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.seil0.projectlaogai.controller
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.nfc.NfcAdapter
|
||||
import android.nfc.Tag
|
||||
import android.nfc.tech.IsoDep
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.codebutler.farebot.Utils
|
||||
import com.codebutler.farebot.card.desfire.DesfireFileSettings
|
||||
import com.codebutler.farebot.card.desfire.DesfireProtocol
|
||||
import org.mosad.seil0.projectlaogai.R
|
||||
import java.lang.Exception
|
||||
|
||||
class NFCMensaCard {
|
||||
|
||||
companion object {
|
||||
private const val appId = 0x5F8415
|
||||
private const val fileId = 1
|
||||
|
||||
/**
|
||||
* read the current balance and last payment from the mensa card
|
||||
* @param intent a nfc intent
|
||||
* @param context the context to show the dialog in
|
||||
*/
|
||||
fun readBalance(intent: Intent, context: Context) {
|
||||
val tag: Tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
||||
val isoDep = IsoDep.get(tag)
|
||||
isoDep.connect()
|
||||
|
||||
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 }
|
||||
|
||||
MaterialDialog(context)
|
||||
.title(R.string.mensa_credit)
|
||||
.message(text = lookAtMe(context, data, settings.value))
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* generate the values for current balance and last payment
|
||||
* if the easter egg is active use schmeckles as currency
|
||||
* 0.0000075 = 1.11 / 148 / 1000 (dollar / shm / card multiplier)
|
||||
* @param context the context to access resources
|
||||
* @param currentRaw the raw card value of the current balance
|
||||
* @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 {
|
||||
val current = if (!PreferencesController.oGiants) {
|
||||
String.format("%.2f €", (currentRaw.toFloat() / 1000))
|
||||
} else {
|
||||
String.format("%.4f shm", (currentRaw.toFloat() * 0.0000075))
|
||||
}
|
||||
|
||||
val last = if (!PreferencesController.oGiants) {
|
||||
String.format("%.2f €", (lastRaw.toFloat() / 1000))
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
@ -42,6 +42,7 @@ class PreferencesController {
|
||||
var cColorAccent: Int = Color.parseColor("#3F51B5")
|
||||
var cCourse = Course("https://www.hs-offenburg.de/index.php?id=6627&class=class&iddV=DA64F6FE-9DDB-429E-A677-05D0D40CB636&week=0", "AI3")
|
||||
var cShowBuffet = true
|
||||
var oGiants = false
|
||||
|
||||
// the save function
|
||||
fun save(context: Context) {
|
||||
|
@ -124,6 +124,11 @@ class SettingsFragment : Fragment() {
|
||||
// open a new dialog
|
||||
}
|
||||
|
||||
linLayoutUser.setOnLongClickListener {
|
||||
PreferencesController.oGiants = true // enable easter egg
|
||||
return@setOnLongClickListener true
|
||||
}
|
||||
|
||||
linLayoutCourse.setOnClickListener {
|
||||
selectCourse(context!!)
|
||||
txtView_Course.text = cCourse.courseName // update txtView
|
||||
|
Reference in New Issue
Block a user