182 lines
6.6 KiB
Dart
182 lines
6.6 KiB
Dart
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/services/location_service.dart';
|
||
import 'dart:developer' as developer;
|
||
|
||
|
||
class PermissionController {
|
||
|
||
/*
|
||
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);
|
||
}
|
||
*/
|
||
|
||
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(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();
|
||
}
|
||
}
|
||
|
||
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: Text('キャンセル'),
|
||
onPressed: () => Get.back(),
|
||
),
|
||
TextButton(
|
||
child: Text('設定'),
|
||
onPressed: () {
|
||
Get.back();
|
||
openAppSettings();
|
||
},
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
} |