Skip to content

Commit 737ef12

Browse files
Also support distance filters for android
1 parent e58669c commit 737ef12

File tree

7 files changed

+24
-4
lines changed

7 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
## 1.1.0 - 12-10-2021
1+
## 1.1.0 - 14-10-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 option to Android specific options to only get updates every x meters
56

67
## 1.0.2 - 22-06-2021
78
#Fixed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ internal class MethodCallHelper(private val ctx: Context) : MethodChannel.Method
5757
val cancelTrackingActionText = call.argument<String>(cancelTrackingActionTextKey)!!
5858
val enableCancelTrackingAction = call.argument<Boolean>(enableCancelTrackingActionKey)!!
5959
val trackingInterval = call.argument<Long>(trackingIntervalKey)!!
60+
val distanceFilter = (call.argument<Double>(trackingIntervalKey) ?: 0.0).toFloat()
6061
SharedPrefsUtil.saveLoggingEnabled(ctx, loggingEnabled)
6162
SharedPrefsUtil.saveTrackingInterval(ctx, trackingInterval)
63+
SharedPrefsUtil.saveDistanceFilter(ctx, distanceFilter)
6264
Logger.enabled = loggingEnabled
6365
NotificationUtil.createNotificationChannels(ctx, channelName)
6466
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Future<void> main() async {
1919
androidConfig: AndroidConfig(
2020
notificationIcon: 'explore',
2121
trackingInterval: Duration(seconds: 4),
22+
distanceFilterMeters: 5,
2223
),
2324
iOSConfig: IOSConfig(
2425
activityType: ActivityType.FITNESS,

lib/src/channel/foreground_channel.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ 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,
4344
},

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)