Add location tracking switch disabled by default
This commit is contained in:
parent
623634cb6e
commit
cece4d1e16
@ -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<String, String> 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<String, String> 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");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,8 +9,9 @@ class ConfigPanelWidget extends StatefulWidget {
|
||||
|
||||
class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
||||
|
||||
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<ConfigPanelWidget> {
|
||||
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<ConfigPanelWidget> {
|
||||
Divider(),
|
||||
Text("Location tracking", style: TextStyle(fontSize: Sizes.largeFontSize-2)),
|
||||
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:"),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
@ -142,7 +160,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
||||
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<ConfigPanelWidget> {
|
||||
|
||||
@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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user