Disabling location tracking

This commit is contained in:
estevez-dev
2019-09-03 20:13:23 +03:00
parent ab5bf3b807
commit 620aa3b8d8
6 changed files with 1 additions and 273 deletions

View File

@ -2,146 +2,4 @@ part of '../main.dart';
class LocationManager {
static void updateDeviceLocationIsolate() {
print("[Location isolate #${Isolate.current.hashCode}] started");
SharedPreferences.getInstance().then((prefs){
print("[Location isolate #${Isolate.current.hashCode}] loading settings");
String webhookId = prefs.getString('app-webhook-id');
String domain = prefs.getString('hassio-domain');
String port = prefs.getString('hassio-port');
String httpWebHost =
"${prefs.getString('hassio-res-protocol')}://$domain:$port";
if (webhookId != null && webhookId.isNotEmpty) {
Logger.d("[Location isolate #${Isolate.current.hashCode}] Getting device location...");
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
Logger.d("[Location isolate #${Isolate.current.hashCode}] Got location: ${location.latitude} ${location.longitude}. Sending home...");
int battery = DateTime.now().hour;
String url = "$httpWebHost/api/webhook/$webhookId";
Map<String, String> headers = {};
headers["Content-Type"] = "application/json";
var data = {
"type": "update_location",
"data": {
"gps": [location.latitude, location.longitude],
"gps_accuracy": location.accuracy,
"battery": battery
}
};
http.post(
url,
headers: headers,
body: json.encode(data)
).catchError((e) => print("[Location isolate #${Isolate.current.hashCode}] Error sending data: ${e.toString()}"));
});
} else {
print("[Location isolate #${Isolate.current.hashCode}] No webhook id. Aborting");
}
});
}
static final LocationManager _instance = LocationManager
._internal();
factory LocationManager() {
return _instance;
}
LocationManager._internal() {
init();
}
final int defaultUpdateIntervalMinutes = 15;
final int alarmId = 34901199;
Duration _updateInterval;
bool _isEnabled;
void init() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
_updateInterval = Duration(minutes: prefs.getInt("location-interval") ??
defaultUpdateIntervalMinutes);
_isEnabled = prefs.getBool("location-enabled") ?? false;
if (_isEnabled) {
_startLocationService();
}
}
void setSettings(bool enabled, int interval) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
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()
.httpWebHost}/api/webhook/${ConnectionManager().webhookId}";
Map<String, String> headers = {};
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;
var data = {
"type": "update_location",
"data": {
"gps": [location.latitude, location.longitude],
"gps_accuracy": location.accuracy,
"battery": battery
}
};
headers["Content-Type"] = "application/json";
await http.post(
url,
headers: headers,
body: json.encode(data)
);
Logger.d("[Location] ...done.");
} else {
Logger.d("[Location] No webhook id. Aborting");
}
} else {
Logger.d("[Location] Location tracking is disabled");
}
}
}

View File

@ -11,43 +11,18 @@ class StartupUserMessagesManager {
StartupUserMessagesManager._internal() {}
bool _locationTrackingMessageShown;
bool _supportAppDevelopmentMessageShown;
static final _locationTrackingMessageKey = "user-message-shown-location_3";
static final _supportAppDevelopmentMessageKey = "user-message-shown-support-development_3";
void checkMessagesToShow() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
_locationTrackingMessageShown = prefs.getBool(_locationTrackingMessageKey) ?? false;
_supportAppDevelopmentMessageShown = prefs.getBool(_supportAppDevelopmentMessageKey) ?? false;
if (!_locationTrackingMessageShown) {
_showLocationTrackingMessage();
} else if (!_supportAppDevelopmentMessageShown) {
if (!_supportAppDevelopmentMessageShown) {
_showSupportAppDevelopmentMessage();
}
}
void _showLocationTrackingMessage() {
eventBus.fire(ShowPopupDialogEvent(
title: "Device location tracking is here!",
body: "HA Client now support sending your device gps data to device_tracker instance created for current app integration. You can control location tracking in Configuration.",
positiveText: "Enable now",
negativeText: "Cancel",
onPositive: () {
SharedPreferences.getInstance().then((prefs) {
prefs.setBool(_locationTrackingMessageKey, true);
LocationManager().setSettings(true, 15);
});
},
onNegative: () {
SharedPreferences.getInstance().then((prefs) {
prefs.setBool(_locationTrackingMessageKey, true);
});
}
));
}
void _showSupportAppDevelopmentMessage() {
eventBus.fire(ShowPopupDialogEvent(
title: "Hi!",