Skip to content
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
5 changes: 5 additions & 0 deletions .changes/geolocation-non-gms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"geolocation": "minor"
---

Add support for the geolocation plugin on non-GMS android devices.
1 change: 1 addition & 0 deletions examples/api/src-tauri/capabilities/mobile.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"geolocation:allow-request-permissions",
"geolocation:allow-watch-position",
"geolocation:allow-get-current-position",
"geolocation:allow-clear-watch",
"haptics:allow-impact-feedback",
"haptics:allow-notification-feedback",
"haptics:allow-selection-feedback",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue May 10 19:22:52 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
64 changes: 62 additions & 2 deletions examples/api/src/views/Geolocation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
import {
checkPermissions,
requestPermissions,
getCurrentPosition
getCurrentPosition,

watchPosition,
clearWatch

} from '@tauri-apps/plugin-geolocation'

export let onMessage

let pos = null
let watchId = null

async function getPosition() {
let permissions = await checkPermissions()
if (
Expand All @@ -17,13 +24,66 @@
}

if (permissions.location === 'granted') {
getCurrentPosition().then(onMessage).catch(onMessage)
getCurrentPosition().then((position) => {
pos = position
onMessage(position)
}).catch((err) => {
pos = null
onMessage(err)
})
} else {
onMessage('permission denied')
}
}

async function watchPos() {
let permissions = await checkPermissions()
if (
permissions.location === 'prompt' ||
permissions.location === 'prompt-with-rationale'
) {
permissions = await requestPermissions(['location'])
}

if (permissions.location === 'granted') {
watchId = await watchPosition({
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}, (position) => {
pos = position
onMessage(position)
})
onMessage('watchId: ' + watchId)
} else {
onMessage('permission denied')
}
}

async function stopWatching() {
await clearWatch(watchId)
watchId = null
pos = null
}

</script>

<button class="btn" id="cli-matches" on:click={getPosition}>
Get Position
</button>

<button class="btn" on:click={watchPos}>
Watch Position
</button>

<button class="btn" on:click={stopWatching}>
Stop Watching
</button>

{#if watchId}
<span>Watch ID: {watchId}</span>
{/if}

{#if pos}
<pre>{JSON.stringify(pos, null, 2)}</pre>
{/if}
58 changes: 52 additions & 6 deletions plugins/geolocation/android/src/main/java/Geolocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationRequest as GmsLocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import android.location.LocationRequest
import android.location.LocationListener


public class Geolocation(private val context: Context) {
private var fusedLocationClient: FusedLocationProviderClient? = null
private var locationCallback: LocationCallback? = null

private var locationCallback: LocationCallback? = null // For gms
private var locationListener: LocationListener? = null // For android

fun isLocationServicesEnabled(): Boolean {
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
Expand Down Expand Up @@ -64,7 +66,27 @@ public class Geolocation(private val context: Context) {
errorCallback("Location disabled.")
}
} else {
errorCallback("Google Play Services unavailable.")
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val provider = locationManager.getProviderProperties(LocationManager.GPS_PROVIDER)
if (provider == null) {
errorCallback("Location unavailable.")
return
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
errorCallback("Location disabled.")
return
}
val req = LocationRequest.Builder(1_000L)
.setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
.setMaxUpdates(1)
.build()
locationManager.getCurrentLocation(LocationManager.GPS_PROVIDER, req, null, context.mainExecutor) { location ->
if (location == null) {
errorCallback("Location unavailable.")
} else {
successCallback(location)
}
}
}
}

Expand All @@ -89,7 +111,7 @@ public class Geolocation(private val context: Context) {
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 locationRequest = LocationRequest.Builder(timeout)
val locationRequest = GmsLocationRequest.Builder(timeout)
.setMaxUpdateDelayMillis(timeout)
.setMinUpdateIntervalMillis(timeout)
.setPriority(prio)
Expand All @@ -112,7 +134,26 @@ public class Geolocation(private val context: Context) {
errorCallback("Location disabled.")
}
} else {
errorCallback("Google Play Services not available.")
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val provider = locationManager.getProviderProperties(LocationManager.GPS_PROVIDER)
if (provider == null) {
errorCallback("Location unavailable.")
return
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
errorCallback("Location disabled.")
return
}
val req = LocationRequest.Builder(timeout)
.setQuality(if (enableHighAccuracy) LocationRequest.QUALITY_HIGH_ACCURACY else LocationRequest.QUALITY_LOW_POWER)
.build()
val listener = object : android.location.LocationListener {
override fun onLocationChanged(location: android.location.Location) {
successCallback(location)
}
}
locationListener = listener
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, req, context.mainExecutor, listener)
}
}

Expand All @@ -121,6 +162,11 @@ public class Geolocation(private val context: Context) {
fusedLocationClient?.removeLocationUpdates(locationCallback!!)
locationCallback = null
}
if (locationListener != null) {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.removeUpdates(locationListener!!)
locationListener = null
}
}

@SuppressLint("MissingPermission")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class GeolocationPlugin(private val activity: Activity): Plugin(activity) {
{ error -> args.channel.sendObject(error) })

watchers[args.channel.id] = Pair(invoke, args)

invoke.resolve()
}

@Command
Expand Down
Loading