2024-04-10 15:21:08 +08:00
|
|
|
|
import 'package:flutter/cupertino.dart';
|
2024-04-10 09:47:05 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
import 'package:star_lock/app_settings/app_settings.dart';
|
2024-04-10 15:21:08 +08:00
|
|
|
|
import 'package:star_lock/tools/storage.dart';
|
|
|
|
|
|
import 'package:star_lock/translations/trans_lib.dart';
|
2024-04-10 09:47:05 +08:00
|
|
|
|
|
|
|
|
|
|
class PermissionDialog {
|
|
|
|
|
|
static Map<Permission, String> titles = {
|
2024-04-10 17:18:53 +08:00
|
|
|
|
Permission.camera: '相机'.tr,
|
|
|
|
|
|
Permission.photos: '相册'.tr,
|
|
|
|
|
|
Permission.storage: '读写'.tr,
|
|
|
|
|
|
Permission.location: '定位'.tr,
|
2024-04-18 17:41:14 +08:00
|
|
|
|
Permission.bluetooth: '蓝牙'.tr,
|
|
|
|
|
|
Permission.bluetoothScan: '蓝牙'.tr,
|
|
|
|
|
|
Permission.bluetoothConnect: '蓝牙'.tr,
|
2024-04-10 09:47:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Map<Permission, String> contents = {
|
2024-04-10 17:18:53 +08:00
|
|
|
|
Permission.camera: '需要访问相机权限才能拍照上传文件例如头像上传'.tr,
|
|
|
|
|
|
Permission.photos: '需要访问相机权限才能使用相册图片上传文件上传头像'.tr,
|
|
|
|
|
|
Permission.storage: '需要访问读写权限才能使用本地图片上传头像'.tr,
|
|
|
|
|
|
Permission.location: '需要访问定位权限才能使用添加钥匙功能的位置信息'.tr,
|
2024-04-18 17:41:14 +08:00
|
|
|
|
Permission.bluetooth: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
|
|
|
|
|
Permission.bluetoothScan: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
|
|
|
|
|
Permission.bluetoothConnect: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
2024-04-10 09:47:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-04-18 17:41:14 +08:00
|
|
|
|
static Future<bool> requestBluetooth() async {
|
|
|
|
|
|
if (Get.context == null) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool application = true;
|
|
|
|
|
|
Map<Permission, PermissionStatus> statuses = await [
|
|
|
|
|
|
Permission.bluetoothScan,
|
|
|
|
|
|
Permission.bluetoothConnect,
|
|
|
|
|
|
].request();
|
|
|
|
|
|
Permission permission = Permission.bluetoothScan;
|
|
|
|
|
|
dynamic cache = await Storage.getString(titles[permission]);
|
|
|
|
|
|
bool isGranted = statuses.values.every((element) => element.isGranted);
|
|
|
|
|
|
if (AppPlatform.isAndroid && !isGranted && cache is! String) {
|
|
|
|
|
|
application = await showCupertinoDialog(
|
|
|
|
|
|
context: Get.context!,
|
|
|
|
|
|
builder: (context) {
|
|
|
|
|
|
return PopScope(
|
|
|
|
|
|
canPop: false,
|
|
|
|
|
|
child: CupertinoAlertDialog(
|
|
|
|
|
|
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
|
|
|
|
|
content: Text(contents[permission] ?? ''),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
CupertinoDialogAction(
|
|
|
|
|
|
child: Text('不允许'.tr),
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(result: false);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
CupertinoDialogAction(
|
|
|
|
|
|
child: Text('允许'.tr),
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(result: true);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
));
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (application) {
|
|
|
|
|
|
await Storage.setString(titles[permission], titles[permission]);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (cache is String) {
|
|
|
|
|
|
bool isDenied = statuses.values.every((element) => element.isDenied);
|
|
|
|
|
|
if (isDenied) {
|
|
|
|
|
|
showSet(permission);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (application) {
|
|
|
|
|
|
PermissionStatus status = await permission.request();
|
|
|
|
|
|
return status.isGranted;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-10 09:47:05 +08:00
|
|
|
|
//显示权限判断申请框
|
2024-04-24 16:04:07 +08:00
|
|
|
|
static Future<bool> request(Permission permission, [String? content]) async {
|
2024-04-10 09:47:05 +08:00
|
|
|
|
if (Get.context == null) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool application = true;
|
2024-04-10 15:21:08 +08:00
|
|
|
|
PermissionStatus status = await permission.status;
|
|
|
|
|
|
dynamic cache = await Storage.getString(titles[permission]);
|
|
|
|
|
|
if (AppPlatform.isAndroid && !status.isGranted && cache is! String) {
|
|
|
|
|
|
application = await showCupertinoDialog(
|
2024-04-10 09:47:05 +08:00
|
|
|
|
context: Get.context!,
|
2024-04-10 15:21:08 +08:00
|
|
|
|
builder: (context) {
|
|
|
|
|
|
return PopScope(
|
|
|
|
|
|
canPop: false,
|
|
|
|
|
|
child: CupertinoAlertDialog(
|
2024-04-10 17:18:53 +08:00
|
|
|
|
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
2024-04-24 16:04:07 +08:00
|
|
|
|
content: Text(content ?? contents[permission] ?? ''),
|
2024-04-10 15:21:08 +08:00
|
|
|
|
actions: [
|
|
|
|
|
|
CupertinoDialogAction(
|
2024-04-10 17:18:53 +08:00
|
|
|
|
child: Text('不允许'.tr),
|
2024-04-10 15:21:08 +08:00
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(result: false);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
CupertinoDialogAction(
|
2024-04-10 17:18:53 +08:00
|
|
|
|
child: Text('允许'.tr),
|
2024-04-10 15:21:08 +08:00
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(result: true);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
));
|
2024-04-10 09:47:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
2024-04-10 17:18:53 +08:00
|
|
|
|
if (application) {
|
2024-04-10 15:21:08 +08:00
|
|
|
|
await Storage.setString(titles[permission], titles[permission]);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (cache is String) {
|
|
|
|
|
|
if (status.isDenied) {
|
|
|
|
|
|
showSet(permission);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-04-10 09:47:05 +08:00
|
|
|
|
}
|
2024-04-10 15:21:08 +08:00
|
|
|
|
if (application) {
|
|
|
|
|
|
PermissionStatus status = await permission.request();
|
|
|
|
|
|
return status.isGranted;
|
2024-04-10 09:47:05 +08:00
|
|
|
|
}
|
2024-04-10 15:21:08 +08:00
|
|
|
|
return false;
|
2024-04-10 09:47:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//显示设置页面
|
|
|
|
|
|
static Future<void> showSet(Permission permission) async {
|
|
|
|
|
|
if (Get.context == null) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-04-10 15:21:08 +08:00
|
|
|
|
showCupertinoDialog(
|
2024-04-10 09:47:05 +08:00
|
|
|
|
context: Get.context!,
|
2024-04-10 15:21:08 +08:00
|
|
|
|
builder: (context) {
|
|
|
|
|
|
return PopScope(
|
|
|
|
|
|
canPop: false,
|
|
|
|
|
|
child: CupertinoAlertDialog(
|
2024-04-10 17:18:53 +08:00
|
|
|
|
title: Text('${titles[permission] ?? ''}${'权限被拒绝'.tr}'),
|
2024-04-10 15:21:08 +08:00
|
|
|
|
content: Text(
|
2024-04-10 17:18:53 +08:00
|
|
|
|
'${contents[permission] ?? ''},${'请手动在系统设置中开启'.tr}${titles[permission]}${'权限以继续使用应用'.tr}'),
|
2024-04-10 15:21:08 +08:00
|
|
|
|
actions: [
|
|
|
|
|
|
CupertinoDialogAction(
|
2024-04-10 17:18:53 +08:00
|
|
|
|
child: Text('去设置'.tr),
|
2024-04-10 15:21:08 +08:00
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Get.back(); // 关闭对话框
|
|
|
|
|
|
openAppSettings(); // 打开系统设置页面
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
));
|
2024-04-10 09:47:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|