UI: Keep location service connection until result is available

This commit is contained in:
Marvin W 2022-01-23 20:53:14 +01:00
parent 0b41056020
commit 2fc74cf100
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A

View file

@ -118,42 +118,66 @@ class BackendDetailsFragment : Fragment(R.layout.backend_details), BackendDetail
} }
if (entry.type == LOCATION && entry.enabled.get()) { if (entry.type == LOCATION && entry.enabled.get()) {
if (updateInProgress) return if (updateInProgress) return
locationClient.connect()
updateInProgress = true updateInProgress = true
try {
val locationTemp = locationClient.getLastLocationForBackend(entry.serviceInfo.packageName, entry.serviceInfo.name, entry.firstSignatureDigest) val locationTemp = locationClient.getLastLocationForBackend(
val location = when (locationTemp) { entry.serviceInfo.packageName,
null -> { entry.serviceInfo.name,
delay(500L) // Wait short time to ensure backend was activated entry.firstSignatureDigest
Log.d(TAG, "Location was not available, requesting once") )
locationClient.forceLocationUpdate() val location = when (locationTemp) {
val secondAttempt = locationClient.getLastLocationForBackend(entry.serviceInfo.packageName, entry.serviceInfo.name, entry.firstSignatureDigest) null -> {
if (secondAttempt == null) { delay(500L) // Wait short time to ensure backend was activated
Log.d(TAG, "Location still not available, waiting or giving up") Log.d(TAG, "Location was not available, requesting once")
delay(WAIT_FOR_RESULT) locationClient.forceLocationUpdate()
locationClient.getLastLocationForBackend(entry.serviceInfo.packageName, entry.serviceInfo.name, entry.firstSignatureDigest) val secondAttempt = locationClient.getLastLocationForBackend(
} else { entry.serviceInfo.packageName,
secondAttempt entry.serviceInfo.name,
entry.firstSignatureDigest
)
if (secondAttempt == null) {
Log.d(TAG, "Location still not available, waiting or giving up")
delay(WAIT_FOR_RESULT)
locationClient.getLastLocationForBackend(
entry.serviceInfo.packageName,
entry.serviceInfo.name,
entry.firstSignatureDigest
)
} else {
secondAttempt
}
} }
} else -> locationTemp
else -> locationTemp } ?: return
} ?: return var locationString =
var locationString = "${location.latitude.toStringWithDigits(6)}, ${location.longitude.toStringWithDigits(6)}" "${location.latitude.toStringWithDigits(6)}, ${location.longitude.toStringWithDigits(6)}"
val address = geocodeClient.requestReverseGeocode(ReverseGeocodeRequest(LatLon(location.latitude, location.longitude))).singleOrNull() val address = geocodeClient.requestReverseGeocode(
if (address != null) { ReverseGeocodeRequest(
val addressLine = StringBuilder() LatLon(
var i = 0 location.latitude,
addressLine.append(address.getAddressLine(i)) location.longitude
while (addressLine.length < 10 && address.maxAddressLineIndex > i) { )
i++ )
addressLine.append(", ") ).singleOrNull()
if (address != null) {
val addressLine = StringBuilder()
var i = 0
addressLine.append(address.getAddressLine(i)) addressLine.append(address.getAddressLine(i))
while (addressLine.length < 10 && address.maxAddressLineIndex > i) {
i++
addressLine.append(", ")
addressLine.append(address.getAddressLine(i))
}
locationString = addressLine.toString()
} }
locationString = addressLine.toString() binding.lastLocationString = locationString
binding.executePendingBindings()
} finally {
locationClient.disconnect()
updateInProgress = false
} }
updateInProgress = false
binding.lastLocationString = locationString
binding.executePendingBindings()
} else { } else {
Log.d(TAG, "Location is not available for this backend (type: ${entry.type}, enabled ${entry.enabled.get()}") Log.d(TAG, "Location is not available for this backend (type: ${entry.type}, enabled ${entry.enabled.get()}")
binding.lastLocationString = "" binding.lastLocationString = ""