Skip to content

Commit

Permalink
Remove NMEA messages option from android settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Wackymax committed Apr 11, 2022
1 parent 8a561de commit eac1adc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public FusedLocationClient(@NonNull Context context, @Nullable LocationOptions l
this.fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
this.locationOptions = locationOptions;
this.activityRequestCode = generateActivityRequestCode();
this.nmeaClient = new NmeaClient(context, locationOptions);
this.nmeaClient = new NmeaClient(context);

locationCallback =
new LocationCallback() {
Expand Down Expand Up @@ -122,7 +122,9 @@ private synchronized int generateActivityRequestCode() {

@SuppressLint("MissingPermission")
private void requestPositionUpdates(LocationOptions locationOptions) {
this.nmeaClient.start();
if (locationOptions != null && locationOptions.getUseMSLAltitude()) {
this.nmeaClient.start();
}
LocationRequest locationRequest = buildLocationRequest(locationOptions);
fusedLocationProviderClient.requestLocationUpdates(
locationRequest, locationCallback, Looper.getMainLooper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public LocationManagerClient(
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
this.locationOptions = locationOptions;
this.context = context;
this.nmeaClient = new NmeaClient(context, locationOptions);
this.nmeaClient = new NmeaClient(context);
}

static boolean isBetterLocation(Location location, Location bestLocation) {
Expand Down Expand Up @@ -195,7 +195,9 @@ public void startPositionUpdates(

this.isListening = true;

this.nmeaClient.start();
if (locationOptions != null && locationOptions.getUseMSLAltitude()) {
this.nmeaClient.start();
}
this.locationManager.requestLocationUpdates(
this.currentLocationProvider, timeInterval, distanceFilter, this, Looper.getMainLooper());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class LocationOptions {
private final LocationAccuracy accuracy;
private final long distanceFilter;
private final long timeInterval;
private final boolean includeNmeaMessages;
private final boolean useMSLAltitude;

private LocationOptions(
Expand All @@ -18,7 +17,6 @@ private LocationOptions(
this.accuracy = accuracy;
this.distanceFilter = distanceFilter;
this.timeInterval = timeInterval;
this.includeNmeaMessages = includeNmeaMessages;
this.useMSLAltitude = useMSLAltitude;
}

Expand Down Expand Up @@ -85,10 +83,6 @@ public long getTimeInterval() {
return timeInterval;
}

public boolean getIncludeNmeaMessages() {
return includeNmeaMessages;
}

public boolean getUseMSLAltitude() {
return useMSLAltitude;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,73 @@

public class NmeaClient {

public static final String NMEA_MESSAGE_EXTRA = "geolocator_nmeaMessage";
public static final String NMEA_ALTITUDE_EXTRA = "geolocator_mslAltitude";

private final Context context;
private final LocationManager locationManager;
@Nullable private final LocationOptions locationOptions;

@TargetApi(Build.VERSION_CODES.N)
private OnNmeaMessageListener nmeaMessageListener;

private String lastNmeaMessage;
private boolean listenerAdded = false;

public NmeaClient(@NonNull Context context, @Nullable LocationOptions locationOptions) {
this.context = context;
this.locationOptions = locationOptions;
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
nmeaMessageListener =
(message, timestamp) -> {
if (message.startsWith("$")) {
lastNmeaMessage = message;
}
};
}
}
public static final String NMEA_MESSAGE_EXTRA = "geolocator_nmeaMessage";
public static final String NMEA_ALTITUDE_EXTRA = "geolocator_mslAltitude";

@SuppressLint("MissingPermission")
public void start() {
if(listenerAdded) {
return;
}
private final Context context;
private final LocationManager locationManager;

@TargetApi(Build.VERSION_CODES.N)
private OnNmeaMessageListener nmeaMessageListener;

private String lastNmeaMessage;
private boolean listenerAdded = false;

if (locationOptions != null
&& (locationOptions.getIncludeNmeaMessages() || locationOptions.getUseMSLAltitude())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locationManager != null) {
locationManager.addNmeaListener(nmeaMessageListener, null);
listenerAdded = true;
public NmeaClient(@NonNull Context context) {
this.context = context;
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
nmeaMessageListener =
(message, timestamp) -> {
if (message.startsWith("$")) {
lastNmeaMessage = message;
}
}
};
}
}

public void stop() {
if (locationOptions != null
&& (locationOptions.getIncludeNmeaMessages() || locationOptions.getUseMSLAltitude())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locationManager != null) {
locationManager.removeNmeaListener(nmeaMessageListener);
listenerAdded = false;
}
}
@SuppressLint("MissingPermission")
public void start() {
if (listenerAdded) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locationManager != null) {
locationManager.addNmeaListener(nmeaMessageListener, null);
listenerAdded = true;
}
}

public void enrichExtrasWithNmea(@Nullable Location location) {
public void stop() {
if (!listenerAdded) {
return;
}

if(location == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locationManager != null) {
locationManager.removeNmeaListener(nmeaMessageListener);
listenerAdded = false;
}
}

if (lastNmeaMessage != null && locationOptions != null && listenerAdded) {
public void enrichExtrasWithNmea(@Nullable Location location) {

if (locationOptions.getIncludeNmeaMessages()) {
location.getExtras().putString(NMEA_MESSAGE_EXTRA, lastNmeaMessage);
}
if (location == null) {
return;
}

if (locationOptions.getUseMSLAltitude()) {
String[] tokens = lastNmeaMessage.split(",");
String type = tokens[0];

// Parse altitude above sea level, Detailed description of NMEA string here
// http://aprs.gids.nl/nmea/#gga
if (type.startsWith("$GPGGA") && tokens.length > 9) {
if (!tokens[9].isEmpty()) {
double mslAltitude = Double.parseDouble(tokens[9]);
location.getExtras().putDouble(NMEA_ALTITUDE_EXTRA, mslAltitude);
}
}
}
if (lastNmeaMessage != null && listenerAdded) {

String[] tokens = lastNmeaMessage.split(",");
String type = tokens[0];

// Parse altitude above sea level, Detailed description of NMEA string here
// http://aprs.gids.nl/nmea/#gga
if (type.startsWith("$GPGGA") && tokens.length > 9) {
if (!tokens[9].isEmpty()) {
double mslAltitude = Double.parseDouble(tokens[9]);
location.getExtras().putDouble(NMEA_ALTITUDE_EXTRA, mslAltitude);
}
}
}
}
}
2 changes: 0 additions & 2 deletions geolocator_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
final androidSettings = AndroidSettings(
accuracy: LocationAccuracy.best,
forceLocationManager: false,
includeNmeaMessages: true,
useMSLAltitude: false,
intervalDuration: const Duration(seconds: 1),
foregroundNotificationConfig: const ForegroundNotificationConfig(
Expand All @@ -310,7 +309,6 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
_PositionItemType.position,
position.toString(),
);
print('NMEA Message: ${position.nmeaMessage}');
});
_positionStreamSubscription?.pause();
}
Expand Down
4 changes: 0 additions & 4 deletions geolocator_android/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ dev_dependencies:
sdk: flutter
flutter_lints: ^1.0.4

dependency_overrides:
geolocator_platform_interface:
path: ../../geolocator_platform_interface

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

Expand Down
12 changes: 0 additions & 12 deletions geolocator_android/lib/src/types/android_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class AndroidSettings extends LocationSettings {
this.intervalDuration,
Duration? timeLimit,
this.foregroundNotificationConfig,
this.includeNmeaMessages = false,
this.useMSLAltitude = false,
}) : super(
accuracy: accuracy,
Expand All @@ -34,20 +33,10 @@ class AndroidSettings extends LocationSettings {
/// set this property to true.
final bool forceLocationManager;

/// Set to true to include NMEA messages with position updates.
///
/// NMEA messages are only available starting from Android N
final bool includeNmeaMessages;

/// Set to true if altitude should be calculated as MSL (EGM2008) from NMEA message
/// and reported as the altitude instead of using the geoidal height (WSG84). Setting
/// this property true will help to align Android altitude to that of iOS which uses MSL.
///
/// Setting this option to true will also set includeNmeaMessages to true. To determine
/// if NMEA was used to calculate the altitude you should check for the presence of the
/// NMEA message in the position update. If the NMEA message is empty then the altitude reported
/// will be the standard WSG84 altitude from the GPS receiver.
///
/// MSL Altitude is only available starting from Android N.
final bool useMSLAltitude;

Expand All @@ -66,7 +55,6 @@ class AndroidSettings extends LocationSettings {
..addAll({
'forceLocationManager': forceLocationManager,
'timeInterval': intervalDuration?.inMilliseconds,
'includeNmeaMessages': includeNmeaMessages,
'useMSLAltitude': useMSLAltitude,
'foregroundNotificationConfig': foregroundNotificationConfig?.toJson(),
});
Expand Down

0 comments on commit eac1adc

Please sign in to comment.