Almoullim / background_location

Flutter background location plugin for Android and iOS
https://pub.dev/packages/background_location
Other
224 stars 236 forks source link

Implement android embedding v2 #118

Closed lukaskurz closed 3 years ago

lukaskurz commented 3 years ago

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:

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.