From 10a34ead17340bf56d1005aa5702dbd592bc0571 Mon Sep 17 00:00:00 2001 From: birjuvachhani Date: Sun, 5 Jun 2022 13:37:25 +0530 Subject: [PATCH] Fix #65 (cherry picked from commit 1085bb61f34d3b3f6ef92ffca968ba8c12e99bfa) --- .../com/birjuvachhani/locus/LocationProvider.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/locus/src/main/java/com/birjuvachhani/locus/LocationProvider.kt b/locus/src/main/java/com/birjuvachhani/locus/LocationProvider.kt index bb2a6c2..a58fd06 100644 --- a/locus/src/main/java/com/birjuvachhani/locus/LocationProvider.kt +++ b/locus/src/main/java/com/birjuvachhani/locus/LocationProvider.kt @@ -21,6 +21,8 @@ import android.content.Context import android.os.Looper import androidx.lifecycle.MutableLiveData import com.google.android.gms.location.* +import com.google.android.gms.tasks.CancellationToken +import com.google.android.gms.tasks.OnTokenCanceledListener import java.util.concurrent.atomic.AtomicBoolean /* @@ -94,7 +96,11 @@ internal class LocationProvider(context: Context) { onUpdate(LocusResult.error(error = error)) } } - mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location -> + + mFusedLocationProviderClient.getCurrentLocation( + request.priority, + EmptyCancellationToken() + ).addOnSuccessListener { location -> location?.let { onUpdate(LocusResult.success(it)) } }.addOnFailureListener { logError(it) @@ -112,4 +118,10 @@ internal class LocationProvider(context: Context) { locationLiveData = MutableLiveData() mFusedLocationProviderClient.removeLocationUpdates(pendingIntent) } +} + +class EmptyCancellationToken : CancellationToken() { + override fun onCanceledRequested(p0: OnTokenCanceledListener): CancellationToken = this + + override fun isCancellationRequested(): Boolean = false } \ No newline at end of file