From cece4d1e16296bd34225b32f2d93085c9ded3887 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Sat, 31 Aug 2019 23:09:30 +0300 Subject: [PATCH] Add location tracking switch disabled by default --- lib/managers/location_manager.class.dart | 129 ++++++++++++++--------- lib/panels/config_panel_widget.dart | 49 ++++++--- 2 files changed, 112 insertions(+), 66 deletions(-) diff --git a/lib/managers/location_manager.class.dart b/lib/managers/location_manager.class.dart index e902dca..dabde95 100644 --- a/lib/managers/location_manager.class.dart +++ b/lib/managers/location_manager.class.dart @@ -87,64 +87,89 @@ class LocationManager { void _startLocationService() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.reload(); - Duration locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ?? defaultUpdateIntervalMinutes); - Logger.d("Canceling previous schedule if any..."); - await AndroidAlarmManager.cancel(alarmId); - Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes..."); - await AndroidAlarmManager.periodic( - locationUpdateInterval, - alarmId, - LocationManager.updateDeviceLocationIsolate, - wakeup: true, - rescheduleOnReboot: true - ); + bool enabled = prefs.getBool("location-enabled") ?? false; + if (enabled) { + Duration locationUpdateInterval = Duration( + minutes: prefs.getInt("location-interval") ?? + defaultUpdateIntervalMinutes); + Logger.d("Canceling previous schedule if any..."); + await AndroidAlarmManager.cancel(alarmId); + 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 { print("[Location] started"); - if (ConnectionManager().webhookId != null && ConnectionManager().webhookId.isNotEmpty) { - DateTime currentTime = DateTime.now(); - String timeData = "${currentTime.year}-${currentTime.month}-${currentTime.day} ${currentTime.hour}:${currentTime.minute}"; - print("[Location] Sending test time data home..."); - String url = "${ConnectionManager().httpWebHost}/api/webhook/${ConnectionManager().webhookId}"; - Map headers = {}; - headers["Content-Type"] = "application/json"; - var data = { - "type": "call_service", - "data": { - "domain": "input_datetime", - "service": "set_datetime", - "service_data": { - "entity_id": "input_datetime.app_alarm_service_test", - "datetime": timeData + SharedPreferences prefs = await SharedPreferences.getInstance(); + await prefs.reload(); + bool enabled = prefs.getBool("location-enabled") ?? false; + if (enabled) { + if (ConnectionManager().webhookId != null && + ConnectionManager().webhookId.isNotEmpty) { + DateTime currentTime = DateTime.now(); + String timeData = "${currentTime.year}-${currentTime + .month}-${currentTime.day} ${currentTime.hour}:${currentTime + .minute}"; + print("[Location] Sending test time data home..."); + String url = "${ConnectionManager() + .httpWebHost}/api/webhook/${ConnectionManager().webhookId}"; + Map headers = {}; + headers["Content-Type"] = "application/json"; + var data = { + "type": "call_service", + "data": { + "domain": "input_datetime", + "service": "set_datetime", + "service_data": { + "entity_id": "input_datetime.app_alarm_service_test", + "datetime": timeData + } } - } - }; - await http.post( - url, - headers: headers, - body: json.encode(data) - ); - Logger.d("[Location] Getting device location..."); - Position location = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium); - Logger.d("[Location] Got location: ${location.latitude} ${location.longitude}. Sending home..."); - int battery = DateTime.now().hour; - data = { - "type": "update_location", - "data": { - "gps": [location.latitude, location.longitude], - "gps_accuracy": location.accuracy, - "battery": battery - } - }; - await http.post( - url, - headers: headers, - body: json.encode(data) - ); - Logger.d("[Location] ...done."); + }; + await http.post( + url, + headers: headers, + body: json.encode(data) + ); + Logger.d("[Location] Getting device location..."); + Position location = await Geolocator().getCurrentPosition( + desiredAccuracy: LocationAccuracy.medium); + Logger.d("[Location] Got location: ${location.latitude} ${location + .longitude}. Sending home..."); + int battery = DateTime + .now() + .hour; + data = { + "type": "update_location", + "data": { + "gps": [location.latitude, location.longitude], + "gps_accuracy": location.accuracy, + "battery": battery + } + }; + await http.post( + url, + headers: headers, + body: json.encode(data) + ); + Logger.d("[Location] ...done."); + } else { + print("[Location] No webhook id. Aborting"); + } } else { - print("[Location] No webhook id. Aborting"); + Logger.d("[Location] Location tracking is disabled"); } } diff --git a/lib/panels/config_panel_widget.dart b/lib/panels/config_panel_widget.dart index ad492cf..fe166e2 100644 --- a/lib/panels/config_panel_widget.dart +++ b/lib/panels/config_panel_widget.dart @@ -9,8 +9,9 @@ class ConfigPanelWidget extends StatefulWidget { class _ConfigPanelWidgetState extends State { - int locationInterval; - bool needToRestartLocationTracking = false; + int _locationInterval = LocationManager().defaultUpdateIntervalMinutes; + bool _locationTrackingEnabled = false; + bool _needToRestartLocationTracking = false; @override void initState() { @@ -23,29 +24,30 @@ class _ConfigPanelWidgetState extends State { await prefs.reload(); SharedPreferences.getInstance().then((prefs) { setState(() { - locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; + _locationTrackingEnabled = prefs.getBool("location-enabled") ?? false; + _locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; }); }); } void incLocationInterval() { - needToRestartLocationTracking = true; - if (locationInterval < 720) { + _needToRestartLocationTracking = true; + if (_locationInterval < 720) { setState(() { - locationInterval = locationInterval + 1; + _locationInterval = _locationInterval + 1; }); - SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", locationInterval)); + SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", _locationInterval)); } } void decLocationInterval() { - needToRestartLocationTracking = true; - if (locationInterval > 1) { + _needToRestartLocationTracking = true; + if (_locationInterval > 1) { setState(() { - locationInterval = locationInterval - 1; + _locationInterval = _locationInterval - 1; }); SharedPreferences.getInstance().then((prefs) { - prefs.setInt("location-interval", locationInterval); + prefs.setInt("location-interval", _locationInterval); }); } } @@ -131,6 +133,22 @@ class _ConfigPanelWidgetState extends State { Divider(), Text("Location tracking", style: TextStyle(fontSize: Sizes.largeFontSize-2)), Container(height: Sizes.rowPadding,), + Row( + children: [ + Text("Enable device location tracking"), + Switch( + value: _locationTrackingEnabled, + onChanged: (value) { + SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value)); + setState(() { + _locationTrackingEnabled = value; + _needToRestartLocationTracking = true; + }); + }, + ), + ], + ), + Container(height: Sizes.rowPadding,), Text("Location update interval in minutes:"), Row( mainAxisAlignment: MainAxisAlignment.center, @@ -142,7 +160,7 @@ class _ConfigPanelWidgetState extends State { child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)), onPressed: () => incLocationInterval(), ), - Text("$locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)), + Text("$_locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)), FlatButton( padding: EdgeInsets.all(0.0), child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)), @@ -173,9 +191,12 @@ class _ConfigPanelWidgetState extends State { @override void dispose() { - if (needToRestartLocationTracking) { - Logger.d("Location tracking interval was changed. Rescheduling location service..."); + if (_needToRestartLocationTracking) { + Logger.d("Location tracking settings was changed. Restarting location service..."); LocationManager()._startLocationService(); + if (_locationTrackingEnabled) { + LocationManager().updateDeviceLocation(); + } } super.dispose(); }