Add location tracking switch disabled by default

This commit is contained in:
estevez-dev 2019-08-31 23:09:30 +03:00
parent 623634cb6e
commit cece4d1e16
2 changed files with 112 additions and 66 deletions

View File

@ -87,10 +87,15 @@ class LocationManager {
void _startLocationService() async { void _startLocationService() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload(); await prefs.reload();
Duration locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ?? defaultUpdateIntervalMinutes); 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..."); Logger.d("Canceling previous schedule if any...");
await AndroidAlarmManager.cancel(alarmId); await AndroidAlarmManager.cancel(alarmId);
Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes..."); Logger.d("Scheduling location update for every ${locationUpdateInterval
.inMinutes} minutes...");
await AndroidAlarmManager.periodic( await AndroidAlarmManager.periodic(
locationUpdateInterval, locationUpdateInterval,
alarmId, alarmId,
@ -98,15 +103,28 @@ class LocationManager {
wakeup: true, wakeup: true,
rescheduleOnReboot: 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 updateDeviceLocation() async {
print("[Location] started"); print("[Location] started");
if (ConnectionManager().webhookId != null && ConnectionManager().webhookId.isNotEmpty) { 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(); DateTime currentTime = DateTime.now();
String timeData = "${currentTime.year}-${currentTime.month}-${currentTime.day} ${currentTime.hour}:${currentTime.minute}"; String timeData = "${currentTime.year}-${currentTime
.month}-${currentTime.day} ${currentTime.hour}:${currentTime
.minute}";
print("[Location] Sending test time data home..."); print("[Location] Sending test time data home...");
String url = "${ConnectionManager().httpWebHost}/api/webhook/${ConnectionManager().webhookId}"; String url = "${ConnectionManager()
.httpWebHost}/api/webhook/${ConnectionManager().webhookId}";
Map<String, String> headers = {}; Map<String, String> headers = {};
headers["Content-Type"] = "application/json"; headers["Content-Type"] = "application/json";
var data = { var data = {
@ -126,9 +144,13 @@ class LocationManager {
body: json.encode(data) body: json.encode(data)
); );
Logger.d("[Location] Getting device location..."); Logger.d("[Location] Getting device location...");
Position location = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium); Position location = await Geolocator().getCurrentPosition(
Logger.d("[Location] Got location: ${location.latitude} ${location.longitude}. Sending home..."); desiredAccuracy: LocationAccuracy.medium);
int battery = DateTime.now().hour; Logger.d("[Location] Got location: ${location.latitude} ${location
.longitude}. Sending home...");
int battery = DateTime
.now()
.hour;
data = { data = {
"type": "update_location", "type": "update_location",
"data": { "data": {
@ -146,6 +168,9 @@ class LocationManager {
} else { } else {
print("[Location] No webhook id. Aborting"); print("[Location] No webhook id. Aborting");
} }
} else {
Logger.d("[Location] Location tracking is disabled");
}
} }
} }

View File

@ -9,8 +9,9 @@ class ConfigPanelWidget extends StatefulWidget {
class _ConfigPanelWidgetState extends State<ConfigPanelWidget> { class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
int locationInterval; int _locationInterval = LocationManager().defaultUpdateIntervalMinutes;
bool needToRestartLocationTracking = false; bool _locationTrackingEnabled = false;
bool _needToRestartLocationTracking = false;
@override @override
void initState() { void initState() {
@ -23,29 +24,30 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
await prefs.reload(); await prefs.reload();
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {
setState(() { setState(() {
locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; _locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
_locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
}); });
}); });
} }
void incLocationInterval() { void incLocationInterval() {
needToRestartLocationTracking = true; _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)); SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", _locationInterval));
} }
} }
void decLocationInterval() { void decLocationInterval() {
needToRestartLocationTracking = true; _needToRestartLocationTracking = true;
if (locationInterval > 1) { if (_locationInterval > 1) {
setState(() { setState(() {
locationInterval = locationInterval - 1; _locationInterval = _locationInterval - 1;
}); });
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {
prefs.setInt("location-interval", locationInterval); prefs.setInt("location-interval", _locationInterval);
}); });
} }
} }
@ -131,6 +133,22 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
Divider(), Divider(),
Text("Location tracking", style: TextStyle(fontSize: Sizes.largeFontSize-2)), Text("Location tracking", style: TextStyle(fontSize: Sizes.largeFontSize-2)),
Container(height: Sizes.rowPadding,), Container(height: Sizes.rowPadding,),
Row(
children: <Widget>[
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:"), Text("Location update interval in minutes:"),
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@ -142,7 +160,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)), child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)),
onPressed: () => incLocationInterval(), onPressed: () => incLocationInterval(),
), ),
Text("$locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)), Text("$_locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)),
FlatButton( FlatButton(
padding: EdgeInsets.all(0.0), padding: EdgeInsets.all(0.0),
child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)), child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)),
@ -173,9 +191,12 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
@override @override
void dispose() { void dispose() {
if (needToRestartLocationTracking) { if (_needToRestartLocationTracking) {
Logger.d("Location tracking interval was changed. Rescheduling location service..."); Logger.d("Location tracking settings was changed. Restarting location service...");
LocationManager()._startLocationService(); LocationManager()._startLocationService();
if (_locationTrackingEnabled) {
LocationManager().updateDeviceLocation();
}
} }
super.dispose(); super.dispose();
} }