Created
March 28, 2021 11:42
-
-
Save manorgass/2356a44dacd3cec63d481c124da6ad72 to your computer and use it in GitHub Desktop.
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
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