Skip to content
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

Added a new feature to receive NMEA messages #605

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e46ddf9
fix: return LocationPermission.always for requestPermission on Androi…
danilocubos Nov 29, 2020
f8c4be4
fix: result callback
danilocubos Nov 29, 2020
9f3db18
push voor daniel
Sempakonka Nov 17, 2020
6c787cf
nmea message clients both for gps and gnss added. Added nmea message …
Sempakonka Nov 18, 2020
dfce813
nmea message clients both for gps and gnss added. Added nmea message …
Sempakonka Nov 24, 2020
10fa655
nmea message clients both for gps and gnss added. Added nmea message …
Sempakonka Nov 24, 2020
6a88988
fixed prefer_relative_imports and fixed pubspec.yaml version solving …
Sempakonka Nov 24, 2020
103bbf8
fixed platform_interface unit test error
Sempakonka Nov 24, 2020
cb1aaf8
Implemented requested changes as well as improved documentation. Adde…
Sempakonka Nov 26, 2020
6c5ca1a
Implemented requested changes.
Sempakonka Nov 30, 2020
7db55c2
fix formatting
Sempakonka Nov 30, 2020
6f4f493
Merge pull request #611 from daniloapr/master
mvanbeusekom Dec 1, 2020
8203eb6
Prepare for version 6.1.9
mvanbeusekom Dec 1, 2020
633de46
Add unit-test to verify timeInterval parameter
mvanbeusekom Dec 1, 2020
ed9b67c
push voor daniel
Sempakonka Nov 17, 2020
8dfcb37
nmea message clients both for gps and gnss added. Added nmea message …
Sempakonka Nov 18, 2020
a1bbbbe
nmea message clients both for gps and gnss added. Added nmea message …
Sempakonka Nov 24, 2020
d77aa1b
nmea message clients both for gps and gnss added. Added nmea message …
Sempakonka Nov 24, 2020
fc1984a
fixed prefer_relative_imports and fixed pubspec.yaml version solving …
Sempakonka Nov 24, 2020
102198d
fixed platform_interface unit test error
Sempakonka Nov 24, 2020
38acda5
Implemented requested changes as well as improved documentation. Adde…
Sempakonka Nov 26, 2020
7b390b8
Implemented requested changes.
Sempakonka Nov 30, 2020
723a78b
fix formatting
Sempakonka Nov 30, 2020
11d7b02
Improved naming of variables in example app
Sempakonka Dec 1, 2020
982053c
Add unit-test to verify timeInterval parameter
mvanbeusekom Dec 1, 2020
3ba9250
Merge branch 'issue#516' of github.com:Baseflow/flutter-geolocator in…
Sempakonka Dec 1, 2020
8a88f54
version bumb
Sempakonka Dec 1, 2020
818653f
CI test error fix
Sempakonka Dec 1, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To use this plugin, add `geolocator` as a [dependency in your pubspec.yaml file]

```yaml
dependencies:
geolocator: ^6.1.7
geolocator: ^6.1.9
```

<details>
Expand Down
8 changes: 8 additions & 0 deletions geolocator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 6.2.0

- Added a functionality to receive NMEA messages.

## 6.1.9

- Return `LocationPermission.always` when requesting permission on Android 5.1 and below (see issue [#610](https://github.com/Baseflow/flutter-geolocator/issues/610)).

## 6.1.8+1

- Fixed Dart formatting issue.
Expand Down
7 changes: 1 addition & 6 deletions geolocator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ A Flutter geolocation plugin which provides easy access to platform specific loc

## Usage

To use this plugin, add `geolocator` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/). For example:

```yaml
dependencies:
geolocator: ^6.1.8
```
To add the geolocator to your Flutter application read the [install](https://pub.dev/packages/geolocator/install) instructions. Below are some Android and iOS specifics that are required for the geolocator to work correctly.

<details>
<summary>Android</summary>
Expand Down
2 changes: 1 addition & 1 deletion geolocator/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
minSdkVersion 16
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.baseflow.geolocator;

import android.util.Log;

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

import com.baseflow.geolocator.location.GeolocationManager;
import com.baseflow.geolocator.nmea.NmeaMessageManager;
import com.baseflow.geolocator.permission.PermissionManager;
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 All @@ -18,18 +20,25 @@ public class GeolocatorPlugin implements FlutterPlugin, ActivityAware {
private static final String TAG = "GeocodingPlugin";
private final PermissionManager permissionManager;
private final GeolocationManager geolocationManager;
private final NmeaMessageManager nmeaMessageManager;

@Nullable private MethodCallHandlerImpl methodCallHandler;

@Nullable private StreamHandlerImpl streamHandler;
@Nullable
private PositionStreamHandlerImpl positionStreamHandler;

@Nullable private Registrar pluginRegistrar;
@Nullable
private NmeaStreamHandlerImpl nmeaStreamHandler;

@Nullable private ActivityPluginBinding pluginBinding;
@Nullable
private Registrar pluginRegistrar;

public GeolocatorPlugin() {
@Nullable
private ActivityPluginBinding pluginBinding;

public GeolocatorPlugin() {
this.permissionManager = new PermissionManager();
this.nmeaMessageManager = new NmeaMessageManager(permissionManager);
this.geolocationManager = new GeolocationManager(permissionManager);
}

Expand All @@ -45,27 +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());

StreamHandlerImpl streamHandler = new StreamHandlerImpl(geolocatorPlugin.geolocationManager);
streamHandler.startListening(registrar.context(), registrar.messenger());
streamHandler.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());
streamHandler = new StreamHandlerImpl(this.geolocationManager);
streamHandler.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
configureListeners(flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
}

@Override
Expand All @@ -75,23 +70,21 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
methodCallHandler = null;
}

if (streamHandler != null) {
streamHandler.stopListening();
streamHandler = null;
if (positionStreamHandler != null) {
positionStreamHandler.stopListening();
positionStreamHandler = null;
}

if (nmeaStreamHandler != null) {
nmeaStreamHandler.stopListening();
nmeaStreamHandler = null;
}
}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
if (methodCallHandler != null) {
methodCallHandler.setActivity(binding.getActivity());
}
if (streamHandler != null) {
streamHandler.setActivity(binding.getActivity());
}

this.pluginBinding = binding;
registerListeners();
setActivity(binding.getActivity());
}

@Override
Expand All @@ -106,14 +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 (streamHandler != null) {
streamHandler.setActivity(null);
if (positionStreamHandler != null) {
positionStreamHandler.setActivity(activity);
}
if (nmeaStreamHandler != 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
@@ -0,0 +1,106 @@
package com.baseflow.geolocator;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import androidx.annotation.Nullable;
import com.baseflow.geolocator.errors.ErrorCodes;
import com.baseflow.geolocator.location.GeolocationManager;
import com.baseflow.geolocator.nmea.NmeaMessageManager;
import com.baseflow.geolocator.nmea.NmeaMessageaClient;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import java.util.HashMap;
import java.util.Map;

class NmeaStreamHandlerImpl implements EventChannel.StreamHandler {

private static final String TAG = "NmeaStreamHandlerImpl";

private final NmeaMessageManager nmeaMessageManager;

@Nullable
private EventChannel channel;
@Nullable
private Context context;
@Nullable
private Activity activity;
@Nullable
private NmeaMessageaClient nmeaMessageaClient;

public NmeaStreamHandlerImpl(NmeaMessageManager nmeaMessageManager) {
this.nmeaMessageManager = nmeaMessageManager;
}

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

Map<String, Object> nmeaMap = new HashMap<>();

nmeaMap.put("timestamp", timestamp);
nmeaMap.put("message", message);

return nmeaMap;
}

void setActivity(@Nullable Activity activity) {
this.activity = activity;
}

/**
* Registers this instance as event stream handler on the given {@code messenger}.
*
* <p>Stops any previously started and unstopped calls.
*
* <p>This should be cleaned with {@link #stopListening} once the messenger is disposed of.
*/
void startListening(Context context, BinaryMessenger messenger) {
if (channel != null) {
Log.w(TAG, "Setting a event call handler before the last was disposed.");
stopListening();
}

channel = new EventChannel(messenger, "flutter.baseflow.com/nmea_updates");
channel.setStreamHandler(this);
this.context = context;
}

/**
* Clears this instance from listening to method calls.
*
* <p>Does nothing if {@link #startListening} hasn't been called, or if we're already stopped.
*/
void stopListening() {
if (channel == null) {
Log.d(TAG, "Tried to stop listening when no MethodChannel had been initialized.");
return;
}

channel.setStreamHandler(null);
channel = null;
}

@Override
public void onListen(Object arguments, EventChannel.EventSink events) {

this.nmeaMessageaClient = nmeaMessageManager.createNmeaClient(context);

nmeaMessageManager.startNmeaUpdates(
context,
activity,
this.nmeaMessageaClient,
(String message, long timestamp) -> events.success(toMap(message, timestamp)),
(ErrorCodes errorCodes) ->
events.error(errorCodes.toString(), errorCodes.toDescription(), null));
}

@Override
public void onCancel(Object arguments) {
if (this.nmeaMessageaClient != null) {
nmeaMessageManager.stopNmeaUpdates(this.nmeaMessageaClient);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

import java.util.Map;

class StreamHandlerImpl implements EventChannel.StreamHandler {
private static final String TAG = "StreamHandlerImpl";
class PositionStreamHandlerImpl implements EventChannel.StreamHandler {

private static final String TAG = "PositionStreamHandler";

private final GeolocationManager geolocationManager;

Expand All @@ -25,7 +26,7 @@ class StreamHandlerImpl implements EventChannel.StreamHandler {
@Nullable private Activity activity;
@Nullable private LocationClient locationClient;

public StreamHandlerImpl(GeolocationManager geolocationManager) {
public PositionStreamHandlerImpl(GeolocationManager geolocationManager) {
this.geolocationManager = geolocationManager;
}

Expand Down
Loading