From 2c72c43c29b922867944c4e017bdf2bffee927a7 Mon Sep 17 00:00:00 2001 From: CaramelDunes Date: Thu, 24 Sep 2020 18:22:00 +0200 Subject: [PATCH] Add background location recording. --- android/app/src/main/AndroidManifest.xml | 6 ++++-- lib/main.dart | 2 +- lib/{ => map}/flight_map.dart | 2 +- lib/{ => map}/map_screen.dart | 16 ++++++++-------- lib/record_button.dart | 14 ++++++++++++++ pubspec.lock | 7 +++++++ pubspec.yaml | 6 +++++- 7 files changed, 40 insertions(+), 13 deletions(-) rename lib/{ => map}/flight_map.dart (99%) rename lib/{ => map}/map_screen.dart (96%) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 557805d..a1f125e 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -4,11 +4,11 @@ - + + + diff --git a/lib/main.dart b/lib/main.dart index 494d919..e3faaaf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'map_screen.dart'; +import 'map/map_screen.dart'; void main() { runApp(MyApp()); diff --git a/lib/flight_map.dart b/lib/map/flight_map.dart similarity index 99% rename from lib/flight_map.dart rename to lib/map/flight_map.dart index 8757767..9000914 100644 --- a/lib/flight_map.dart +++ b/lib/map/flight_map.dart @@ -6,7 +6,7 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:nofussppg/instruments_data_source.dart'; import 'package:latlong/latlong.dart'; -import 'preset_layers.dart'; +import '../preset_layers.dart'; class FlightMap extends StatefulWidget { final Stream dataStream; diff --git a/lib/map_screen.dart b/lib/map/map_screen.dart similarity index 96% rename from lib/map_screen.dart rename to lib/map/map_screen.dart index 303c518..8352ef0 100644 --- a/lib/map_screen.dart +++ b/lib/map/map_screen.dart @@ -7,15 +7,15 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:latlong/latlong.dart'; import 'package:wakelock/wakelock.dart'; -import 'record_button.dart'; -import 'route_manager.dart'; +import '../record_button.dart'; +import '../route_manager.dart'; import 'flight_map.dart'; -import 'instruments.dart'; -import 'instruments_data_source.dart'; -import 'over_cities_screen.dart'; -import 'navigation_data_source.dart'; -import 'navigation_info.dart'; -import 'settings_screen.dart'; +import '../instruments.dart'; +import '../instruments_data_source.dart'; +import '../over_cities_screen.dart'; +import '../navigation_data_source.dart'; +import '../navigation_info.dart'; +import '../settings_screen.dart'; class MapScreen extends StatefulWidget { @override diff --git a/lib/record_button.dart b/lib/record_button.dart index 6bd47f9..0841fe9 100644 --- a/lib/record_button.dart +++ b/lib/record_button.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter_foreground_plugin/flutter_foreground_plugin.dart'; import 'package:path_provider/path_provider.dart'; import 'flight_recorder.dart'; @@ -53,6 +54,8 @@ class _RecordButtonState extends State { _recorderSubscription = null; _flightRecorder.dispose(); _flightRecorder = null; + + FlutterForegroundPlugin.stopForegroundService(); } else { getExternalStorageDirectory().then((value) { DateTime now = DateTime.now(); @@ -60,6 +63,7 @@ class _RecordButtonState extends State { '${value.path}/${now.year}-${now.month}-${now.day}_${now.hour}-${now.minute}.csv'; File outputFile = File(path); + print('Saving trace to $path'); if (!outputFile.existsSync()) { _flightRecorder = FlightRecorder(outputFile); @@ -71,7 +75,17 @@ class _RecordButtonState extends State { _recorderSubscription = widget.dataSource.data.listen((event) { _flightRecorder.appendData(event); }); + } else { + print('File already exists!'); } + + // Start a foreground service to keep receiving location updates when + // the app is in the background. + // Turns out I don't need any special background location library :-) + FlutterForegroundPlugin.startForegroundService( + title: "No Fuss PPG location service", + iconName: "ic_launcher", + ); }); } } diff --git a/pubspec.lock b/pubspec.lock index e5e1363..41c381d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -146,6 +146,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.4.2" + flutter_foreground_plugin: + dependency: "direct main" + description: + name: flutter_foreground_plugin + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.0" flutter_image: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5775f10..64ae793 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,6 +20,7 @@ dependencies: cupertino_icons: ^0.1.3 + flutter_foreground_plugin: ^0.5.0 cached_network_image: ^2.3.1 @@ -38,5 +39,8 @@ flutter: uses-material-design: true assets: - - assets/offline_layers/oaci_vfr + - assets/offline_layers/oaci_vfr/8/ + - assets/offline_layers/oaci_vfr/9/ + - assets/offline_layers/oaci_vfr/10/ + - assets/offline_layers/oaci_vfr/11/ - assets/survol.png