|
|
|
@ -3,20 +3,12 @@ package net.pokeranalytics.android.util |
|
|
|
import android.Manifest.permission.ACCESS_FINE_LOCATION |
|
|
|
import android.Manifest.permission.ACCESS_FINE_LOCATION |
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
import android.content.pm.PackageManager |
|
|
|
import android.content.pm.PackageManager |
|
|
|
|
|
|
|
import android.os.Looper |
|
|
|
import androidx.core.content.ContextCompat |
|
|
|
import androidx.core.content.ContextCompat |
|
|
|
import com.google.android.gms.common.api.ApiException |
|
|
|
import com.google.android.gms.location.* |
|
|
|
import com.google.android.gms.location.FusedLocationProviderClient |
|
|
|
|
|
|
|
import com.google.android.gms.location.LocationServices |
|
|
|
|
|
|
|
import com.google.android.libraries.places.api.Places |
|
|
|
|
|
|
|
import com.google.android.libraries.places.api.model.Place |
|
|
|
|
|
|
|
import com.google.android.libraries.places.api.model.PlaceLikelihood |
|
|
|
|
|
|
|
import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest |
|
|
|
|
|
|
|
import io.realm.Realm |
|
|
|
import io.realm.Realm |
|
|
|
import io.realm.kotlin.where |
|
|
|
import io.realm.kotlin.where |
|
|
|
import net.pokeranalytics.android.model.realm.Location |
|
|
|
import net.pokeranalytics.android.model.realm.Location |
|
|
|
import timber.log.Timber |
|
|
|
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
import kotlin.collections.ArrayList |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocationManager(private var context: Context) { |
|
|
|
class LocationManager(private var context: Context) { |
|
|
|
|
|
|
|
|
|
|
|
@ -27,46 +19,46 @@ class LocationManager(private var context: Context) { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Return the [places] around the user |
|
|
|
* Return the [places] around the user |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun askForPlacesRequest(callback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)?) { |
|
|
|
// fun askForPlacesRequest(callback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)?) { |
|
|
|
|
|
|
|
// |
|
|
|
// Initialize Places. |
|
|
|
// // Initialize Places. |
|
|
|
Places.initialize(context, context.getString(net.pokeranalytics.android.R.string.google_places_api)) |
|
|
|
// Places.initialize(context, context.getString(net.pokeranalytics.android.R.string.google_places_api)) |
|
|
|
|
|
|
|
// |
|
|
|
// Create a new Places client instance. |
|
|
|
// // Create a new Places client instance. |
|
|
|
val placesClient = Places.createClient(context) |
|
|
|
// val placesClient = Places.createClient(context) |
|
|
|
|
|
|
|
// |
|
|
|
// Use fields to define the data types to return. |
|
|
|
// // Use fields to define the data types to return. |
|
|
|
val placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG) |
|
|
|
// val placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG) |
|
|
|
|
|
|
|
// |
|
|
|
// Use the builder to create a FindCurrentPlaceRequest. |
|
|
|
// // Use the builder to create a FindCurrentPlaceRequest. |
|
|
|
val request = FindCurrentPlaceRequest.builder(placeFields).build() |
|
|
|
// val request = FindCurrentPlaceRequest.builder(placeFields).build() |
|
|
|
|
|
|
|
// |
|
|
|
// Call findCurrentPlace and handle the response (first check that the user has granted permission). |
|
|
|
// // Call findCurrentPlace and handle the response (first check that the user has granted permission). |
|
|
|
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
// if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
val placeResponse = placesClient.findCurrentPlace(request) |
|
|
|
// val placeResponse = placesClient.findCurrentPlace(request) |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
// |
|
|
|
placeResponse.addOnCompleteListener { task -> |
|
|
|
// placeResponse.addOnCompleteListener { task -> |
|
|
|
val places = ArrayList<PlaceLikelihood>() |
|
|
|
// val places = ArrayList<PlaceLikelihood>() |
|
|
|
if (task.isSuccessful) { |
|
|
|
// if (task.isSuccessful) { |
|
|
|
val response = task.result |
|
|
|
// val response = task.result |
|
|
|
response?.placeLikelihoods?.let { |
|
|
|
// response?.placeLikelihoods?.let { |
|
|
|
places.addAll(it) |
|
|
|
// places.addAll(it) |
|
|
|
} |
|
|
|
// } |
|
|
|
} else { |
|
|
|
// } else { |
|
|
|
val exception = task.exception |
|
|
|
// val exception = task.exception |
|
|
|
if (exception is ApiException) { |
|
|
|
// if (exception is ApiException) { |
|
|
|
Timber.d("Error: ${"Place not found: " + exception.statusCode}") |
|
|
|
// Timber.d("Error: ${"Place not found: " + exception.statusCode}") |
|
|
|
} |
|
|
|
// } |
|
|
|
} |
|
|
|
// } |
|
|
|
|
|
|
|
// |
|
|
|
callback?.invoke(task.isSuccessful, places) |
|
|
|
// callback?.invoke(task.isSuccessful, places) |
|
|
|
} |
|
|
|
// } |
|
|
|
} else { |
|
|
|
// } else { |
|
|
|
// If we don't have the permission, return |
|
|
|
// // If we don't have the permission, return |
|
|
|
callback?.invoke(false, ArrayList()) |
|
|
|
// callback?.invoke(false, ArrayList()) |
|
|
|
} |
|
|
|
// } |
|
|
|
} |
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Return if the database contains one or many locations with coordinates |
|
|
|
* Return if the database contains one or many locations with coordinates |
|
|
|
@ -171,4 +163,42 @@ class LocationManager(private var context: Context) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val fusedLocationClient: FusedLocationProviderClient = |
|
|
|
|
|
|
|
LocationServices.getFusedLocationProviderClient(context) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*** |
|
|
|
|
|
|
|
* Finds and returns the current user location |
|
|
|
|
|
|
|
* Gets one update then stops |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun getSingleLocation(callback: ((location: android.location.Location?) -> Unit)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var locationResultReceived = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val locationCallback = object : LocationCallback() { |
|
|
|
|
|
|
|
override fun onLocationResult(locationResult: LocationResult?) { |
|
|
|
|
|
|
|
super.onLocationResult(locationResult) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
locationResult?.locations?.let { |
|
|
|
|
|
|
|
if (!locationResultReceived && it.isNotEmpty()) { |
|
|
|
|
|
|
|
locationResultReceived = true |
|
|
|
|
|
|
|
callback(it.first()) |
|
|
|
|
|
|
|
fusedLocationClient.removeLocationUpdates(this) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val locationRequest = LocationRequest() |
|
|
|
|
|
|
|
locationRequest.interval = 200L |
|
|
|
|
|
|
|
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper()) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// If we don't have the permission, return null |
|
|
|
|
|
|
|
callback.invoke(null) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |