You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
6.5 KiB
222 lines
6.5 KiB
import 'package:get/get.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'dart:io' show Platform;
|
|
import '../../../core/utils/permission_util.dart';
|
|
|
|
class PermissionsController extends GetxController {
|
|
// 权限状态映射
|
|
final RxMap<Permission, PermissionStatus> permissionStatuses =
|
|
<Permission, PermissionStatus>{}.obs;
|
|
|
|
// 是否正在请求权限
|
|
final RxBool isRequestingPermission = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
checkAllPermissions();
|
|
}
|
|
|
|
/// 检查所有权限状态
|
|
Future<Map<Permission, PermissionStatus>> checkAllPermissions() async {
|
|
final Map<Permission, PermissionStatus> statuses = {};
|
|
|
|
// 根据平台获取需要检查的权限列表
|
|
final permissions = _getPlatformPermissions();
|
|
|
|
for (final permission in permissions) {
|
|
try {
|
|
statuses[permission] = await permission.status;
|
|
} catch (e) {
|
|
// 某些权限在特定平台可能不支持,设置为拒绝状态
|
|
statuses[permission] = PermissionStatus.denied;
|
|
}
|
|
}
|
|
|
|
permissionStatuses.value = statuses;
|
|
return statuses;
|
|
}
|
|
|
|
/// 获取平台特定的权限列表
|
|
List<Permission> _getPlatformPermissions() {
|
|
if (Platform.isIOS) {
|
|
return [
|
|
Permission.calendarFullAccess,
|
|
Permission.location,
|
|
Permission.contacts,
|
|
Permission.bluetooth,
|
|
Permission.notification,
|
|
Permission.storage,
|
|
Permission.microphone,
|
|
];
|
|
} else if (Platform.isAndroid) {
|
|
return [
|
|
Permission.calendarFullAccess,
|
|
Permission.location,
|
|
Permission.phone,
|
|
Permission.sms,
|
|
Permission.contacts,
|
|
Permission.bluetooth,
|
|
Permission.notification,
|
|
Permission.scheduleExactAlarm,
|
|
Permission.storage,
|
|
Permission.microphone,
|
|
];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/// 请求单个权限
|
|
Future<bool> requestPermission(Permission permission) async {
|
|
if (isRequestingPermission.value) return false;
|
|
|
|
isRequestingPermission.value = true;
|
|
|
|
try {
|
|
final permissionName = getPermissionName(permission);
|
|
final explanationText = _getDetailedDescription(permission);
|
|
|
|
final hasPermission = await PermissionUtil.instance.requestPermission(
|
|
permissionType: permission,
|
|
permissionName: permissionName,
|
|
explanationText: explanationText,
|
|
permanentDenialText: 'permanentDenialText'.tr.replaceAll(
|
|
'{permission}', permissionName),
|
|
settingsButtonText: 'goToSettings'.tr,
|
|
cancelButtonText: 'cancel'.tr,
|
|
);
|
|
|
|
if (hasPermission) {
|
|
_showSuccessMessage();
|
|
}
|
|
|
|
// 刷新权限状态
|
|
await checkAllPermissions();
|
|
|
|
return hasPermission;
|
|
} catch (e) {
|
|
_showErrorMessage();
|
|
return false;
|
|
} finally {
|
|
isRequestingPermission.value = false;
|
|
}
|
|
}
|
|
|
|
/// 打开系统设置
|
|
void openSettings() {
|
|
openAppSettings();
|
|
}
|
|
|
|
/// 获取权限的用户友好名称
|
|
String getPermissionName(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phone'.tr;
|
|
case Permission.sms:
|
|
return 'sms'.tr;
|
|
case Permission.contacts:
|
|
return 'contacts'.tr;
|
|
case Permission.bluetooth:
|
|
return 'bluetooth'.tr;
|
|
case Permission.notification:
|
|
return 'notification'.tr;
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWrite'.tr;
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccess'.tr;
|
|
case Permission.location:
|
|
return 'locationInfo'.tr;
|
|
case Permission.scheduleExactAlarm:
|
|
return 'alarm'.tr;
|
|
case Permission.storage:
|
|
return 'storagePermission'.tr;
|
|
case Permission.microphone:
|
|
return 'microphonePermission'.tr;
|
|
default:
|
|
return permission.toString();
|
|
}
|
|
}
|
|
|
|
/// 获取权限说明文本
|
|
String _getDetailedDescription(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phonePermissionDetailDesc'.tr;
|
|
case Permission.sms:
|
|
return 'smsPermissionDetailDesc'.tr;
|
|
case Permission.contacts:
|
|
return 'contactsPermissionDetailDesc'.tr;
|
|
case Permission.bluetooth:
|
|
return 'bluetoothPermissionDetailDesc'.tr;
|
|
case Permission.notification:
|
|
return 'notificationPermissionDetailDesc'.tr;
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWritePermissionDetailDesc'.tr;
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccessPermissionDetailDesc'.tr;
|
|
case Permission.location:
|
|
return 'locationPermissionDetailDesc'.tr;
|
|
case Permission.scheduleExactAlarm:
|
|
return 'alarmPermissionDetailDesc'.tr;
|
|
case Permission.storage:
|
|
return 'storagePermissionExplanation'.tr;
|
|
case Permission.microphone:
|
|
return 'microphonePermissionExplanation'.tr;
|
|
default:
|
|
return 'appNeedsPermissionForFullFunction'.tr;
|
|
}
|
|
}
|
|
|
|
/// 获取权限描述
|
|
String getPermissionDescription(Permission permission) {
|
|
switch (permission) {
|
|
case Permission.phone:
|
|
return 'phonePermissionDesc'.tr;
|
|
case Permission.sms:
|
|
return 'smsPermissionDesc'.tr;
|
|
case Permission.contacts:
|
|
return 'contactsPermissionDesc'.tr;
|
|
case Permission.bluetooth:
|
|
return 'bluetoothPermissionDesc'.tr;
|
|
case Permission.notification:
|
|
return 'notificationPermissionDesc'.tr;
|
|
case Permission.calendarWriteOnly:
|
|
return 'calendarWritePermissionDesc'.tr;
|
|
case Permission.calendarFullAccess:
|
|
return 'calendarFullAccessPermissionDesc'.tr;
|
|
case Permission.location:
|
|
return 'locationPermissionDesc'.tr;
|
|
case Permission.scheduleExactAlarm:
|
|
return 'alarmPermissionDesc'.tr;
|
|
case Permission.storage:
|
|
return 'storagePermissionExplanation'.tr;
|
|
case Permission.microphone:
|
|
return 'microphonePermissionExplanation'.tr;
|
|
default:
|
|
return 'appFunctionNeeds'.tr;
|
|
}
|
|
}
|
|
|
|
/// 显示成功消息
|
|
void _showSuccessMessage() {
|
|
Get.snackbar(
|
|
'success'.tr,
|
|
'permissionGranted'.tr,
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
backgroundColor: Colors.green,
|
|
colorText: Colors.white,
|
|
);
|
|
}
|
|
|
|
/// 显示错误消息
|
|
void _showErrorMessage() {
|
|
Get.snackbar(
|
|
'error'.tr,
|
|
'permissionRequestError'.tr,
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
backgroundColor: Colors.red,
|
|
colorText: Colors.white,
|
|
);
|
|
}
|
|
}
|