Skip to content

Commit

Permalink
Add location observer
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Oct 30, 2021
1 parent 9de4130 commit b5bfe7d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
applicationId "com.hicham.flowlifecycle"
minSdk 21
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -37,6 +37,9 @@ dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
implementation "androidx.fragment:fragment-ktx:1.3.6"
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
testImplementation 'junit:junit:4.13.2'
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hicham.flowlifecycle">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
55 changes: 55 additions & 0 deletions app/src/main/java/com/hicham/flowlifecycle/LocationObserver.kt
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)
}
}
}
}

0 comments on commit b5bfe7d

Please sign in to comment.