Location manager optimizations

This commit is contained in:
estevez-dev
2019-09-01 23:12:43 +03:00
parent 27e6198d83
commit 389d28a1e1
3 changed files with 51 additions and 46 deletions

View File

@ -52,42 +52,66 @@ class LocationManager {
}
LocationManager._internal() {
startLocationService();
init();
}
final int defaultUpdateIntervalMinutes = 15;
final int alarmId = 34901199;
Duration _updateInterval;
bool _isEnabled;
void startLocationService() async {
void init() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
bool enabled = prefs.getBool("location-enabled") ?? false;
if (enabled) {
Duration locationUpdateInterval = Duration(
minutes: prefs.getInt("location-interval") ??
defaultUpdateIntervalMinutes);
Logger.d("Scheduling location update for every ${locationUpdateInterval
.inMinutes} minutes...");
await AndroidAlarmManager.periodic(
locationUpdateInterval,
alarmId,
LocationManager.updateDeviceLocationIsolate,
wakeup: true,
rescheduleOnReboot: true
);
} else {
Logger.d("Location tracking is disabled");
Logger.d("Canceling previous schedule if any...");
await AndroidAlarmManager.cancel(alarmId);
_updateInterval = Duration(minutes: prefs.getInt("location-interval") ??
defaultUpdateIntervalMinutes);
_isEnabled = prefs.getBool("location-enabled") ?? false;
if (_isEnabled) {
_startLocationService();
}
}
void updateDeviceLocation() async {
print("[Location] started");
void setSettings(bool enabled, int interval) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
bool enabled = prefs.getBool("location-enabled") ?? false;
if (enabled) {
if (interval != _updateInterval.inMinutes) {
prefs.setInt("location-interval", interval);
_updateInterval = Duration(minutes: interval);
}
if (enabled && !_isEnabled) {
Logger.d("Enabling location service");
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("location-enabled", enabled);
_isEnabled = true;
_startLocationService();
updateDeviceLocation();
} else if (!enabled && _isEnabled) {
Logger.d("Disabling location service");
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("location-enabled", enabled);
_isEnabled = false;
_stopLocationService();
}
}
void _startLocationService() async {
Logger.d("Scheduling location update for every ${_updateInterval
.inMinutes} minutes...");
await AndroidAlarmManager.periodic(
_updateInterval,
alarmId,
LocationManager.updateDeviceLocationIsolate,
wakeup: true,
rescheduleOnReboot: true
);
}
void _stopLocationService() async {
Logger.d("Canceling previous schedule if any...");
await AndroidAlarmManager.cancel(alarmId);
}
void updateDeviceLocation() async {
if (_isEnabled) {
if (ConnectionManager().webhookId != null &&
ConnectionManager().webhookId.isNotEmpty) {
String url = "${ConnectionManager()

View File

@ -35,12 +35,7 @@ class StartupUserMessagesManager {
positiveText: "Enable now",
negativeText: "Cancel",
onPositive: () {
SharedPreferences.getInstance().then((prefs) {
prefs.setBool("location-enabled", true);
prefs.setBool(_locationTrackingMessageKey, true);
LocationManager().startLocationService();
LocationManager().updateDeviceLocation();
});
LocationManager().setSettings(true, 15);
},
onNegative: () {
SharedPreferences.getInstance().then((prefs) {