Skip to content

Commit

Permalink
Prevent a crash when viewing the GNSS UI when no GPS provider is avai…
Browse files Browse the repository at this point in the history
…lable on the device
  • Loading branch information
christianrowlands committed Sep 30, 2024
1 parent 2865ef5 commit b3c368a
Showing 1 changed file with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.SharedPreferences
import android.location.Location
import android.location.LocationManager
import android.os.Build
import android.util.Log
import androidx.core.location.LocationListenerCompat
import com.craxiom.networksurvey.R
Expand All @@ -19,6 +20,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.shareIn
import timber.log.Timber

private const val TAG = "SharedLocationManager"

Expand Down Expand Up @@ -65,13 +67,26 @@ class SharedLocationManager(
_receivingLocationUpdates.value = true

try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
minTimeMillis(context, prefs),
minDistance(context, prefs),
callback,
context.mainLooper
)
val hasGspProvider: Boolean
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
hasGspProvider = locationManager.hasProvider(LocationManager.GPS_PROVIDER)
} else {
hasGspProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER) != null
}

if (hasGspProvider) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
minTimeMillis(context, prefs),
minDistance(context, prefs),
callback,
context.mainLooper
)
} else {
Timber.e("No GPS provider available")
close()

}
} catch (e: Exception) {
Log.e(TAG, "Exception in location flow: $e")
close(e) // in case of exception, close the Flow
Expand Down

0 comments on commit b3c368a

Please sign in to comment.