-
-
Notifications
You must be signed in to change notification settings - Fork 680
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
[geolocator_android] Introduce option to force a specific location provider Issue/1171 #1613
base: main
Are you sure you want to change the base?
Changes from all commits
7d33f0e
eeb5e61
e90bc45
0f6e828
5f5e9ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.baseflow.geolocator.location; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
public class LocationOptions { | ||
|
@@ -9,24 +11,27 @@ public class LocationOptions { | |
private final long distanceFilter; | ||
private final long timeInterval; | ||
private final boolean useMSLAltitude; | ||
private final String forceProvider; | ||
|
||
private LocationOptions( | ||
LocationAccuracy accuracy, long distanceFilter, long timeInterval, boolean useMSLAltitude) { | ||
LocationAccuracy accuracy, long distanceFilter, long timeInterval, boolean useMSLAltitude, String provider) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we keep the same name for the |
||
this.accuracy = accuracy; | ||
this.distanceFilter = distanceFilter; | ||
this.timeInterval = timeInterval; | ||
this.useMSLAltitude = useMSLAltitude; | ||
this.forceProvider = provider; | ||
} | ||
|
||
public static LocationOptions parseArguments(Map<String, Object> arguments) { | ||
if (arguments == null) { | ||
return new LocationOptions(LocationAccuracy.best, 0, 5000, false); | ||
return new LocationOptions(LocationAccuracy.best, 0, 5000, false, null); | ||
} | ||
|
||
final Integer accuracy = (Integer) arguments.get("accuracy"); | ||
final Integer distanceFilter = (Integer) arguments.get("distanceFilter"); | ||
final Integer timeInterval = (Integer) arguments.get("timeInterval"); | ||
final Boolean useMSLAltitude = (Boolean) arguments.get("useMSLAltitude"); | ||
final String provider = (String) arguments.get("forceProvider"); | ||
|
||
LocationAccuracy locationAccuracy = LocationAccuracy.best; | ||
|
||
|
@@ -54,10 +59,11 @@ public static LocationOptions parseArguments(Map<String, Object> arguments) { | |
} | ||
|
||
return new LocationOptions( | ||
locationAccuracy, | ||
distanceFilter != null ? distanceFilter : 0, | ||
timeInterval != null ? timeInterval : 5000, | ||
useMSLAltitude != null && useMSLAltitude); | ||
locationAccuracy, | ||
distanceFilter != null ? distanceFilter : 0, | ||
timeInterval != null ? timeInterval : 5000, | ||
useMSLAltitude != null && useMSLAltitude, | ||
provider); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Perhaps forceProvider would be easier to understand for other developers. |
||
} | ||
|
||
public LocationAccuracy getAccuracy() { | ||
|
@@ -75,4 +81,9 @@ public long getTimeInterval() { | |
public boolean isUseMSLAltitude() { | ||
return useMSLAltitude; | ||
} | ||
|
||
@Nullable | ||
public String getForceProvider() { | ||
return forceProvider; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ class AndroidSettings extends LocationSettings { | |
Duration? timeLimit, | ||
this.foregroundNotificationConfig, | ||
this.useMSLAltitude = false, | ||
this.forceProvider, | ||
}) : super( | ||
accuracy: accuracy, | ||
distanceFilter: distanceFilter, | ||
|
@@ -74,6 +75,11 @@ class AndroidSettings extends LocationSettings { | |
/// Defaults to false | ||
final bool useMSLAltitude; | ||
|
||
/// Set this to use a specific [AndroidLocationProvider]. | ||
/// Set this value only in conjunction with [forceLocationManager] set to true. Be sure the provider is available on your targeted devices. | ||
/// Defaults to null. | ||
final AndroidLocationProvider? forceProvider; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this only works in combination with the forceLocationManager, should desired provider perhaps be a more convenient name? |
||
|
||
@override | ||
Map<String, dynamic> toJson() { | ||
return super.toJson() | ||
|
@@ -82,6 +88,22 @@ class AndroidSettings extends LocationSettings { | |
'timeInterval': intervalDuration?.inMilliseconds, | ||
'foregroundNotificationConfig': foregroundNotificationConfig?.toJson(), | ||
'useMSLAltitude': useMSLAltitude, | ||
'forceProvider': forceProvider?.name, | ||
}); | ||
} | ||
} | ||
|
||
/// Represents the different [Android location providers](https://developer.android.com/reference/android/location/LocationManager#constants_1) | ||
enum AndroidLocationProvider { | ||
/// [GPS_PROVIDER](https://developer.android.com/reference/android/location/LocationManager#GPS_PROVIDER) | ||
gps, | ||
|
||
/// [FUSED_PROVIDER](https://developer.android.com/reference/android/location/LocationManager#FUSED_PROVIDER) | ||
fused, | ||
|
||
/// [NETWORK_PROVIDER](https://developer.android.com/reference/android/location/LocationManager#NETWORK_PROVIDER) | ||
network, | ||
|
||
/// [PASSIVE_PROVIDER](https://developer.android.com/reference/android/location/LocationManager#PASSIVE_PROVIDER) | ||
passive, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this as a very helpful addition. However, I also see a lot of questions appearing when we "force" a provider, but if the provider is not available on the device we give an bit of misleading error back that the location services are disabled (or location provider is unavailable), although we actually mean that the "forcedProvider" is not available (we currently don't know if locationServicesDisabled or the forcedProvider is not available)... Can't we do this smarter by returning a more useful error? Then, we give the developer the option to decide how to "proceed". With the best "default" provider (or perhaps an other "forced" provider), or not al all...