Skip to content

Instantly share code, notes, and snippets.

@manorgass
Created March 28, 2021 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manorgass/2356a44dacd3cec63d481c124da6ad72 to your computer and use it in GitHub Desktop.
Save manorgass/2356a44dacd3cec63d481c124da6ad72 to your computer and use it in GitHub Desktop.
package com.example.fusedlocationtest.provider
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.util.Log
import androidx.core.app.ActivityCompat
import com.example.fusedlocationtest.OnLocationUpdateListener
class LegacyLocationProvider(
private val context: Context,
private val listener: OnLocationUpdateListener
) {
private val locationManager: LocationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
private val locationListenerExt = LocationListenerExt()
fun startLocationUpdates(type: ProviderType) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
locationManager.requestLocationUpdates(
type.value,
1000,
1f,
locationListenerExt
)
}
fun stopLocationUpdates() {
locationManager.removeUpdates(locationListenerExt)
}
companion object {
private const val TAG = "LegacyLocationManager"
}
inner class LocationListenerExt : LocationListener {
override fun onLocationChanged(location: Location) {
Log.d(TAG, "Location Changed!")
listener.onLocationUpdated(location)
}
}
enum class ProviderType(val value: String) {
NETWORK(LocationManager.NETWORK_PROVIDER),
GPS(LocationManager.GPS_PROVIDER)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment