diff --git a/geolocator/android/src/main/java/com/baseflow/geolocator/GeolocatorPlugin.java b/geolocator/android/src/main/java/com/baseflow/geolocator/GeolocatorPlugin.java index 1f6f67b7..e245bf86 100644 --- a/geolocator/android/src/main/java/com/baseflow/geolocator/GeolocatorPlugin.java +++ b/geolocator/android/src/main/java/com/baseflow/geolocator/GeolocatorPlugin.java @@ -1,5 +1,7 @@ package com.baseflow.geolocator; +import android.app.Activity; +import android.content.Context; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -9,6 +11,7 @@ import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.embedding.engine.plugins.activity.ActivityAware; import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; +import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.PluginRegistry.Registrar; /** GeolocatorPlugin */ @@ -51,34 +54,13 @@ public GeolocatorPlugin() { public static void registerWith(Registrar registrar) { GeolocatorPlugin geolocatorPlugin = new GeolocatorPlugin(); geolocatorPlugin.pluginRegistrar = registrar; - geolocatorPlugin.registerListeners(); - - MethodCallHandlerImpl methodCallHandler = - new MethodCallHandlerImpl( - geolocatorPlugin.permissionManager, geolocatorPlugin.geolocationManager); - methodCallHandler.startListening(registrar.context(), registrar.messenger()); - methodCallHandler.setActivity(registrar.activity()); - - PositionStreamHandlerImpl streamHandler = new PositionStreamHandlerImpl(geolocatorPlugin.geolocationManager); - streamHandler.startListening(registrar.context(), registrar.messenger()); - streamHandler.setActivity(registrar.activity()); - - NmeaStreamHandlerImpl nmeaStream = new NmeaStreamHandlerImpl(geolocatorPlugin.nmeaMessageManager); - nmeaStream.startListening(registrar.context(), registrar.messenger()); - nmeaStream.setActivity(registrar.activity()); + geolocatorPlugin.configureListeners(registrar.context(), registrar.messenger()); + geolocatorPlugin.setActivity(registrar.activity()); } @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { - methodCallHandler = new MethodCallHandlerImpl(this.permissionManager, this.geolocationManager); - methodCallHandler.startListening( - flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger()); - positionStreamHandler = new PositionStreamHandlerImpl(this.geolocationManager); - positionStreamHandler.startListening( - flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger()); - nmeaStreamHandler = new NmeaStreamHandlerImpl(this.nmeaMessageManager); - nmeaStreamHandler.startListening( - flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger()); + configureListeners(flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger()); } @Override @@ -101,19 +83,8 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { @Override public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { - if (methodCallHandler != null) { - methodCallHandler.setActivity(binding.getActivity()); - } - if (positionStreamHandler != null) { - positionStreamHandler.setActivity(binding.getActivity()); - } - - if (nmeaStreamHandler != null) { - nmeaStreamHandler.setActivity(binding.getActivity()); - } - this.pluginBinding = binding; - registerListeners(); + setActivity(binding.getActivity()); } @Override @@ -128,18 +99,38 @@ public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBindin @Override public void onDetachedFromActivity() { + setActivity(null); + } + + + void configureListeners(Context applicationContext, BinaryMessenger messenger) { + methodCallHandler = + new MethodCallHandlerImpl(permissionManager, geolocationManager); + methodCallHandler.startListening(applicationContext, messenger); + + positionStreamHandler = new PositionStreamHandlerImpl(geolocationManager); + positionStreamHandler.startListening(applicationContext, messenger); + + nmeaStreamHandler = new NmeaStreamHandlerImpl(nmeaMessageManager); + nmeaStreamHandler.startListening(applicationContext, messenger); + } + + void setActivity(@Nullable Activity activity) { if (methodCallHandler != null) { - methodCallHandler.setActivity(null); + methodCallHandler.setActivity(activity); } if (positionStreamHandler != null) { - positionStreamHandler.setActivity(null); + positionStreamHandler.setActivity(activity); } - if (nmeaStreamHandler != null) { - nmeaStreamHandler.setActivity(null); + nmeaStreamHandler.setActivity(activity); } - deregisterListeners(); + if (activity != null) { + registerListeners(); + } else { + deregisterListeners(); + } } private void registerListeners() { diff --git a/geolocator/android/src/main/java/com/baseflow/geolocator/NmeaStreamHandlerImpl.java b/geolocator/android/src/main/java/com/baseflow/geolocator/NmeaStreamHandlerImpl.java index e758ccc9..9d0f5dc9 100644 --- a/geolocator/android/src/main/java/com/baseflow/geolocator/NmeaStreamHandlerImpl.java +++ b/geolocator/android/src/main/java/com/baseflow/geolocator/NmeaStreamHandlerImpl.java @@ -15,7 +15,7 @@ class NmeaStreamHandlerImpl implements EventChannel.StreamHandler { - private static final String TAG = "NmeaStreamImpl"; + private static final String TAG = "NmeaStreamHandlerImpl"; private final NmeaMessageManager nmeaMessageManager; @@ -32,17 +32,17 @@ public NmeaStreamHandlerImpl(NmeaMessageManager nmeaMessageManager) { this.nmeaMessageManager = nmeaMessageManager; } - private static Map toMap(String n, Long l) { - if (n == null || l == null) { + private static Map toMap(String message, Long timestamp) { + if (message == null || timestamp == null) { return null; } - Map Nmea = new HashMap<>(); + Map nmeaMap = new HashMap<>(); - Nmea.put("timestamp", l); - Nmea.put("message", n); + nmeaMap.put("timestamp", timestamp); + nmeaMap.put("message", message); - return Nmea; + return nmeaMap; } void setActivity(@Nullable Activity activity) { @@ -91,7 +91,7 @@ public void onListen(Object arguments, EventChannel.EventSink events) { context, activity, this.nmeaMessageaClient, - (String n, long l) -> events.success(toMap(n, l)), + (String message, long timestamp) -> events.success(toMap(message, timestamp)), (ErrorCodes errorCodes) -> events.error(errorCodes.toString(), errorCodes.toDescription(), null)); } diff --git a/geolocator/android/src/main/java/com/baseflow/geolocator/PositionStreamHandlerImpl.java b/geolocator/android/src/main/java/com/baseflow/geolocator/PositionStreamHandlerImpl.java index c763bdc6..f3f5a54d 100644 --- a/geolocator/android/src/main/java/com/baseflow/geolocator/PositionStreamHandlerImpl.java +++ b/geolocator/android/src/main/java/com/baseflow/geolocator/PositionStreamHandlerImpl.java @@ -17,7 +17,7 @@ class PositionStreamHandlerImpl implements EventChannel.StreamHandler { - private static final String TAG = "PositionStreamImpl"; + private static final String TAG = "PositionStreamHandler"; private final GeolocationManager geolocationManager; diff --git a/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/GnssNmeaMessageClient.java b/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/GnssNmeaMessageClient.java index 32d95505..c0c8175d 100644 --- a/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/GnssNmeaMessageClient.java +++ b/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/GnssNmeaMessageClient.java @@ -53,16 +53,15 @@ public void stopNmeaUpdates() { @Override - public void onNmeaMessage(String s, long l) { + public void onNmeaMessage(String message, long timestamp) { if (this.nmeaChangedCallback != null) { - this.nmeaChangedCallback.onNmeaMessage(s, l); + this.nmeaChangedCallback.onNmeaMessage(message, timestamp); } } @Override public void onLocationChanged(@NonNull Location location) { - System.out.println("location changed"); } @@ -72,8 +71,8 @@ public void onProviderEnabled(String s) { @SuppressLint("MissingPermission") @Override - public void onProviderDisabled(String s) { - if (s.equals(LocationManager.GPS_PROVIDER)) { + public void onProviderDisabled(String provider) { + if (provider.equals(LocationManager.GPS_PROVIDER)) { if (isListening) { this.locationManager.removeUpdates(this); } diff --git a/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/NmeaMessageManager.java b/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/NmeaMessageManager.java index d2c6e5b0..69c22cb7 100644 --- a/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/NmeaMessageManager.java +++ b/geolocator/android/src/main/java/com/baseflow/geolocator/nmea/NmeaMessageManager.java @@ -34,7 +34,8 @@ public void stopNmeaUpdates(NmeaMessageaClient client) { } public NmeaMessageaClient createNmeaClient(Context context) { - return android.os.Build.VERSION.SDK_INT >= VERSION_CODES.N ? new GnssNmeaMessageClient(context) + return android.os.Build.VERSION.SDK_INT >= VERSION_CODES.N + ? new GnssNmeaMessageClient(context) : new GpsNmeaMessageClient(context); } diff --git a/geolocator/lib/geolocator.dart b/geolocator/lib/geolocator.dart index 4ffb5ca3..b840b4df 100644 --- a/geolocator/lib/geolocator.dart +++ b/geolocator/lib/geolocator.dart @@ -261,17 +261,19 @@ class Geolocator { timeLimit: timeLimit, ); - /// Returns a stream emitting NMEA-0183 sentences when they are received from - /// the GNSS engine. With devices running a Android API level lower than 24 - /// NMEA-0183 sentences are received from the GPS engine. + /// Returns a stream emitting NMEA-0183 sentences Android only, no-op on iOS. + /// + /// With devices running an Android API level lower than 24 NMEA-0183 sentences + /// are received from the GPS engine. From API level 24 and up the GNSS engine + /// is used. /// /// This event starts all location sensors on the device and will keep them /// active until you cancel listening to the stream or when the application /// is killed. /// /// ``` - /// StreamSubscription nmeaStream = Geolocator.getNmeaStream() - /// .listen((NmeaMessage nmea) { + /// StreamSubscription nmeaStream = + /// Geolocator.getNmeaMessageStream().listen((NmeaMessage nmea) { /// // Handle NMEA changes /// }); /// ``` diff --git a/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart b/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart index fd3b394e..1e16bce4 100644 --- a/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart +++ b/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart @@ -22,13 +22,14 @@ class MethodChannelGeolocator extends GeolocatorPlatform { /// The event channel used to receive [Position] updates from the native /// platform. @visibleForTesting - EventChannel eventChannel = + EventChannel positionEventChannel = EventChannel('flutter.baseflow.com/geolocator_updates'); /// The event channel used to receive [NmeaMessage] updates from the native /// platform. @visibleForTesting - EventChannel nmeaChannel = EventChannel('flutter.baseflow.com/nmea_updates'); + EventChannel nmeaEventChannel = + EventChannel('flutter.baseflow.com/nmea_updates'); /// On Android devices you can set [forceAndroidLocationManager] /// to true to force the plugin to use the [LocationManager] to determine the @@ -149,7 +150,7 @@ class MethodChannelGeolocator extends GeolocatorPlatform { return _positionStream; } - var positionStream = eventChannel.receiveBroadcastStream( + var positionStream = positionEventChannel.receiveBroadcastStream( locationOptions.toJson(), ); @@ -190,7 +191,7 @@ class MethodChannelGeolocator extends GeolocatorPlatform { return _nmeaMessageStream; } - final nmeaStream = nmeaChannel.receiveBroadcastStream(); + final nmeaStream = nmeaEventChannel.receiveBroadcastStream(); _nmeaMessageStream = nmeaStream .map((dynamic element) => diff --git a/geolocator_platform_interface/lib/src/models/position.dart b/geolocator_platform_interface/lib/src/models/position.dart index 0abf0a4f..0194de63 100644 --- a/geolocator_platform_interface/lib/src/models/position.dart +++ b/geolocator_platform_interface/lib/src/models/position.dart @@ -88,7 +88,6 @@ class Position { o.timestamp == timestamp && o.isMocked == isMocked; - return areEqual; } @@ -130,35 +129,35 @@ class Position { final timestamp = positionMap['timestamp'] != null ? DateTime.fromMillisecondsSinceEpoch(positionMap['timestamp'].toInt(), - isUtc: true) + isUtc: true) : null; return Position( - latitude: positionMap['latitude'], - longitude: positionMap['longitude'], - timestamp: timestamp, - altitude: positionMap['altitude'] ?? 0.0, - accuracy: positionMap['accuracy'] ?? 0.0, - heading: positionMap['heading'] ?? 0.0, - floor: positionMap['floor'], - speed: positionMap['speed'] ?? 0.0, - speedAccuracy: positionMap['speed_accuracy'] ?? 0.0, - isMocked: positionMap['is_mocked'] ?? false, + latitude: positionMap['latitude'], + longitude: positionMap['longitude'], + timestamp: timestamp, + altitude: positionMap['altitude'] ?? 0.0, + accuracy: positionMap['accuracy'] ?? 0.0, + heading: positionMap['heading'] ?? 0.0, + floor: positionMap['floor'], + speed: positionMap['speed'] ?? 0.0, + speedAccuracy: positionMap['speed_accuracy'] ?? 0.0, + isMocked: positionMap['is_mocked'] ?? false, ); } /// Converts the [Position] instance into a [Map] instance that can be /// serialized to JSON. Map toJson() => { - 'longitude': longitude, - 'latitude': latitude, - 'timestamp': timestamp?.millisecondsSinceEpoch, - 'accuracy': accuracy, - 'altitude': altitude, - 'floor': floor, - 'heading': heading, - 'speed': speed, - 'speed_accuracy': speedAccuracy, - 'is_mocked': isMocked, - }; + 'longitude': longitude, + 'latitude': latitude, + 'timestamp': timestamp?.millisecondsSinceEpoch, + 'accuracy': accuracy, + 'altitude': altitude, + 'floor': floor, + 'heading': heading, + 'speed': speed, + 'speed_accuracy': speedAccuracy, + 'is_mocked': isMocked, + }; }