diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index d6cedd6f..1dcce3b9 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 30 + compileSdkVersion 31 sourceSets { main.java.srcDirs += 'src/main/kotlin' diff --git a/example/lib/location_callback_handler.dart b/example/lib/location_callback_handler.dart index 6956e2b8..4b1fa3b3 100644 --- a/example/lib/location_callback_handler.dart +++ b/example/lib/location_callback_handler.dart @@ -1,7 +1,5 @@ import 'dart:async'; - import 'package:background_locator/location_dto.dart'; - import 'location_service_repository.dart'; class LocationCallbackHandler { diff --git a/example/lib/location_service_repository.dart b/example/lib/location_service_repository.dart index 14a0edb3..50a8f616 100644 --- a/example/lib/location_service_repository.dart +++ b/example/lib/location_service_repository.dart @@ -39,7 +39,7 @@ class LocationServiceRepository { } print("$_count"); await setLogLabel("start"); - final SendPort send = IsolateNameServer.lookupPortByName(isolateName); + final SendPort? send = IsolateNameServer.lookupPortByName(isolateName); send?.send(null); } @@ -47,17 +47,18 @@ class LocationServiceRepository { print("***********Dispose callback handler"); print("$_count"); await setLogLabel("end"); - final SendPort send = IsolateNameServer.lookupPortByName(isolateName); + final SendPort? send = IsolateNameServer.lookupPortByName(isolateName); send?.send(null); } - Future callback(LocationDto locationDto) async { - print('$_count location in dart: ${locationDto.toString()}'); - await setLogPosition(_count, locationDto); - final SendPort send = IsolateNameServer.lookupPortByName(isolateName); - send?.send(locationDto); - _count++; - + Future callback(dynamic locationDto) async { + if (locationDto is LocationDto) { + print('$_count location in dart: ${locationDto.toString()}'); + await setLogPosition(_count, locationDto); + final SendPort? send = IsolateNameServer.lookupPortByName(isolateName); + send?.send(locationDto); + _count++; + } } static Future setLogLabel(String label) async { @@ -73,7 +74,7 @@ class LocationServiceRepository { } static double dp(double val, int places) { - double mod = pow(10.0, places); + double mod = pow(10.0, places).toDouble(); return ((val * mod).round().toDouble() / mod); } diff --git a/example/lib/main.dart b/example/lib/main.dart index a13d16f6..a928ec51 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:ffi'; import 'dart:isolate'; import 'dart:ui'; @@ -9,7 +8,7 @@ import 'package:background_locator/settings/android_settings.dart'; import 'package:background_locator/settings/ios_settings.dart'; import 'package:background_locator/settings/locator_settings.dart'; import 'package:flutter/material.dart'; -import 'package:location_permissions/location_permissions.dart'; +import 'package:permission_handler/permission_handler.dart'; import 'file_manager.dart'; import 'location_callback_handler.dart'; @@ -26,8 +25,8 @@ class _MyAppState extends State { ReceivePort port = ReceivePort(); String logStr = ''; - bool isRunning; - LocationDto lastLocation; + bool isRunning = false; + LocationDto? lastLocation; @override void initState() { @@ -56,20 +55,18 @@ class _MyAppState extends State { super.dispose(); } - Future updateUI(LocationDto data) async { + Future updateUI(LocationDto? data) async { final log = await FileManager.readLogFile(); await _updateNotificationText(data); setState(() { - if (data != null) { - lastLocation = data; - } + lastLocation = data; logStr = log; }); } - Future _updateNotificationText(LocationDto data) async { + Future _updateNotificationText(LocationDto? data) async { if (data == null) { return; } @@ -125,12 +122,10 @@ class _MyAppState extends State { ), ); String msgStatus = "-"; - if (isRunning != null) { - if (isRunning) { - msgStatus = 'Is running'; - } else { - msgStatus = 'Is not running'; - } + if (isRunning) { + msgStatus = 'Is running'; + } else { + msgStatus = 'Is not running'; } final status = Text("Status: $msgStatus"); @@ -180,32 +175,40 @@ class _MyAppState extends State { } Future _checkLocationPermission() async { - final access = await LocationPermissions().checkPermissionStatus(); - switch (access) { - case PermissionStatus.unknown: - case PermissionStatus.denied: - case PermissionStatus.restricted: - final permission = await LocationPermissions().requestPermissions( - permissionLevel: LocationPermissionLevel.locationAlways, - ); - if (permission == PermissionStatus.granted) { - return true; - } else { - return false; + try { + /// Check service + /// Check Permission + PermissionStatus status = await Permission.location.status; + PermissionStatus statusAlways = await Permission.locationAlways.status; + if (statusAlways.isGranted) { + return true; + } + if (status.isDenied) { + /// Request if Denied + status = await Permission.location.request(); + } + // At first, make request for foreground location access. + // And then you can request background location access. + if (status.isGranted) { + if (!statusAlways.isGranted) { + statusAlways = await Permission.locationAlways.request(); + statusAlways = await Permission.locationAlways.status; } - break; - case PermissionStatus.granted: + } + if (statusAlways.isGranted) { return true; - break; - default: + } else { return false; - break; + } + } catch (e) { + return false; } } - Future _startLocator() async{ + Future _startLocator() async { Map data = {'countInit': 1}; - return await BackgroundLocator.registerLocationUpdate(LocationCallbackHandler.callback, + return await BackgroundLocator.registerLocationUpdate( + LocationCallbackHandler.callback, initCallback: LocationCallbackHandler.initCallback, initDataCallback: data, disposeCallback: LocationCallbackHandler.disposeCallback, diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ce0011f9..9d31e5f3 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,7 +3,7 @@ description: Demonstrates how to use the background_locator plugin. publish_to: 'none' environment: - sdk: ">=2.8.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: @@ -21,7 +21,7 @@ dev_dependencies: path: ../ path_provider: ^2.0.8 - location_permissions: ^3.0.0+1 + permission_handler: ^9.1.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index dab59c24..8b137891 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -1,26 +1 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility that Flutter provides. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. -import 'package:background_locator_example/main.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - -void main() { - testWidgets('Verify Platform version', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(MyApp()); - - // Verify that platform version is retrieved. - expect( - find.byWidgetPredicate( - (Widget widget) => - widget is Text && widget.data.startsWith('Running on:'), - ), - findsOneWidget, - ); - }); -} diff --git a/lib/location_dto.dart b/lib/location_dto.dart index c78022fe..512919fd 100644 --- a/lib/location_dto.dart +++ b/lib/location_dto.dart @@ -40,7 +40,7 @@ class LocationDto { json[Keys.ARG_HEADING], json[Keys.ARG_TIME], isLocationMocked, - json[Keys.ARG_PROVIDER], + json[Keys.ARG_PROVIDER] ?? '', ); }