Skip to content

Commit

Permalink
fix: Use LocationListenerCompat to avoid crashes on API <=30
Browse files Browse the repository at this point in the history
Fixes #627
  • Loading branch information
barbeau committed Apr 27, 2023
1 parent 74645d7 commit 3cea066
Showing 1 changed file with 6 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) ||
Expand Down

0 comments on commit 3cea066

Please sign in to comment.