Skip to content

Commit

Permalink
Android location stream not cancelling (#1153)
Browse files Browse the repository at this point in the history
* Fixes a bug where the Android services does not shut down correctly

* Update foreground notification description
  • Loading branch information
Wackymax committed Oct 14, 2022
1 parent 85b45ff commit 57b82e9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions geolocator_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.1.4

- Fix a bug where the location service would not stop correctly when the location stream is cancelled.

## 4.1.3

- Export `AndroidResource` class at `geolocator_android.at`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class GeolocatorLocationService extends Service {
// Service is foreground
private boolean isForeground = false;
private int connectedEngines = 0;
private int listenerCount = 0;
@Nullable private Activity activity = null;
@Nullable private GeolocationManager geolocationManager = null;
@Nullable private LocationClient locationClient;
Expand Down Expand Up @@ -83,7 +84,10 @@ public void onDestroy() {
super.onDestroy();
}

public boolean canStopLocationService() {
public boolean canStopLocationService(boolean cancellationRequested) {
if(cancellationRequested) {
return listenerCount == 1;
}
return connectedEngines == 0;
}

Expand All @@ -104,6 +108,7 @@ public void startLocationService(
LocationOptions locationOptions,
EventChannel.EventSink events) {

listenerCount++;
if (geolocationManager != null) {
locationClient =
geolocationManager.createLocationClient(
Expand All @@ -121,6 +126,7 @@ public void startLocationService(
}

public void stopLocationService() {
listenerCount--;
Log.d(TAG, "Stopping location service.");
if (locationClient != null && geolocationManager != null) {
geolocationManager.stopPositionUpdates(locationClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void stopListening() {
return;
}

disposeListeners();
disposeListeners(false);
channel.setStreamHandler(null);
channel = null;
}
Expand Down Expand Up @@ -145,12 +145,12 @@ public void onListen(Object arguments, EventChannel.EventSink events) {

@Override
public void onCancel(Object arguments) {
disposeListeners();
disposeListeners(true);
}

private void disposeListeners() {
private void disposeListeners(boolean cancelled) {
Log.e(TAG, "Geolocator position updates stopped");
if (foregroundLocationService != null && foregroundLocationService.canStopLocationService()) {
if (foregroundLocationService != null && foregroundLocationService.canStopLocationService(cancelled)) {
foregroundLocationService.stopLocationService();
foregroundLocationService.disableBackgroundMode();
} else {
Expand Down
6 changes: 6 additions & 0 deletions geolocator_android/lib/src/types/android_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class AndroidSettings extends LocationSettings {

/// If this is set then the services is started as a Foreground service with a persistent notification
/// showing the user that the service will continue running in the background.
///
/// Note: Using this foreground notification does not run your service in the background, it just
/// increases the priority of your activity making it less likely for Android to kill the activity
/// when switching between apps. It does not prevent Android from killing the activity. If you want to
/// receive background location updates even if the activity is destroyed you need to use a third party
/// background service package that will start a new Flutter Engine that is not tied to your main activity.
final ForegroundNotificationConfig? foregroundNotificationConfig;

/// Set to true if altitude should be calculated as MSL (EGM2008) from NMEA messages
Expand Down
2 changes: 1 addition & 1 deletion geolocator_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: geolocator_android
description: Geolocation plugin for Flutter. This plugin provides the Android implementation for the geolocator.
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_android
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
version: 4.1.3
version: 4.1.4

environment:
sdk: ">=2.15.0 <3.0.0"
Expand Down

0 comments on commit 57b82e9

Please sign in to comment.