don't crash on devices without nfc

This commit is contained in:
Jannik 2019-08-21 20:43:08 +02:00
parent b4ed1ca927
commit 6cc1671a52
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
2 changed files with 22 additions and 10 deletions

View File

@ -23,10 +23,12 @@
package org.mosad.seil0.projectlaogai
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.nfc.NfcAdapter
import android.nfc.NfcManager
import android.nfc.tech.NfcA
import android.os.Bundle
import android.view.Menu
@ -58,6 +60,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private lateinit var pendingIntent: PendingIntent
private lateinit var intentFiltersArray: Array<IntentFilter>
private lateinit var techListsArray: Array<Array<String>>
private var useNFC = false
override fun onCreate(savedInstanceState: Bundle?) {
Aesthetic.attach(this)
@ -99,14 +102,16 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
override fun onResume() {
super.onResume()
Aesthetic.resume(this)
adapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray)
if(useNFC)
adapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray)
}
override fun onPause() {
super.onPause()
Aesthetic.pause(this)
adapter.disableForegroundDispatch(this)
if(useNFC)
adapter.disableForegroundDispatch(this)
}
override fun onBackPressed() {
@ -189,14 +194,19 @@ 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
)
val nfcManager = this.getSystemService(Context.NFC_SERVICE) as NfcManager
val nfcAdapter = nfcManager.defaultAdapter
if (nfcAdapter != null) {
useNFC = true
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
)
}
}
}

View File

@ -1,5 +1,7 @@
This release 0.5.0 is called "artistical Apollon".
* new: it's now possible to choos a theme (light, dark or balck)
* change: updated some libs, updated kotlin to 1.3.40
* new: you can now check your current mensa card balance
* change: updated some libs, updated kotlin to 1.3.41
* fix: the mensa should now show the correct meals on sunday/monday
* fix: the timetable should now show the correct on the sunday/monday change