Skip to content

Commit

Permalink
Implemented requested changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sempakonka committed Nov 30, 2020
1 parent cb1aaf8 commit 6c5ca1a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.baseflow.geolocator;

import android.app.Activity;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -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 */
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,17 +32,17 @@ public NmeaStreamHandlerImpl(NmeaMessageManager nmeaMessageManager) {
this.nmeaMessageManager = nmeaMessageManager;
}

private static Map<String, Object> toMap(String n, Long l) {
if (n == null || l == null) {
private static Map<String, Object> toMap(String message, Long timestamp) {
if (message == null || timestamp == null) {
return null;
}

Map<String, Object> Nmea = new HashMap<>();
Map<String, Object> 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) {
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 7 additions & 5 deletions geolocator/lib/geolocator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<NmeaMessage> nmeaStream = Geolocator.getNmeaStream()
/// .listen((NmeaMessage nmea) {
/// StreamSubscription<NmeaMessage> nmeaStream =
/// Geolocator.getNmeaMessageStream().listen((NmeaMessage nmea) {
/// // Handle NMEA changes
/// });
/// ```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -149,7 +150,7 @@ class MethodChannelGeolocator extends GeolocatorPlatform {
return _positionStream;
}

var positionStream = eventChannel.receiveBroadcastStream(
var positionStream = positionEventChannel.receiveBroadcastStream(
locationOptions.toJson(),
);

Expand Down Expand Up @@ -190,7 +191,7 @@ class MethodChannelGeolocator extends GeolocatorPlatform {
return _nmeaMessageStream;
}

final nmeaStream = nmeaChannel.receiveBroadcastStream();
final nmeaStream = nmeaEventChannel.receiveBroadcastStream();

_nmeaMessageStream = nmeaStream
.map<NmeaMessage>((dynamic element) =>
Expand Down
45 changes: 22 additions & 23 deletions geolocator_platform_interface/lib/src/models/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class Position {
o.timestamp == timestamp &&
o.isMocked == isMocked;


return areEqual;
}

Expand Down Expand Up @@ -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<String, dynamic> 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,
};
}

0 comments on commit 6c5ca1a

Please sign in to comment.