Skip to content

Commit 6c5ca1a

Browse files
committed
Implemented requested changes.
1 parent cb1aaf8 commit 6c5ca1a

File tree

8 files changed

+81
-88
lines changed

8 files changed

+81
-88
lines changed

geolocator/android/src/main/java/com/baseflow/geolocator/GeolocatorPlugin.java

+32-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baseflow.geolocator;
22

3+
import android.app.Activity;
4+
import android.content.Context;
35
import androidx.annotation.NonNull;
46
import androidx.annotation.Nullable;
57

@@ -9,6 +11,7 @@
911
import io.flutter.embedding.engine.plugins.FlutterPlugin;
1012
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
1113
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
14+
import io.flutter.plugin.common.BinaryMessenger;
1215
import io.flutter.plugin.common.PluginRegistry.Registrar;
1316

1417
/** GeolocatorPlugin */
@@ -51,34 +54,13 @@ public GeolocatorPlugin() {
5154
public static void registerWith(Registrar registrar) {
5255
GeolocatorPlugin geolocatorPlugin = new GeolocatorPlugin();
5356
geolocatorPlugin.pluginRegistrar = registrar;
54-
geolocatorPlugin.registerListeners();
55-
56-
MethodCallHandlerImpl methodCallHandler =
57-
new MethodCallHandlerImpl(
58-
geolocatorPlugin.permissionManager, geolocatorPlugin.geolocationManager);
59-
methodCallHandler.startListening(registrar.context(), registrar.messenger());
60-
methodCallHandler.setActivity(registrar.activity());
61-
62-
PositionStreamHandlerImpl streamHandler = new PositionStreamHandlerImpl(geolocatorPlugin.geolocationManager);
63-
streamHandler.startListening(registrar.context(), registrar.messenger());
64-
streamHandler.setActivity(registrar.activity());
65-
66-
NmeaStreamHandlerImpl nmeaStream = new NmeaStreamHandlerImpl(geolocatorPlugin.nmeaMessageManager);
67-
nmeaStream.startListening(registrar.context(), registrar.messenger());
68-
nmeaStream.setActivity(registrar.activity());
57+
geolocatorPlugin.configureListeners(registrar.context(), registrar.messenger());
58+
geolocatorPlugin.setActivity(registrar.activity());
6959
}
7060

7161
@Override
7262
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
73-
methodCallHandler = new MethodCallHandlerImpl(this.permissionManager, this.geolocationManager);
74-
methodCallHandler.startListening(
75-
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
76-
positionStreamHandler = new PositionStreamHandlerImpl(this.geolocationManager);
77-
positionStreamHandler.startListening(
78-
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
79-
nmeaStreamHandler = new NmeaStreamHandlerImpl(this.nmeaMessageManager);
80-
nmeaStreamHandler.startListening(
81-
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
63+
configureListeners(flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
8264
}
8365

8466
@Override
@@ -101,19 +83,8 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
10183

10284
@Override
10385
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
104-
if (methodCallHandler != null) {
105-
methodCallHandler.setActivity(binding.getActivity());
106-
}
107-
if (positionStreamHandler != null) {
108-
positionStreamHandler.setActivity(binding.getActivity());
109-
}
110-
111-
if (nmeaStreamHandler != null) {
112-
nmeaStreamHandler.setActivity(binding.getActivity());
113-
}
114-
11586
this.pluginBinding = binding;
116-
registerListeners();
87+
setActivity(binding.getActivity());
11788
}
11889

11990
@Override
@@ -128,18 +99,38 @@ public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBindin
12899

129100
@Override
130101
public void onDetachedFromActivity() {
102+
setActivity(null);
103+
}
104+
105+
106+
void configureListeners(Context applicationContext, BinaryMessenger messenger) {
107+
methodCallHandler =
108+
new MethodCallHandlerImpl(permissionManager, geolocationManager);
109+
methodCallHandler.startListening(applicationContext, messenger);
110+
111+
positionStreamHandler = new PositionStreamHandlerImpl(geolocationManager);
112+
positionStreamHandler.startListening(applicationContext, messenger);
113+
114+
nmeaStreamHandler = new NmeaStreamHandlerImpl(nmeaMessageManager);
115+
nmeaStreamHandler.startListening(applicationContext, messenger);
116+
}
117+
118+
void setActivity(@Nullable Activity activity) {
131119
if (methodCallHandler != null) {
132-
methodCallHandler.setActivity(null);
120+
methodCallHandler.setActivity(activity);
133121
}
134122
if (positionStreamHandler != null) {
135-
positionStreamHandler.setActivity(null);
123+
positionStreamHandler.setActivity(activity);
136124
}
137-
138125
if (nmeaStreamHandler != null) {
139-
nmeaStreamHandler.setActivity(null);
126+
nmeaStreamHandler.setActivity(activity);
140127
}
141128

142-
deregisterListeners();
129+
if (activity != null) {
130+
registerListeners();
131+
} else {
132+
deregisterListeners();
133+
}
143134
}
144135

145136
private void registerListeners() {

geolocator/android/src/main/java/com/baseflow/geolocator/NmeaStreamHandlerImpl.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class NmeaStreamHandlerImpl implements EventChannel.StreamHandler {
1717

18-
private static final String TAG = "NmeaStreamImpl";
18+
private static final String TAG = "NmeaStreamHandlerImpl";
1919

2020
private final NmeaMessageManager nmeaMessageManager;
2121

@@ -32,17 +32,17 @@ public NmeaStreamHandlerImpl(NmeaMessageManager nmeaMessageManager) {
3232
this.nmeaMessageManager = nmeaMessageManager;
3333
}
3434

35-
private static Map<String, Object> toMap(String n, Long l) {
36-
if (n == null || l == null) {
35+
private static Map<String, Object> toMap(String message, Long timestamp) {
36+
if (message == null || timestamp == null) {
3737
return null;
3838
}
3939

40-
Map<String, Object> Nmea = new HashMap<>();
40+
Map<String, Object> nmeaMap = new HashMap<>();
4141

42-
Nmea.put("timestamp", l);
43-
Nmea.put("message", n);
42+
nmeaMap.put("timestamp", timestamp);
43+
nmeaMap.put("message", message);
4444

45-
return Nmea;
45+
return nmeaMap;
4646
}
4747

4848
void setActivity(@Nullable Activity activity) {
@@ -91,7 +91,7 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
9191
context,
9292
activity,
9393
this.nmeaMessageaClient,
94-
(String n, long l) -> events.success(toMap(n, l)),
94+
(String message, long timestamp) -> events.success(toMap(message, timestamp)),
9595
(ErrorCodes errorCodes) ->
9696
events.error(errorCodes.toString(), errorCodes.toDescription(), null));
9797
}

geolocator/android/src/main/java/com/baseflow/geolocator/PositionStreamHandlerImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class PositionStreamHandlerImpl implements EventChannel.StreamHandler {
1919

20-
private static final String TAG = "PositionStreamImpl";
20+
private static final String TAG = "PositionStreamHandler";
2121

2222
private final GeolocationManager geolocationManager;
2323

geolocator/android/src/main/java/com/baseflow/geolocator/nmea/GnssNmeaMessageClient.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ public void stopNmeaUpdates() {
5353

5454

5555
@Override
56-
public void onNmeaMessage(String s, long l) {
56+
public void onNmeaMessage(String message, long timestamp) {
5757
if (this.nmeaChangedCallback != null) {
58-
this.nmeaChangedCallback.onNmeaMessage(s, l);
58+
this.nmeaChangedCallback.onNmeaMessage(message, timestamp);
5959
}
6060
}
6161

6262

6363
@Override
6464
public void onLocationChanged(@NonNull Location location) {
65-
System.out.println("location changed");
6665
}
6766

6867

@@ -72,8 +71,8 @@ public void onProviderEnabled(String s) {
7271

7372
@SuppressLint("MissingPermission")
7473
@Override
75-
public void onProviderDisabled(String s) {
76-
if (s.equals(LocationManager.GPS_PROVIDER)) {
74+
public void onProviderDisabled(String provider) {
75+
if (provider.equals(LocationManager.GPS_PROVIDER)) {
7776
if (isListening) {
7877
this.locationManager.removeUpdates(this);
7978
}

geolocator/android/src/main/java/com/baseflow/geolocator/nmea/NmeaMessageManager.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public void stopNmeaUpdates(NmeaMessageaClient client) {
3434
}
3535

3636
public NmeaMessageaClient createNmeaClient(Context context) {
37-
return android.os.Build.VERSION.SDK_INT >= VERSION_CODES.N ? new GnssNmeaMessageClient(context)
37+
return android.os.Build.VERSION.SDK_INT >= VERSION_CODES.N
38+
? new GnssNmeaMessageClient(context)
3839
: new GpsNmeaMessageClient(context);
3940
}
4041

geolocator/lib/geolocator.dart

+7-5
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,19 @@ class Geolocator {
261261
timeLimit: timeLimit,
262262
);
263263

264-
/// Returns a stream emitting NMEA-0183 sentences when they are received from
265-
/// the GNSS engine. With devices running a Android API level lower than 24
266-
/// NMEA-0183 sentences are received from the GPS engine.
264+
/// Returns a stream emitting NMEA-0183 sentences Android only, no-op on iOS.
265+
///
266+
/// With devices running an Android API level lower than 24 NMEA-0183 sentences
267+
/// are received from the GPS engine. From API level 24 and up the GNSS engine
268+
/// is used.
267269
///
268270
/// This event starts all location sensors on the device and will keep them
269271
/// active until you cancel listening to the stream or when the application
270272
/// is killed.
271273
///
272274
/// ```
273-
/// StreamSubscription<NmeaMessage> nmeaStream = Geolocator.getNmeaStream()
274-
/// .listen((NmeaMessage nmea) {
275+
/// StreamSubscription<NmeaMessage> nmeaStream =
276+
/// Geolocator.getNmeaMessageStream().listen((NmeaMessage nmea) {
275277
/// // Handle NMEA changes
276278
/// });
277279
/// ```

geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class MethodChannelGeolocator extends GeolocatorPlatform {
2222
/// The event channel used to receive [Position] updates from the native
2323
/// platform.
2424
@visibleForTesting
25-
EventChannel eventChannel =
25+
EventChannel positionEventChannel =
2626
EventChannel('flutter.baseflow.com/geolocator_updates');
2727

2828
/// The event channel used to receive [NmeaMessage] updates from the native
2929
/// platform.
3030
@visibleForTesting
31-
EventChannel nmeaChannel = EventChannel('flutter.baseflow.com/nmea_updates');
31+
EventChannel nmeaEventChannel =
32+
EventChannel('flutter.baseflow.com/nmea_updates');
3233

3334
/// On Android devices you can set [forceAndroidLocationManager]
3435
/// to true to force the plugin to use the [LocationManager] to determine the
@@ -149,7 +150,7 @@ class MethodChannelGeolocator extends GeolocatorPlatform {
149150
return _positionStream;
150151
}
151152

152-
var positionStream = eventChannel.receiveBroadcastStream(
153+
var positionStream = positionEventChannel.receiveBroadcastStream(
153154
locationOptions.toJson(),
154155
);
155156

@@ -190,7 +191,7 @@ class MethodChannelGeolocator extends GeolocatorPlatform {
190191
return _nmeaMessageStream;
191192
}
192193

193-
final nmeaStream = nmeaChannel.receiveBroadcastStream();
194+
final nmeaStream = nmeaEventChannel.receiveBroadcastStream();
194195

195196
_nmeaMessageStream = nmeaStream
196197
.map<NmeaMessage>((dynamic element) =>

geolocator_platform_interface/lib/src/models/position.dart

+22-23
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class Position {
8888
o.timestamp == timestamp &&
8989
o.isMocked == isMocked;
9090

91-
9291
return areEqual;
9392
}
9493

@@ -130,35 +129,35 @@ class Position {
130129

131130
final timestamp = positionMap['timestamp'] != null
132131
? DateTime.fromMillisecondsSinceEpoch(positionMap['timestamp'].toInt(),
133-
isUtc: true)
132+
isUtc: true)
134133
: null;
135134

136135
return Position(
137-
latitude: positionMap['latitude'],
138-
longitude: positionMap['longitude'],
139-
timestamp: timestamp,
140-
altitude: positionMap['altitude'] ?? 0.0,
141-
accuracy: positionMap['accuracy'] ?? 0.0,
142-
heading: positionMap['heading'] ?? 0.0,
143-
floor: positionMap['floor'],
144-
speed: positionMap['speed'] ?? 0.0,
145-
speedAccuracy: positionMap['speed_accuracy'] ?? 0.0,
146-
isMocked: positionMap['is_mocked'] ?? false,
136+
latitude: positionMap['latitude'],
137+
longitude: positionMap['longitude'],
138+
timestamp: timestamp,
139+
altitude: positionMap['altitude'] ?? 0.0,
140+
accuracy: positionMap['accuracy'] ?? 0.0,
141+
heading: positionMap['heading'] ?? 0.0,
142+
floor: positionMap['floor'],
143+
speed: positionMap['speed'] ?? 0.0,
144+
speedAccuracy: positionMap['speed_accuracy'] ?? 0.0,
145+
isMocked: positionMap['is_mocked'] ?? false,
147146
);
148147
}
149148

150149
/// Converts the [Position] instance into a [Map] instance that can be
151150
/// serialized to JSON.
152151
Map<String, dynamic> toJson() => {
153-
'longitude': longitude,
154-
'latitude': latitude,
155-
'timestamp': timestamp?.millisecondsSinceEpoch,
156-
'accuracy': accuracy,
157-
'altitude': altitude,
158-
'floor': floor,
159-
'heading': heading,
160-
'speed': speed,
161-
'speed_accuracy': speedAccuracy,
162-
'is_mocked': isMocked,
163-
};
152+
'longitude': longitude,
153+
'latitude': latitude,
154+
'timestamp': timestamp?.millisecondsSinceEpoch,
155+
'accuracy': accuracy,
156+
'altitude': altitude,
157+
'floor': floor,
158+
'heading': heading,
159+
'speed': speed,
160+
'speed_accuracy': speedAccuracy,
161+
'is_mocked': isMocked,
162+
};
164163
}

0 commit comments

Comments
 (0)