permission.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'package:permission_handler/permission_handler.dart';
  2. import 'interfaces/permission.dart';
  3. class PermissionManager implements IPermissionManager {
  4. PermissionManager();
  5. @override
  6. Future<bool> requestLocationPermission() async {
  7. PermissionStatus status = await Permission.location.request();
  8. if (status.isGranted) {
  9. return true;
  10. } else {
  11. return false;
  12. }
  13. }
  14. @override
  15. Future<bool> requestBluetoothConnectPermission() async {
  16. PermissionStatus status = await Permission.bluetoothConnect.request();
  17. if (status.isGranted) {
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. }
  23. @override
  24. Future<bool> requestBluetoothScanPermission() async {
  25. PermissionStatus status = await Permission.bluetoothScan.request();
  26. if (status.isGranted) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. @override
  33. Future<bool> requestBluetoothAdvertisePermission() async {
  34. PermissionStatus status = await Permission.bluetoothAdvertise.request();
  35. if (status.isGranted) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. @override
  42. Future<bool> requestInstallPackagesPermission() async {
  43. PermissionStatus status = await Permission.requestInstallPackages.request();
  44. if (status.isGranted) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. @override
  51. Future<bool> requestCameraPermissions() async {
  52. var status = await Permission.camera.status;
  53. if (status.isGranted) {
  54. return true;
  55. } else {
  56. PermissionStatus status = await Permission.camera.request();
  57. if (status.isGranted) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. Map<String, Future<bool> Function()> get perMissionMap {
  64. return {
  65. 'Location': requestLocationPermission,
  66. 'BluetoothConnect': requestBluetoothConnectPermission,
  67. 'BluetoothScan': requestBluetoothScanPermission,
  68. 'BluetoothAdvertise': requestBluetoothAdvertisePermission,
  69. 'Camera': requestCameraPermissions,
  70. 'InstallPackages': requestInstallPackagesPermission,
  71. };
  72. }
  73. ///批量获取权限
  74. @override
  75. Future<bool> requestPermissions(List<String> permissions) async {
  76. List<String> unauthorizedPermissions = [];
  77. for (var element in permissions) {
  78. var functionToCall = perMissionMap[element];
  79. if (functionToCall != null) {
  80. var result = await functionToCall();
  81. if (!result) {
  82. unauthorizedPermissions.add(element);
  83. }
  84. }
  85. }
  86. if (unauthorizedPermissions.isNotEmpty) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. @override
  92. Future<bool> openAppSettingsAsync() async {
  93. return await openAppSettings();
  94. }
  95. }
  96. enum PermissionEnum {
  97. //音视频相关
  98. VideoAndVoiceRelevant,
  99. //IO相关
  100. IORelevant,
  101. //App 需要的所有权限
  102. All,
  103. }