Skip to content

Commit

Permalink
Implemented requested changes as well as improved documentation. Adde…
Browse files Browse the repository at this point in the history
…d tests to increase code coverage to 100%.
  • Loading branch information
Sempakonka committed Nov 30, 2020
1 parent 103bbf8 commit cb1aaf8
Show file tree
Hide file tree
Showing 21 changed files with 519 additions and 131 deletions.
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.8
geolocator: ^6.1.9
```
<details>
Expand Down
2 changes: 1 addition & 1 deletion geolocator/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.8
geolocator: ^6.1.9
```
<details>
Expand Down
2 changes: 0 additions & 2 deletions geolocator/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baseflow.geolocator">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.baseflow.geolocator;

import android.util.Log;

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;
Expand All @@ -18,14 +17,15 @@ 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 PositionStreamImpl streamHandler;
private PositionStreamHandlerImpl positionStreamHandler;

@Nullable
private NmeaStreamImpl nmeaStream;
private NmeaStreamHandlerImpl nmeaStreamHandler;

@Nullable
private Registrar pluginRegistrar;
Expand All @@ -34,8 +34,8 @@ public class GeolocatorPlugin implements FlutterPlugin, ActivityAware {
private ActivityPluginBinding pluginBinding;

public GeolocatorPlugin() {

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

Expand All @@ -59,11 +59,11 @@ public static void registerWith(Registrar registrar) {
methodCallHandler.startListening(registrar.context(), registrar.messenger());
methodCallHandler.setActivity(registrar.activity());

PositionStreamImpl streamHandler = new PositionStreamImpl(geolocatorPlugin.geolocationManager);
PositionStreamHandlerImpl streamHandler = new PositionStreamHandlerImpl(geolocatorPlugin.geolocationManager);
streamHandler.startListening(registrar.context(), registrar.messenger());
streamHandler.setActivity(registrar.activity());

NmeaStreamImpl nmeaStream = new NmeaStreamImpl(geolocatorPlugin.geolocationManager);
NmeaStreamHandlerImpl nmeaStream = new NmeaStreamHandlerImpl(geolocatorPlugin.nmeaMessageManager);
nmeaStream.startListening(registrar.context(), registrar.messenger());
nmeaStream.setActivity(registrar.activity());
}
Expand All @@ -73,11 +73,11 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
methodCallHandler = new MethodCallHandlerImpl(this.permissionManager, this.geolocationManager);
methodCallHandler.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
streamHandler = new PositionStreamImpl(this.geolocationManager);
streamHandler.startListening(
positionStreamHandler = new PositionStreamHandlerImpl(this.geolocationManager);
positionStreamHandler.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
nmeaStream = new NmeaStreamImpl(this.geolocationManager);
nmeaStream.startListening(
nmeaStreamHandler = new NmeaStreamHandlerImpl(this.nmeaMessageManager);
nmeaStreamHandler.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
}

Expand All @@ -88,14 +88,14 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
methodCallHandler = null;
}

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

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

Expand All @@ -104,12 +104,12 @@ public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
if (methodCallHandler != null) {
methodCallHandler.setActivity(binding.getActivity());
}
if (streamHandler != null) {
streamHandler.setActivity(binding.getActivity());
if (positionStreamHandler != null) {
positionStreamHandler.setActivity(binding.getActivity());
}

if (nmeaStream != null) {
nmeaStream.setActivity(binding.getActivity());
if (nmeaStreamHandler != null) {
nmeaStreamHandler.setActivity(binding.getActivity());
}

this.pluginBinding = binding;
Expand All @@ -131,12 +131,12 @@ public void onDetachedFromActivity() {
if (methodCallHandler != null) {
methodCallHandler.setActivity(null);
}
if (streamHandler != null) {
streamHandler.setActivity(null);
if (positionStreamHandler != null) {
positionStreamHandler.setActivity(null);
}

if (nmeaStream != null) {
nmeaStream.setActivity(null);
if (nmeaStreamHandler != null) {
nmeaStreamHandler.setActivity(null);
}

deregisterListeners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import androidx.annotation.Nullable;
import com.baseflow.geolocator.errors.ErrorCodes;
import com.baseflow.geolocator.location.GeolocationManager;
import com.baseflow.geolocator.location.NmeaMessageaClient;
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 NmeaStreamImpl implements EventChannel.StreamHandler {
class NmeaStreamHandlerImpl implements EventChannel.StreamHandler {

private static final String TAG = "NmeaStreamImpl";

private final GeolocationManager geolocationManager;
private final NmeaMessageManager nmeaMessageManager;

@Nullable
private EventChannel channel;
Expand All @@ -27,11 +28,11 @@ class NmeaStreamImpl implements EventChannel.StreamHandler {
@Nullable
private NmeaMessageaClient nmeaMessageaClient;

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

public static Map<String, Object> toMap(String n, Long l) {
private static Map<String, Object> toMap(String n, Long l) {
if (n == null || l == null) {
return null;
}
Expand Down Expand Up @@ -84,9 +85,9 @@ void stopListening() {
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {

this.nmeaMessageaClient = geolocationManager.createNmeaClient(context);
this.nmeaMessageaClient = nmeaMessageManager.createNmeaClient(context);

geolocationManager.startNmeaUpdates(
nmeaMessageManager.startNmeaUpdates(
context,
activity,
this.nmeaMessageaClient,
Expand All @@ -98,7 +99,7 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
@Override
public void onCancel(Object arguments) {
if (this.nmeaMessageaClient != null) {
geolocationManager.stopNmeaUpdates(this.nmeaMessageaClient);
nmeaMessageManager.stopNmeaUpdates(this.nmeaMessageaClient);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.Map;

class PositionStreamImpl implements EventChannel.StreamHandler {
class PositionStreamHandlerImpl implements EventChannel.StreamHandler {

private static final String TAG = "PositionStreamImpl";

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION_CODES;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.baseflow.geolocator.errors.ErrorCallback;
import com.baseflow.geolocator.errors.ErrorCodes;
import com.baseflow.geolocator.errors.PermissionUndefinedException;
import com.baseflow.geolocator.permission.LocationPermission;
import com.baseflow.geolocator.permission.PermissionManager;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
Expand All @@ -37,7 +33,7 @@ public void getLastKnownPosition(
PositionChangedCallback positionChangedCallback,
ErrorCallback errorCallback) {

handlePermissions(
permissionManager.handlePermissions(
context,
activity,
() -> {
Expand Down Expand Up @@ -66,7 +62,7 @@ public void startPositionUpdates(

this.locationClients.add(locationClient);

handlePermissions(
permissionManager.handlePermissions(
context,
activity,
() -> locationClient.startPositionUpdates(activity, positionChangedCallback, errorCallback),
Expand All @@ -78,26 +74,6 @@ public void stopPositionUpdates(@NonNull LocationClient locationClient) {
locationClient.stopPositionUpdates();
}


public void startNmeaUpdates(Context context, Activity activity, NmeaMessageaClient client,
NmeaChangedCallback nmeaChangedCallback, ErrorCallback errorCallback) {

handlePermissions(
context,
activity,
() -> client.startNmeaUpdates(nmeaChangedCallback, errorCallback),
errorCallback);
}

public void stopNmeaUpdates(NmeaMessageaClient client) {
client.stopNmeaUpdates();
}

public NmeaMessageaClient createNmeaClient(Context context) {
return android.os.Build.VERSION.SDK_INT >= VERSION_CODES.N ? new GnssNmeaMessageClient(context)
: new GpsNmeaMessageClient(context);
}

public LocationClient createLocationClient(
Context context,
boolean forceAndroidLocationManager,
Expand All @@ -117,46 +93,6 @@ private boolean isGooglePlayServicesAvailable(Context context) {
return resultCode == ConnectionResult.SUCCESS;
}

private void handlePermissions(
Context context,
@Nullable Activity activity,
Runnable hasPermissionCallback,
ErrorCallback errorCallback) {
try {
LocationPermission permissionStatus =
permissionManager.checkPermissionStatus(context, activity);

if (permissionStatus == LocationPermission.deniedForever) {
errorCallback.onError(ErrorCodes.permissionDenied);
return;
}

if (permissionStatus == LocationPermission.whileInUse
|| permissionStatus == LocationPermission.always) {
hasPermissionCallback.run();
return;
}

if (permissionStatus == LocationPermission.denied && activity != null) {
permissionManager.requestPermission(
activity,
(permission) -> {
if (permission == LocationPermission.whileInUse
|| permission == LocationPermission.always) {
hasPermissionCallback.run();
} else {
errorCallback.onError(ErrorCodes.permissionDenied);
}
},
errorCallback);
} else {
errorCallback.onError(ErrorCodes.permissionDenied);
}
} catch (PermissionUndefinedException ex) {
errorCallback.onError(ErrorCodes.permissionDefinitionsNotFound);
}
}

@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
for (LocationClient client : this.locationClients) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baseflow.geolocator.location;
package com.baseflow.geolocator.nmea;

import android.annotation.SuppressLint;
import android.content.Context;
Expand Down Expand Up @@ -32,17 +32,19 @@ public GnssNmeaMessageClient(
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}

@SuppressLint("MissingPermission")
public void startNmeaUpdates(NmeaChangedCallback nmeaChangedCallback,
ErrorCallback errorCallback) {

this.locationManager.addNmeaListener(this, null);
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this,
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this,
Looper.getMainLooper());
this.nmeaChangedCallback = nmeaChangedCallback;
this.errorCallback = errorCallback;
this.isListening = true;
}

@SuppressLint("MissingPermission")
@Override
public void stopNmeaUpdates() {
this.isListening = false;
Expand All @@ -60,16 +62,18 @@ public void onNmeaMessage(String s, long l) {

@Override
public void onLocationChanged(@NonNull Location location) {
System.out.println("location changed");
}


@Override
public void onProviderEnabled(String s) {
}

@SuppressLint("MissingPermission")
@Override
public void onProviderDisabled(String s) {
if (s.equals(locationManager.GPS_PROVIDER)) {
if (s.equals(LocationManager.GPS_PROVIDER)) {
if (isListening) {
this.locationManager.removeUpdates(this);
}
Expand Down
Loading

0 comments on commit cb1aaf8

Please sign in to comment.