47 lines
1.5 KiB
Kotlin
47 lines
1.5 KiB
Kotlin
package ru.officialdakari.fmd
|
|
|
|
import android.app.Application
|
|
import android.content.Context
|
|
import android.location.Location
|
|
import android.location.LocationListener
|
|
import android.location.LocationManager
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
import ru.officialdakari.fmd.executors.ThreadPerTaskExecutor
|
|
import java.util.concurrent.Executor
|
|
|
|
class LocationUtils(val context: Context) {
|
|
private var locationManager: LocationManager? = null
|
|
private lateinit var loc: Location
|
|
public fun start() {
|
|
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
}
|
|
|
|
public fun isStarted(): Boolean {
|
|
return locationManager != null
|
|
}
|
|
|
|
public fun getLocation(gotLocation: (Location) -> Unit) {
|
|
try {
|
|
locationManager!!.getCurrentLocation(
|
|
LocationManager.GPS_PROVIDER,
|
|
null,
|
|
ThreadPerTaskExecutor(),
|
|
{ location ->
|
|
gotLocation(location)
|
|
}
|
|
)
|
|
} catch (ex: SecurityException) {
|
|
|
|
}
|
|
}
|
|
|
|
private val locationListener: LocationListener = object : LocationListener {
|
|
override fun onLocationChanged(location: Location) {
|
|
this@LocationUtils.loc = location
|
|
}
|
|
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
|
|
override fun onProviderEnabled(provider: String) {}
|
|
override fun onProviderDisabled(provider: String) {}
|
|
}
|
|
} |