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

Implement android embedding v2 #118

Merged
merged 14 commits into from
Sep 28, 2021
Merged

Implement android embedding v2 #118

merged 14 commits into from
Sep 28, 2021

Conversation

lukaskurz
Copy link
Contributor

@lukaskurz lukaskurz commented Sep 16, 2021

This is PR is a fix for #113

Reason

The plugin does not yet support the android embedding v2, which is now part of flutter stable as of 2.5.0 afaik.
This prompts a warning when building an app that has this lib as dependency.

What was done

I followed the guide from the offical flutter website and also the guide for upgrading pre 1.12 projects.

Refactoring .idea files

I had issues opening the project using android studio, due to issues with environment and .idea files. I thus created a new plugin using the latest version flutter cli (stable 2.5.0), compared it to the plugin and accordingly changed the files (mostly build.gradle files and the .idea files). With the changed files it is now possible to open the project as recommended by the official guide, by opening the `/background_location/example/android/build.gradle file.

Implementing FlutterPlugin

The new FlutterPlugin interface requires methods for when the plugin is attached/detached from the FlutterEngine. This replaces the old registerWith(registrar) functionality, as the FlutterBinding received when attaching, contains the applicationContext and messenger, that was previously obtained from the registrar.

The guide recommends maintaining backwards compatibility to the v1 embedding, by leaving the registerWith method and use a private function which can be used by both onAttachedToEngine and registerWith. I took some inspiration from other similar libraries and did this by moving the code inside the BackgroundLocationPlugin to a seperate class called BackgroundLocationService.

Implementing ActivityAware

The ActivityAware interface requires methods for when an activity is attached/detached for various reasons and is also the starting point for doing UI related stuff, such as asking/checking for permissions. On detachment, the LocationUpdateService ~ ForegroundService, if running, is stopped.

Some refactoring

Most of the other changes are just small refactorings:

  • Moving logic from the plugin class to a seperate service class
  • Updating the preference login in Utils.kt to not use deprecated functionalities
  • Updating the various Manifests, to comply with signal v2 embedding
  • Fix a typo in the repo from backgeound to background
  • Add some documentation to methods and fields

I am by no means a Kotlin or Native Android expert, so please excuse me, if I made some egregious style/code guideline mistakes :)

edit: Also, if you could check that everything still works as you intended it, would be nice. I did test it on my device, and it seemed to work.

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