Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(geolocation): add support for getCurrentPosition via Android Fra… #2116

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 71 additions & 18 deletions plugins/geolocation/android/src/main/java/Geolocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import android.location.LocationManager
import android.location.LocationRequest as LocationRequestAndroid
import android.os.SystemClock
import androidx.core.location.LocationManagerCompat
import app.tauri.Logger
Expand All @@ -32,7 +33,12 @@ public class Geolocation(private val context: Context) {
}

@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);
if (resultCode == ConnectionResult.SUCCESS) {
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
Expand All @@ -46,22 +52,59 @@ public class Geolocation(private val context: Context) {
Logger.error("isProviderEnabled failed")
}

val lowPrio = if (networkEnabled) Priority.PRIORITY_BALANCED_POWER_ACCURACY else Priority.PRIORITY_LOW_POWER
val prio = if (enableHighAccuracy) Priority.PRIORITY_HIGH_ACCURACY else lowPrio
if (gms) {
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
.getFusedLocationProviderClient(context)
.getCurrentLocation(prio, null)
.addOnFailureListener { e -> e.message?.let { errorCallback(it) } }
.addOnSuccessListener { location ->
if (location == null) {
errorCallback("Location unavailable.")
} else {
successCallback(location)
LocationServices
.getFusedLocationProviderClient(context)
.getCurrentLocation(prio, null)
.addOnFailureListener { e -> e.message?.let { errorCallback(it) } }
.addOnSuccessListener { location ->
if (location == null) {
errorCallback("Location unavailable.")
} else {
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 {
errorCallback("Location disabled.")
}
Expand All @@ -71,7 +114,12 @@ public class Geolocation(private val context: Context) {
}

@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);
if (resultCode == ConnectionResult.SUCCESS) {
clearLocationUpdates()
Expand All @@ -88,7 +136,8 @@ public class Geolocation(private val context: Context) {
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

Logger.error(prio.toString())
Expand All @@ -111,7 +160,11 @@ public class Geolocation(private val context: Context) {
}
}

fusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback!!, null)
fusedLocationClient?.requestLocationUpdates(
locationRequest,
locationCallback!!,
null
)
} else {
errorCallback("Location disabled.")
}
Expand Down Expand Up @@ -145,4 +198,4 @@ public class Geolocation(private val context: Context) {

return lastLoc
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PositionOptions {
var enableHighAccuracy: Boolean = false
var maximumAge: Long = 0
var timeout: Long = 10000
var gms: Boolean = true
}

@InvokeArg
Expand Down Expand Up @@ -108,7 +109,9 @@ class GeolocationPlugin(private val activity: Activity): Plugin(activity) {
if (location != null) {
invoke.resolve(convertLocation(location))
} else {
implementation.sendLocation(args.enableHighAccuracy,
implementation.sendLocation(
args.enableHighAccuracy,
args.gms,
{ loc -> invoke.resolve(convertLocation(loc)) },
{ error -> invoke.reject(error) })
}
Expand Down
6 changes: 6 additions & 0 deletions plugins/geolocation/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export type PositionOptions = {
* Ignored on iOS
*/
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(
Expand Down
4 changes: 4 additions & 0 deletions plugins/geolocation/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub struct PositionOptions {
// TODO: Handle Infinity.
// TODO: Should be u64+ but specta doesn't like that?
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)]
Expand Down