Location manager optimizations
This commit is contained in:
		| @@ -52,42 +52,66 @@ class LocationManager { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   LocationManager._internal() { |   LocationManager._internal() { | ||||||
|     startLocationService(); |     init(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   final int defaultUpdateIntervalMinutes = 15; |   final int defaultUpdateIntervalMinutes = 15; | ||||||
|   final int alarmId = 34901199; |   final int alarmId = 34901199; | ||||||
|  |   Duration _updateInterval; | ||||||
|  |   bool _isEnabled; | ||||||
|  |  | ||||||
|   void startLocationService() async { |   void init() async { | ||||||
|     SharedPreferences prefs = await SharedPreferences.getInstance(); |     SharedPreferences prefs = await SharedPreferences.getInstance(); | ||||||
|     await prefs.reload(); |     await prefs.reload(); | ||||||
|     bool enabled = prefs.getBool("location-enabled") ?? false; |     _updateInterval = Duration(minutes: prefs.getInt("location-interval") ?? | ||||||
|     if (enabled) { |         defaultUpdateIntervalMinutes); | ||||||
|       Duration locationUpdateInterval = Duration( |     _isEnabled = prefs.getBool("location-enabled") ?? false; | ||||||
|           minutes: prefs.getInt("location-interval") ?? |     if (_isEnabled) { | ||||||
|               defaultUpdateIntervalMinutes); |       _startLocationService(); | ||||||
|       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); |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void updateDeviceLocation() async { |   void setSettings(bool enabled, int interval) async { | ||||||
|     print("[Location] started"); |  | ||||||
|     SharedPreferences prefs = await SharedPreferences.getInstance(); |     SharedPreferences prefs = await SharedPreferences.getInstance(); | ||||||
|     await prefs.reload(); |     if (interval != _updateInterval.inMinutes) { | ||||||
|     bool enabled = prefs.getBool("location-enabled") ?? false; |       prefs.setInt("location-interval", interval); | ||||||
|     if (enabled) { |       _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 && |       if (ConnectionManager().webhookId != null && | ||||||
|           ConnectionManager().webhookId.isNotEmpty) { |           ConnectionManager().webhookId.isNotEmpty) { | ||||||
|         String url = "${ConnectionManager() |         String url = "${ConnectionManager() | ||||||
|   | |||||||
| @@ -35,12 +35,7 @@ class StartupUserMessagesManager { | |||||||
|       positiveText: "Enable now", |       positiveText: "Enable now", | ||||||
|       negativeText: "Cancel", |       negativeText: "Cancel", | ||||||
|       onPositive: () { |       onPositive: () { | ||||||
|         SharedPreferences.getInstance().then((prefs) { |         LocationManager().setSettings(true, 15); | ||||||
|           prefs.setBool("location-enabled", true); |  | ||||||
|           prefs.setBool(_locationTrackingMessageKey, true); |  | ||||||
|           LocationManager().startLocationService(); |  | ||||||
|           LocationManager().updateDeviceLocation(); |  | ||||||
|         }); |  | ||||||
|       }, |       }, | ||||||
|       onNegative: () { |       onNegative: () { | ||||||
|         SharedPreferences.getInstance().then((prefs) { |         SharedPreferences.getInstance().then((prefs) { | ||||||
|   | |||||||
| @@ -11,7 +11,6 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> { | |||||||
|  |  | ||||||
|   int _locationInterval = LocationManager().defaultUpdateIntervalMinutes; |   int _locationInterval = LocationManager().defaultUpdateIntervalMinutes; | ||||||
|   bool _locationTrackingEnabled = false; |   bool _locationTrackingEnabled = false; | ||||||
|   bool _needToRestartLocationTracking = false; |  | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void initState() { |   void initState() { | ||||||
| @@ -31,24 +30,18 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void incLocationInterval() { |   void incLocationInterval() { | ||||||
|     _needToRestartLocationTracking = true; |  | ||||||
|     if (_locationInterval < 720) { |     if (_locationInterval < 720) { | ||||||
|       setState(() { |       setState(() { | ||||||
|         _locationInterval = _locationInterval + 1; |         _locationInterval = _locationInterval + 1; | ||||||
|       }); |       }); | ||||||
|       SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", _locationInterval)); |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void decLocationInterval() { |   void decLocationInterval() { | ||||||
|     _needToRestartLocationTracking = true; |  | ||||||
|     if (_locationInterval > 1) { |     if (_locationInterval > 1) { | ||||||
|       setState(() { |       setState(() { | ||||||
|         _locationInterval = _locationInterval - 1; |         _locationInterval = _locationInterval - 1; | ||||||
|       }); |       }); | ||||||
|       SharedPreferences.getInstance().then((prefs) { |  | ||||||
|         prefs.setInt("location-interval", _locationInterval); |  | ||||||
|       }); |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -142,7 +135,6 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> { | |||||||
|                         SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value)); |                         SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value)); | ||||||
|                         setState(() { |                         setState(() { | ||||||
|                           _locationTrackingEnabled = value; |                           _locationTrackingEnabled = value; | ||||||
|                           _needToRestartLocationTracking = true; |  | ||||||
|                         }); |                         }); | ||||||
|                       }, |                       }, | ||||||
|                     ), |                     ), | ||||||
| @@ -191,13 +183,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> { | |||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void dispose() { |   void dispose() { | ||||||
|     if (_needToRestartLocationTracking) { |     LocationManager().setSettings(_locationTrackingEnabled, _locationInterval); | ||||||
|       Logger.d("Location tracking settings was changed. Restarting location service..."); |  | ||||||
|       LocationManager().startLocationService(); |  | ||||||
|       if (_locationTrackingEnabled) { |  | ||||||
|         LocationManager().updateDeviceLocation(); |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|     super.dispose(); |     super.dispose(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user