From 3cea066d9c36f2b96f4c0c4fad696550242f27d7 Mon Sep 17 00:00:00 2001 From: barbeau Date: Thu, 27 Apr 2023 15:14:10 -0400 Subject: [PATCH] fix: Use LocationListenerCompat to avoid crashes on API <=30 Fixes #627 --- .../library/data/SharedLocationManager.kt | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/library/src/main/java/com/android/gpstest/library/data/SharedLocationManager.kt b/library/src/main/java/com/android/gpstest/library/data/SharedLocationManager.kt index fe34ed97f..41482423c 100644 --- a/library/src/main/java/com/android/gpstest/library/data/SharedLocationManager.kt +++ b/library/src/main/java/com/android/gpstest/library/data/SharedLocationManager.kt @@ -20,10 +20,9 @@ import android.annotation.SuppressLint import android.content.Context import android.content.SharedPreferences import android.location.Location -import android.location.LocationListener import android.location.LocationManager -import android.os.Bundle import android.util.Log +import androidx.core.location.LocationListenerCompat import com.android.gpstest.library.util.PreferenceUtil.minDistance import com.android.gpstest.library.util.PreferenceUtil.minTimeMillis import com.android.gpstest.library.util.hasPermission @@ -54,25 +53,11 @@ class SharedLocationManager constructor( @SuppressLint("MissingPermission") private val _locationUpdates = callbackFlow { val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager - // Implementation of unused methods is still required for API Level 30 and lower (#627) - @Suppress("RedundantOverride") val callback = object : LocationListener { - override fun onLocationChanged(location: Location) { - //Log.d(TAG, "New location: ${location.toNotificationTitle()}") - // Send the new location to the Flow observers - trySend(location) - } - override fun onProviderDisabled(provider: String) { - super.onProviderDisabled(provider) - } - - override fun onProviderEnabled(provider: String) { - super.onProviderEnabled(provider) - } - - @Deprecated("Deprecated in Java") - override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { - super.onStatusChanged(provider, status, extras) - } + // Use LocationListenerCompat to avoid crashes on API Level 30 and lower (#627) + val callback = LocationListenerCompat { location -> + //Log.d(TAG, "New location: ${location.toNotificationTitle()}") + // Send the new location to the Flow observers + trySend(location) } if (!context.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION) ||