-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9de4130
commit b5bfe7d
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/com/hicham/flowlifecycle/LocationObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.hicham.flowlifecycle | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.location.Location | ||
import android.os.Looper | ||
import android.util.Log | ||
import androidx.annotation.RequiresPermission | ||
import com.google.android.gms.location.LocationCallback | ||
import com.google.android.gms.location.LocationRequest | ||
import com.google.android.gms.location.LocationResult | ||
import com.google.android.gms.location.LocationServices | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
|
||
private const val TAG = "LocationObserver" | ||
|
||
class LocationObserver(private val context: Context) { | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) | ||
fun observeLocationUpdates(): Flow<Location> { | ||
return callbackFlow { | ||
Log.d(TAG, "observing location updates") | ||
|
||
val client = LocationServices.getFusedLocationProviderClient(context) | ||
val locationRequest = LocationRequest | ||
.create() | ||
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) | ||
.setInterval(0) | ||
.setFastestInterval(0) | ||
|
||
val locationCallback = object : LocationCallback() { | ||
override fun onLocationResult(locationResult: LocationResult?) { | ||
if (locationResult != null) { | ||
Log.d(TAG, "got location ${locationResult.lastLocation}") | ||
trySend(locationResult.lastLocation) | ||
} | ||
} | ||
} | ||
|
||
client.requestLocationUpdates( | ||
locationRequest, | ||
locationCallback, | ||
Looper.getMainLooper() | ||
) | ||
|
||
awaitClose { | ||
Log.d(TAG, "stop observing location updates") | ||
client.removeLocationUpdates(locationCallback) | ||
} | ||
} | ||
} | ||
} |