Disabling location tracking
This commit is contained in:
parent
ab5bf3b807
commit
620aa3b8d8
@ -1,7 +1,5 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:typed_data';
|
|
||||||
import 'dart:isolate';
|
|
||||||
import 'package:flutter/rendering.dart';
|
import 'package:flutter/rendering.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
@ -23,8 +21,6 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|||||||
import 'package:device_info/device_info.dart';
|
import 'package:device_info/device_info.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||||
import 'package:android_alarm_manager/android_alarm_manager.dart';
|
|
||||||
import 'package:geolocator/geolocator.dart';
|
|
||||||
|
|
||||||
part 'const.dart';
|
part 'const.dart';
|
||||||
part 'utils/launcher.dart';
|
part 'utils/launcher.dart';
|
||||||
@ -299,7 +295,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
_showInfoBottomBar(progress: true,);
|
_showInfoBottomBar(progress: true,);
|
||||||
_subscribe().then((_) {
|
_subscribe().then((_) {
|
||||||
ConnectionManager().init(loadSettings: true, forceReconnect: true).then((__){
|
ConnectionManager().init(loadSettings: true, forceReconnect: true).then((__){
|
||||||
LocationManager();
|
|
||||||
_fetchData();
|
_fetchData();
|
||||||
StartupUserMessagesManager().checkMessagesToShow();
|
StartupUserMessagesManager().checkMessagesToShow();
|
||||||
}, onError: (e) {
|
}, onError: (e) {
|
||||||
@ -312,7 +307,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
_hideBottomBar();
|
_hideBottomBar();
|
||||||
_showInfoBottomBar(progress: true,);
|
_showInfoBottomBar(progress: true,);
|
||||||
ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){
|
ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){
|
||||||
LocationManager().updateDeviceLocation();
|
|
||||||
_fetchData();
|
_fetchData();
|
||||||
StartupUserMessagesManager().checkMessagesToShow();
|
StartupUserMessagesManager().checkMessagesToShow();
|
||||||
}, onError: (e) {
|
}, onError: (e) {
|
||||||
|
@ -2,146 +2,4 @@ part of '../main.dart';
|
|||||||
|
|
||||||
class LocationManager {
|
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -11,43 +11,18 @@ class StartupUserMessagesManager {
|
|||||||
|
|
||||||
StartupUserMessagesManager._internal() {}
|
StartupUserMessagesManager._internal() {}
|
||||||
|
|
||||||
bool _locationTrackingMessageShown;
|
|
||||||
bool _supportAppDevelopmentMessageShown;
|
bool _supportAppDevelopmentMessageShown;
|
||||||
static final _locationTrackingMessageKey = "user-message-shown-location_3";
|
|
||||||
static final _supportAppDevelopmentMessageKey = "user-message-shown-support-development_3";
|
static final _supportAppDevelopmentMessageKey = "user-message-shown-support-development_3";
|
||||||
|
|
||||||
void checkMessagesToShow() async {
|
void checkMessagesToShow() async {
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
await prefs.reload();
|
await prefs.reload();
|
||||||
_locationTrackingMessageShown = prefs.getBool(_locationTrackingMessageKey) ?? false;
|
|
||||||
_supportAppDevelopmentMessageShown = prefs.getBool(_supportAppDevelopmentMessageKey) ?? false;
|
_supportAppDevelopmentMessageShown = prefs.getBool(_supportAppDevelopmentMessageKey) ?? false;
|
||||||
if (!_locationTrackingMessageShown) {
|
if (!_supportAppDevelopmentMessageShown) {
|
||||||
_showLocationTrackingMessage();
|
|
||||||
} else if (!_supportAppDevelopmentMessageShown) {
|
|
||||||
_showSupportAppDevelopmentMessage();
|
_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() {
|
void _showSupportAppDevelopmentMessage() {
|
||||||
eventBus.fire(ShowPopupDialogEvent(
|
eventBus.fire(ShowPopupDialogEvent(
|
||||||
title: "Hi!",
|
title: "Hi!",
|
||||||
|
@ -9,40 +9,9 @@ class ConfigPanelWidget extends StatefulWidget {
|
|||||||
|
|
||||||
class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
||||||
|
|
||||||
int _locationInterval = LocationManager().defaultUpdateIntervalMinutes;
|
|
||||||
bool _locationTrackingEnabled = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
_loadSettings() async {
|
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
||||||
await prefs.reload();
|
|
||||||
SharedPreferences.getInstance().then((prefs) {
|
|
||||||
setState(() {
|
|
||||||
_locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
|
|
||||||
_locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void incLocationInterval() {
|
|
||||||
if (_locationInterval < 720) {
|
|
||||||
setState(() {
|
|
||||||
_locationInterval = _locationInterval + 1;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void decLocationInterval() {
|
|
||||||
if (_locationInterval > 1) {
|
|
||||||
setState(() {
|
|
||||||
_locationInterval = _locationInterval - 1;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
restart() {
|
restart() {
|
||||||
@ -123,43 +92,6 @@ 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;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
Container(height: Sizes.rowPadding,),
|
|
||||||
Text("Location update interval in minutes:"),
|
|
||||||
Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
mainAxisSize: MainAxisSize.max,
|
|
||||||
children: <Widget>[
|
|
||||||
//Expanded(child: Container(),),
|
|
||||||
FlatButton(
|
|
||||||
padding: EdgeInsets.all(0.0),
|
|
||||||
child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)),
|
|
||||||
onPressed: () => incLocationInterval(),
|
|
||||||
),
|
|
||||||
Text("$_locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)),
|
|
||||||
FlatButton(
|
|
||||||
padding: EdgeInsets.all(0.0),
|
|
||||||
child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)),
|
|
||||||
onPressed: () => decLocationInterval(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -183,7 +115,6 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
LocationManager().setSettings(_locationTrackingEnabled, _locationInterval);
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
pubspec.lock
28
pubspec.lock
@ -1,13 +1,6 @@
|
|||||||
# Generated by pub
|
# Generated by pub
|
||||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||||
packages:
|
packages:
|
||||||
android_alarm_manager:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: android_alarm_manager
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "0.4.4"
|
|
||||||
archive:
|
archive:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -172,20 +165,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.3.7"
|
version: "0.3.7"
|
||||||
geolocator:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: geolocator
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "5.1.3"
|
|
||||||
google_api_availability:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: google_api_availability
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "2.0.1"
|
|
||||||
http:
|
http:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -228,13 +207,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.4.0"
|
version: "2.4.0"
|
||||||
location_permissions:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: location_permissions
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "2.0.2"
|
|
||||||
logging:
|
logging:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -26,8 +26,6 @@ dependencies:
|
|||||||
flutter_secure_storage: ^3.2.1+1
|
flutter_secure_storage: ^3.2.1+1
|
||||||
device_info: ^0.4.0+2
|
device_info: ^0.4.0+2
|
||||||
flutter_local_notifications: ^0.8.2
|
flutter_local_notifications: ^0.8.2
|
||||||
android_alarm_manager: ^0.4.4
|
|
||||||
geolocator: ^5.1.3
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Reference in New Issue
Block a user