Bugfix/various fixes (#674)

This commit is contained in:
Justin Bassett 2020-07-21 00:59:07 -04:00 committed by GitHub
parent 16073dccbf
commit f468ecefdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 52 deletions

View file

@ -3,6 +3,7 @@ package io.homeassistant.companion.android.onboarding.discovery
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.util.Log
import java.lang.Exception
import java.net.URL
import java.util.concurrent.locks.ReentrantLock
import okio.internal.commonToUtf8String
@ -26,7 +27,13 @@ class HomeAssistantSearcher constructor(
if (isSearching)
return
isSearching = true
nsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, this)
try {
nsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, this)
} catch (e: Exception) {
Log.e(TAG, "Issue starting discover.", e)
isSearching = false
discoveryView.onScanError()
}
}
fun stopSearch() {

View file

@ -20,7 +20,7 @@ class GeocodeSensorManager : SensorManager {
val sensor = getGeocodedLocation(context)
if (sensor != null) {
return listOf(
SensorRegistration<Any>(
SensorRegistration(
sensor,
"Geocoded Location"
)
@ -44,11 +44,12 @@ class GeocodeSensorManager : SensorManager {
Log.w(TAG, "Tried getting gecoded location without permission.")
return null
}
Tasks.await(LocationServices.getFusedLocationProviderClient(context).lastLocation)?.let {
if (it.accuracy > LocationBroadcastReceiver.MINIMUM_ACCURACY)
return null
try {
val locApi = LocationServices.getFusedLocationProviderClient(context)
Tasks.await(locApi.lastLocation)?.let {
if (it.accuracy > LocationBroadcastReceiver.MINIMUM_ACCURACY)
return null
try {
Geocoder(context)
.getFromLocation(it.latitude, it.longitude, 1)
.firstOrNull()?.let { address ->
@ -71,10 +72,10 @@ class GeocodeSensorManager : SensorManager {
)
)
}
} catch (e: Exception) {
// We don't want to crash if the device cannot get a geocoded location
Log.e(TAG, "Issue getting geocoded location ", e)
}
} catch (e: Exception) {
// We don't want to crash if the device cannot get a geocoded location
Log.e(TAG, "Issue getting geocoded location ", e)
}
return null
}

View file

@ -12,6 +12,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.AutoCompleteTextView
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
@ -44,56 +45,65 @@ class ButtonWidgetConfigureActivity : Activity() {
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private var onClickListener = View.OnClickListener {
val context = this@ButtonWidgetConfigureActivity
try {
val context = this@ButtonWidgetConfigureActivity
// Set up a broadcast intent and pass the service call data as extras
val intent = Intent()
intent.action = ButtonWidget.RECEIVE_DATA
intent.component = ComponentName(context, ButtonWidget::class.java)
// Set up a broadcast intent and pass the service call data as extras
val intent = Intent()
intent.action = ButtonWidget.RECEIVE_DATA
intent.component = ComponentName(context, ButtonWidget::class.java)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
// Analyze and send service and domain
val serviceText = context.widget_text_config_service.text.toString()
val domain = services[serviceText]?.domain ?: serviceText.split(".", limit = 2)[0]
val service = services[serviceText]?.service ?: serviceText.split(".", limit = 2)[1]
intent.putExtra(
ButtonWidget.EXTRA_DOMAIN,
domain
)
intent.putExtra(
ButtonWidget.EXTRA_SERVICE,
service
)
// Analyze and send service and domain
val serviceText = context.widget_text_config_service.text.toString()
val domain = services[serviceText]?.domain ?: serviceText.split(".", limit = 2)[0]
val service = services[serviceText]?.service ?: serviceText.split(".", limit = 2)[1]
intent.putExtra(
ButtonWidget.EXTRA_DOMAIN,
domain
)
intent.putExtra(
ButtonWidget.EXTRA_SERVICE,
service
)
// Fetch and send label and icon
intent.putExtra(
ButtonWidget.EXTRA_LABEL,
context.label.text.toString()
)
intent.putExtra(
ButtonWidget.EXTRA_ICON,
context.widget_config_spinner.selectedItemId.toInt()
)
// Fetch and send label and icon
intent.putExtra(
ButtonWidget.EXTRA_LABEL,
context.label.text.toString()
)
intent.putExtra(
ButtonWidget.EXTRA_ICON,
context.widget_config_spinner.selectedItemId.toInt()
)
// Analyze and send service data
val serviceDataMap = HashMap<String, Any>()
dynamicFields.forEach {
if (it.value != null) {
serviceDataMap[it.field] = it.value!!
// Analyze and send service data
val serviceDataMap = HashMap<String, Any>()
dynamicFields.forEach {
if (it.value != null) {
serviceDataMap[it.field] = it.value!!
}
}
intent.putExtra(
ButtonWidget.EXTRA_SERVICE_DATA,
jacksonObjectMapper().writeValueAsString(serviceDataMap)
)
context.sendBroadcast(intent)
// Make sure we pass back the original appWidgetId
setResult(
RESULT_OK,
Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
)
finish()
} catch (e: Exception) {
Log.e(TAG, "Issue configuring widget", e)
Toast.makeText(applicationContext, R.string.widget_creation_error, Toast.LENGTH_LONG)
.show()
}
intent.putExtra(
ButtonWidget.EXTRA_SERVICE_DATA,
jacksonObjectMapper().writeValueAsString(serviceDataMap)
)
context.sendBroadcast(intent)
// Make sure we pass back the original appWidgetId
setResult(RESULT_OK, Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId))
finish()
}
private val onAddFieldListener = View.OnClickListener {

View file

@ -120,4 +120,5 @@ like to connect to:</string>
<string name="widget_text_hint_service_service">Service</string>
<string name="firebase_error_title">Firebase Error</string>
<string name="firebase_error_message">By skipping/ignoring this error you will be unable to receive any push notifications.</string>
<string name="widget_creation_error">Unable to create widget.</string>
</resources>