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 package org.mosad.seil0.projectlaogai
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.graphics.Color import android.graphics.Color
import android.nfc.NfcAdapter import android.nfc.NfcAdapter
import android.nfc.NfcManager
import android.nfc.tech.NfcA import android.nfc.tech.NfcA
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
@ -58,6 +60,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private lateinit var pendingIntent: PendingIntent private lateinit var pendingIntent: PendingIntent
private lateinit var intentFiltersArray: Array<IntentFilter> private lateinit var intentFiltersArray: Array<IntentFilter>
private lateinit var techListsArray: Array<Array<String>> private lateinit var techListsArray: Array<Array<String>>
private var useNFC = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Aesthetic.attach(this) Aesthetic.attach(this)
@ -99,14 +102,16 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
Aesthetic.resume(this) Aesthetic.resume(this)
adapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray) if(useNFC)
adapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray)
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
Aesthetic.pause(this) Aesthetic.pause(this)
adapter.disableForegroundDispatch(this) if(useNFC)
adapter.disableForegroundDispatch(this)
} }
override fun onBackPressed() { override fun onBackPressed() {
@ -189,14 +194,19 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
} }
private fun initForegroundDispatch() { private fun initForegroundDispatch() {
intentFiltersArray = arrayOf(IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED).apply { addDataType("*/*") }) val nfcManager = this.getSystemService(Context.NFC_SERVICE) as NfcManager
techListsArray = arrayOf(arrayOf(NfcA::class.java.name)) val nfcAdapter = nfcManager.defaultAdapter
adapter = NfcAdapter.getDefaultAdapter(this)
pendingIntent = PendingIntent.getActivity(
this, 0,
Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
)
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". This release 0.5.0 is called "artistical Apollon".
* new: it's now possible to choos a theme (light, dark or balck) * 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 mensa should now show the correct meals on sunday/monday
* fix: the timetable should now show the correct on the sunday/monday change