2022-07-20 15:57:40 +05:30
|
|
|
|
import 'package:flutter/material.dart';
|
2024-05-24 07:21:28 +09:00
|
|
|
|
import 'package:flutter/services.dart';
|
2022-07-20 15:57:40 +05:30
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2024-05-24 07:21:28 +09:00
|
|
|
|
import 'package:rogapp/services/location_service.dart';
|
|
|
|
|
|
import 'dart:developer' as developer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PermissionController {
|
|
|
|
|
|
|
2024-05-25 11:05:02 +09:00
|
|
|
|
/*
|
2024-05-24 07:21:28 +09:00
|
|
|
|
static Future<bool> checkLocationPermissions() async {
|
|
|
|
|
|
debugPrint("(gifunavi)== checkLocationPermissions ==");
|
|
|
|
|
|
final alwaysPermission = await Permission.locationAlways.status;
|
|
|
|
|
|
final whenInUsePermission = await Permission.locationWhenInUse.status;
|
|
|
|
|
|
final locationPermission = await Permission.location.status;
|
|
|
|
|
|
|
|
|
|
|
|
return (alwaysPermission == PermissionStatus.granted || whenInUsePermission == PermissionStatus.granted) &&
|
|
|
|
|
|
(locationPermission == PermissionStatus.granted);
|
2024-05-25 11:05:02 +09:00
|
|
|
|
}
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static Future<bool> checkStoragePermission() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== checkStoragePermission ==");
|
2024-05-25 11:05:02 +09:00
|
|
|
|
final storagePermission = await Permission.storage.status;
|
|
|
|
|
|
return storagePermission == PermissionStatus.granted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Future<void> requestStoragePermission() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== requestStoragePermission ==");
|
2024-05-25 11:05:02 +09:00
|
|
|
|
final storagePermission = await Permission.storage.request();
|
|
|
|
|
|
|
|
|
|
|
|
if (storagePermission == PermissionStatus.granted) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (storagePermission == PermissionStatus.permanentlyDenied) {
|
|
|
|
|
|
// リクエストが完了するまで待機
|
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 500));
|
|
|
|
|
|
showPermissionDeniedDialog('storage_permission_needed_title','storage_permission_needed_main');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Future<bool> checkLocationBasicPermission() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== checkLocationBasicPermission ==");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
final locationPermission = await Permission.location.status;
|
|
|
|
|
|
return locationPermission == PermissionStatus.granted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Future<bool> checkLocationWhenInUsePermission() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== checkLocationWhenInUsePermission ==");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
final whenInUsePermission = await Permission.locationWhenInUse.status;
|
|
|
|
|
|
return whenInUsePermission == PermissionStatus.granted;
|
2022-07-20 15:57:40 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
static Future<bool> checkLocationAlwaysPermission() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== checkLocationAlwaysPermission ==");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
final alwaysPermission = await Permission.locationAlways.status;
|
|
|
|
|
|
return alwaysPermission == PermissionStatus.granted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool isBasicPermission=false;
|
|
|
|
|
|
static Future<void> requestLocationBasicPermissions() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== requestLocationBasicPermissions ==");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
try{
|
|
|
|
|
|
if(!isBasicPermission){
|
|
|
|
|
|
isBasicPermission=true;
|
|
|
|
|
|
final locationStatus = await Permission.location.request();
|
2022-07-20 15:57:40 +05:30
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
if (locationStatus != PermissionStatus.granted) {
|
2024-05-25 11:05:02 +09:00
|
|
|
|
showPermissionDeniedDialog('location_permission_needed_title','location_permission_needed_main');
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}
|
2023-10-06 16:25:21 +05:30
|
|
|
|
}
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}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 {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== requestLocationWhenInUsePermissions ==");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
if(!isRequestedWhenInUsePermission){
|
|
|
|
|
|
isRequestedWhenInUsePermission=true;
|
|
|
|
|
|
final whenInUseStatus = await Permission.locationWhenInUse.request();
|
|
|
|
|
|
|
|
|
|
|
|
if (whenInUseStatus != PermissionStatus.granted) {
|
2024-05-25 11:05:02 +09:00
|
|
|
|
showPermissionDeniedDialog('location_permission_needed_title','location_permission_needed_main');
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}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}'.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-20 15:57:40 +05:30
|
|
|
|
}
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}catch (e, stackTrace){
|
|
|
|
|
|
debugPrint('Exception: $e');
|
|
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
|
debugPrintStack(label: 'Exception occurred', stackTrace: stackTrace);
|
2022-07-20 15:57:40 +05:30
|
|
|
|
}
|
2024-05-24 07:21:28 +09:00
|
|
|
|
|
2022-07-20 15:57:40 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
static bool isRequestedAlwaysPermission = false;
|
|
|
|
|
|
|
|
|
|
|
|
static Future<void> requestLocationAlwaysPermissions() async {
|
2024-05-26 11:00:06 +09:00
|
|
|
|
//debugPrint("(gifunavi)== requestLocationAlwaysPermissions ==");
|
2024-05-24 07:21:28 +09:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
if( !isRequestedAlwaysPermission ){
|
|
|
|
|
|
isRequestedAlwaysPermission=true;
|
|
|
|
|
|
final alwaysStatus = await Permission.locationAlways.request();
|
|
|
|
|
|
|
|
|
|
|
|
if (alwaysStatus != PermissionStatus.granted) {
|
2024-05-25 11:05:02 +09:00
|
|
|
|
showPermissionDeniedDialog('location_permission_needed_title','location_permission_needed_main');
|
2024-05-24 07:21:28 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (e, stackTrace){
|
|
|
|
|
|
print('Exception: $e');
|
|
|
|
|
|
print('Stack trace: $stackTrace');
|
|
|
|
|
|
debugPrintStack(label: 'Exception occurred', stackTrace: stackTrace);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
|
}
|
2022-07-20 15:57:40 +05:30
|
|
|
|
|
2024-05-24 07:21:28 +09:00
|
|
|
|
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();
|
2023-07-07 16:16:56 +05:30
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-20 13:42:34 +05:30
|
|
|
|
|
2024-05-25 11:05:02 +09:00
|
|
|
|
static void showPermissionDeniedDialog(String title,String message) {
|
2024-05-24 07:21:28 +09:00
|
|
|
|
Get.dialog(
|
|
|
|
|
|
AlertDialog(
|
2024-05-25 11:05:02 +09:00
|
|
|
|
//title: Text('location_permission_needed_title'.tr),
|
|
|
|
|
|
title: Text(title.tr),
|
2024-05-24 07:21:28 +09:00
|
|
|
|
// 位置情報への許可が必要です
|
2024-05-25 11:05:02 +09:00
|
|
|
|
//content: Text('location_permission_needed_main'.tr),
|
|
|
|
|
|
content: Text(message.tr),
|
2024-05-24 07:21:28 +09:00
|
|
|
|
// 岐阜ロゲでは、位置情報を使用してスタート・チェックイン・ゴール等の通過照明及び移動手段の記録のために、位置情報のトラッキングを行なっています。このためバックグラウンドでもトラッキングができるように位置情報の権限が必要です。
|
|
|
|
|
|
// 設定画面で、「岐阜ナビ」に対して、常に位置情報を許可するように設定してください。
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
child: Text('キャンセル'),
|
|
|
|
|
|
onPressed: () => Get.back(),
|
|
|
|
|
|
),
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
child: Text('設定'),
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back();
|
|
|
|
|
|
openAppSettings();
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2023-07-20 13:42:34 +05:30
|
|
|
|
}
|
2024-05-24 07:21:28 +09:00
|
|
|
|
|
2024-05-15 17:59:25 +09:00
|
|
|
|
}
|