Skip to content

Commit 2ac0f3a

Browse files
Merge pull request #32 from icapps/feature/ExtraAndroidOptions
Also support distance filters for android
2 parents e58669c + 0e87230 commit 2ac0f3a

File tree

11 files changed

+50
-4
lines changed

11 files changed

+50
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
## 1.1.0 - 12-10-2021
1+
## 1.1.0 - xx-xx-2021
22
#Added
33
- Added more options to android config to specify update interval
44
- Added iOS specific options to control activity type and/or distance filter
5+
- Added iOS specific options to restart the tracking after killing the app
6+
- Added option to Android specific options to only get updates every x meters
57

68
## 1.0.2 - 22-06-2021
79
#Fixed

android/src/main/kotlin/com/icapps/background_location_tracker/MethodCallHelper.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ internal class MethodCallHelper(private val ctx: Context) : MethodChannel.Method
3737
val enableNotificationLocationUpdatesKey = "android_config_enable_notification_location_updates"
3838
val enableCancelTrackingActionKey = "android_config_enable_cancel_tracking_action"
3939
val cancelTrackingActionTextKey = "android_config_cancel_tracking_action_text"
40+
val distanceFilterKey = "android_distance_filter"
4041
val keys = listOf(
4142
callbackHandleKey,
4243
loggingEnabledKey,
@@ -57,8 +58,10 @@ internal class MethodCallHelper(private val ctx: Context) : MethodChannel.Method
5758
val cancelTrackingActionText = call.argument<String>(cancelTrackingActionTextKey)!!
5859
val enableCancelTrackingAction = call.argument<Boolean>(enableCancelTrackingActionKey)!!
5960
val trackingInterval = call.argument<Long>(trackingIntervalKey)!!
61+
val distanceFilter = (call.argument<Double>(distanceFilterKey) ?: 0.0).toFloat()
6062
SharedPrefsUtil.saveLoggingEnabled(ctx, loggingEnabled)
6163
SharedPrefsUtil.saveTrackingInterval(ctx, trackingInterval)
64+
SharedPrefsUtil.saveDistanceFilter(ctx, distanceFilter)
6265
Logger.enabled = loggingEnabled
6366
NotificationUtil.createNotificationChannels(ctx, channelName)
6467
SharedPrefsUtil.saveCallbackDispatcherHandleKey(ctx, callbackHandle)

android/src/main/kotlin/com/icapps/background_location_tracker/service/LocationUpdatesService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import android.content.res.Configuration
88
import android.location.Location
99
import android.os.Binder
1010
import android.os.Build
11-
import android.os.Handler
12-
import android.os.HandlerThread
1311
import android.os.IBinder
1412
import android.os.Looper
1513
import android.os.PowerManager
@@ -221,11 +219,13 @@ internal class LocationUpdatesService : Service() {
221219
*/
222220
private fun createLocationRequest() {
223221
val interval = SharedPrefsUtil.trackingInterval(this)
222+
val distanceFilter = SharedPrefsUtil.distanceFilter(this)
224223
locationRequest = LocationRequest()
225224
locationRequest?.let {
226225
it.interval = interval
227226
it.fastestInterval = interval / 2
228-
it.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
227+
it.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
228+
it.setSmallestDisplacement(distanceFilter)
229229
}
230230
}
231231

android/src/main/kotlin/com/icapps/background_location_tracker/utils/SharedPrefsUtil.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal object SharedPrefsUtil {
99
private const val KEY_IS_TRACKING = "background.location.tracker.manager.IS_TRACKING"
1010
private const val KEY_LOGGING_ENABED = "background.location.tracker.manager.LOGGIN_ENABLED"
1111
private const val KEY_TRACKING_INTERVAL = "background.location.tracker.manager.TRACKING_INTERVAL"
12+
private const val KEY_DISTANCE_FILTER = "background.location.tracker.manager.DISTANCE_FILTER"
1213

1314
private const val KEY_NOTIFICATION_BODY = "background.location.tracker.manager.NOTIFICATION_BODY"
1415
private const val KEY_NOTIFICATION_ICON = "background.location.tracker.manager.NOTIFICATION_ICON"
@@ -52,10 +53,19 @@ internal object SharedPrefsUtil {
5253
.apply()
5354
}
5455

56+
fun saveDistanceFilter(ctx: Context, filter: Float) {
57+
ctx.prefs()
58+
.edit()
59+
.putFloat(KEY_DISTANCE_FILTER, filter)
60+
.apply()
61+
}
62+
5563
fun isLoggingEnabled(ctx: Context): Boolean = ctx.prefs().getBoolean(KEY_LOGGING_ENABED, false)
5664

5765
fun trackingInterval(ctx: Context): Long = ctx.prefs().getLong(KEY_TRACKING_INTERVAL, 10000)
5866

67+
fun distanceFilter(ctx: Context) : Float = ctx.prefs().getFloat(KEY_DISTANCE_FILTER, 0.0f)
68+
5969
//NotificationConfig
6070
fun saveNotificationConfig(ctx: Context, notificationBody: String, notificationIcon: String?, cancelTrackingActionText: String, enableNotificationLocationUpdates: Boolean, enableCancelTrackingAction: Boolean) {
6171
ctx.prefs()

example/lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ Future<void> main() async {
1616
await BackgroundLocationTrackerManager.initialize(
1717
_backgroundCallback,
1818
config: const BackgroundLocationTrackerConfig(
19+
loggingEnabled: true,
1920
androidConfig: AndroidConfig(
2021
notificationIcon: 'explore',
2122
trackingInterval: Duration(seconds: 4),
23+
distanceFilterMeters: 5,
2224
),
2325
iOSConfig: IOSConfig(
2426
activityType: ActivityType.FITNESS,
2527
distanceFilterMeters: 5,
28+
restartAfterKill: true,
2629
),
2730
),
2831
);

ios/Classes/ForegroundChannel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class ForegroundChannel : NSObject {
5151
let loggingEnabledKey = "logging_enabled"
5252
let activityTypeKey = "ios_activity_type"
5353
let distanceFilterKey = "ios_distance_filter"
54+
let restartAfterKillKey = "ios_restart_after_kill"
5455
let map = call.arguments as? [String: Any]
5556
guard let callbackDispatcherHandle = map?[callBackHandleKey] else {
5657
result(false)
@@ -60,6 +61,7 @@ public class ForegroundChannel : NSObject {
6061

6162
let loggingEnabled: Bool = map?[loggingEnabledKey] as? Bool ?? false
6263
SharedPrefsUtil.saveLoggingEnabled(loggingEnabled)
64+
SharedPrefsUtil.saveRestartAfterKillEnabled(map?[restartAfterKillKey] as? Bool ?? false)
6365

6466
let activityType: CLActivityType
6567
switch (map?[activityTypeKey] as? String ?? "AUTOMOTIVE") {

ios/Classes/SharedPrefsUtil.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct SharedPrefsUtil {
1212

1313
private static let KEY_CALLBACK_HANDLER = "background.location.tracker.manager.CALLBACK_DISPATCHER_HANDLE_KEY"
1414
private static let KEY_IS_TRACKING = "background.location.tracker.manager.IS_TRACKING"
15+
private static let KEY_IS_RESTART_AFTER_KILL = "background.location.tracker.manager.RESTART_AFTER_KILL"
1516
private static let KEY_LOGGING_ENABLED = "background.location.tracker.manager.LOGGIN_ENABLED"
1617
private static let KEY_DISTANCE_FILTER = "background.location.tracker.manager.DISTANCE_FILTER"
1718
private static let KEY_ACTIVITY_TYPE = "background.location.tracker.manager.KEY_ACTIVITY_TYPE"
@@ -38,6 +39,14 @@ struct SharedPrefsUtil {
3839
return getValue(for: SharedPrefsUtil.KEY_IS_TRACKING) ?? false
3940
}
4041

42+
static func restartAfterKill() -> Bool {
43+
return getValue(for: SharedPrefsUtil.KEY_IS_RESTART_AFTER_KILL) ?? false
44+
}
45+
46+
static func saveRestartAfterKillEnabled(_ isRestartEnabled: Bool) {
47+
store(isRestartEnabled, key: SharedPrefsUtil.KEY_IS_RESTART_AFTER_KILL)
48+
}
49+
4150
static func saveLoggingEnabled(_ isLoggingEnabled: Bool) {
4251
store(isLoggingEnabled, key: SharedPrefsUtil.KEY_LOGGING_ENABLED)
4352
}

ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin {
3636
let instance = SwiftBackgroundLocationTrackerPlugin()
3737
registrar.addMethodCallDelegate(instance, channel: methodChannel)
3838
registrar.addApplicationDelegate(instance)
39+
40+
if (SharedPrefsUtil.isTracking() && SharedPrefsUtil.restartAfterKill()) {
41+
instance.locationManager.delegate = instance
42+
instance.locationManager.startUpdatingLocation()
43+
}
3944
}
4045

4146
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

lib/src/channel/foreground_channel.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class ForegroundChannel {
3838
config.androidConfig.cancelTrackingActionText,
3939
'android_config_enable_cancel_tracking_action':
4040
config.androidConfig.enableCancelTrackingAction,
41+
'android_distance_filter': config.androidConfig.distanceFilterMeters,
4142
'ios_activity_type': _activityTypeString(config.iOSConfig.activityType),
4243
'ios_distance_filter': config.iOSConfig.distanceFilterMeters,
44+
'ios_restart_after_kill': config.iOSConfig.restartAfterKill,
4345
},
4446
);
4547
}

lib/src/model/config/android_config.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class AndroidConfig {
3535
/// Defaults to an update every 10 seconds
3636
final Duration trackingInterval;
3737

38+
/// The distance in meters that should be moved before updates are sent.
39+
/// Defaults to no filter (null)
40+
final double? distanceFilterMeters;
41+
3842
const AndroidConfig({
3943
this.channelName = 'Background Tracking',
4044
this.notificationBody = 'Background tracking active. Tap to open.',
@@ -43,5 +47,6 @@ class AndroidConfig {
4347
this.cancelTrackingActionText = 'Stop Tracking',
4448
this.enableCancelTrackingAction = true,
4549
this.trackingInterval = const Duration(seconds: 10),
50+
this.distanceFilterMeters,
4651
});
4752
}

0 commit comments

Comments
 (0)