Skip to content

Commit 0390133

Browse files
committed
feat(geolocation): add support for android devices without GMS
1 parent a01d6bb commit 0390133

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

plugins/geolocation/android/src/main/java/Geolocation.kt

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ import com.google.android.gms.common.ConnectionResult
1515
import com.google.android.gms.common.GoogleApiAvailability
1616
import com.google.android.gms.location.FusedLocationProviderClient
1717
import com.google.android.gms.location.LocationCallback
18-
import com.google.android.gms.location.LocationRequest
18+
import com.google.android.gms.location.LocationRequest as GmsLocationRequest
1919
import com.google.android.gms.location.LocationResult
2020
import com.google.android.gms.location.LocationServices
2121
import com.google.android.gms.location.Priority
22+
import android.location.LocationRequest
23+
import android.location.LocationListener
2224

2325

2426
public class Geolocation(private val context: Context) {
2527
private var fusedLocationClient: FusedLocationProviderClient? = null
26-
private var locationCallback: LocationCallback? = null
27-
28+
private var locationCallback: LocationCallback? = null // For gms
29+
private var locationListener: LocationListener? = null // For android
2830

2931
fun isLocationServicesEnabled(): Boolean {
3032
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
@@ -64,7 +66,27 @@ public class Geolocation(private val context: Context) {
6466
errorCallback("Location disabled.")
6567
}
6668
} else {
67-
errorCallback("Google Play Services unavailable.")
69+
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
70+
val provider = locationManager.getProviderProperties(LocationManager.GPS_PROVIDER)
71+
if (provider == null) {
72+
errorCallback("Location unavailable.")
73+
return
74+
}
75+
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
76+
errorCallback("Location disabled.")
77+
return
78+
}
79+
val req = LocationRequest.Builder(1_000L)
80+
.setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
81+
.setMaxUpdates(1)
82+
.build()
83+
locationManager.getCurrentLocation(LocationManager.GPS_PROVIDER, req, null, context.mainExecutor) { location ->
84+
if (location == null) {
85+
errorCallback("Location unavailable.")
86+
} else {
87+
successCallback(location)
88+
}
89+
}
6890
}
6991
}
7092

@@ -89,7 +111,7 @@ public class Geolocation(private val context: Context) {
89111
val lowPrio = if (networkEnabled) Priority.PRIORITY_BALANCED_POWER_ACCURACY else Priority.PRIORITY_LOW_POWER
90112
val prio = if (enableHighAccuracy) Priority.PRIORITY_HIGH_ACCURACY else lowPrio
91113

92-
val locationRequest = LocationRequest.Builder(timeout)
114+
val locationRequest = GmsLocationRequest.Builder(timeout)
93115
.setMaxUpdateDelayMillis(timeout)
94116
.setMinUpdateIntervalMillis(timeout)
95117
.setPriority(prio)
@@ -112,7 +134,26 @@ public class Geolocation(private val context: Context) {
112134
errorCallback("Location disabled.")
113135
}
114136
} else {
115-
errorCallback("Google Play Services not available.")
137+
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
138+
val provider = locationManager.getProviderProperties(LocationManager.GPS_PROVIDER)
139+
if (provider == null) {
140+
errorCallback("Location unavailable.")
141+
return
142+
}
143+
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
144+
errorCallback("Location disabled.")
145+
return
146+
}
147+
val req = LocationRequest.Builder(timeout)
148+
.setQuality(if (enableHighAccuracy) LocationRequest.QUALITY_HIGH_ACCURACY else LocationRequest.QUALITY_LOW_POWER)
149+
.build()
150+
val listener = object : android.location.LocationListener {
151+
override fun onLocationChanged(location: android.location.Location) {
152+
successCallback(location)
153+
}
154+
}
155+
locationListener = listener
156+
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, req, context.mainExecutor, listener)
116157
}
117158
}
118159

@@ -121,6 +162,11 @@ public class Geolocation(private val context: Context) {
121162
fusedLocationClient?.removeLocationUpdates(locationCallback!!)
122163
locationCallback = null
123164
}
165+
if (locationListener != null) {
166+
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
167+
locationManager.removeUpdates(locationListener!!)
168+
locationListener = null
169+
}
124170
}
125171

126172
@SuppressLint("MissingPermission")

0 commit comments

Comments
 (0)