mirror of
https://github.com/home-assistant/android
synced 2024-10-15 12:32:54 +00:00
Catch potential play services errors (#4019)
This commit is contained in:
parent
e44cbf0113
commit
1aa56166c7
|
@ -194,7 +194,12 @@ class HighAccuracyLocationService : Service() {
|
||||||
.setPriority(Priority.PRIORITY_HIGH_ACCURACY)
|
.setPriority(Priority.PRIORITY_HIGH_ACCURACY)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
|
fusedLocationProviderClient = try {
|
||||||
|
LocationServices.getFusedLocationProviderClient(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to get fused location provider client", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
fusedLocationProviderClient?.requestLocationUpdates(request, getLocationUpdateIntent())
|
fusedLocationProviderClient?.requestLocationUpdates(request, getLocationUpdateIntent())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,10 @@ import javax.inject.Inject
|
||||||
@SuppressLint("VisibleForTests") // https://issuetracker.google.com/issues/239451111
|
@SuppressLint("VisibleForTests") // https://issuetracker.google.com/issues/239451111
|
||||||
class WearOnboardingListener : WearableListenerService() {
|
class WearOnboardingListener : WearableListenerService() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "WearOnboardingListener"
|
||||||
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var serverManager: ServerManager
|
lateinit var serverManager: ServerManager
|
||||||
|
|
||||||
|
@ -41,8 +45,16 @@ class WearOnboardingListener : WearableListenerService() {
|
||||||
setUrgent()
|
setUrgent()
|
||||||
asPutDataRequest()
|
asPutDataRequest()
|
||||||
}
|
}
|
||||||
Wearable.getDataClient(this@WearOnboardingListener).putDataItem(putDataReq).addOnCompleteListener {
|
try {
|
||||||
Log.d("WearOnboardingListener", "sendHomeAssistantInstance: ${if (it.isSuccessful) "success" else "failed"}")
|
Wearable.getDataClient(this@WearOnboardingListener).putDataItem(putDataReq)
|
||||||
|
.addOnCompleteListener {
|
||||||
|
Log.d(
|
||||||
|
TAG,
|
||||||
|
"sendHomeAssistantInstance: ${if (it.isSuccessful) "success" else "failed"}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Failed to send home assistant instance", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -227,7 +227,11 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
|
||||||
|
|
||||||
Log.d(TAG, "Registering for activity updates.")
|
Log.d(TAG, "Registering for activity updates.")
|
||||||
val fastUpdate = SensorReceiverBase.shouldDoFastUpdates(context)
|
val fastUpdate = SensorReceiverBase.shouldDoFastUpdates(context)
|
||||||
actReg.requestActivityUpdates(TimeUnit.MINUTES.toMillis(if (fastUpdate) 1 else 2), pendingIntent)
|
try {
|
||||||
|
actReg.requestActivityUpdates(TimeUnit.MINUTES.toMillis(if (fastUpdate) 1 else 2), pendingIntent)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to register for activity updates", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ((
|
if ((
|
||||||
isEnabled(context, sleepConfidence) || isEnabled(
|
isEnabled(context, sleepConfidence) || isEnabled(
|
||||||
|
@ -238,47 +242,43 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
|
||||||
) {
|
) {
|
||||||
val pendingIntent = getSleepPendingIntent(context)
|
val pendingIntent = getSleepPendingIntent(context)
|
||||||
Log.d(TAG, "Registering for sleep updates")
|
Log.d(TAG, "Registering for sleep updates")
|
||||||
val task = when {
|
try {
|
||||||
(
|
val task = when {
|
||||||
isEnabled(context, sleepConfidence) && !isEnabled(
|
(isEnabled(context, sleepConfidence) && !isEnabled(context, sleepSegment)) -> {
|
||||||
context,
|
Log.d(TAG, "Registering for sleep confidence updates only")
|
||||||
sleepSegment
|
ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
|
||||||
)
|
pendingIntent,
|
||||||
) -> {
|
SleepSegmentRequest(SleepSegmentRequest.CLASSIFY_EVENTS_ONLY)
|
||||||
Log.d(TAG, "Registering for sleep confidence updates only")
|
)
|
||||||
ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
|
}
|
||||||
pendingIntent,
|
|
||||||
SleepSegmentRequest(SleepSegmentRequest.CLASSIFY_EVENTS_ONLY)
|
(!isEnabled(context, sleepConfidence) && isEnabled(context, sleepSegment)) -> {
|
||||||
)
|
Log.d(TAG, "Registering for sleep segment updates only")
|
||||||
|
ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
|
||||||
|
pendingIntent,
|
||||||
|
SleepSegmentRequest(SleepSegmentRequest.SEGMENT_EVENTS_ONLY)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
Log.d(TAG, "Registering for both sleep confidence and segment updates")
|
||||||
|
ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
|
||||||
|
pendingIntent,
|
||||||
|
SleepSegmentRequest.getDefaultSleepSegmentRequest()
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
(
|
task.addOnSuccessListener {
|
||||||
!isEnabled(context, sleepConfidence) && isEnabled(
|
Log.d(TAG, "Successfully registered for sleep updates")
|
||||||
context,
|
sleepRegistration = true
|
||||||
sleepSegment
|
|
||||||
)
|
|
||||||
) -> {
|
|
||||||
Log.d(TAG, "Registering for sleep segment updates only")
|
|
||||||
ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
|
|
||||||
pendingIntent,
|
|
||||||
SleepSegmentRequest(SleepSegmentRequest.SEGMENT_EVENTS_ONLY)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
else -> {
|
task.addOnFailureListener {
|
||||||
Log.d(TAG, "Registering for both sleep confidence and segment updates")
|
Log.e(TAG, "Failed to register for sleep updates", it)
|
||||||
ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
|
ActivityRecognition.getClient(context).removeSleepSegmentUpdates(pendingIntent)
|
||||||
pendingIntent,
|
sleepRegistration = false
|
||||||
SleepSegmentRequest.getDefaultSleepSegmentRequest()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
} catch (e: Exception) {
|
||||||
task.addOnSuccessListener {
|
Log.e(TAG, "Unable to register for sleep updates", e)
|
||||||
Log.d(TAG, "Successfully registered for sleep updates")
|
|
||||||
sleepRegistration = true
|
|
||||||
}
|
|
||||||
task.addOnFailureListener {
|
|
||||||
Log.e(TAG, "Failed to register for sleep updates", it)
|
|
||||||
ActivityRecognition.getClient(context).removeSleepSegmentUpdates(pendingIntent)
|
|
||||||
sleepRegistration = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package io.homeassistant.companion.android.sensors
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.util.Log
|
||||||
import androidx.car.app.connection.CarConnection
|
import androidx.car.app.connection.CarConnection
|
||||||
import androidx.lifecycle.Observer
|
import androidx.lifecycle.Observer
|
||||||
import io.homeassistant.companion.android.common.sensors.SensorManager
|
import io.homeassistant.companion.android.common.sensors.SensorManager
|
||||||
|
@ -56,7 +57,12 @@ class AndroidAutoSensorManager : SensorManager, Observer<Int> {
|
||||||
}
|
}
|
||||||
CoroutineScope(Dispatchers.Main + Job()).launch {
|
CoroutineScope(Dispatchers.Main + Job()).launch {
|
||||||
if (carConnection == null) {
|
if (carConnection == null) {
|
||||||
carConnection = CarConnection(context.applicationContext)
|
carConnection = try {
|
||||||
|
CarConnection(context.applicationContext)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Failed to get car connection", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
carConnection?.type?.observeForever(this@AndroidAutoSensorManager)
|
carConnection?.type?.observeForever(this@AndroidAutoSensorManager)
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,12 @@ class GeocodeSensorManager : SensorManager {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val location = LocationServices.getFusedLocationProviderClient(context).lastLocation.await()
|
val location = try {
|
||||||
|
LocationServices.getFusedLocationProviderClient(context).lastLocation.await()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Failed to get fused location provider client", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
var address: Address? = null
|
var address: Address? = null
|
||||||
try {
|
try {
|
||||||
if (location == null) {
|
if (location == null) {
|
||||||
|
@ -128,7 +133,13 @@ class GeocodeSensorManager : SensorManager {
|
||||||
|
|
||||||
val prettyAddress = address?.getAddressLine(0)
|
val prettyAddress = address?.getAddressLine(0)
|
||||||
|
|
||||||
HighAccuracyLocationService.updateNotificationAddress(context, location, if (!prettyAddress.isNullOrEmpty()) prettyAddress else context.getString(commonR.string.unknown_address))
|
if (location != null) {
|
||||||
|
HighAccuracyLocationService.updateNotificationAddress(
|
||||||
|
context,
|
||||||
|
location,
|
||||||
|
if (!prettyAddress.isNullOrEmpty()) prettyAddress else context.getString(commonR.string.unknown_address)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
onSensorUpdated(
|
onSensorUpdated(
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -647,7 +647,13 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Registering for location updates.")
|
Log.d(TAG, "Registering for location updates.")
|
||||||
|
|
||||||
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(latestContext)
|
fusedLocationProviderClient = try {
|
||||||
|
LocationServices.getFusedLocationProviderClient(latestContext)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to get fused location provider client", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
val intent = getLocationUpdateIntent(false)
|
val intent = getLocationUpdateIntent(false)
|
||||||
|
|
||||||
fusedLocationProviderClient?.requestLocationUpdates(
|
fusedLocationProviderClient?.requestLocationUpdates(
|
||||||
|
@ -1109,65 +1115,88 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
|
||||||
setMaxUpdates(maxRetries)
|
setMaxUpdates(maxRetries)
|
||||||
setMinUpdateIntervalMillis(5000)
|
setMinUpdateIntervalMillis(5000)
|
||||||
}.build()
|
}.build()
|
||||||
LocationServices.getFusedLocationProviderClient(latestContext)
|
try {
|
||||||
.requestLocationUpdates(
|
LocationServices.getFusedLocationProviderClient(latestContext)
|
||||||
request,
|
.requestLocationUpdates(
|
||||||
object : LocationCallback() {
|
request,
|
||||||
val wakeLock: PowerManager.WakeLock? =
|
object : LocationCallback() {
|
||||||
latestContext.getSystemService<PowerManager>()
|
val wakeLock: PowerManager.WakeLock? =
|
||||||
?.newWakeLock(
|
latestContext.getSystemService<PowerManager>()
|
||||||
PowerManager.PARTIAL_WAKE_LOCK,
|
?.newWakeLock(
|
||||||
"HomeAssistant::AccurateLocation"
|
PowerManager.PARTIAL_WAKE_LOCK,
|
||||||
)?.apply { acquire(10 * 60 * 1000L /*10 minutes*/) }
|
"HomeAssistant::AccurateLocation"
|
||||||
var numberCalls = 0
|
)?.apply { acquire(10 * 60 * 1000L /*10 minutes*/) }
|
||||||
override fun onLocationResult(locationResult: LocationResult) {
|
var numberCalls = 0
|
||||||
numberCalls++
|
override fun onLocationResult(locationResult: LocationResult) {
|
||||||
Log.d(
|
numberCalls++
|
||||||
TAG,
|
Log.d(
|
||||||
"Got single accurate location update: ${locationResult.lastLocation}"
|
TAG,
|
||||||
)
|
"Got single accurate location update: ${locationResult.lastLocation}"
|
||||||
if (locationResult.equals(null)) {
|
)
|
||||||
Log.w(TAG, "No location provided.")
|
if (locationResult.equals(null)) {
|
||||||
return
|
Log.w(TAG, "No location provided.")
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
when {
|
when {
|
||||||
locationResult.lastLocation!!.accuracy <= minAccuracy -> {
|
locationResult.lastLocation!!.accuracy <= minAccuracy -> {
|
||||||
Log.d(TAG, "Location accurate enough, all done with high accuracy.")
|
Log.d(
|
||||||
runBlocking {
|
TAG,
|
||||||
locationResult.lastLocation?.let {
|
"Location accurate enough, all done with high accuracy."
|
||||||
getEnabledServers(latestContext, singleAccurateLocation).forEach { serverId ->
|
)
|
||||||
sendLocationUpdate(it, serverId, LocationUpdateTrigger.SINGLE_ACCURATE_LOCATION)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (wakeLock?.isHeld == true) wakeLock.release()
|
|
||||||
}
|
|
||||||
numberCalls >= maxRetries -> {
|
|
||||||
Log.d(
|
|
||||||
TAG,
|
|
||||||
"No location was accurate enough, sending our last location anyway"
|
|
||||||
)
|
|
||||||
if (locationResult.lastLocation!!.accuracy <= minAccuracy * 2) {
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
getEnabledServers(latestContext, singleAccurateLocation).forEach { serverId ->
|
locationResult.lastLocation?.let {
|
||||||
sendLocationUpdate(locationResult.lastLocation!!, serverId, LocationUpdateTrigger.SINGLE_ACCURATE_LOCATION)
|
getEnabledServers(
|
||||||
|
latestContext,
|
||||||
|
singleAccurateLocation
|
||||||
|
).forEach { serverId ->
|
||||||
|
sendLocationUpdate(
|
||||||
|
it,
|
||||||
|
serverId,
|
||||||
|
LocationUpdateTrigger.SINGLE_ACCURATE_LOCATION
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (wakeLock?.isHeld == true) wakeLock.release()
|
||||||
|
}
|
||||||
|
|
||||||
|
numberCalls >= maxRetries -> {
|
||||||
|
Log.d(
|
||||||
|
TAG,
|
||||||
|
"No location was accurate enough, sending our last location anyway"
|
||||||
|
)
|
||||||
|
if (locationResult.lastLocation!!.accuracy <= minAccuracy * 2) {
|
||||||
|
runBlocking {
|
||||||
|
getEnabledServers(
|
||||||
|
latestContext,
|
||||||
|
singleAccurateLocation
|
||||||
|
).forEach { serverId ->
|
||||||
|
sendLocationUpdate(
|
||||||
|
locationResult.lastLocation!!,
|
||||||
|
serverId,
|
||||||
|
LocationUpdateTrigger.SINGLE_ACCURATE_LOCATION
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (wakeLock?.isHeld == true) wakeLock.release()
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
Log.w(
|
||||||
|
TAG,
|
||||||
|
"Location not accurate enough on retry $numberCalls of $maxRetries"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if (wakeLock?.isHeld == true) wakeLock.release()
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
Log.w(
|
|
||||||
TAG,
|
|
||||||
"Location not accurate enough on retry $numberCalls of $maxRetries"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
Looper.getMainLooper()
|
||||||
Looper.getMainLooper()
|
)
|
||||||
)
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Failed to get location data for single accurate sensor", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun docsLink(): String {
|
override fun docsLink(): String {
|
||||||
|
|
|
@ -34,9 +34,9 @@ class SettingsWearActivity : AppCompatActivity(), CapabilityClient.OnCapabilityC
|
||||||
|
|
||||||
private lateinit var binding: ActivitySettingsWearBinding
|
private lateinit var binding: ActivitySettingsWearBinding
|
||||||
|
|
||||||
private lateinit var capabilityClient: CapabilityClient
|
private var capabilityClient: CapabilityClient? = null
|
||||||
private lateinit var nodeClient: NodeClient
|
private var nodeClient: NodeClient? = null
|
||||||
private lateinit var remoteActivityHelper: RemoteActivityHelper
|
private var remoteActivityHelper: RemoteActivityHelper? = null
|
||||||
|
|
||||||
private var wearNodesWithApp: Set<Node>? = null
|
private var wearNodesWithApp: Set<Node>? = null
|
||||||
private var allConnectedNodes: List<Node>? = null
|
private var allConnectedNodes: List<Node>? = null
|
||||||
|
@ -49,9 +49,24 @@ class SettingsWearActivity : AppCompatActivity(), CapabilityClient.OnCapabilityC
|
||||||
|
|
||||||
setSupportActionBar(binding.toolbar)
|
setSupportActionBar(binding.toolbar)
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
capabilityClient = Wearable.getCapabilityClient(this)
|
capabilityClient = try {
|
||||||
nodeClient = Wearable.getNodeClient(this)
|
Wearable.getCapabilityClient(this)
|
||||||
remoteActivityHelper = RemoteActivityHelper(this)
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to get capability client", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
nodeClient = try {
|
||||||
|
Wearable.getNodeClient(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to get node client", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
remoteActivityHelper = try {
|
||||||
|
RemoteActivityHelper(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to get remote activity helper", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
binding.remoteOpenButton.setOnClickListener {
|
binding.remoteOpenButton.setOnClickListener {
|
||||||
openPlayStoreOnWearDevicesWithoutApp()
|
openPlayStoreOnWearDevicesWithoutApp()
|
||||||
|
@ -81,13 +96,13 @@ class SettingsWearActivity : AppCompatActivity(), CapabilityClient.OnCapabilityC
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
capabilityClient.removeListener(this, CAPABILITY_WEAR_APP)
|
capabilityClient?.removeListener(this, CAPABILITY_WEAR_APP)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
title = getString(commonR.string.wear_os_settings_title)
|
title = getString(commonR.string.wear_os_settings_title)
|
||||||
capabilityClient.addListener(this, CAPABILITY_WEAR_APP)
|
capabilityClient?.addListener(this, CAPABILITY_WEAR_APP)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -106,11 +121,11 @@ class SettingsWearActivity : AppCompatActivity(), CapabilityClient.OnCapabilityC
|
||||||
private suspend fun findWearDevicesWithApp() {
|
private suspend fun findWearDevicesWithApp() {
|
||||||
try {
|
try {
|
||||||
val capabilityInfo = capabilityClient
|
val capabilityInfo = capabilityClient
|
||||||
.getCapability(CAPABILITY_WEAR_APP, CapabilityClient.FILTER_ALL)
|
?.getCapability(CAPABILITY_WEAR_APP, CapabilityClient.FILTER_ALL)
|
||||||
.await()
|
?.await()
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
wearNodesWithApp = capabilityInfo.nodes
|
wearNodesWithApp = capabilityInfo?.nodes
|
||||||
Log.d(TAG, "Capable Nodes: $wearNodesWithApp")
|
Log.d(TAG, "Capable Nodes: $wearNodesWithApp")
|
||||||
updateUI()
|
updateUI()
|
||||||
}
|
}
|
||||||
|
@ -124,7 +139,7 @@ class SettingsWearActivity : AppCompatActivity(), CapabilityClient.OnCapabilityC
|
||||||
|
|
||||||
private suspend fun findAllWearDevices() {
|
private suspend fun findAllWearDevices() {
|
||||||
try {
|
try {
|
||||||
val connectedNodes = nodeClient.connectedNodes.await()
|
val connectedNodes = nodeClient?.connectedNodes?.await()
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
allConnectedNodes = connectedNodes
|
allConnectedNodes = connectedNodes
|
||||||
|
@ -187,11 +202,11 @@ class SettingsWearActivity : AppCompatActivity(), CapabilityClient.OnCapabilityC
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
remoteActivityHelper
|
remoteActivityHelper
|
||||||
.startRemoteActivity(
|
?.startRemoteActivity(
|
||||||
targetIntent = intent,
|
targetIntent = intent,
|
||||||
targetNodeId = node.id
|
targetNodeId = node.id
|
||||||
)
|
)
|
||||||
.await()
|
?.await()
|
||||||
|
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
this@SettingsWearActivity,
|
this@SettingsWearActivity,
|
||||||
|
|
|
@ -84,7 +84,11 @@ class SettingsWearViewModel @Inject constructor(
|
||||||
val resultSnackbar = _resultSnackbar.asSharedFlow()
|
val resultSnackbar = _resultSnackbar.asSharedFlow()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
Wearable.getDataClient(application).addListener(this)
|
try {
|
||||||
|
Wearable.getDataClient(application).addListener(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to get wearable data client", e)
|
||||||
|
}
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
try {
|
try {
|
||||||
val capabilityInfo = Wearable.getCapabilityClient(application)
|
val capabilityInfo = Wearable.getCapabilityClient(application)
|
||||||
|
@ -113,7 +117,11 @@ class SettingsWearViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
Wearable.getDataClient(getApplication<HomeAssistantApplication>()).removeListener(this)
|
try {
|
||||||
|
Wearable.getDataClient(getApplication<HomeAssistantApplication>()).removeListener(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to remove listener from wearable data client", e)
|
||||||
|
}
|
||||||
|
|
||||||
if (serverId != 0) {
|
if (serverId != 0) {
|
||||||
CoroutineScope(Dispatchers.Main + Job()).launch {
|
CoroutineScope(Dispatchers.Main + Job()).launch {
|
||||||
|
@ -218,15 +226,18 @@ class SettingsWearViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
val app = getApplication<HomeAssistantApplication>()
|
val app = getApplication<HomeAssistantApplication>()
|
||||||
Wearable.getDataClient(app).putDataItem(putDataRequest).apply {
|
try {
|
||||||
addOnSuccessListener { Log.d(TAG, "Successfully sent auth to wear") }
|
Wearable.getDataClient(app).putDataItem(putDataRequest).apply {
|
||||||
addOnFailureListener { e ->
|
addOnSuccessListener { Log.d(TAG, "Successfully sent auth to wear") }
|
||||||
Log.e(TAG, "Failed to send auth to wear", e)
|
addOnFailureListener { e ->
|
||||||
_hasData.value = true
|
Log.e(TAG, "Failed to send auth to wear", e)
|
||||||
viewModelScope.launch {
|
_hasData.value = true
|
||||||
_resultSnackbar.emit(app.getString(commonR.string.failed_watch_connection))
|
watchConnectionError(app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to send auth to wear", e)
|
||||||
|
watchConnectionError(app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,9 +249,14 @@ class SettingsWearViewModel @Inject constructor(
|
||||||
asPutDataRequest()
|
asPutDataRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
Wearable.getDataClient(getApplication<HomeAssistantApplication>()).putDataItem(putDataRequest).apply {
|
try {
|
||||||
addOnSuccessListener { Log.d(TAG, "Successfully sent tile template to wear") }
|
Wearable.getDataClient(getApplication<HomeAssistantApplication>())
|
||||||
addOnFailureListener { e -> Log.e(TAG, "Failed to send tile template to wear", e) }
|
.putDataItem(putDataRequest).apply {
|
||||||
|
addOnSuccessListener { Log.d(TAG, "Successfully sent tile template to wear") }
|
||||||
|
addOnFailureListener { e -> Log.e(TAG, "Failed to send tile template to wear", e) }
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Unable to send template tile to wear", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,4 +364,10 @@ class SettingsWearViewModel @Inject constructor(
|
||||||
|
|
||||||
authenticateId = null
|
authenticateId = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun watchConnectionError(app: HomeAssistantApplication) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
_resultSnackbar.emit(app.getString(commonR.string.failed_watch_connection))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue