Experimental location tracking for every 10 or 5 minutes
This commit is contained in:
parent
2c900333a5
commit
5792652619
@ -14,7 +14,7 @@ class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final int defaultUpdateIntervalMinutes = 20;
|
final int defaultUpdateIntervalMinutes = 20;
|
||||||
final String backgroundTaskId = "haclocationtask4352";
|
final String backgroundTaskId = "haclocationtask0";
|
||||||
final String backgroundTaskTag = "haclocation";
|
final String backgroundTaskTag = "haclocation";
|
||||||
Duration _updateInterval;
|
Duration _updateInterval;
|
||||||
bool _isRunning;
|
bool _isRunning;
|
||||||
@ -57,29 +57,50 @@ class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_startLocationService() async {
|
_startLocationService() async {
|
||||||
Logger.d("Scheduling location update for every ${_updateInterval
|
|
||||||
.inMinutes} minutes...");
|
|
||||||
String webhookId = ConnectionManager().webhookId;
|
String webhookId = ConnectionManager().webhookId;
|
||||||
String httpWebHost = ConnectionManager().httpWebHost;
|
String httpWebHost = ConnectionManager().httpWebHost;
|
||||||
if (webhookId != null && webhookId.isNotEmpty) {
|
if (webhookId != null && webhookId.isNotEmpty) {
|
||||||
|
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(
|
await workManager.Workmanager.registerPeriodicTask(
|
||||||
backgroundTaskId,
|
"$backgroundTaskId$n",
|
||||||
"haClientLocationTracking",
|
"haClientLocationTracking",
|
||||||
tag: backgroundTaskTag,
|
tag: backgroundTaskTag,
|
||||||
inputData: {
|
inputData: {
|
||||||
"webhookId": webhookId,
|
"webhookId": webhookId,
|
||||||
"httpWebHost": httpWebHost
|
"httpWebHost": httpWebHost
|
||||||
},
|
},
|
||||||
frequency: _updateInterval,
|
frequency: interval,
|
||||||
|
initialDelay: Duration(minutes: delay),
|
||||||
existingWorkPolicy: workManager.ExistingWorkPolicy.keep,
|
existingWorkPolicy: workManager.ExistingWorkPolicy.keep,
|
||||||
backoffPolicy: workManager.BackoffPolicy.linear,
|
backoffPolicy: workManager.BackoffPolicy.linear,
|
||||||
backoffPolicyDelay: _updateInterval,
|
backoffPolicyDelay: interval,
|
||||||
constraints: workManager.Constraints(
|
constraints: workManager.Constraints(
|
||||||
networkType: workManager.NetworkType.connected
|
networkType: workManager.NetworkType.connected
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_stopLocationService() async {
|
_stopLocationService() async {
|
||||||
Logger.d("Canceling previous schedule if any...");
|
Logger.d("Canceling previous schedule if any...");
|
||||||
|
@ -29,6 +29,9 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
_locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
|
_locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
|
||||||
_locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
|
_locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
|
||||||
|
if (_locationInterval % 5 != 0) {
|
||||||
|
_locationInterval = 5 * (_locationInterval ~/ 5);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -36,15 +39,15 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
|
|||||||
void incLocationInterval() {
|
void incLocationInterval() {
|
||||||
if (_locationInterval < 720) {
|
if (_locationInterval < 720) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_locationInterval = _locationInterval + 1;
|
_locationInterval = _locationInterval + 5;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void decLocationInterval() {
|
void decLocationInterval() {
|
||||||
if (_locationInterval > 15) {
|
if (_locationInterval > 5) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_locationInterval = _locationInterval - 1;
|
_locationInterval = _locationInterval - 5;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user