From 5792652619749834745a8d661b879859ac7ecb33 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Sun, 10 Nov 2019 14:41:29 +0000 Subject: [PATCH] Experimental location tracking for every 10 or 5 minutes --- lib/managers/location_manager.class.dart | 59 ++++++++++++++++-------- lib/pages/integration_settings.page.dart | 9 ++-- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/lib/managers/location_manager.class.dart b/lib/managers/location_manager.class.dart index e90acc3..3092018 100644 --- a/lib/managers/location_manager.class.dart +++ b/lib/managers/location_manager.class.dart @@ -14,7 +14,7 @@ class LocationManager { } final int defaultUpdateIntervalMinutes = 20; - final String backgroundTaskId = "haclocationtask4352"; + final String backgroundTaskId = "haclocationtask0"; final String backgroundTaskTag = "haclocation"; Duration _updateInterval; bool _isRunning; @@ -57,27 +57,48 @@ class LocationManager { } _startLocationService() async { - Logger.d("Scheduling location update for every ${_updateInterval - .inMinutes} minutes..."); String webhookId = ConnectionManager().webhookId; String httpWebHost = ConnectionManager().httpWebHost; if (webhookId != null && webhookId.isNotEmpty) { - await workManager.Workmanager.registerPeriodicTask( - backgroundTaskId, - "haClientLocationTracking", - tag: backgroundTaskTag, - inputData: { - "webhookId": webhookId, - "httpWebHost": httpWebHost - }, - frequency: _updateInterval, - existingWorkPolicy: workManager.ExistingWorkPolicy.keep, - backoffPolicy: workManager.BackoffPolicy.linear, - backoffPolicyDelay: _updateInterval, - constraints: workManager.Constraints( - networkType: workManager.NetworkType.connected - ) - ); + Duration interval; + int delayFactor; + int taskCount; + Logger.d("Starting location update for every ${_updateInterval + .inMinutes} minutes..."); + if (_updateInterval.inMinutes == 10) { + interval = Duration(minutes: 20); + taskCount = 2; + delayFactor = 10; + } else if (_updateInterval.inMinutes == 5) { + interval = Duration(minutes: 15); + taskCount = 3; + delayFactor = 5; + } else { + interval = _updateInterval; + taskCount = 1; + delayFactor = 0; + } + for (int i = 1; i <= taskCount; i++) { + int delay = i*delayFactor; + Logger.d("Scheduling location update task #$i for every ${interval.inMinutes} minutes in $delay minutes..."); + await workManager.Workmanager.registerPeriodicTask( + "$backgroundTaskId$n", + "haClientLocationTracking", + tag: backgroundTaskTag, + inputData: { + "webhookId": webhookId, + "httpWebHost": httpWebHost + }, + frequency: interval, + initialDelay: Duration(minutes: delay), + existingWorkPolicy: workManager.ExistingWorkPolicy.keep, + backoffPolicy: workManager.BackoffPolicy.linear, + backoffPolicyDelay: interval, + constraints: workManager.Constraints( + networkType: workManager.NetworkType.connected + ) + ); + } } } diff --git a/lib/pages/integration_settings.page.dart b/lib/pages/integration_settings.page.dart index 3fc73e0..a65abee 100644 --- a/lib/pages/integration_settings.page.dart +++ b/lib/pages/integration_settings.page.dart @@ -29,6 +29,9 @@ class _IntegrationSettingsPageState extends State { setState(() { _locationTrackingEnabled = prefs.getBool("location-enabled") ?? false; _locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; + if (_locationInterval % 5 != 0) { + _locationInterval = 5 * (_locationInterval ~/ 5); + } }); }); } @@ -36,15 +39,15 @@ class _IntegrationSettingsPageState extends State { void incLocationInterval() { if (_locationInterval < 720) { setState(() { - _locationInterval = _locationInterval + 1; + _locationInterval = _locationInterval + 5; }); } } void decLocationInterval() { - if (_locationInterval > 15) { + if (_locationInterval > 5) { setState(() { - _locationInterval = _locationInterval - 1; + _locationInterval = _locationInterval - 5; }); } }