NFC Fixes (#744)

* Ensure NFC is only enabled for valid Home Assistant Versions.

* Use correct deviceId.

* Fix crash when scanning some tags.
This commit is contained in:
Justin Bassett 2020-08-12 22:45:08 -04:00 committed by GitHub
parent d016aff47e
commit 15f6e2449e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 57 additions and 7 deletions

View file

@ -3,6 +3,7 @@ package io.homeassistant.companion.android.nfc
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.LayoutInflater
import android.view.View
@ -55,15 +56,15 @@ class NfcEditFragment : Fragment() {
return inflater.inflate(R.layout.fragment_nfc_edit, container, false)
}
@SuppressLint("SetTextI18n")
@SuppressLint("SetTextI18n", "HardwareIds")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val nfcReadObserver = Observer<String> { uuid ->
mainScope.launch {
et_tag_identifier_content.setText(uuid)
val deviceName = integrationUseCase.getRegistration().deviceName!!
et_tag_example_trigger_content.setText("- platform: event\n event_type: tag_scanned\n event_data:\n device_id: $deviceName\n tag_id: $uuid")
val deviceId = Settings.Secure.getString(requireActivity().contentResolver, Settings.Secure.ANDROID_ID)
et_tag_example_trigger_content.setText("- platform: event\n event_type: tag_scanned\n event_data:\n device_id: $deviceId\n tag_id: $uuid")
}
}
viewModel.nfcReadEvent.observe(viewLifecycleOwner, nfcReadObserver)

View file

@ -71,7 +71,7 @@ class NfcSetupActivity : AppCompatActivity() {
// Create new nfc tag
if (nfcTagToWriteUUID == null) {
val rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
val ndefMessage = rawMessages.first() as NdefMessage
val ndefMessage = rawMessages?.firstOrNull() as NdefMessage?
val url = ndefMessage?.records?.get(0)?.toUri().toString()
val nfcTagId = UrlHandler.splitNfcTagId(url)
if (nfcTagId == null) {

View file

@ -93,9 +93,12 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
true
}
findPreference<Preference>("nfc_tags")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
startActivity(NfcSetupActivity.newInstance(requireActivity()))
true
findPreference<Preference>("nfc_tags")?.let {
it.isVisible = presenter.nfcEnabled()
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
startActivity(NfcSetupActivity.newInstance(requireActivity()))
true
}
}
findPreference<EditTextPreference>("connection_internal")?.onPreferenceChangeListener =

View file

@ -7,6 +7,7 @@ interface SettingsPresenter {
fun getPreferenceDataStore(): PreferenceDataStore
fun onCreate()
fun onFinish()
fun nfcEnabled(): Boolean
fun getPanels(): Array<Panel>
fun isLockEnabled(): Boolean
fun sessionTimeOut(): Int

View file

@ -176,4 +176,20 @@ class SettingsPresenterImpl @Inject constructor(
integrationUseCase.getSessionExpireMillis()
}
}
// Make sure Core is above 0.114.0 because that's the first time NFC is available.
override fun nfcEnabled(): Boolean {
return runBlocking {
var splitVersion = listOf<String>()
try {
splitVersion = integrationUseCase.getHomeAssistantVersion().split(".")
} catch (e: Exception) {
Log.e(TAG, "Unable to get core version.", e)
}
return@runBlocking splitVersion.size > 2 &&
(Integer.parseInt(splitVersion[0]) > 0 || Integer.parseInt(splitVersion[1]) >= 114)
}
}
}

View file

@ -294,6 +294,27 @@ class IntegrationRepositoryImpl @Inject constructor(
throw IntegrationException()
}
override suspend fun getHomeAssistantVersion(): String {
val getConfigRequest =
IntegrationRequest(
"get_config",
null
)
var response: GetConfigResponse? = null
for (it in urlRepository.getApiUrls()) {
try {
response = integrationService.getConfig(it.toHttpUrlOrNull()!!, getConfigRequest)
} catch (e: Exception) {
// Ignore failure until we are out of URLS to try!
}
if (response != null)
return response.version
}
throw IntegrationException()
}
// TODO: Use websocket to get panels.
override suspend fun getPanels(): Array<Panel> {
return arrayOf()

View file

@ -23,6 +23,8 @@ interface IntegrationRepository {
suspend fun getThemeColor(): String
suspend fun getHomeAssistantVersion(): String
suspend fun getPanels(): Array<Panel>
suspend fun getServices(): Array<Service>

View file

@ -42,6 +42,8 @@ interface IntegrationUseCase {
suspend fun getThemeColor(): String
suspend fun getHomeAssistantVersion(): String
suspend fun getPanels(): Array<Panel>
suspend fun registerSensor(sensorRegistration: SensorRegistration<Any>)

View file

@ -96,6 +96,10 @@ class IntegrationUseCaseImpl @Inject constructor(
return integrationRepository.getThemeColor()
}
override suspend fun getHomeAssistantVersion(): String {
return integrationRepository.getHomeAssistantVersion()
}
override suspend fun registerSensor(sensorRegistration: SensorRegistration<Any>) {
return integrationRepository.registerSensor(sensorRegistration)
}