feat(geolocation): add support for getCurrentPosition via Android Framework Location API

pull/2116/head
MagicCrazyMan 8 months ago
parent ab9a24b89b
commit f3a8cb663f
No known key found for this signature in database
GPG Key ID: 301861D7DBD46CD6

@ -8,6 +8,7 @@ import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.location.Location import android.location.Location
import android.location.LocationManager import android.location.LocationManager
import android.location.LocationRequest as LocationRequestAndroid
import android.os.SystemClock import android.os.SystemClock
import androidx.core.location.LocationManagerCompat import androidx.core.location.LocationManagerCompat
import app.tauri.Logger import app.tauri.Logger
@ -32,7 +33,12 @@ public class Geolocation(private val context: Context) {
} }
@SuppressWarnings("MissingPermission") @SuppressWarnings("MissingPermission")
fun sendLocation(enableHighAccuracy: Boolean, successCallback: (location: Location) -> Unit, errorCallback: (error: String) -> Unit) { fun sendLocation(
enableHighAccuracy: Boolean,
gms: Boolean,
successCallback: (location: Location) -> Unit,
errorCallback: (error: String) -> Unit
) {
val resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context); val resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
if (resultCode == ConnectionResult.SUCCESS) { if (resultCode == ConnectionResult.SUCCESS) {
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
@ -46,22 +52,59 @@ public class Geolocation(private val context: Context) {
Logger.error("isProviderEnabled failed") Logger.error("isProviderEnabled failed")
} }
val lowPrio = if (networkEnabled) Priority.PRIORITY_BALANCED_POWER_ACCURACY else Priority.PRIORITY_LOW_POWER if (gms) {
val prio = if (enableHighAccuracy) Priority.PRIORITY_HIGH_ACCURACY else lowPrio val lowPrio =
if (networkEnabled) Priority.PRIORITY_BALANCED_POWER_ACCURACY else Priority.PRIORITY_LOW_POWER
val prio = if (enableHighAccuracy) Priority.PRIORITY_HIGH_ACCURACY else lowPrio
Logger.error(prio.toString()) Logger.error(prio.toString())
LocationServices LocationServices
.getFusedLocationProviderClient(context) .getFusedLocationProviderClient(context)
.getCurrentLocation(prio, null) .getCurrentLocation(prio, null)
.addOnFailureListener { e -> e.message?.let { errorCallback(it) } } .addOnFailureListener { e -> e.message?.let { errorCallback(it) } }
.addOnSuccessListener { location -> .addOnSuccessListener { location ->
if (location == null) { if (location == null) {
errorCallback("Location unavailable.") errorCallback("Location unavailable.")
} else { } else {
successCallback(location) successCallback(location)
}
} }
} } else {
val providers = lm.getAllProviders()
val provider =
if (enableHighAccuracy && providers.contains(LocationManager.GPS_PROVIDER) && lm.isProviderEnabled(
LocationManager.GPS_PROVIDER
)
)
LocationManager.GPS_PROVIDER
else if (providers.contains(LocationManager.FUSED_PROVIDER) && lm.isProviderEnabled(
LocationManager.FUSED_PROVIDER
)
)
LocationManager.FUSED_PROVIDER
else if (providers.contains(LocationManager.NETWORK_PROVIDER) && lm.isProviderEnabled(
LocationManager.NETWORK_PROVIDER
)
)
LocationManager.NETWORK_PROVIDER
else
LocationManager.PASSIVE_PROVIDER
lm.getCurrentLocation(
provider,
null,
context.getMainExecutor(),
{ location ->
if (location == null) {
errorCallback("Location unavailable.")
} else {
successCallback(location)
}
}
);
}
} else { } else {
errorCallback("Location disabled.") errorCallback("Location disabled.")
} }
@ -71,7 +114,12 @@ public class Geolocation(private val context: Context) {
} }
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
fun requestLocationUpdates(enableHighAccuracy: Boolean, timeout: Long, successCallback: (location: Location) -> Unit, errorCallback: (error: String) -> Unit) { fun requestLocationUpdates(
enableHighAccuracy: Boolean,
timeout: Long,
successCallback: (location: Location) -> Unit,
errorCallback: (error: String) -> Unit
) {
val resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context); val resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
if (resultCode == ConnectionResult.SUCCESS) { if (resultCode == ConnectionResult.SUCCESS) {
clearLocationUpdates() clearLocationUpdates()
@ -88,7 +136,8 @@ public class Geolocation(private val context: Context) {
Logger.error("isProviderEnabled failed") Logger.error("isProviderEnabled failed")
} }
val lowPrio = if (networkEnabled) Priority.PRIORITY_BALANCED_POWER_ACCURACY else Priority.PRIORITY_LOW_POWER val lowPrio =
if (networkEnabled) Priority.PRIORITY_BALANCED_POWER_ACCURACY else Priority.PRIORITY_LOW_POWER
val prio = if (enableHighAccuracy) Priority.PRIORITY_HIGH_ACCURACY else lowPrio val prio = if (enableHighAccuracy) Priority.PRIORITY_HIGH_ACCURACY else lowPrio
Logger.error(prio.toString()) Logger.error(prio.toString())
@ -111,7 +160,11 @@ public class Geolocation(private val context: Context) {
} }
} }
fusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback!!, null) fusedLocationClient?.requestLocationUpdates(
locationRequest,
locationCallback!!,
null
)
} else { } else {
errorCallback("Location disabled.") errorCallback("Location disabled.")
} }
@ -145,4 +198,4 @@ public class Geolocation(private val context: Context) {
return lastLoc return lastLoc
} }
} }

@ -26,6 +26,7 @@ class PositionOptions {
var enableHighAccuracy: Boolean = false var enableHighAccuracy: Boolean = false
var maximumAge: Long = 0 var maximumAge: Long = 0
var timeout: Long = 10000 var timeout: Long = 10000
var gms: Boolean = true
} }
@InvokeArg @InvokeArg
@ -108,7 +109,9 @@ class GeolocationPlugin(private val activity: Activity): Plugin(activity) {
if (location != null) { if (location != null) {
invoke.resolve(convertLocation(location)) invoke.resolve(convertLocation(location))
} else { } else {
implementation.sendLocation(args.enableHighAccuracy, implementation.sendLocation(
args.enableHighAccuracy,
args.gms,
{ loc -> invoke.resolve(convertLocation(loc)) }, { loc -> invoke.resolve(convertLocation(loc)) },
{ error -> invoke.reject(error) }) { error -> invoke.reject(error) })
} }

@ -90,6 +90,12 @@ export type PositionOptions = {
* Ignored on iOS * Ignored on iOS
*/ */
maximumAge: number maximumAge: number
/**
* Uses Google Play Location API or Android Framework Location API to get current position.
* Default: true
* Only Available for getCurrentPosition on Android.
*/
gms?: boolean
} }
export async function watchPosition( export async function watchPosition(

@ -44,6 +44,10 @@ pub struct PositionOptions {
// TODO: Handle Infinity. // TODO: Handle Infinity.
// TODO: Should be u64+ but specta doesn't like that? // TODO: Should be u64+ but specta doesn't like that?
pub maximum_age: u32, pub maximum_age: u32,
/// Uses Google Play Location API or Android Framework Location API to get current position.
/// Default: true
/// Only Available for getCurrentPosition on Android.
pub gms: bool,
} }
#[derive(Debug, Clone, Serialize, Deserialize, Type)] #[derive(Debug, Clone, Serialize, Deserialize, Type)]

Loading…
Cancel
Save