大幅変更&環境バージョンアップ
This commit is contained in:
@ -1,154 +1,311 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:rogapp/routes/app_pages.dart';
|
||||
import 'dart:async';
|
||||
|
||||
class PermissionHandlerScreen extends StatefulWidget {
|
||||
const PermissionHandlerScreen({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<PermissionHandlerScreen> createState() => _PermissionHandlerScreenState();
|
||||
}
|
||||
class PermissionController {
|
||||
|
||||
class _PermissionHandlerScreenState extends State<PermissionHandlerScreen> {
|
||||
|
||||
static bool _isRequestingPermission = false;
|
||||
static Completer<bool>? _permissionCompleter;
|
||||
|
||||
Future<void> _showMyDialog() async {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false, // user must tap button!
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('ロケーション許可'),
|
||||
content: const SingleChildScrollView(
|
||||
child: ListBody(
|
||||
children: <Widget>[
|
||||
Text( 'このアプリでは、位置情報の収集を行います。'),
|
||||
Text( 'このアプリでは、開始時点で位置情報を収集します。'),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: const Text('わかった'),
|
||||
onPressed: () {
|
||||
//Navigator.of(context).pop();
|
||||
Get.toNamed(AppPages.TRAVEL);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
static Future<bool> checkLocationPermissions() async {
|
||||
final locationPermission = await Permission.location.status;
|
||||
final whenInUsePermission = await Permission.locationWhenInUse.status;
|
||||
final alwaysPermission = await Permission.locationAlways.status;
|
||||
|
||||
return locationPermission == PermissionStatus.granted &&
|
||||
(whenInUsePermission == PermissionStatus.granted || alwaysPermission == PermissionStatus.granted);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
|
||||
//permissionServiceCall();
|
||||
}
|
||||
static Future<bool> checkAndRequestPermissions() async {
|
||||
if (_isRequestingPermission) {
|
||||
return _permissionCompleter!.future;
|
||||
}
|
||||
|
||||
Future<PermissionStatus> checkLocationPermission() async {
|
||||
return await Permission.location.status;
|
||||
}
|
||||
_isRequestingPermission = true;
|
||||
_permissionCompleter = Completer<bool>();
|
||||
|
||||
permissionServiceCall() async {
|
||||
await permissionServices().then(
|
||||
(value) {
|
||||
if (value[Permission.location]!.isGranted ) {
|
||||
/* ========= New Screen Added ============= */
|
||||
|
||||
Get.toNamed(AppPages.TRAVEL);
|
||||
|
||||
// Navigator.pushReplacement(
|
||||
// context,
|
||||
// MaterialPageRoute(builder: (context) => SplashScreen()),
|
||||
// );
|
||||
bool hasPermissions = await checkLocationPermissions();
|
||||
if (!hasPermissions) {
|
||||
bool userAgreed = await showLocationDisclosure();
|
||||
if (userAgreed) {
|
||||
try {
|
||||
await requestAllLocationPermissions();
|
||||
hasPermissions = await checkLocationPermissions();
|
||||
} catch (e) {
|
||||
print('Error requesting location permissions: $e');
|
||||
hasPermissions = false;
|
||||
}
|
||||
else{
|
||||
_showMyDialog();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*Permission services*/
|
||||
Future<Map<Permission, PermissionStatus>> permissionServices() async {
|
||||
// You can request multiple permissions at once.
|
||||
Map<Permission, PermissionStatus> statuses = await [
|
||||
Permission.location,
|
||||
|
||||
//add more permission to request here.
|
||||
].request();
|
||||
|
||||
if (statuses[Permission.location]!.isPermanentlyDenied) {
|
||||
await openAppSettings().then(
|
||||
(value) async {
|
||||
if (value) {
|
||||
if (await Permission.location.status.isPermanentlyDenied == true &&
|
||||
await Permission.location.status.isGranted == false) {
|
||||
// openAppSettings();
|
||||
permissionServiceCall(); /* opens app settings until permission is granted */
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
} else {
|
||||
if (statuses[Permission.location]!.isDenied) {
|
||||
permissionServiceCall();
|
||||
} else {
|
||||
print('User did not agree to location usage');
|
||||
hasPermissions = false;
|
||||
// アプリを終了
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/*{Permission.camera: PermissionStatus.granted, Permission.storage: PermissionStatus.granted}*/
|
||||
return statuses;
|
||||
|
||||
_isRequestingPermission = false;
|
||||
_permissionCompleter!.complete(hasPermissions);
|
||||
return _permissionCompleter!.future;
|
||||
}
|
||||
|
||||
static Future<void> requestAllLocationPermissions() async {
|
||||
await Permission.location.request();
|
||||
await Permission.locationWhenInUse.request();
|
||||
await Permission.locationAlways.request();
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var status = Permission.location.status.then((value){
|
||||
if(value.isGranted == false){
|
||||
Future.delayed(Duration.zero, () => showAlert(context));
|
||||
if (await Permission.locationAlways.isGranted) {
|
||||
const platform = MethodChannel('location');
|
||||
try {
|
||||
await platform.invokeMethod('startLocationService');
|
||||
} on PlatformException catch (e) {
|
||||
debugPrint("Failed to start location service: '${e.message}'.");
|
||||
}
|
||||
else {
|
||||
Get.toNamed(AppPages.TRAVEL);
|
||||
}
|
||||
});
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
child: const Text(""),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void showAlert(BuildContext context) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('ロケーション許可'),
|
||||
content: const SingleChildScrollView(
|
||||
child: ListBody(
|
||||
children: <Widget>[
|
||||
Text( 'このアプリでは、位置情報の収集を行います。'),
|
||||
Text('岐阜ナビアプリではチェックポイントの自動チェックインの機能を可能にするために、現在地のデータが収集されます。アプリを閉じている時や、使用していないときにも収集されます。位置情報は、個人を特定できない統計的な情報として、ユーザーの個人情報とは一切結びつかない形で送信されます。お知らせの配信、位置情報の利用を許可しない場合は、この後表示されるダイアログで「許可しない」を選択してください。'),
|
||||
],
|
||||
),
|
||||
static Future<bool> showLocationDisclosure() async {
|
||||
return await Get.dialog<bool>(
|
||||
AlertDialog(
|
||||
title: const Text('位置情報の使用について'),
|
||||
content: const SingleChildScrollView(
|
||||
child: ListBody(
|
||||
children: <Widget>[
|
||||
Text('このアプリでは、以下の目的で位置情報を使用します:'),
|
||||
Text('• チェックポイントの自動チェックイン(アプリが閉じているときも含む)'),
|
||||
Text('• 移動履歴の記録(バックグラウンドでも継続)'),
|
||||
Text('• 現在地周辺の情報表示'),
|
||||
Text('\nバックグラウンドでも位置情報を継続的に取得します。'),
|
||||
Text('これにより、バッテリーの消費が増加する可能性があります。'),
|
||||
Text('同意しない場合には、アプリは終了します。'),
|
||||
],
|
||||
),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: const Text('わかった'),
|
||||
onPressed: () {
|
||||
permissionServiceCall();
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: const Text('同意しない'),
|
||||
onPressed: () => Get.back(result: false),
|
||||
),
|
||||
TextButton(
|
||||
child: const Text('同意する'),
|
||||
onPressed: () => Get.back(result: true),
|
||||
),
|
||||
],
|
||||
),
|
||||
barrierDismissible: false,
|
||||
) ?? false;
|
||||
}
|
||||
|
||||
static void showPermissionDeniedDialog(String title,String message) {
|
||||
Get.dialog(
|
||||
AlertDialog(
|
||||
//title: Text('location_permission_needed_title'.tr),
|
||||
title: Text(title.tr),
|
||||
// 位置情報への許可が必要です
|
||||
//content: Text('location_permission_needed_main'.tr),
|
||||
content: Text(message.tr),
|
||||
// 岐阜ロゲでは、位置情報を使用してスタート・チェックイン・ゴール等の通過照明及び移動手段の記録のために、位置情報のトラッキングを行なっています。このためバックグラウンドでもトラッキングができるように位置情報の権限が必要です。
|
||||
// 設定画面で、「岐阜ナビ」に対して、常に位置情報を許可するように設定してください。
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text('キャンセル'),
|
||||
onPressed: () => Get.back(),
|
||||
),
|
||||
TextButton(
|
||||
child: const Text('設定'),
|
||||
onPressed: () {
|
||||
Get.back();
|
||||
openAppSettings();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
static Future<bool> requestLocationPermissions(BuildContext context) async {
|
||||
if (_isRequestingPermission) {
|
||||
// If a request is already in progress, wait for it to complete
|
||||
return _permissionCompleter!.future;
|
||||
}
|
||||
|
||||
_isRequestingPermission = true;
|
||||
_permissionCompleter = Completer<bool>();
|
||||
|
||||
bool userAgreed = await showLocationDisclosure(context);
|
||||
if (userAgreed) {
|
||||
try {
|
||||
final locationStatus = await Permission.location.request();
|
||||
final whenInUseStatus = await Permission.locationWhenInUse.request();
|
||||
final alwaysStatus = await Permission.locationAlways.request();
|
||||
|
||||
if (locationStatus == PermissionStatus.granted &&
|
||||
(whenInUseStatus == PermissionStatus.granted || alwaysStatus == PermissionStatus.granted)) {
|
||||
_permissionCompleter!.complete(true);
|
||||
} else {
|
||||
showPermissionDeniedDialog('location_permission_needed_title', 'location_permission_needed_main');
|
||||
_permissionCompleter!.complete(false);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error requesting location permission: $e');
|
||||
_permissionCompleter!.complete(false);
|
||||
}
|
||||
} else {
|
||||
print('User did not agree to location usage');
|
||||
_permissionCompleter!.complete(false);
|
||||
// Exit the app
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
|
||||
_isRequestingPermission = false;
|
||||
return _permissionCompleter!.future;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static Future<bool> checkStoragePermission() async {
|
||||
//debugPrint("(gifunavi)== checkStoragePermission ==");
|
||||
final storagePermission = await Permission.storage.status;
|
||||
return storagePermission == PermissionStatus.granted;
|
||||
}
|
||||
|
||||
static Future<void> requestStoragePermission() async {
|
||||
//debugPrint("(gifunavi)== requestStoragePermission ==");
|
||||
final storagePermission = await Permission.storage.request();
|
||||
|
||||
if (storagePermission == PermissionStatus.granted) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (storagePermission == PermissionStatus.permanentlyDenied) {
|
||||
// リクエストが完了するまで待機
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
showPermissionDeniedDialog('storage_permission_needed_title','storage_permission_needed_main');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
static Future<bool> checkLocationBasicPermission() async {
|
||||
//debugPrint("(gifunavi)== checkLocationBasicPermission ==");
|
||||
final locationPermission = await Permission.location.status;
|
||||
return locationPermission == PermissionStatus.granted;
|
||||
}
|
||||
|
||||
static Future<bool> checkLocationWhenInUsePermission() async {
|
||||
//debugPrint("(gifunavi)== checkLocationWhenInUsePermission ==");
|
||||
final whenInUsePermission = await Permission.locationWhenInUse.status;
|
||||
return whenInUsePermission == PermissionStatus.granted;
|
||||
}
|
||||
|
||||
static Future<bool> checkLocationAlwaysPermission() async {
|
||||
//debugPrint("(gifunavi)== checkLocationAlwaysPermission ==");
|
||||
final alwaysPermission = await Permission.locationAlways.status;
|
||||
return alwaysPermission == PermissionStatus.granted;
|
||||
}
|
||||
|
||||
static bool isBasicPermission=false;
|
||||
static Future<void> requestLocationBasicPermissions() async {
|
||||
//debugPrint("(gifunavi)== requestLocationBasicPermissions ==");
|
||||
try{
|
||||
if(!isBasicPermission){
|
||||
isBasicPermission=true;
|
||||
final locationStatus = await Permission.location.request();
|
||||
|
||||
if (locationStatus != PermissionStatus.granted) {
|
||||
showPermissionDeniedDialog('location_permission_needed_title','location_permission_needed_main');
|
||||
}
|
||||
}
|
||||
}catch (e, stackTrace){
|
||||
print('Exception: $e');
|
||||
print('Stack trace: $stackTrace');
|
||||
debugPrintStack(label: 'Exception occurred', stackTrace: stackTrace);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static bool isLocationServiceRunning = false;
|
||||
static bool isRequestedWhenInUsePermission = false;
|
||||
|
||||
static Future<void> requestLocationWhenInUsePermissions() async {
|
||||
//debugPrint("(gifunavi)== requestLocationWhenInUsePermissions ==");
|
||||
|
||||
try{
|
||||
if(!isRequestedWhenInUsePermission){
|
||||
isRequestedWhenInUsePermission=true;
|
||||
final whenInUseStatus = await Permission.locationWhenInUse.request();
|
||||
|
||||
if (whenInUseStatus != PermissionStatus.granted) {
|
||||
showPermissionDeniedDialog('location_permission_needed_title','location_permission_needed_main');
|
||||
}else{
|
||||
if( !isLocationServiceRunning ){
|
||||
isLocationServiceRunning=true;
|
||||
const platform = MethodChannel('location');
|
||||
try {
|
||||
await platform.invokeMethod('startLocationService'); // Location Service を開始する。
|
||||
} on PlatformException catch (e) {
|
||||
debugPrint("Failed to start location service: '${e.message}'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (e, stackTrace){
|
||||
debugPrint('Exception: $e');
|
||||
debugPrint('Stack trace: $stackTrace');
|
||||
debugPrintStack(label: 'Exception occurred', stackTrace: stackTrace);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static bool isRequestedAlwaysPermission = false;
|
||||
|
||||
static Future<void> requestLocationAlwaysPermissions() async {
|
||||
//debugPrint("(gifunavi)== requestLocationAlwaysPermissions ==");
|
||||
|
||||
try {
|
||||
if( !isRequestedAlwaysPermission ){
|
||||
isRequestedAlwaysPermission=true;
|
||||
final alwaysStatus = await Permission.locationAlways.request();
|
||||
|
||||
if (alwaysStatus != PermissionStatus.granted) {
|
||||
showPermissionDeniedDialog('location_permission_needed_title','location_permission_needed_main');
|
||||
}
|
||||
}
|
||||
}catch (e, stackTrace){
|
||||
print('Exception: $e');
|
||||
print('Stack trace: $stackTrace');
|
||||
debugPrintStack(label: 'Exception occurred', stackTrace: stackTrace);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static Future<void> checkAndRequestPermissions() async {
|
||||
final hasPermissions = await checkLocationBasicPermission();
|
||||
if (!hasPermissions) {
|
||||
await requestLocationBasicPermissions();
|
||||
}
|
||||
|
||||
final hasWIUPermissions = await checkLocationWhenInUsePermission();
|
||||
if (!hasWIUPermissions) {
|
||||
await requestLocationWhenInUsePermissions();
|
||||
}
|
||||
|
||||
final hasAlwaysPermissions = await checkLocationAlwaysPermission();
|
||||
if (!hasAlwaysPermissions) {
|
||||
await requestLocationAlwaysPermissions();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user