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

[Feature] Android secondary position stream for foreground notification updates #1406

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class GeolocatorPlugin implements FlutterPlugin, ActivityAware {
@Nullable private MethodCallHandlerImpl methodCallHandler;

@Nullable private StreamHandlerImpl streamHandler;
@Nullable private StreamHandlerImpl streamHandlerFGN;

private final ServiceConnection serviceConnection =
new ServiceConnection() {

Expand Down Expand Up @@ -68,9 +70,12 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
this.permissionManager, this.geolocationManager, this.locationAccuracyManager);
methodCallHandler.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
streamHandler = new StreamHandlerImpl(this.permissionManager, this.geolocationManager);
streamHandler = new StreamHandlerImpl(this.permissionManager, this.geolocationManager, "flutter.baseflow.com/geolocator_updates_android");
streamHandler.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
streamHandlerFGN = new StreamHandlerImpl(this.permissionManager, this.geolocationManager, "flutter.baseflow.com/geolocator_updates_android_fgn");
streamHandlerFGN.startListening(
flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());

locationServiceHandler = new LocationServiceHandlerImpl();
locationServiceHandler.setContext(flutterPluginBinding.getApplicationContext());
Expand All @@ -97,6 +102,9 @@ public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
if (streamHandler != null) {
streamHandler.setActivity(binding.getActivity());
}
if (streamHandlerFGN != null) {
streamHandlerFGN.setActivity(binding.getActivity());
}
if (foregroundLocationService != null) {
foregroundLocationService.setActivity(pluginBinding.getActivity());
}
Expand All @@ -122,6 +130,9 @@ public void onDetachedFromActivity() {
if (streamHandler != null) {
streamHandler.setActivity(null);
}
if (streamHandlerFGN != null) {
streamHandlerFGN.setActivity(null);
}
if (foregroundLocationService != null) {
foregroundLocationService.setActivity(null);
}
Expand Down Expand Up @@ -164,8 +175,8 @@ private void initialize(GeolocatorLocationService service) {
foregroundLocationService.setGeolocationManager(geolocationManager);
foregroundLocationService.flutterEngineConnected();

if (streamHandler != null) {
streamHandler.setForegroundLocationService(service);
if (streamHandlerFGN != null) {
streamHandlerFGN.setForegroundLocationService(service);
}
}

Expand All @@ -178,9 +189,13 @@ private void dispose() {
}
if (streamHandler != null) {
streamHandler.stopListening();
streamHandler.setForegroundLocationService(null);
streamHandler = null;
}
if (streamHandlerFGN != null) {
streamHandlerFGN.stopListening();
streamHandlerFGN.setForegroundLocationService(null);
streamHandlerFGN = null;
}
if (locationServiceHandler != null) {
locationServiceHandler.setContext(null);
locationServiceHandler.stopListening();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class StreamHandlerImpl implements EventChannel.StreamHandler {
private static final String TAG = "FlutterGeolocator";

private final PermissionManager permissionManager;
private final String eventChannelIdentifier;

@Nullable private EventChannel channel;
@Nullable private Context context;
Expand All @@ -33,9 +34,10 @@ class StreamHandlerImpl implements EventChannel.StreamHandler {
@Nullable private GeolocationManager geolocationManager;
@Nullable private LocationClient locationClient;

public StreamHandlerImpl(PermissionManager permissionManager, GeolocationManager geolocationManager) {
public StreamHandlerImpl(PermissionManager permissionManager, GeolocationManager geolocationManager, String eventChannelIdentifier) {
this.permissionManager = permissionManager;
this.geolocationManager = geolocationManager;
this.eventChannelIdentifier = eventChannelIdentifier;
}

public void setForegroundLocationService(
Expand Down Expand Up @@ -65,7 +67,7 @@ void startListening(Context context, BinaryMessenger messenger) {
stopListening();
}

channel = new EventChannel(messenger, "flutter.baseflow.com/geolocator_updates_android");
channel = new EventChannel(messenger, eventChannelIdentifier);
channel.setStreamHandler(this);
this.context = context;
}
Expand Down Expand Up @@ -105,11 +107,6 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
return;
}

if (foregroundLocationService == null) {
Log.e(TAG, "Location background service has not started correctly");
return;
}

@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) arguments;
boolean forceLocationManager = false;
Expand All @@ -125,6 +122,10 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
(Map<String, Object>) map.get("foregroundNotificationConfig"));
}
if (foregroundNotificationOptions != null) {
if (foregroundLocationService == null) {
Log.e(TAG, "Location background service has not started correctly");
return;
}
Log.e(TAG, "Geolocator position updates started using Android foreground service");
foregroundLocationService.startLocationService(forceLocationManager, locationOptions, events);
foregroundLocationService.enableBackgroundMode(foregroundNotificationOptions);
Expand Down
51 changes: 42 additions & 9 deletions geolocator_android/lib/src/geolocator_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class GeolocatorAndroid extends GeolocatorPlatform {
static const _eventChannel =
EventChannel('flutter.baseflow.com/geolocator_updates_android');

/// The secondary event channel used to receive [Position] updates from the
/// native platform.
static const _eventChannelFGN =
EventChannel('flutter.baseflow.com/geolocator_updates_android_fgn');

/// The event channel used to receive [LocationServiceStatus] updates from the
/// native platform.
static const _serviceStatusEventChannel =
Expand All @@ -33,6 +38,7 @@ class GeolocatorAndroid extends GeolocatorPlatform {
bool forcedLocationManager = false;

Stream<Position>? _positionStream;
Stream<Position>? _positionStreamFGN;
Stream<ServiceStatus>? _serviceStatusStream;

final Uuid _uuid = const Uuid();
Expand Down Expand Up @@ -166,21 +172,36 @@ class GeolocatorAndroid extends GeolocatorPlatform {
Stream<Position> getPositionStream({
LocationSettings? locationSettings,
}) {
if (_positionStream != null) {
return _positionStream!;
final bool useForegroundNotification = locationSettings is AndroidSettings
? locationSettings.foregroundNotificationConfig != null
: false;
if (useForegroundNotification) {
if (_positionStreamFGN != null) {
return _positionStreamFGN!;
}
} else {
if (_positionStream != null) {
return _positionStream!;
}
}
var originalStream = _eventChannel.receiveBroadcastStream(
var originalStream =
(useForegroundNotification ? _eventChannelFGN : _eventChannel)
.receiveBroadcastStream(
locationSettings?.toJson(),
);
var positionStream = _wrapStream(originalStream);
var positionStream = _wrapStream(originalStream, useForegroundNotification);

var timeLimit = locationSettings?.timeLimit;

if (timeLimit != null) {
positionStream = positionStream.timeout(
timeLimit,
onTimeout: (s) {
_positionStream = null;
if (useForegroundNotification) {
_positionStreamFGN = null;
} else {
_positionStream = null;
}
s.addError(TimeoutException(
'Time limit reached while waiting for position update.',
timeLimit,
Expand All @@ -190,7 +211,7 @@ class GeolocatorAndroid extends GeolocatorPlatform {
);
}

_positionStream = positionStream
final Stream<Position> tmpStream = positionStream
.map<Position>((dynamic element) =>
AndroidPosition.fromMap(element.cast<String, dynamic>()))
.handleError(
Expand All @@ -201,13 +222,25 @@ class GeolocatorAndroid extends GeolocatorPlatform {
throw error;
},
);
return _positionStream!;
if (useForegroundNotification) {
_positionStreamFGN = tmpStream;
} else {
_positionStream = tmpStream;
}
return tmpStream;
}

Stream<dynamic> _wrapStream(Stream<dynamic> incoming) {
Stream<dynamic> _wrapStream(
Stream<dynamic> incoming,
bool useForegroundNotification,
) {
return incoming.asBroadcastStream(onCancel: (subscription) {
subscription.cancel();
_positionStream = null;
if (useForegroundNotification) {
_positionStreamFGN = null;
} else {
_positionStream = null;
}
});
}

Expand Down