47 lines
1.3 KiB
Kotlin
47 lines
1.3 KiB
Kotlin
package com.module.breeze
|
|
|
|
import android.Manifest
|
|
import android.content.Context
|
|
import android.content.pm.PackageManager
|
|
import android.location.Location
|
|
import android.location.LocationListener
|
|
import android.location.LocationManager
|
|
import android.os.Bundle
|
|
import androidx.core.content.ContextCompat
|
|
|
|
// AI
|
|
class GpsRepository(private val context: Context) : LocationListener {
|
|
private var locationManager: LocationManager? = null
|
|
private var onLocationChanged: ((Location) -> Unit)? = null
|
|
|
|
fun startListening(onLocationChanged: (Location) -> Unit) {
|
|
this.onLocationChanged = onLocationChanged
|
|
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
|
|
if (ContextCompat.checkSelfPermission(
|
|
context,
|
|
Manifest.permission.ACCESS_FINE_LOCATION,
|
|
) == PackageManager.PERMISSION_GRANTED
|
|
) {
|
|
locationManager?.requestLocationUpdates(
|
|
LocationManager.GPS_PROVIDER,
|
|
1000L,
|
|
1f,
|
|
this,
|
|
)
|
|
}
|
|
}
|
|
|
|
fun stopListening() {
|
|
locationManager?.removeUpdates(this)
|
|
}
|
|
|
|
override fun onLocationChanged(location: Location) {
|
|
onLocationChanged?.invoke(location)
|
|
}
|
|
|
|
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
|
|
override fun onProviderEnabled(provider: String) {}
|
|
override fun onProviderDisabled(provider: String) {}
|
|
}
|