import 'package:permission_handler/permission_handler.dart'; import 'interfaces/permission.dart'; class PermissionManager implements IPermissionManager { PermissionManager(); @override Future requestLocationPermission() async { PermissionStatus status = await Permission.location.request(); if (status.isGranted) { return true; } else { return false; } } @override Future requestBluetoothConnectPermission() async { PermissionStatus status = await Permission.bluetoothConnect.request(); if (status.isGranted) { return true; } else { return false; } } @override Future requestBluetoothScanPermission() async { PermissionStatus status = await Permission.bluetoothScan.request(); if (status.isGranted) { return true; } else { return false; } } @override Future requestBluetoothAdvertisePermission() async { PermissionStatus status = await Permission.bluetoothAdvertise.request(); if (status.isGranted) { return true; } else { return false; } } @override Future requestInstallPackagesPermission() async { PermissionStatus status = await Permission.requestInstallPackages.request(); if (status.isGranted) { return true; } else { return false; } } @override Future requestCameraPermissions() async { var status = await Permission.camera.status; if (status.isGranted) { return true; } else { PermissionStatus status = await Permission.camera.request(); if (status.isGranted) { return true; } } return false; } Map Function()> get perMissionMap { return { 'Location': requestLocationPermission, 'BluetoothConnect': requestBluetoothConnectPermission, 'BluetoothScan': requestBluetoothScanPermission, 'BluetoothAdvertise': requestBluetoothAdvertisePermission, 'Camera': requestCameraPermissions, 'InstallPackages': requestInstallPackagesPermission, }; } ///批量获取权限 @override Future requestPermissions(List permissions) async { List unauthorizedPermissions = []; for (var element in permissions) { var functionToCall = perMissionMap[element]; if (functionToCall != null) { var result = await functionToCall(); if (!result) { unauthorizedPermissions.add(element); } } } if (unauthorizedPermissions.isNotEmpty) { return false; } return true; } @override Future openAppSettingsAsync() async { return await openAppSettings(); } } enum PermissionEnum { //音视频相关 VideoAndVoiceRelevant, //IO相关 IORelevant, //App 需要的所有权限 All, }