Update AndroidManifest to include foreground service permissions and implement tracking service in excursion screens. Refactor tracking logic to utilize TrackingService for better state management and streamline position updates.
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<!-- <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> -->
|
||||
<!-- <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" /> -->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
|
||||
<application
|
||||
android:label="LUPUS"
|
||||
android:name="${applicationName}"
|
||||
@@ -37,11 +37,12 @@
|
||||
android:name="flutterEmbedding"
|
||||
android:value="2" />
|
||||
|
||||
<!-- <service -->
|
||||
<!-- android:name="com.dexterous.flutterlocalnotifications.ForegroundService" -->
|
||||
<!-- android:exported="false" -->
|
||||
<!-- android:stopWithTask="false" -->
|
||||
<!-- android:foregroundServiceType="<location>"> -->
|
||||
<service
|
||||
android:name=".TrackingService"
|
||||
android:exported="false"
|
||||
android:stopWithTask="false"
|
||||
android:foregroundServiceType="location">
|
||||
</service>
|
||||
|
||||
</application>
|
||||
<!-- Required to query activities that can process text, see:
|
||||
|
||||
BIN
assets/icons/tracking_off.png
Normal file
BIN
assets/icons/tracking_off.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
BIN
assets/icons/tracking_on.png
Normal file
BIN
assets/icons/tracking_on.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.4 KiB |
@@ -19,6 +19,7 @@ import 'package:fforte/screens/sharedMethods/save_template.dart';
|
||||
import 'package:fforte/screens/sharedWidgets/datum.dart';
|
||||
import 'package:fforte/screens/sharedWidgets/var_text_field.dart';
|
||||
import 'package:fforte/l10n/app_localizations.dart';
|
||||
import 'package:fforte/services/tracking_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -147,7 +148,6 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
return currentPosition;
|
||||
});
|
||||
|
||||
|
||||
if (widget.existingData?.isNotEmpty ?? false) {
|
||||
for (var key in widget.existingData!.keys) {
|
||||
rmap[key]!["controller"]!.text =
|
||||
@@ -267,8 +267,7 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10)),
|
||||
child: ExpansionPanelList(
|
||||
expansionCallback:
|
||||
((int index, bool isExpanded) =>
|
||||
expansionCallback: ((int index, bool isExpanded) =>
|
||||
setState(() => bimaExtended = isExpanded)),
|
||||
expandedHeaderPadding: EdgeInsets.all(0),
|
||||
children: [
|
||||
@@ -277,7 +276,13 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
canTapOnHeader: true,
|
||||
headerBuilder: (context, bool isOpen) => Padding(
|
||||
padding: const EdgeInsets.only(left: 15),
|
||||
child: Align(alignment: Alignment.centerLeft, child: Text("BImA", style: Theme.of(context).textTheme.bodyLarge,),),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Text(
|
||||
"BImA",
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(15),
|
||||
@@ -286,7 +291,8 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
const SizedBox(height: 10),
|
||||
VarTextField(
|
||||
textController: rmap["BimaNr"]!["controller"]!,
|
||||
localization: AppLocalizations.of(context)!.bimaNr,
|
||||
localization:
|
||||
AppLocalizations.of(context)!.bimaNr,
|
||||
dbName: "BimaNr",
|
||||
required: false,
|
||||
dbDesignation: DatabasesEnum.excursion,
|
||||
@@ -294,8 +300,10 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
const SizedBox(height: 10),
|
||||
// ---------- Bima name
|
||||
VarTextField(
|
||||
textController: rmap["BimaName"]!["controller"]!,
|
||||
localization: AppLocalizations.of(context)!.bimaName,
|
||||
textController:
|
||||
rmap["BimaName"]!["controller"]!,
|
||||
localization:
|
||||
AppLocalizations.of(context)!.bimaName,
|
||||
dbName: "BimaName",
|
||||
required: false,
|
||||
dbDesignation: DatabasesEnum.excursion,
|
||||
@@ -305,7 +313,8 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
BimaNutzer(
|
||||
onBimaNutzerChanged: (value) {
|
||||
setState(() {
|
||||
rmap["BimaNutzer"]!["controller"]!.text = value;
|
||||
rmap["BimaNutzer"]!["controller"]!.text =
|
||||
value;
|
||||
});
|
||||
},
|
||||
),
|
||||
@@ -313,7 +322,8 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
// ---------- Bima AGV
|
||||
VarTextField(
|
||||
textController: rmap["BimaAGV"]!["controller"]!,
|
||||
localization: AppLocalizations.of(context)!.bimaAGV,
|
||||
localization:
|
||||
AppLocalizations.of(context)!.bimaAGV,
|
||||
dbName: "BimaAGV",
|
||||
required: false,
|
||||
dbDesignation: DatabasesEnum.excursion,
|
||||
@@ -334,19 +344,19 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
children: [
|
||||
// ---------- Tracking
|
||||
ElevatedButton(
|
||||
onPressed:
|
||||
() => Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
onPressed: () async {
|
||||
await Navigator.push(context, MaterialPageRoute(
|
||||
builder: (context) {
|
||||
return Tracking(
|
||||
weg: rmap["Weg"]!["controller"]!,
|
||||
startPosition: currentPosition,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
child: Text(AppLocalizations.of(context)!.trackingAnAusschalten),
|
||||
));
|
||||
setState(() {});
|
||||
},
|
||||
child:
|
||||
Text(AppLocalizations.of(context)!.trackingAnAusschalten),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
@@ -369,7 +379,8 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
// ---------- Last precipitation
|
||||
LetzterNiederschlag(controller: rmap["RegenVor"]!["controller"]!),
|
||||
LetzterNiederschlag(
|
||||
controller: rmap["RegenVor"]!["controller"]!),
|
||||
const SizedBox(height: 20),
|
||||
// ---------- Track conditions
|
||||
StreckeUSpurbedingungen(
|
||||
@@ -449,7 +460,16 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
|
||||
// Begin of widget tree
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(AppLocalizations.of(context)!.excursion)),
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)!.excursion),
|
||||
actions: [
|
||||
// Text(TrackingService().isTracking ? "Tracking" : "Not tracking")
|
||||
Image.asset(
|
||||
TrackingService().isTracking ? "assets/icons/tracking_on.png" : "assets/icons/tracking_off.png",
|
||||
width: 40,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: PageTransitionSwitcher(
|
||||
duration: const Duration(microseconds: 800),
|
||||
transitionBuilder: (
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fforte/l10n/app_localizations.dart';
|
||||
import 'package:fforte/screens/addCam/services/geolocator_service.dart';
|
||||
import 'package:fforte/screens/helper/add_entries_dialog_helper.dart';
|
||||
import 'package:fforte/screens/helper/snack_bar_helper.dart';
|
||||
import 'package:fforte/services/notification_service.dart';
|
||||
import 'package:fforte/services/tracking_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
|
||||
@@ -22,20 +21,14 @@ class Tracking extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TrackingState extends State<Tracking> {
|
||||
List<LatLng> pathList = [];
|
||||
StreamSubscription<Position>? positionStream;
|
||||
final TrackingService _trackingService = TrackingService();
|
||||
LocationMarkerPosition? locationMarkerPosition;
|
||||
bool positionStreamRunning = false;
|
||||
MapController mapController = MapController();
|
||||
|
||||
Random rand = Random();
|
||||
StreamSubscription? _positionSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// debugging (i guess)
|
||||
// pathList.add(
|
||||
// LatLng(widget.startPosition.latitude, widget.startPosition.longitude),
|
||||
// );
|
||||
super.initState();
|
||||
|
||||
if (widget.weg.text.isNotEmpty) {
|
||||
for (var element in widget.weg.text.split(";")) {
|
||||
@@ -47,7 +40,7 @@ class _TrackingState extends State<Tracking> {
|
||||
// ignore because the double is short enough then
|
||||
}
|
||||
|
||||
pathList.add(
|
||||
_trackingService.pathList.add(
|
||||
LatLng(double.parse(posSplit.first), double.parse(posSplit[1])),
|
||||
);
|
||||
}
|
||||
@@ -59,7 +52,15 @@ class _TrackingState extends State<Tracking> {
|
||||
accuracy: widget.startPosition.accuracy,
|
||||
);
|
||||
|
||||
super.initState();
|
||||
_positionSubscription = _trackingService.positionStream$.listen((position) {
|
||||
setState(() {
|
||||
locationMarkerPosition = LocationMarkerPosition(
|
||||
latitude: position.latitude,
|
||||
longitude: position.longitude,
|
||||
accuracy: position.accuracy,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
GeolocatorService.alwaysPositionEnabled().then((value) {
|
||||
if (!value && mounted) {
|
||||
@@ -71,101 +72,30 @@ class _TrackingState extends State<Tracking> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (positionStream != null) positionStream!.cancel();
|
||||
bool isFirst = true;
|
||||
if (pathList.isNotEmpty) {
|
||||
for (var pos in pathList) {
|
||||
if (!isFirst) {
|
||||
widget.weg.text += ";";
|
||||
} else {
|
||||
isFirst = false;
|
||||
}
|
||||
|
||||
widget.weg.text += "${pos.latitude},${pos.longitude}";
|
||||
}
|
||||
}
|
||||
NotificationService().deleteNotification();
|
||||
|
||||
_positionSubscription?.cancel();
|
||||
widget.weg.text = _trackingService.getPathAsString();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
final LocationSettings streamLocationSettings = LocationSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
distanceFilter: 10,
|
||||
);
|
||||
|
||||
void onTrackingStart() async {
|
||||
// notification handling for tracking in background notification
|
||||
await NotificationService().initNotification();
|
||||
|
||||
if (mounted) {
|
||||
NotificationService().showNotification(
|
||||
title: AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
);
|
||||
}
|
||||
|
||||
positionStream = Geolocator.getPositionStream(
|
||||
locationSettings: AndroidSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
distanceFilter: 0,
|
||||
foregroundNotificationConfig:
|
||||
mounted
|
||||
? ForegroundNotificationConfig(
|
||||
notificationTitle:
|
||||
AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
notificationText: "",
|
||||
)
|
||||
: null,
|
||||
|
||||
),
|
||||
).listen((Position? position) {
|
||||
if (position != null) {
|
||||
setState(() {
|
||||
pathList.add(LatLng(position.latitude, position.longitude));
|
||||
// Random value for debugging
|
||||
// pathList.add(LatLng(rand.nextInt(5) + 40, position.longitude));
|
||||
|
||||
locationMarkerPosition = LocationMarkerPosition(
|
||||
latitude: position.latitude,
|
||||
longitude: position.longitude,
|
||||
accuracy: position.accuracy,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
if (mounted) {
|
||||
SnackBarHelper.showSnackBarMessage(
|
||||
context,
|
||||
AppLocalizations.of(context)!.couldntDeterminePosition,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
positionStream!.onError((e) {
|
||||
NotificationService().deleteNotification();
|
||||
NotificationService().showNotification(title: "ERROR: $e");
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)!.tracking),
|
||||
leading: IconButton(onPressed: () {
|
||||
leading: IconButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
}, icon: Icon(Icons.arrow_back_rounded)),
|
||||
},
|
||||
icon: Icon(Icons.arrow_back_rounded)
|
||||
),
|
||||
actions: [
|
||||
if (!positionStreamRunning)
|
||||
if (!_trackingService.isTracking)
|
||||
IconButton(
|
||||
onPressed: () async {
|
||||
bool delete =
|
||||
await AddEntriesDialogHelper.deleteCompleteRouteDialog(
|
||||
context,
|
||||
);
|
||||
|
||||
bool delete = await AddEntriesDialogHelper.deleteCompleteRouteDialog(context);
|
||||
if (delete) {
|
||||
setState(() {
|
||||
pathList = [];
|
||||
_trackingService.clearPath();
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -174,32 +104,32 @@ class _TrackingState extends State<Tracking> {
|
||||
color: Theme.of(context).colorScheme.errorContainer,
|
||||
),
|
||||
),
|
||||
if (positionStreamRunning)
|
||||
if (_trackingService.isTracking)
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
positionStreamRunning = false;
|
||||
positionStream!.cancel();
|
||||
NotificationService().deleteNotification();
|
||||
_trackingService.stopTracking();
|
||||
});
|
||||
},
|
||||
child: Text(AppLocalizations.of(context)!.trackingStop),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
if (positionStreamRunning) {
|
||||
positionStreamRunning = false;
|
||||
positionStream?.pause();
|
||||
setState(() {
|
||||
if (_trackingService.isTracking) {
|
||||
_trackingService.pauseTracking();
|
||||
} else {
|
||||
positionStreamRunning = true;
|
||||
onTrackingStart();
|
||||
if (_trackingService.positionStream == null) {
|
||||
_trackingService.startTracking(context);
|
||||
} else {
|
||||
_trackingService.resumeTracking();
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
},
|
||||
child:
|
||||
positionStreamRunning
|
||||
child: _trackingService.isTracking
|
||||
? Text(AppLocalizations.of(context)!.trackingPause)
|
||||
: Text(AppLocalizations.of(context)!.trackingStart)
|
||||
: Text(AppLocalizations.of(context)!.trackingStart),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -219,8 +149,7 @@ class _TrackingState extends State<Tracking> {
|
||||
mapController: mapController,
|
||||
options: MapOptions(
|
||||
interactionOptions: const InteractionOptions(
|
||||
flags:
|
||||
InteractiveFlag.pinchZoom |
|
||||
flags: InteractiveFlag.pinchZoom |
|
||||
InteractiveFlag.drag |
|
||||
InteractiveFlag.pinchMove,
|
||||
),
|
||||
@@ -235,22 +164,16 @@ class _TrackingState extends State<Tracking> {
|
||||
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
userAgentPackageName: 'de.lupus.apps',
|
||||
),
|
||||
if (pathList.isNotEmpty)
|
||||
if (_trackingService.pathList.isNotEmpty)
|
||||
PolylineLayer(
|
||||
polylines: [
|
||||
Polyline(strokeWidth: 2.0, points: pathList, color: Colors.red),
|
||||
Polyline(
|
||||
strokeWidth: 2.0,
|
||||
points: _trackingService.pathList,
|
||||
color: Colors.red
|
||||
),
|
||||
],
|
||||
),
|
||||
// CircleLayer(
|
||||
// circles: [
|
||||
// CircleMarker(
|
||||
// color: Colors.blue,
|
||||
// point: pathList.isEmpty ? widget.startPosition : pathList.last,
|
||||
// radius: 5,
|
||||
// useRadiusInMeter: true,
|
||||
// ),
|
||||
// ],
|
||||
// ),
|
||||
CurrentLocationLayer(),
|
||||
],
|
||||
),
|
||||
|
||||
83
lib/services/tracking_service.dart
Normal file
83
lib/services/tracking_service.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:fforte/l10n/app_localizations.dart';
|
||||
import 'package:fforte/services/notification_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
class TrackingService {
|
||||
static final TrackingService _instance = TrackingService._internal();
|
||||
factory TrackingService() => _instance;
|
||||
TrackingService._internal();
|
||||
|
||||
List<LatLng> pathList = [];
|
||||
StreamSubscription<Position>? positionStream;
|
||||
bool isTracking = false;
|
||||
final _positionController = StreamController<Position>.broadcast();
|
||||
|
||||
Stream<Position> get positionStream$ => _positionController.stream;
|
||||
|
||||
Future<void> startTracking(BuildContext context) async {
|
||||
if (isTracking) return;
|
||||
|
||||
await NotificationService().initNotification();
|
||||
if (context.mounted) {
|
||||
NotificationService().showNotification(
|
||||
title: AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
);
|
||||
}
|
||||
|
||||
positionStream = Geolocator.getPositionStream(
|
||||
locationSettings: AndroidSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
distanceFilter: 0,
|
||||
foregroundNotificationConfig: ForegroundNotificationConfig(
|
||||
notificationTitle: context.mounted ? AppLocalizations.of(context)!.trackingRunningInBackground : "Tracking",
|
||||
notificationText: "",
|
||||
),
|
||||
),
|
||||
).listen((Position? position) {
|
||||
if (position != null) {
|
||||
pathList.add(LatLng(position.latitude, position.longitude));
|
||||
_positionController.add(position);
|
||||
}
|
||||
});
|
||||
|
||||
positionStream!.onError((e) {
|
||||
NotificationService().deleteNotification();
|
||||
NotificationService().showNotification(title: "ERROR: $e");
|
||||
});
|
||||
|
||||
isTracking = true;
|
||||
}
|
||||
|
||||
void pauseTracking() {
|
||||
positionStream?.pause();
|
||||
isTracking = false;
|
||||
}
|
||||
|
||||
void resumeTracking() {
|
||||
positionStream?.resume();
|
||||
isTracking = true;
|
||||
}
|
||||
|
||||
void stopTracking() {
|
||||
positionStream?.cancel();
|
||||
NotificationService().deleteNotification();
|
||||
isTracking = false;
|
||||
}
|
||||
|
||||
void clearPath() {
|
||||
pathList.clear();
|
||||
}
|
||||
|
||||
String getPathAsString() {
|
||||
return pathList.map((pos) => "${pos.latitude},${pos.longitude}").join(";");
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
stopTracking();
|
||||
_positionController.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user