diff --git a/CHANGELOG.md b/CHANGELOG.md
index 81a7d176..b42a91f0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,57 @@
+## 2.0.6
+* Fixing iOS build
+* Fixing memory leak on Android
+* Fixing callbacks on older Android devices
+## 2.0.6-dev.1
+* Fix Type mismatch: inferred type is String? but Any was expected
+
+## 2.0.6-dev.2
+* Fixing iOS build
+* Fixing memory leak on Android
+* Fixing callbacks on older Android devices
+
+## 2.0.6-dev.1
+* Fix Type mismatch: inferred type is String? but Any was expected
+
+## 2.0.5
+* Fixing demo gif
+
+## 2.0.4
+* Merging 2.0.4-dev1 and 2.0.4-dev2 to the main branch
+
+## 2.0.4-dev.2
+* Adding auto stop on app terminate on iOS
+* Fixing import on example test script
+
+## 2.0.4-dev.1
+* Fixing iOS build
+* Replacing package:background_locator_2 by background_locator_2
+
+## 2.0.3
+* Replacing background_locator by background_locator_2 **(BREAKING CHANGE)**
+* Replacing rekab by yukams
+
+## 2.0.2
+* Adding fields to pubspec.yaml
+* Changing README.md to prepare it to be published on pub.dev
+* Changing LICENCE file
+
+## 2.0.1
+* Reverting minSdk from 31 to 29
+
+## 2.0.0
+* New fork
+* Support for Flutter 3.0
+* Support for NullSafety
+* Fix initCallback is now called first as expected
+* Fix callback not called
+* Fix example
+* Fix FLAG_IMMUTABLE error for SDK 23+
+* Fix library not working after a phone reboot
+* Fix library not working after an app crash due to revoking permissions
+* Adding a bit more logs
+* Changing minSdk from 29 to 31
+
## 1.6.12
* Fixes onStatusChanged crash;
* Fixed issue #94;
diff --git a/LICENSE b/LICENSE
index 5a2cdb00..b0546dd0 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
Copyright 2020 REKAB
+Copyright 2022 Yukams
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
diff --git a/README.md b/README.md
index 583abc9e..64508f9a 100644
--- a/README.md
+++ b/README.md
@@ -1,25 +1,27 @@
+This package is a V2 of the background_locator package, fixing it and making it work for the newest versions of Flutter. Please read the wiki in order to make this plugin work with flutter 3.x.
-# background_locator  [](https://pub.dartlang.org/packages/background_locator)  
+# background_locator_2 ! [](https://pub.dartlang.org/packages/background_locator_2)  
A Flutter plugin for getting location updates even when the app is killed.

-Refer to [wiki](https://github.com/rekab-app/background_locator/wiki) page for install and setup instruction or jump to specific subject with below links:
+Refer to [wiki](https://github.com/Yukams/background_locator_fixed/wiki) page for install and setup instruction or jump to specific subject with below links:
-* [Installation](https://github.com/rekab-app/background_locator/wiki/Installation)
-* [Setup](https://github.com/rekab-app/background_locator/wiki/Setup)
-* [How to use](https://github.com/rekab-app/background_locator/wiki/How-to-use)
-* [Use other plugins in callback](https://github.com/rekab-app/background_locator/wiki/Use-other-plugins-in-callback)
-* [Stop on app terminate](https://github.com/rekab-app/background_locator/wiki/Stop-on-app-terminate)
-* [LocationSettings options](https://github.com/rekab-app/background_locator/wiki/LocationSettings-options)
-* [Restart service on device reboot (Android only)](https://github.com/rekab-app/background_locator/wiki/Restart-service-on-device-reboot)
+* [Installation](https://github.com/Yukams/background_locator_fixed/wiki/Installation)
+* [Setup](https://github.com/Yukams/background_locator_fixed/wiki/Setup)
+* [How to use](https://github.com/Yukams/background_locator_fixed/wiki/How-to-use)
+* [Use other plugins in callback](https://github.com/Yukams/background_locator_fixed/wiki/Use-other-plugins-in-callback)
+* [Stop on app terminate](https://github.com/Yukams/background_locator_fixed/wiki/Stop-on-app-terminate)
+* [LocationSettings options](https://github.com/Yukams/background_locator_fixed/wiki/LocationSettings-options)
+* [Restart service on device reboot (Android only)](https://github.com/Yukams/background_locator_fixed/wiki/Restart-service-on-device-reboot)
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
## Contributor
Thanks to all who contributed on this plugin to fix bugs and adding new feature, including:
+* [Rekab](https://github.com/rekabhq) (creator of V1)
* [Gerardo Ibarra](https://github.com/gpibarra)
* [RomanJos](https://github.com/RomanJos)
* [Marcelo Henrique Neppel](https://github.com/marceloneppel)
diff --git a/android/build.gradle b/android/build.gradle
index 82b8893c..35b2a6a9 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -1,15 +1,15 @@
-group 'rekab.app.background_locator'
+group 'yukams.app.background_locator_2'
version '1.0-SNAPSHOT'
buildscript {
- ext.kotlin_version = '1.4.21'
+ ext.kotlin_version = '1.7.20'
repositories {
google()
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:4.1.1'
+ classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
@@ -25,13 +25,13 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
- compileSdkVersion 30
+ compileSdkVersion 29
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
- minSdkVersion 16
+ minSdkVersion 19
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
@@ -41,6 +41,7 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
- implementation "com.google.android.gms:play-services-location:18.0.0"
+ implementation "com.google.android.gms:play-services-location:21.0.1"
implementation 'com.google.code.gson:gson:2.8.6'
+ implementation 'com.google.android.material:material:1.0.0'
}
diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties
index e4bd74a3..a4552108 100644
--- a/android/gradle/wrapper/gradle-wrapper.properties
+++ b/android/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip
diff --git a/android/settings.gradle b/android/settings.gradle
index 96d4be49..58ff1d48 100644
--- a/android/settings.gradle
+++ b/android/settings.gradle
@@ -1 +1 @@
-rootProject.name = 'background_locator'
+rootProject.name = 'background_locator_2'
diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml
index 3c4a94fa..14c2cb86 100644
--- a/android/src/main/AndroidManifest.xml
+++ b/android/src/main/AndroidManifest.xml
@@ -1 +1 @@
-
+
diff --git a/android/src/main/kotlin/rekab/app/background_locator/IsolateHolderExtension.kt b/android/src/main/kotlin/rekab/app/background_locator/IsolateHolderExtension.kt
deleted file mode 100644
index 9c0f65d0..00000000
--- a/android/src/main/kotlin/rekab/app/background_locator/IsolateHolderExtension.kt
+++ /dev/null
@@ -1,65 +0,0 @@
-package rekab.app.background_locator
-
-import android.content.Context
-import android.content.Intent
-import com.google.android.gms.location.LocationRequest
-import io.flutter.FlutterInjector
-import io.flutter.embedding.engine.FlutterEngine
-import io.flutter.embedding.engine.dart.DartExecutor
-import io.flutter.plugin.common.MethodChannel
-import io.flutter.view.FlutterCallbackInformation
-import rekab.app.background_locator.provider.LocationRequestOptions
-import java.util.concurrent.atomic.AtomicBoolean
-
-internal fun IsolateHolderService.startLocatorService(context: Context) {
-
- val serviceStarted = AtomicBoolean(IsolateHolderService.isServiceRunning)
- // start synchronized block to prevent multiple service instant
- synchronized(serviceStarted) {
- this.context = context
- if (IsolateHolderService.backgroundEngine == null) {
-
- val callbackHandle = context.getSharedPreferences(
- Keys.SHARED_PREFERENCES_KEY,
- Context.MODE_PRIVATE)
- .getLong(Keys.CALLBACK_DISPATCHER_HANDLE_KEY, 0)
- val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
-
- // We need flutter engine to handle callback, so if it is not available we have to create a
- // Flutter engine without any view
- IsolateHolderService.backgroundEngine = FlutterEngine(context)
-
- val args = DartExecutor.DartCallback(
- context.assets,
- FlutterInjector.instance().flutterLoader().findAppBundlePath(),
- callbackInfo
- )
- IsolateHolderService.backgroundEngine?.dartExecutor?.executeDartCallback(args)
- }
- }
-
- backgroundChannel =
- MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger,
- Keys.BACKGROUND_CHANNEL_ID)
- backgroundChannel.setMethodCallHandler(this)
-}
-
-fun getLocationRequest(intent: Intent): LocationRequestOptions {
- val interval: Long = (intent.getIntExtra(Keys.SETTINGS_INTERVAL, 10) * 1000).toLong()
- val accuracyKey = intent.getIntExtra(Keys.SETTINGS_ACCURACY, 4)
- val accuracy = getAccuracy(accuracyKey)
- val distanceFilter = intent.getDoubleExtra(Keys.SETTINGS_DISTANCE_FILTER, 0.0)
-
- return LocationRequestOptions(interval, accuracy, distanceFilter.toFloat())
-}
-
-fun getAccuracy(key: Int): Int {
- return when (key) {
- 0 -> LocationRequest.PRIORITY_NO_POWER
- 1 -> LocationRequest.PRIORITY_LOW_POWER
- 2 -> LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
- 3 -> LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
- 4 -> LocationRequest.PRIORITY_HIGH_ACCURACY
- else -> LocationRequest.PRIORITY_HIGH_ACCURACY
- }
-}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/pluggables/DisposePluggable.kt b/android/src/main/kotlin/rekab/app/background_locator/pluggables/DisposePluggable.kt
deleted file mode 100644
index 69358109..00000000
--- a/android/src/main/kotlin/rekab/app/background_locator/pluggables/DisposePluggable.kt
+++ /dev/null
@@ -1,26 +0,0 @@
-package rekab.app.background_locator.pluggables
-
-import android.content.Context
-import android.os.Handler
-import io.flutter.plugin.common.MethodChannel
-import rekab.app.background_locator.IsolateHolderService
-import rekab.app.background_locator.Keys
-import rekab.app.background_locator.PreferencesManager
-
-class DisposePluggable : Pluggable {
- override fun setCallback(context: Context, callbackHandle: Long) {
- PreferencesManager.setCallbackHandle(context, Keys.DISPOSE_CALLBACK_HANDLE_KEY, callbackHandle)
- }
-
- override fun onServiceDispose(context: Context) {
- (PreferencesManager.getCallbackHandle(context, Keys.DISPOSE_CALLBACK_HANDLE_KEY))?.let { disposeCallback ->
- val backgroundChannel = MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger,
- Keys.BACKGROUND_CHANNEL_ID)
- Handler(context.mainLooper)
- .post {
- backgroundChannel.invokeMethod(Keys.BCM_DISPOSE,
- hashMapOf(Keys.ARG_DISPOSE_CALLBACK to disposeCallback))
- }
- }
- }
-}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/pluggables/InitPluggable.kt b/android/src/main/kotlin/rekab/app/background_locator/pluggables/InitPluggable.kt
deleted file mode 100644
index a33a7ecf..00000000
--- a/android/src/main/kotlin/rekab/app/background_locator/pluggables/InitPluggable.kt
+++ /dev/null
@@ -1,41 +0,0 @@
-package rekab.app.background_locator.pluggables
-
-import android.content.Context
-import android.os.Handler
-import io.flutter.plugin.common.MethodChannel
-import rekab.app.background_locator.IsolateHolderService
-import rekab.app.background_locator.Keys
-import rekab.app.background_locator.PreferencesManager
-
-class InitPluggable : Pluggable {
- private var isInitCallbackCalled = false
-
- override fun setCallback(context: Context, callbackHandle: Long) {
- PreferencesManager.setCallbackHandle(context, Keys.INIT_CALLBACK_HANDLE_KEY, callbackHandle)
-
- }
-
- override fun onServiceStart(context: Context) {
- if (!isInitCallbackCalled) {
- (PreferencesManager.getCallbackHandle(context, Keys.INIT_CALLBACK_HANDLE_KEY))?.let { initCallback ->
- val initialDataMap = PreferencesManager.getDataCallback(context, Keys.INIT_DATA_CALLBACK_KEY)
- val backgroundChannel = MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger,
- Keys.BACKGROUND_CHANNEL_ID)
- Handler(context.mainLooper)
- .post {
- backgroundChannel.invokeMethod(Keys.BCM_INIT,
- hashMapOf(Keys.ARG_INIT_CALLBACK to initCallback, Keys.ARG_INIT_DATA_CALLBACK to initialDataMap))
- }
- }
- isInitCallbackCalled = true
- }
- }
-
- override fun onServiceDispose(context: Context) {
- isInitCallbackCalled = false
- }
-
- fun setInitData(context: Context, data: Map<*, *>) {
- PreferencesManager.setDataCallback(context, Keys.INIT_DATA_CALLBACK_KEY, data)
- }
-}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationUpdateListener.kt b/android/src/main/kotlin/rekab/app/background_locator/provider/LocationUpdateListener.kt
deleted file mode 100644
index fa89abcf..00000000
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationUpdateListener.kt
+++ /dev/null
@@ -1,7 +0,0 @@
-package rekab.app.background_locator.provider
-
-import java.util.HashMap
-
-interface LocationUpdateListener {
- fun onLocationUpdated(location: HashMap?)
-}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/BackgroundLocatorPlugin.kt b/android/src/main/kotlin/yukams/app/background_locator_2/BackgroundLocatorPlugin.kt
similarity index 82%
rename from android/src/main/kotlin/rekab/app/background_locator/BackgroundLocatorPlugin.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/BackgroundLocatorPlugin.kt
index 405b458b..c6751fa7 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/BackgroundLocatorPlugin.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/BackgroundLocatorPlugin.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator
+package yukams.app.background_locator_2
import android.Manifest
import android.annotation.SuppressLint
@@ -19,12 +19,12 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry
-import rekab.app.background_locator.pluggables.DisposePluggable
-import rekab.app.background_locator.pluggables.InitPluggable
+import yukams.app.background_locator_2.pluggables.DisposePluggable
+import yukams.app.background_locator_2.pluggables.InitPluggable
class BackgroundLocatorPlugin
: MethodCallHandler, FlutterPlugin, PluginRegistry.NewIntentListener, ActivityAware {
- private var context: Context? = null
+ var context: Context? = null
private var activity: Activity? = null
companion object {
@@ -99,23 +99,24 @@ class BackgroundLocatorPlugin
@JvmStatic
private fun startIsolateService(context: Context, settings: Map<*, *>) {
+ Log.e("BackgroundLocatorPlugin", "startIsolateService")
val intent = Intent(context, IsolateHolderService::class.java)
intent.action = IsolateHolderService.ACTION_START
intent.putExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_CHANNEL_NAME,
- settings[Keys.SETTINGS_ANDROID_NOTIFICATION_CHANNEL_NAME] as String)
+ settings[Keys.SETTINGS_ANDROID_NOTIFICATION_CHANNEL_NAME] as? String)
intent.putExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE,
- settings[Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE] as String)
+ settings[Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE] as? String)
intent.putExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_MSG,
- settings[Keys.SETTINGS_ANDROID_NOTIFICATION_MSG] as String)
+ settings[Keys.SETTINGS_ANDROID_NOTIFICATION_MSG] as? String)
intent.putExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG,
- settings[Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG] as String)
+ settings[Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG] as? String)
intent.putExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_ICON,
- settings[Keys.SETTINGS_ANDROID_NOTIFICATION_ICON] as String)
+ settings[Keys.SETTINGS_ANDROID_NOTIFICATION_ICON] as? String)
intent.putExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_ICON_COLOR,
- settings[Keys.SETTINGS_ANDROID_NOTIFICATION_ICON_COLOR] as Long)
- intent.putExtra(Keys.SETTINGS_INTERVAL, settings[Keys.SETTINGS_INTERVAL] as Int)
- intent.putExtra(Keys.SETTINGS_ACCURACY, settings[Keys.SETTINGS_ACCURACY] as Int)
- intent.putExtra(Keys.SETTINGS_DISTANCE_FILTER, settings[Keys.SETTINGS_DISTANCE_FILTER] as Double)
+ settings[Keys.SETTINGS_ANDROID_NOTIFICATION_ICON_COLOR] as? Long)
+ intent.putExtra(Keys.SETTINGS_INTERVAL, settings[Keys.SETTINGS_INTERVAL] as? Int)
+ intent.putExtra(Keys.SETTINGS_ACCURACY, settings[Keys.SETTINGS_ACCURACY] as? Int)
+ intent.putExtra(Keys.SETTINGS_DISTANCE_FILTER, settings[Keys.SETTINGS_DISTANCE_FILTER] as? Double)
if (settings.containsKey(Keys.SETTINGS_ANDROID_WAKE_LOCK_TIME)) {
intent.putExtra(Keys.SETTINGS_ANDROID_WAKE_LOCK_TIME,
@@ -136,6 +137,7 @@ class BackgroundLocatorPlugin
private fun stopIsolateService(context: Context) {
val intent = Intent(context, IsolateHolderService::class.java)
intent.action = IsolateHolderService.ACTION_SHUTDOWN
+ Log.d("BackgroundLocatorPlugin", "stopIsolateService => Shutting down locator plugin")
ContextCompat.startForegroundService(context, intent)
}
@@ -205,26 +207,35 @@ class BackgroundLocatorPlugin
initializeService(context, args)
val settings = args[Keys.ARG_SETTINGS] as Map<*, *>
- startIsolateService(context, settings)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
+ context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
+ == PackageManager.PERMISSION_GRANTED
+ ) {
+ startIsolateService(context, settings)
+ }
}
}
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
Keys.METHOD_PLUGIN_INITIALIZE_SERVICE -> {
- val args: Map = call.arguments()
+ val args: Map? = call.arguments()
// save callback dispatcher to use it when device reboots
- PreferencesManager.saveCallbackDispatcher(context!!, args)
+ PreferencesManager.saveCallbackDispatcher(context!! , args!!)
+
+
+
initializeService(context!!, args)
result.success(true)
}
Keys.METHOD_PLUGIN_REGISTER_LOCATION_UPDATE -> {
- val args: Map = call.arguments()
+ val args: Map? = call.arguments()
// save setting to use it when device reboots
- PreferencesManager.saveSettings(context!!, args)
+
+ PreferencesManager.saveSettings(context!!, args!!)
registerLocator(context!!,
args,
@@ -240,8 +251,11 @@ class BackgroundLocatorPlugin
return
}
- val args: Map = call.arguments()
- updateNotificationText(context!!, args)
+ val args: Map? = call.arguments()
+
+ updateNotificationText(context!!, args!!)
+
+
result.success(true)
}
else -> result.notImplemented()
@@ -263,22 +277,33 @@ class BackgroundLocatorPlugin
channel?.setMethodCallHandler(plugin)
}
- override fun onNewIntent(intent: Intent?): Boolean {
- if (intent?.action != Keys.NOTIFICATION_ACTION) {
+ override fun onNewIntent(intent: Intent): Boolean {
+ if (intent.action != Keys.NOTIFICATION_ACTION) {
// this is not our notification
return false
}
- val notificationCallback = PreferencesManager.getCallbackHandle(activity!!, Keys.NOTIFICATION_CALLBACK_HANDLE_KEY)
- if (notificationCallback != null && IsolateHolderService.backgroundEngine != null) {
- val backgroundChannel =
- MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
- activity?.mainLooper?.let {
- Handler(it)
- .post {
- backgroundChannel.invokeMethod(Keys.BCM_NOTIFICATION_CLICK,
- hashMapOf(Keys.ARG_NOTIFICATION_CALLBACK to notificationCallback))
- }
+ IsolateHolderService.getBinaryMessenger(context)?.let { binaryMessenger ->
+ val notificationCallback =
+ PreferencesManager.getCallbackHandle(
+ activity!!,
+ Keys.NOTIFICATION_CALLBACK_HANDLE_KEY
+ )
+ if (notificationCallback != null && IsolateHolderService.backgroundEngine != null) {
+ val backgroundChannel =
+ MethodChannel(
+ binaryMessenger,
+ Keys.BACKGROUND_CHANNEL_ID
+ )
+ activity?.mainLooper?.let {
+ Handler(it)
+ .post {
+ backgroundChannel.invokeMethod(
+ Keys.BCM_NOTIFICATION_CLICK,
+ hashMapOf(Keys.ARG_NOTIFICATION_CALLBACK to notificationCallback)
+ )
+ }
+ }
}
}
@@ -300,4 +325,4 @@ class BackgroundLocatorPlugin
}
-}
+}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/BootBroadcastReceiver.kt b/android/src/main/kotlin/yukams/app/background_locator_2/BootBroadcastReceiver.kt
similarity index 90%
rename from android/src/main/kotlin/rekab/app/background_locator/BootBroadcastReceiver.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/BootBroadcastReceiver.kt
index ac618f63..c93264fd 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/BootBroadcastReceiver.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/BootBroadcastReceiver.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator
+package yukams.app.background_locator_2
import android.content.BroadcastReceiver
import android.content.Context
diff --git a/android/src/main/kotlin/yukams/app/background_locator_2/IsolateHolderExtension.kt b/android/src/main/kotlin/yukams/app/background_locator_2/IsolateHolderExtension.kt
new file mode 100644
index 00000000..03faf12b
--- /dev/null
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/IsolateHolderExtension.kt
@@ -0,0 +1,103 @@
+package yukams.app.background_locator_2
+
+import android.Manifest
+import android.content.Context
+import android.content.Intent
+import android.content.pm.PackageManager
+import android.os.Build
+import android.util.Log
+import com.google.android.gms.location.LocationRequest
+import io.flutter.FlutterInjector
+import io.flutter.embedding.engine.FlutterEngine
+import io.flutter.embedding.engine.dart.DartExecutor
+import io.flutter.plugin.common.MethodChannel
+import io.flutter.view.FlutterCallbackInformation
+import yukams.app.background_locator_2.IsolateHolderService.Companion.isServiceInitialized
+import yukams.app.background_locator_2.provider.LocationRequestOptions
+import java.lang.RuntimeException
+import java.util.concurrent.atomic.AtomicBoolean
+
+internal fun IsolateHolderService.startLocatorService(context: Context) {
+
+ val serviceStarted = AtomicBoolean(IsolateHolderService.isServiceRunning)
+ // start synchronized block to prevent multiple service instant
+ synchronized(serviceStarted) {
+ this.context = context
+ // resetting the background engine to avoid being stuck after an app crash
+ IsolateHolderService.backgroundEngine?.destroy();
+ IsolateHolderService.backgroundEngine = null
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
+ context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
+ == PackageManager.PERMISSION_GRANTED
+ ) {
+ // We need flutter engine to handle callback, so if it is not available we have to create a
+ // Flutter engine without any view
+ Log.e("IsolateHolderService", "startLocatorService: Start Flutter Engine")
+ IsolateHolderService.backgroundEngine = FlutterEngine(context)
+
+ val callbackHandle = context.getSharedPreferences(
+ Keys.SHARED_PREFERENCES_KEY,
+ Context.MODE_PRIVATE
+ )
+ .getLong(Keys.CALLBACK_DISPATCHER_HANDLE_KEY, 0)
+ val callbackInfo =
+ FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
+
+ if(callbackInfo == null) {
+ Log.e("IsolateHolderExtension", "Fatal: failed to find callback");
+ return;
+ }
+
+ val args = DartExecutor.DartCallback(
+ context.assets,
+ FlutterInjector.instance().flutterLoader().findAppBundlePath(),
+ callbackInfo
+ )
+ IsolateHolderService.backgroundEngine?.dartExecutor?.executeDartCallback(args)
+ isServiceInitialized = true
+ Log.e("IsolateHolderExtension", "service initialized")
+ }
+ } catch (e: UnsatisfiedLinkError) {
+ e.printStackTrace()
+ }
+ }
+
+ IsolateHolderService.getBinaryMessenger(context)?.let { binaryMessenger ->
+ backgroundChannel =
+ MethodChannel(
+ binaryMessenger,
+ Keys.BACKGROUND_CHANNEL_ID
+ )
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
+ context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
+ == PackageManager.PERMISSION_GRANTED
+ ) {
+ backgroundChannel.setMethodCallHandler(this)
+ }
+ } catch (e: RuntimeException) {
+ e.printStackTrace()
+ }
+ }
+}
+
+fun getLocationRequest(intent: Intent): LocationRequestOptions {
+ val interval: Long = (intent.getIntExtra(Keys.SETTINGS_INTERVAL, 10) * 1000).toLong()
+ val accuracyKey = intent.getIntExtra(Keys.SETTINGS_ACCURACY, 4)
+ val accuracy = getAccuracy(accuracyKey)
+ val distanceFilter = intent.getDoubleExtra(Keys.SETTINGS_DISTANCE_FILTER, 0.0)
+
+ return LocationRequestOptions(interval, accuracy, distanceFilter.toFloat())
+}
+
+fun getAccuracy(key: Int): Int {
+ return when (key) {
+ 0 -> LocationRequest.PRIORITY_NO_POWER
+ 1 -> LocationRequest.PRIORITY_LOW_POWER
+ 2 -> LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
+ 3 -> LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
+ 4 -> LocationRequest.PRIORITY_HIGH_ACCURACY
+ else -> LocationRequest.PRIORITY_HIGH_ACCURACY
+ }
+}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/IsolateHolderService.kt b/android/src/main/kotlin/yukams/app/background_locator_2/IsolateHolderService.kt
similarity index 54%
rename from android/src/main/kotlin/rekab/app/background_locator/IsolateHolderService.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/IsolateHolderService.kt
index a5dc9e41..0697a24b 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/IsolateHolderService.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/IsolateHolderService.kt
@@ -1,6 +1,7 @@
-package rekab.app.background_locator
+package yukams.app.background_locator_2
import android.app.*
+import android.Manifest
import android.content.Context
import android.content.Intent
import android.os.Build
@@ -9,15 +10,18 @@ import android.os.IBinder
import android.os.PowerManager
import android.util.Log
import androidx.core.app.NotificationCompat
+import android.content.pm.PackageManager
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
+import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
-import rekab.app.background_locator.pluggables.DisposePluggable
-import rekab.app.background_locator.pluggables.InitPluggable
-import rekab.app.background_locator.pluggables.Pluggable
-import rekab.app.background_locator.provider.*
+import yukams.app.background_locator_2.pluggables.DisposePluggable
+import yukams.app.background_locator_2.pluggables.InitPluggable
+import yukams.app.background_locator_2.pluggables.Pluggable
+import yukams.app.background_locator_2.provider.*
import java.util.HashMap
+import androidx.core.app.ActivityCompat
class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateListener, Service() {
companion object {
@@ -41,18 +45,33 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
@JvmStatic
var isServiceRunning = false
+
+ @JvmStatic
+ var isServiceInitialized = false
+
+ fun getBinaryMessenger(context: Context?): BinaryMessenger? {
+ val messenger = backgroundEngine?.dartExecutor?.binaryMessenger
+ return messenger
+ ?: if (context != null) {
+ backgroundEngine = FlutterEngine(context)
+ backgroundEngine?.dartExecutor?.binaryMessenger
+ }else{
+ messenger
+ }
+ }
}
private var notificationChannelName = "Flutter Locator Plugin"
private var notificationTitle = "Start Location Tracking"
private var notificationMsg = "Track location in background"
- private var notificationBigMsg = "Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running."
+ private var notificationBigMsg =
+ "Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running."
private var notificationIconColor = 0
private var icon = 0
private var wakeLockTime = 60 * 60 * 1000L // 1 hour default wake lock time
private var locatorClient: BLLocationProvider? = null
internal lateinit var backgroundChannel: MethodChannel
- internal lateinit var context: Context
+ internal var context: Context? = null
private var pluggables: ArrayList = ArrayList()
override fun onBind(intent: Intent?): IBinder? {
@@ -78,15 +97,17 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
startForeground(notificationId, notification)
pluggables.forEach {
- it.onServiceStart(context)
+ context?.let { it1 -> it.onServiceStart(it1) }
}
}
private fun getNotification(): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Notification channel is available in Android O and up
- val channel = NotificationChannel(Keys.CHANNEL_ID, notificationChannelName,
- NotificationManager.IMPORTANCE_LOW)
+ val channel = NotificationChannel(
+ Keys.CHANNEL_ID, notificationChannelName,
+ NotificationManager.IMPORTANCE_LOW
+ )
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.createNotificationChannel(channel)
@@ -95,14 +116,18 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
val intent = Intent(this, getMainActivityClass(this))
intent.action = Keys.NOTIFICATION_ACTION
- val pendingIntent: PendingIntent = PendingIntent.getActivity(this,
- 1, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+ val pendingIntent: PendingIntent = PendingIntent.getActivity(
+ this,
+ 1, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+ )
return NotificationCompat.Builder(this, Keys.CHANNEL_ID)
.setContentTitle(notificationTitle)
.setContentText(notificationMsg)
- .setStyle(NotificationCompat.BigTextStyle()
- .bigText(notificationBigMsg))
+ .setStyle(
+ NotificationCompat.BigTextStyle()
+ .bigText(notificationBigMsg)
+ )
.setSmallIcon(icon)
.setColor(notificationIconColor)
.setPriority(NotificationCompat.PRIORITY_HIGH)
@@ -113,22 +138,35 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
- if (intent == null) {
- return super.onStartCommand(intent, flags, startId)
+ Log.e("IsolateHolderService", "onStartCommand => intent.action : ${intent?.action}")
+ if(intent == null) {
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
+ || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
+ Log.e("IsolateHolderService", "app has crashed, stopping it")
+ stopSelf()
+ }
+ else {
+ return super.onStartCommand(intent, flags, startId)
+ }
}
when {
- ACTION_SHUTDOWN == intent.action -> {
+ ACTION_SHUTDOWN == intent?.action -> {
isServiceRunning = false
shutdownHolderService()
}
- ACTION_START == intent.action -> {
+ ACTION_START == intent?.action -> {
+ if (isServiceRunning) {
+ isServiceRunning = false
+ shutdownHolderService()
+ }
+
if (!isServiceRunning) {
isServiceRunning = true
startHolderService(intent)
}
}
- ACTION_UPDATE_NOTIFICATION == intent.action -> {
+ ACTION_UPDATE_NOTIFICATION == intent?.action -> {
if (isServiceRunning) {
updateNotification(intent)
}
@@ -139,24 +177,29 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
}
private fun startHolderService(intent: Intent) {
- notificationChannelName = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_CHANNEL_NAME).toString()
- notificationTitle = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE).toString()
+ Log.e("IsolateHolderService", "startHolderService")
+ notificationChannelName =
+ intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_CHANNEL_NAME).toString()
+ notificationTitle =
+ intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE).toString()
notificationMsg = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_MSG).toString()
- notificationBigMsg = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG).toString()
+ notificationBigMsg =
+ intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG).toString()
val iconNameDefault = "ic_launcher"
var iconName = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_ICON)
if (iconName == null || iconName.isEmpty()) {
iconName = iconNameDefault
}
icon = resources.getIdentifier(iconName, "mipmap", packageName)
- notificationIconColor = intent.getLongExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_ICON_COLOR, 0).toInt()
+ notificationIconColor =
+ intent.getLongExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_ICON_COLOR, 0).toInt()
wakeLockTime = intent.getIntExtra(Keys.SETTINGS_ANDROID_WAKE_LOCK_TIME, 60) * 60 * 1000L
- locatorClient = getLocationClient(context)
+ locatorClient = context?.let { getLocationClient(it) }
locatorClient?.requestLocationUpdates(getLocationRequest(intent))
// Fill pluggable list
- if( intent.hasExtra(Keys.SETTINGS_INIT_PLUGGABLE)) {
+ if (intent.hasExtra(Keys.SETTINGS_INIT_PLUGGABLE)) {
pluggables.add(InitPluggable())
}
@@ -168,6 +211,7 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
}
private fun shutdownHolderService() {
+ Log.e("IsolateHolderService", "shutdownHolderService")
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG).apply {
if (isHeld) {
@@ -181,25 +225,30 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
stopSelf()
pluggables.forEach {
- it.onServiceDispose(context)
+ context?.let { it1 -> it.onServiceDispose(it1) }
}
}
private fun updateNotification(intent: Intent) {
+ Log.e("IsolateHolderService", "updateNotification")
if (intent.hasExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE)) {
- notificationTitle = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE).toString()
+ notificationTitle =
+ intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_TITLE).toString()
}
if (intent.hasExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_MSG)) {
- notificationMsg = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_MSG).toString()
+ notificationMsg =
+ intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_MSG).toString()
}
if (intent.hasExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG)) {
- notificationBigMsg = intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG).toString()
+ notificationBigMsg =
+ intent.getStringExtra(Keys.SETTINGS_ANDROID_NOTIFICATION_BIG_MSG).toString()
}
val notification = getNotification()
- val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+ val notificationManager =
+ getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notification)
}
@@ -217,14 +266,18 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
- when (call.method) {
- Keys.METHOD_SERVICE_INITIALIZED -> {
- isServiceRunning = true
+ try {
+ when (call.method) {
+ Keys.METHOD_SERVICE_INITIALIZED -> {
+ isServiceRunning = true
+ }
+ else -> result.notImplemented()
}
- else -> result.notImplemented()
- }
- result.success(null)
+ result.success(null)
+ } catch (e: Exception) {
+
+ }
}
override fun onDestroy() {
@@ -240,21 +293,37 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
}
}
- override fun onLocationUpdated(location: HashMap?) {
- FlutterInjector.instance().flutterLoader().ensureInitializationComplete(context, null)
-
- //https://github.com/flutter/plugins/pull/1641
- //https://github.com/flutter/flutter/issues/36059
- //https://github.com/flutter/plugins/pull/1641/commits/4358fbba3327f1fa75bc40df503ca5341fdbb77d
- // new version of flutter can not invoke method from background thread
- if (location != null) {
- val callback = PreferencesManager.getCallbackHandle(context, Keys.CALLBACK_HANDLE_KEY) as Long
+ override fun onLocationUpdated(location: HashMap?) {
+ try {
+ context?.let {
+ FlutterInjector.instance().flutterLoader().ensureInitializationComplete(
+ it, null
+ )
+ }
- val result: HashMap =
- hashMapOf(Keys.ARG_CALLBACK to callback,
- Keys.ARG_LOCATION to location)
+ //https://github.com/flutter/plugins/pull/1641
+ //https://github.com/flutter/flutter/issues/36059
+ //https://github.com/flutter/plugins/pull/1641/commits/4358fbba3327f1fa75bc40df503ca5341fdbb77d
+ // new version of flutter can not invoke method from background thread
+ if (location != null) {
+ val callback =
+ context?.let {
+ PreferencesManager.getCallbackHandle(
+ it,
+ Keys.CALLBACK_HANDLE_KEY
+ )
+ } as Long
+
+ val result: HashMap =
+ hashMapOf(
+ Keys.ARG_CALLBACK to callback,
+ Keys.ARG_LOCATION to location
+ )
+
+ sendLocationEvent(result)
+ }
+ } catch (e: Exception) {
- sendLocationEvent(result)
}
}
@@ -265,14 +334,18 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
// new version of flutter can not invoke method from background thread
if (backgroundEngine != null) {
- val backgroundChannel =
- MethodChannel(backgroundEngine?.dartExecutor?.binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
- Handler(context.mainLooper)
- .post {
- Log.d("plugin", "sendLocationEvent $result")
- backgroundChannel.invokeMethod(Keys.BCM_SEND_LOCATION, result)
- }
+ context?.let {
+ val backgroundChannel =
+ MethodChannel(
+ getBinaryMessenger(it)!!,
+ Keys.BACKGROUND_CHANNEL_ID
+ )
+ Handler(it.mainLooper)
+ .post {
+ Log.d("plugin", "sendLocationEvent $result")
+ backgroundChannel.invokeMethod(Keys.BCM_SEND_LOCATION, result)
+ }
+ }
}
}
-
}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/Keys.kt b/android/src/main/kotlin/yukams/app/background_locator_2/Keys.kt
similarity index 94%
rename from android/src/main/kotlin/rekab/app/background_locator/Keys.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/Keys.kt
index 9f53718a..441e5779 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/Keys.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/Keys.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator
+package yukams.app.background_locator_2
class Keys {
companion object {
@@ -24,10 +24,10 @@ class Keys {
val DISPOSE_CALLBACK_HANDLE_KEY = "DISPOSE_CALLBACK_HANDLE_KEY"
@JvmStatic
- val CHANNEL_ID = "app.rekab/locator_plugin"
+ val CHANNEL_ID = "app.yukams/locator_plugin"
@JvmStatic
- val BACKGROUND_CHANNEL_ID = "app.rekab/locator_plugin_background"
+ val BACKGROUND_CHANNEL_ID = "app.yukams/locator_plugin_background"
@JvmStatic
val METHOD_SERVICE_INITIALIZED = "LocatorService.initialized"
@@ -157,6 +157,6 @@ class Keys {
val BCM_DISPOSE = "BCM_DISPOSE"
@JvmStatic
- val NOTIFICATION_ACTION = "com.rekab.background_locator.notification"
+ val NOTIFICATION_ACTION = "com.yukams.background_locator_2.notification"
}
}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/PreferencesManager.kt b/android/src/main/kotlin/yukams/app/background_locator_2/PreferencesManager.kt
similarity index 96%
rename from android/src/main/kotlin/rekab/app/background_locator/PreferencesManager.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/PreferencesManager.kt
index b00d4901..13014bee 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/PreferencesManager.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/PreferencesManager.kt
@@ -1,13 +1,13 @@
-package rekab.app.background_locator
+package yukams.app.background_locator_2
import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
-import rekab.app.background_locator.provider.LocationClient
+import yukams.app.background_locator_2.provider.LocationClient
class PreferencesManager {
companion object {
- private const val PREF_NAME = "background_locator"
+ private const val PREF_NAME = "background_locator_2"
@JvmStatic
fun saveCallbackDispatcher(context: Context, map: Map) {
@@ -25,9 +25,10 @@ class PreferencesManager {
val sharedPreferences =
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
+ val callback = map[Keys.ARG_CALLBACK] as Number
sharedPreferences.edit()
.putLong(Keys.ARG_CALLBACK,
- map[Keys.ARG_CALLBACK] as Long)
+ callback.toLong())
.apply()
if (map[Keys.ARG_NOTIFICATION_CALLBACK] as? Long != null) {
diff --git a/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/DisposePluggable.kt b/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/DisposePluggable.kt
new file mode 100644
index 00000000..75ce819f
--- /dev/null
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/DisposePluggable.kt
@@ -0,0 +1,29 @@
+package yukams.app.background_locator_2.pluggables
+
+import android.content.Context
+import android.os.Handler
+import io.flutter.plugin.common.MethodChannel
+import yukams.app.background_locator_2.IsolateHolderService
+import yukams.app.background_locator_2.Keys
+import yukams.app.background_locator_2.PreferencesManager
+
+class DisposePluggable : Pluggable {
+ override fun setCallback(context: Context, callbackHandle: Long) {
+ PreferencesManager.setCallbackHandle(context, Keys.DISPOSE_CALLBACK_HANDLE_KEY, callbackHandle)
+ }
+
+ override fun onServiceDispose(context: Context) {
+ (PreferencesManager.getCallbackHandle(context, Keys.DISPOSE_CALLBACK_HANDLE_KEY))?.let { disposeCallback ->
+ IsolateHolderService.getBinaryMessenger(context)?.let { binaryMessenger ->
+ val backgroundChannel = MethodChannel(binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
+ Handler(context.mainLooper)
+ .post {
+ backgroundChannel.invokeMethod(
+ Keys.BCM_DISPOSE,
+ hashMapOf(Keys.ARG_DISPOSE_CALLBACK to disposeCallback)
+ )
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/InitPluggable.kt b/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/InitPluggable.kt
new file mode 100644
index 00000000..4ca31ceb
--- /dev/null
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/InitPluggable.kt
@@ -0,0 +1,47 @@
+package yukams.app.background_locator_2.pluggables
+
+import android.content.Context
+import android.os.Handler
+import io.flutter.plugin.common.MethodChannel
+import yukams.app.background_locator_2.IsolateHolderService
+import yukams.app.background_locator_2.Keys
+import yukams.app.background_locator_2.PreferencesManager
+
+class InitPluggable : Pluggable {
+ private var isInitCallbackCalled = false
+
+ override fun setCallback(context: Context, callbackHandle: Long) {
+ PreferencesManager.setCallbackHandle(context, Keys.INIT_CALLBACK_HANDLE_KEY, callbackHandle)
+
+ }
+
+ override fun onServiceStart(context: Context) {
+ if (!isInitCallbackCalled) {
+ (PreferencesManager.getCallbackHandle(context, Keys.INIT_CALLBACK_HANDLE_KEY))?.let { initCallback ->
+ IsolateHolderService.getBinaryMessenger(context)?.let { binaryMessenger ->
+ val initialDataMap = PreferencesManager.getDataCallback(context, Keys.INIT_DATA_CALLBACK_KEY)
+ val backgroundChannel = MethodChannel(binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
+ Handler(context.mainLooper)
+ .post {
+ backgroundChannel.invokeMethod(
+ Keys.BCM_INIT,
+ hashMapOf(
+ Keys.ARG_INIT_CALLBACK to initCallback,
+ Keys.ARG_INIT_DATA_CALLBACK to initialDataMap
+ )
+ )
+ }
+ }
+ }
+ isInitCallbackCalled = true
+ }
+ }
+
+ override fun onServiceDispose(context: Context) {
+ isInitCallbackCalled = false
+ }
+
+ fun setInitData(context: Context, data: Map<*, *>) {
+ PreferencesManager.setDataCallback(context, Keys.INIT_DATA_CALLBACK_KEY, data)
+ }
+}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/pluggables/Pluggable.kt b/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/Pluggable.kt
similarity index 82%
rename from android/src/main/kotlin/rekab/app/background_locator/pluggables/Pluggable.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/pluggables/Pluggable.kt
index 7c977d29..a66ef272 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/pluggables/Pluggable.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/pluggables/Pluggable.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator.pluggables
+package yukams.app.background_locator_2.pluggables
import android.content.Context
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/AndroidLocationProviderClient.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/AndroidLocationProviderClient.kt
similarity index 98%
rename from android/src/main/kotlin/rekab/app/background_locator/provider/AndroidLocationProviderClient.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/provider/AndroidLocationProviderClient.kt
index ea4547da..90d2ab00 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/AndroidLocationProviderClient.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/AndroidLocationProviderClient.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator.provider
+package yukams.app.background_locator_2.provider
import android.annotation.SuppressLint
import android.content.Context
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/BLLocationProvider.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/BLLocationProvider.kt
similarity index 78%
rename from android/src/main/kotlin/rekab/app/background_locator/provider/BLLocationProvider.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/provider/BLLocationProvider.kt
index 54bbd013..fc0a0233 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/BLLocationProvider.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/BLLocationProvider.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator.provider
+package yukams.app.background_locator_2.provider
interface BLLocationProvider {
var listener: LocationUpdateListener?
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/GoogleLocationProviderClient.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/GoogleLocationProviderClient.kt
similarity index 92%
rename from android/src/main/kotlin/rekab/app/background_locator/provider/GoogleLocationProviderClient.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/provider/GoogleLocationProviderClient.kt
index 91c056de..626c5723 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/GoogleLocationProviderClient.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/GoogleLocationProviderClient.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator.provider
+package yukams.app.background_locator_2.provider
import android.annotation.SuppressLint
import android.content.Context
@@ -31,7 +31,7 @@ class GoogleLocationProviderClient(context: Context, override var listener: Loca
}
private class LocationListener(val listener: LocationUpdateListener?) : LocationCallback() {
- override fun onLocationResult(location: LocationResult?) {
+ override fun onLocationResult(location: LocationResult) {
listener?.onLocationUpdated(LocationParserUtil.getLocationMapFromLocation(location))
}
}
\ No newline at end of file
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationClient.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationClient.kt
similarity index 78%
rename from android/src/main/kotlin/rekab/app/background_locator/provider/LocationClient.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationClient.kt
index 9ab19760..bdd70bfd 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationClient.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationClient.kt
@@ -1,4 +1,4 @@
-package rekab.app.background_locator.provider
+package yukams.app.background_locator_2.provider
enum class LocationClient(val value: Int) {
Google(0), Android(1);
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationParserUtil.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationParserUtil.kt
similarity index 94%
rename from android/src/main/kotlin/rekab/app/background_locator/provider/LocationParserUtil.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationParserUtil.kt
index bc132275..62408a3c 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationParserUtil.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationParserUtil.kt
@@ -1,14 +1,14 @@
-package rekab.app.background_locator.provider
+package yukams.app.background_locator_2.provider
import android.location.Location
import android.os.Build
import com.google.android.gms.location.LocationResult
-import rekab.app.background_locator.Keys
+import yukams.app.background_locator_2.Keys
import java.util.HashMap
class LocationParserUtil {
companion object {
- fun getLocationMapFromLocation(location: Location): HashMap {
+ fun getLocationMapFromLocation(location: Location): HashMap {
var speedAccuracy = 0f
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
speedAccuracy = location.speedAccuracyMetersPerSecond
@@ -32,7 +32,7 @@ class LocationParserUtil {
)
}
- fun getLocationMapFromLocation(location: LocationResult?): HashMap? {
+ fun getLocationMapFromLocation(location: LocationResult?): HashMap? {
val firstLocation = location?.lastLocation ?: return null
var speedAccuracy = 0f
diff --git a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationRequestOptions.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationRequestOptions.kt
similarity index 65%
rename from android/src/main/kotlin/rekab/app/background_locator/provider/LocationRequestOptions.kt
rename to android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationRequestOptions.kt
index 7380e5ea..5c102f70 100644
--- a/android/src/main/kotlin/rekab/app/background_locator/provider/LocationRequestOptions.kt
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationRequestOptions.kt
@@ -1,3 +1,3 @@
-package rekab.app.background_locator.provider
+package yukams.app.background_locator_2.provider
class LocationRequestOptions(val interval: Long, val accuracy: Int, val distanceFilter: Float)
\ No newline at end of file
diff --git a/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationUpdateListener.kt b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationUpdateListener.kt
new file mode 100644
index 00000000..085ee7f6
--- /dev/null
+++ b/android/src/main/kotlin/yukams/app/background_locator_2/provider/LocationUpdateListener.kt
@@ -0,0 +1,7 @@
+package yukams.app.background_locator_2.provider
+
+import java.util.HashMap
+
+interface LocationUpdateListener {
+ fun onLocationUpdated(location: HashMap?)
+}
\ No newline at end of file
diff --git a/example/.metadata b/example/.metadata
index aeb01ee2..dec81b94 100644
--- a/example/.metadata
+++ b/example/.metadata
@@ -1,10 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
-# This file should be version controlled and should not be manually edited.
+# This file should be version controlled.
version:
- revision: 2d2a1ffec95cc70a3218872a2cd3f8de4933c42f
- channel: stable
+ revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ channel: master
project_type: app
+
+# Tracks metadata for the flutter migrate command
+migration:
+ platforms:
+ - platform: root
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ - platform: android
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ - platform: ios
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ - platform: linux
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ - platform: macos
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ - platform: web
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ - platform: windows
+ create_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+ base_revision: 6c3a0e4d21da2478a58d8b00fbabfa29ef0df045
+
+ # User provided section
+
+ # List of Local paths (relative to this file) that should be
+ # ignored by the migrate tool.
+ #
+ # Files that are not part of the templates will be ignored by default.
+ unmanaged_files:
+ - 'lib/main.dart'
+ - 'ios/Runner.xcodeproj/project.pbxproj'
diff --git a/example/README.md b/example/README.md
index c6c70f91..f70b9d8b 100644
--- a/example/README.md
+++ b/example/README.md
@@ -1,6 +1,6 @@
# background_locator_example
-Demonstrates how to use the background_locator plugin.
+Demonstrates how to use the background_locator_2 plugin.
## Getting Started
diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle
index d6cedd6f..cc453466 100644
--- a/example/android/app/build.gradle
+++ b/example/android/app/build.gradle
@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
- compileSdkVersion 30
+ compileSdkVersion 31
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
@@ -38,8 +38,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
- applicationId "rekab.app.background_locator_example"
- minSdkVersion 16
+ applicationId "yukams.app.background_locator_example"
+ minSdkVersion 21
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
diff --git a/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml
index 73b24028..547ba50a 100644
--- a/example/android/app/src/debug/AndroidManifest.xml
+++ b/example/android/app/src/debug/AndroidManifest.xml
@@ -1,5 +1,5 @@
@@ -41,14 +42,14 @@
-