Location GPS provider

Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
ale
2025-07-14 03:00:02 +02:00
padre c62ac6d2d3
commit ebd7f0d783

Ver fichero

@@ -2,6 +2,7 @@ package com.manalejandro.location
import android.content.Context import android.content.Context
import android.location.Location import android.location.Location
import android.location.LocationManager
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
class LocationService(private val context: Context) { class LocationService(private val context: Context) {
@@ -24,7 +25,7 @@ class LocationService(private val context: Context) {
} }
// Si no hay ubicación simulada, devolver una ubicación predeterminada que Google tendría guardada (Madrid) // Si no hay ubicación simulada, devolver una ubicación predeterminada que Google tendría guardada (Madrid)
return Location("google_provider").apply { return Location(LocationManager.GPS_PROVIDER).apply {
latitude = 40.4168 latitude = 40.4168
longitude = -3.7038 longitude = -3.7038
time = System.currentTimeMillis() - (1000 * 60 * 60 * 2) // 2 horas atrás time = System.currentTimeMillis() - (1000 * 60 * 60 * 2) // 2 horas atrás
@@ -47,7 +48,7 @@ class LocationService(private val context: Context) {
*/ */
suspend fun mockLocation(latitude: Double, longitude: Double): Boolean { suspend fun mockLocation(latitude: Double, longitude: Double): Boolean {
return try { return try {
val preferences = context.getSharedPreferences("google_provider", Context.MODE_PRIVATE) val preferences = context.getSharedPreferences(LocationManager.GPS_PROVIDER, Context.MODE_PRIVATE)
preferences.edit() preferences.edit()
.putString("latitude", latitude.toString()) .putString("latitude", latitude.toString())
.putString("longitude", longitude.toString()) .putString("longitude", longitude.toString())
@@ -66,12 +67,12 @@ class LocationService(private val context: Context) {
* Obtiene la ubicación simulada guardada * Obtiene la ubicación simulada guardada
*/ */
fun getMockedLocation(): Location? { fun getMockedLocation(): Location? {
val preferences = context.getSharedPreferences("google_provider", Context.MODE_PRIVATE) val preferences = context.getSharedPreferences(LocationManager.GPS_PROVIDER, Context.MODE_PRIVATE)
val latStr = preferences.getString("latitude", null) val latStr = preferences.getString("latitude", null)
val lngStr = preferences.getString("longitude", null) val lngStr = preferences.getString("longitude", null)
return if (latStr != null && lngStr != null) { return if (latStr != null && lngStr != null) {
Location("google_provider").apply { Location(LocationManager.GPS_PROVIDER).apply {
latitude = latStr.toDouble() latitude = latStr.toDouble()
longitude = lngStr.toDouble() longitude = lngStr.toDouble()
time = preferences.getLong("timestamp", System.currentTimeMillis()) time = preferences.getLong("timestamp", System.currentTimeMillis())
@@ -90,7 +91,7 @@ class LocationService(private val context: Context) {
val offsetLat = (random.nextDouble() - 0.5) * 2 * (radiusKm / 111.0) // 1 grado lat ≈ 111 km val offsetLat = (random.nextDouble() - 0.5) * 2 * (radiusKm / 111.0) // 1 grado lat ≈ 111 km
val offsetLng = (random.nextDouble() - 0.5) * 2 * (radiusKm / (111.0 * kotlin.math.cos(Math.toRadians(baseLatitude)))) val offsetLng = (random.nextDouble() - 0.5) * 2 * (radiusKm / (111.0 * kotlin.math.cos(Math.toRadians(baseLatitude))))
return Location("google_provider").apply { return Location(LocationManager.GPS_PROVIDER).apply {
latitude = baseLatitude + offsetLat latitude = baseLatitude + offsetLat
longitude = baseLongitude + offsetLng longitude = baseLongitude + offsetLng
time = System.currentTimeMillis() time = System.currentTimeMillis()
@@ -103,31 +104,31 @@ class LocationService(private val context: Context) {
*/ */
fun getPopularLocations(): List<Pair<String, Location>> { fun getPopularLocations(): List<Pair<String, Location>> {
return listOf( return listOf(
"Madrid" to Location("google_provider").apply { "Madrid" to Location(LocationManager.GPS_PROVIDER).apply {
latitude = 40.4168 latitude = 40.4168
longitude = -3.7038 longitude = -3.7038
time = System.currentTimeMillis() time = System.currentTimeMillis()
accuracy = 10.0f accuracy = 10.0f
}, },
"Barcelona" to Location("google_provider").apply { "Barcelona" to Location(LocationManager.GPS_PROVIDER).apply {
latitude = 41.3851 latitude = 41.3851
longitude = 2.1734 longitude = 2.1734
time = System.currentTimeMillis() time = System.currentTimeMillis()
accuracy = 10.0f accuracy = 10.0f
}, },
"Valencia" to Location("google_provider").apply { "Valencia" to Location(LocationManager.GPS_PROVIDER).apply {
latitude = 39.4699 latitude = 39.4699
longitude = -0.3763 longitude = -0.3763
time = System.currentTimeMillis() time = System.currentTimeMillis()
accuracy = 10.0f accuracy = 10.0f
}, },
"Sevilla" to Location("google_provider").apply { "Sevilla" to Location(LocationManager.GPS_PROVIDER).apply {
latitude = 37.3886 latitude = 37.3886
longitude = -5.9823 longitude = -5.9823
time = System.currentTimeMillis() time = System.currentTimeMillis()
accuracy = 10.0f accuracy = 10.0f
}, },
"Bilbao" to Location("google_provider").apply { "Bilbao" to Location(LocationManager.GPS_PROVIDER).apply {
latitude = 43.2627 latitude = 43.2627
longitude = -2.9253 longitude = -2.9253
time = System.currentTimeMillis() time = System.currentTimeMillis()