Skip to content

Commit

Permalink
Avoid exposing location mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
nhoxbypass committed Jul 12, 2020
1 parent 63066eb commit fa24072
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,37 @@ import com.iceteaviet.fastfoodfinder.location.base.ILocationManager
/**
* Created by tom on 2019-05-01.
*/
open class GoogleLocationManager private constructor(context: Context) : AbsLocationManager<LocationListener>(context), ILocationManager<LocationListener>, com.google.android.gms.location.LocationListener, GoogleApiClient.ConnectionCallbacks {
open class GoogleLocationManager private constructor(context: Context)
: AbsLocationManager<LocationListener>(context), ILocationManager<LocationListener> {

private var locationRequest: LocationRequest? = null
private var googleApiClient: GoogleApiClient? = null

/**
* Will be trigger by Google Fused Location Api & manual when Location Api service connected
*/
private val googleLocationListener = com.google.android.gms.location.LocationListener { location ->
currLocation = location
for (listener in listeners) {
listener.onLocationChanged(LatLngAlt(location.latitude, location.longitude, location.altitude))
}
}

private val clientConnectionCallbacks = object : GoogleApiClient.ConnectionCallbacks {
override fun onConnected(extras: Bundle?) {
connected = true
currLocation = getLastLocation()
currLocation?.let {
googleLocationListener.onLocationChanged(it)
}
}

override fun onConnectionSuspended(i: Int) {
onFailed(FailType.GOOGLE_PLAY_SERVICES_CONNECTION_FAIL)
connected = false
}
}

override fun initLocationProvider(context: Context) {
locationRequest = createLocationRequest()
googleApiClient = createGoogleApiClient(context)
Expand All @@ -33,37 +59,13 @@ open class GoogleLocationManager private constructor(context: Context) : AbsLoca
@SuppressLint("MissingPermission")
override fun requestLocationUpdates() {
if (isConnected() && !isRequestingLocationUpdate())
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this)
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, googleLocationListener)
}

override fun terminate() {
super.terminate()
googleApiClient?.disconnect()
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this)
}

override fun onConnected(extras: Bundle?) {
connected = true
currLocation = getLastLocation()
currLocation?.let {
onLocationChanged(it)
}
}

override fun onConnectionSuspended(i: Int) {
onFailed(FailType.GOOGLE_PLAY_SERVICES_CONNECTION_FAIL)
connected = false
}

/**
* Will be trigger by Google Fused Location Api & manual when Location Api service connected
*/
override fun onLocationChanged(location: Location) {
currLocation = location

for (listener in listeners) {
listener.onLocationChanged(LatLngAlt(location.latitude, location.longitude, location.altitude))
}
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, googleLocationListener)
}


Expand All @@ -81,7 +83,7 @@ open class GoogleLocationManager private constructor(context: Context) : AbsLoca

private fun createGoogleApiClient(context: Context): GoogleApiClient {
return GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addConnectionCallbacks(clientConnectionCallbacks)
.addOnConnectionFailedListener {
onFailed(FailType.GOOGLE_PLAY_SERVICES_CONNECTION_FAIL)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.iceteaviet.fastfoodfinder.location
import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import android.location.LocationManager
import android.os.Bundle
import com.iceteaviet.fastfoodfinder.androidext.getLocationManager
import com.iceteaviet.fastfoodfinder.location.base.AbsLocationManager
Expand All @@ -12,13 +13,41 @@ import com.iceteaviet.fastfoodfinder.location.base.ILocationManager
/**
* Created by tom on 2019-05-01.
*/
open class SystemLocationManager private constructor(context: Context) : AbsLocationManager<SystemLocationListener>(context), ILocationManager<SystemLocationListener>, android.location.LocationListener {
open class SystemLocationManager private constructor(context: Context) : AbsLocationManager<SystemLocationListener>(context), ILocationManager<SystemLocationListener> {

private var locationManager: android.location.LocationManager? = null
private var locationManager: LocationManager? = null

private var minTime: Long = MIN_TIME_BW_UPDATES
private var minDistance: Float = MIN_DISTANCE_CHANGE_FOR_UPDATES

private val androidLocationListener = object : android.location.LocationListener {
override fun onLocationChanged(location: Location) {
for (listener in listeners) {
listener.onLocationChanged(LatLngAlt(location.latitude, location.longitude, location.altitude))
}

currLocation = location
}

override fun onProviderDisabled(provider: String) {
for (listener in listeners) {
listener.onProviderDisabled(provider)
}
}

override fun onProviderEnabled(provider: String) {
for (listener in listeners) {
listener.onProviderEnabled(provider)
}
}

override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
for (listener in listeners) {
listener.onStatusChanged(provider, status, extras)
}
}
}

init {
currLocation = getLastLocation()
connected = true
Expand All @@ -33,19 +62,19 @@ open class SystemLocationManager private constructor(context: Context) : AbsLoca
override fun getLastLocation(): Location? {
// Get GPS and network status
var location: Location? = null
val isGPSEnabled = locationManager!!.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager!!.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)
val isGPSEnabled = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

if (isNetworkEnabled) {
location = locationManager!!.getLastKnownLocation(android.location.LocationManager.NETWORK_PROVIDER)
location = locationManager!!.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
}

if (isGPSEnabled) {
location = locationManager!!.getLastKnownLocation(android.location.LocationManager.GPS_PROVIDER)
location = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
}

if (location == null)
location = Location(android.location.LocationManager.PASSIVE_PROVIDER)
location = Location(LocationManager.PASSIVE_PROVIDER)

return location
}
Expand All @@ -56,45 +85,19 @@ open class SystemLocationManager private constructor(context: Context) : AbsLoca
return

// Get GPS and network status
val isGPSEnabled = locationManager!!.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager!!.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)
val isGPSEnabled = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

if (isNetworkEnabled) {
locationManager?.requestLocationUpdates(android.location.LocationManager.NETWORK_PROVIDER,
locationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
minTime,
minDistance, this)
minDistance, androidLocationListener)
}

if (isGPSEnabled) {
locationManager?.requestLocationUpdates(android.location.LocationManager.GPS_PROVIDER,
locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime,
minDistance, this)
}
}

override fun onLocationChanged(location: Location) {
for (listener in listeners) {
listener.onLocationChanged(LatLngAlt(location.latitude, location.longitude, location.altitude))
}

currLocation = location
}

override fun onProviderDisabled(provider: String) {
for (listener in listeners) {
listener.onProviderDisabled(provider)
}
}

override fun onProviderEnabled(provider: String) {
for (listener in listeners) {
listener.onProviderEnabled(provider)
}
}

override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
for (listener in listeners) {
listener.onStatusChanged(provider, status, extras)
minDistance, androidLocationListener)
}
}

Expand Down

0 comments on commit fa24072

Please sign in to comment.