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

Conversation

androidseb
Copy link

Changes summary

These changes aim to make it possible to implement a GPS track recording app with optimal battery usage in the background while also having a highly responsive UI while the app is in the foreground, without having to declare the ACCESS_BACKGROUND_LOCATION permission, essentially fulfilling the following requirements on Android:

  • When the app is active in the foreground, access location through a high frequency update stream (every 1 second)
  • When the app is inactive in the background, access location through a lower frequency update stream (every 5 seconds) to preserve battery
  • Avoid using the ACCESS_BACKGROUND_LOCATION permission at all

Without these changes, a limitation of this library on Android is that you can only have a single location stream active at a given time. And a new location stream cannot start when the app is in the background unless you have declared and granted access to the dangerous ACCESS_BACKGROUND_LOCATION permission. This limitation does not affect iOS since you can still start a location stream for a little while after the app has been put in the background.

If you want to change the location stream parameters (e.g. distanceFilter, intervalDuration, etc.), you have to cancel your current stream subscription and start a new one. Ideally, an app recording location in the background sets parameters to reduce the location updates frequency to preserve battery, but keeps a high frequency update while the app is in the foreground.

These changes allow to have two separate streams (started while the app is in the foreground) with different parameters, and cancel them individually:

  • If foregroundNotificationConfig is null, then the regular stream is started
  • If foregroundNotificationConfig is set, then the foreground service stream is started

When the app is put in the background, the high frequency UI stream can be canceled while the lower frequency foreground service stream remains active.

How to use this in your code today

If you want to test / use this code before it has been merged and published, you can use the following dependency overrides in your pubspec.yaml.

dependency_overrides:
  geolocator:
    git:
      url: https://github.com/androidseb/flutter-geolocator
      ref: feature/android_secondary_position_stream
      path: geolocator
  geolocator_android:
    git:
      url: https://github.com/androidseb/flutter-geolocator
      ref: feature/android_secondary_position_stream
      path: geolocator_android

Draft disclaimer

This is currently a draft, and the pre-launch checklist below is not complete, because I'm not sure if this is a direction the maintainers of this library would want to take. If so, please let me know, I'll put in the work to complete the checklist and polish this PR.

Pre-launch Checklist

  • I made sure the project builds.
  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is does not need version changes.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I rebased onto main.
  • I added new tests to check the change I am making, or this PR does not need tests.
  • I made sure all existing and new tests are passing.
  • I ran dart format . and committed any changes.
  • I ran flutter analyze and fixed any errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant