Implement android embedding v2 #118
Merged
+409
−685
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theFlutterEngine
. This replaces the oldregisterWith(registrar)
functionality, as theFlutterBinding
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 bothonAttachedToEngine
andregisterWith
. I took some inspiration from other similar libraries and did this by moving the code inside theBackgroundLocationPlugin
to a seperate class calledBackgroundLocationService
.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, theLocationUpdateService
~ ForegroundService, if running, is stopped.Some refactoring
Most of the other changes are just small refactorings:
Utils.kt
to not use deprecated functionalitiesbackgeound
tobackground
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.