397 lines
14 KiB
Dart
Executable File
397 lines
14 KiB
Dart
Executable File
import 'package:device_info_plus/device_info_plus.dart';
|
||
import 'package:flutter/cupertino.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
import 'package:star_lock/tools/storage.dart';
|
||
|
||
import '../../tools/bugly/bugly_tool.dart';
|
||
|
||
class PermissionDialog {
|
||
static Map<Permission, String> titles = <Permission, String>{
|
||
Permission.camera: '相机'.tr,
|
||
Permission.photos: '相册'.tr,
|
||
Permission.storage: '读写'.tr,
|
||
Permission.location: '定位'.tr,
|
||
Permission.bluetooth: '蓝牙'.tr,
|
||
Permission.bluetoothScan: '蓝牙'.tr,
|
||
Permission.bluetoothConnect: '蓝牙'.tr,
|
||
};
|
||
|
||
static Map<Permission, String> contents = <Permission, String>{
|
||
Permission.camera: '需要访问相机权限才能拍照上传文件例如头像上传'.tr,
|
||
Permission.photos: '需要访问相机权限才能使用相册图片上传文件上传头像'.tr,
|
||
Permission.storage: '需要访问读写权限才能使用本地图片上传头像'.tr,
|
||
Permission.location: '需要访问定位权限才能使用添加钥匙功能的位置信息'.tr,
|
||
Permission.bluetooth: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
||
Permission.bluetoothScan: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
||
Permission.bluetoothConnect: '需要访问蓝牙权限才能使用添加钥匙功能的位置信息'.tr,
|
||
};
|
||
|
||
static Future<bool> requestBluetooth() async {
|
||
if (Get.context == null) {
|
||
return false;
|
||
}
|
||
bool application = true;
|
||
final List<Permission> permissions = <Permission>[
|
||
Permission.bluetooth,
|
||
Permission.bluetoothScan,
|
||
Permission.bluetoothConnect,
|
||
];
|
||
Future<bool> getPermissionStatus(List<Permission> permissions) async {
|
||
bool isGranted = true;
|
||
final List<PermissionStatus> permissionStatus = <PermissionStatus>[];
|
||
for (final Permission per in permissions) {
|
||
permissionStatus.add(await per.status);
|
||
}
|
||
for (final PermissionStatus per in permissionStatus) {
|
||
isGranted = isGranted && per.isGranted;
|
||
}
|
||
if (GetPlatform.isIOS) {
|
||
isGranted = permissionStatus[0].isGranted;
|
||
}
|
||
return isGranted;
|
||
}
|
||
|
||
const Permission permission = Permission.bluetoothScan;
|
||
final dynamic cache = await Storage.getString(titles[permission]!);
|
||
final bool isGranted = await getPermissionStatus(permissions);
|
||
if (GetPlatform.isAndroid && !isGranted && cache is! String) {
|
||
application = await showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (BuildContext context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
||
content: Text(contents[permission] ?? ''),
|
||
actions: <Widget>[
|
||
CupertinoDialogAction(
|
||
child: Text('不允许'.tr),
|
||
onPressed: () {
|
||
BuglyTool.uploadExceptionWithEvent(
|
||
eventStr:
|
||
'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}',
|
||
detail:
|
||
"不允许'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr} titles:$titles'");
|
||
Get.back(result: false);
|
||
},
|
||
),
|
||
CupertinoDialogAction(
|
||
child: Text('允许'.tr),
|
||
onPressed: () {
|
||
BuglyTool.uploadExceptionWithEvent(
|
||
eventStr:
|
||
'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}',
|
||
detail:
|
||
"不允许'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr} titles:$titles'");
|
||
Get.back(result: true);
|
||
},
|
||
),
|
||
],
|
||
));
|
||
},
|
||
);
|
||
|
||
if (application) {
|
||
await Storage.setString(titles[permission]!, titles[permission]);
|
||
}
|
||
} else if (cache is! String) {
|
||
await Storage.setString(titles[permission]!, titles[permission]);
|
||
} else {
|
||
final bool isGranted = await getPermissionStatus(permissions);
|
||
if (!isGranted) {
|
||
showSet(permission);
|
||
return false;
|
||
}
|
||
}
|
||
if (application) {
|
||
await permissions.request();
|
||
final bool isGranted = await getPermissionStatus(permissions);
|
||
return isGranted;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//相册
|
||
static Future<bool> requestPhotos() async {
|
||
if (Get.context == null) {
|
||
return false;
|
||
}
|
||
bool isAndroid33 = false;
|
||
try {
|
||
final AndroidDeviceInfo androidInfo =
|
||
await DeviceInfoPlugin().androidInfo;
|
||
isAndroid33 = AppPlatform.isAndroid && androidInfo.version.sdkInt >= 33;
|
||
} catch (e) {
|
||
// 如果获取设备信息失败,默认使用旧版本权限申请方式
|
||
isAndroid33 = false;
|
||
}
|
||
//通用的局部函数
|
||
List<Permission> requestPermission() {
|
||
List<Permission> permissions;
|
||
if (isAndroid33) {
|
||
permissions = <Permission>[
|
||
Permission.photos,
|
||
];
|
||
} else {
|
||
permissions = <Permission>[
|
||
Permission.storage,
|
||
];
|
||
}
|
||
return permissions;
|
||
}
|
||
|
||
Future<bool> permissionStatus(List<Permission> permissions) async {
|
||
bool isGranted = true;
|
||
for (final Permission permission in permissions) {
|
||
isGranted =
|
||
isGranted && (await permission.status) == PermissionStatus.granted;
|
||
}
|
||
// if (isAndroid33) {
|
||
// // android 33以上需要申请媒体库权限
|
||
// isGranted =
|
||
// (await Permission.mediaLibrary.status) == PermissionStatus.granted;
|
||
// }
|
||
return isGranted;
|
||
}
|
||
|
||
bool application = true;
|
||
const Permission permission = Permission.photos;
|
||
final List<Permission> permissions = requestPermission();
|
||
final dynamic cache = await Storage.getString(titles[permission] ?? '');
|
||
final bool isGranted = await permissionStatus(permissions);
|
||
if (AppPlatform.isAndroid && !isGranted && cache is! String) {
|
||
application = await showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (BuildContext context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
||
content: Text(contents[permission] ?? ''),
|
||
actions: <Widget>[
|
||
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) {
|
||
final bool isDenied = !(await permissionStatus(permissions));
|
||
if (isDenied) {
|
||
showSet(permission);
|
||
return false;
|
||
}
|
||
}
|
||
if (application) {
|
||
final Map<Permission, PermissionStatus> statuses =
|
||
await permissions.request();
|
||
final bool isGranted = await permissionStatus(statuses.keys.toList());
|
||
return isGranted;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//读写
|
||
static Future<bool> requestStorage() async {
|
||
if (Get.context == null) {
|
||
return false;
|
||
}
|
||
bool isAndroid33 = false;
|
||
try {
|
||
final AndroidDeviceInfo androidInfo =
|
||
await DeviceInfoPlugin().androidInfo;
|
||
isAndroid33 = AppPlatform.isAndroid && androidInfo.version.sdkInt >= 33;
|
||
} catch (e) {
|
||
// 如果获取设备信息失败,默认使用旧版本权限申请方式
|
||
isAndroid33 = false;
|
||
}
|
||
//通用的局部函数
|
||
List<Permission> requestPermission() {
|
||
List<Permission> permissions;
|
||
if (isAndroid33) {
|
||
permissions = <Permission>[
|
||
Permission.mediaLibrary,
|
||
Permission.photos,
|
||
// Permission.audio,
|
||
Permission.videos,
|
||
Permission.manageExternalStorage,
|
||
];
|
||
} else {
|
||
permissions = <Permission>[
|
||
Permission.storage,
|
||
];
|
||
}
|
||
return permissions;
|
||
}
|
||
|
||
Future<bool> permissionStatus(List<Permission> permissions) async {
|
||
bool isGranted = true;
|
||
for (final Permission permission in permissions) {
|
||
isGranted =
|
||
isGranted && (await permission.status) == PermissionStatus.granted;
|
||
}
|
||
if (isAndroid33) {
|
||
// android 33以上需要申请媒体库权限
|
||
isGranted = (await Permission.mediaLibrary.status) ==
|
||
PermissionStatus.granted ||
|
||
(await Permission.manageExternalStorage.status) ==
|
||
PermissionStatus.granted;
|
||
}
|
||
return isGranted;
|
||
}
|
||
|
||
bool application = true;
|
||
const Permission permission = Permission.storage;
|
||
final List<Permission> permissions = requestPermission();
|
||
final dynamic cache = await Storage.getString(titles[permission] ?? '');
|
||
final bool isGranted = await permissionStatus(permissions);
|
||
if (AppPlatform.isAndroid && !isGranted && cache is! String) {
|
||
application = await showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (BuildContext context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
||
content: Text('需要访问读写权限才能使用手动升级固件'.tr),
|
||
actions: <Widget>[
|
||
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) {
|
||
final bool isDenied = !(await permissionStatus(permissions));
|
||
if (isDenied) {
|
||
showSet(permission);
|
||
return false;
|
||
}
|
||
}
|
||
if (application) {
|
||
final Map<Permission, PermissionStatus> statuses =
|
||
await permissions.request();
|
||
final bool isGranted = await permissionStatus(statuses.keys.toList());
|
||
return isGranted;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//显示权限判断申请框
|
||
static Future<bool> request(Permission permission, [String? content]) async {
|
||
AppLog.log('context:$content');
|
||
if (Get.context == null) {
|
||
return false;
|
||
}
|
||
bool application = true;
|
||
final PermissionStatus status = await permission.status;
|
||
final dynamic cache = await Storage.getString(titles[permission] ?? '');
|
||
if (AppPlatform.isAndroid && !status.isGranted && cache is! String) {
|
||
application = await showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (BuildContext context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}'),
|
||
content: Text(content ?? contents[permission] ?? ''),
|
||
actions: <Widget>[
|
||
CupertinoDialogAction(
|
||
child: Text('不允许'.tr),
|
||
onPressed: () {
|
||
BuglyTool.uploadExceptionWithEvent(
|
||
eventStr:
|
||
'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}',
|
||
detail:
|
||
"不允许'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr} titles:$titles'");
|
||
Get.back(result: false);
|
||
},
|
||
),
|
||
CupertinoDialogAction(
|
||
child: Text('允许'.tr),
|
||
onPressed: () {
|
||
BuglyTool.uploadExceptionWithEvent(
|
||
eventStr:
|
||
'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr}',
|
||
detail:
|
||
"允许'${'申请'.tr}${titles[permission] ?? ''}${'权限'.tr} titles:$titles'");
|
||
Get.back(result: true);
|
||
},
|
||
),
|
||
],
|
||
));
|
||
},
|
||
);
|
||
if (application) {
|
||
await Storage.setString(titles[permission] ?? '', titles[permission]);
|
||
}
|
||
} else if (cache is String) {
|
||
if (status.isDenied) {
|
||
showSet(permission);
|
||
return false;
|
||
}
|
||
}
|
||
if (application) {
|
||
final PermissionStatus status = await permission.request();
|
||
return status.isGranted;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//显示设置页面
|
||
static Future<void> showSet(Permission permission) async {
|
||
if (Get.context == null) {
|
||
return;
|
||
}
|
||
showCupertinoDialog(
|
||
context: Get.context!,
|
||
builder: (BuildContext context) {
|
||
return PopScope(
|
||
canPop: false,
|
||
child: CupertinoAlertDialog(
|
||
title: Text('${titles[permission] ?? ''}${'权限被拒绝'.tr}'),
|
||
content: Text(
|
||
'${contents[permission] ?? ''},${'请手动在系统设置中开启'.tr}${titles[permission]}${'权限以继续使用应用'.tr}'),
|
||
actions: <Widget>[
|
||
CupertinoDialogAction(
|
||
child: Text('去设置'.tr),
|
||
onPressed: () {
|
||
Get.back(); // 关闭对话框
|
||
openAppSettings(); // 打开系统设置页面
|
||
},
|
||
),
|
||
],
|
||
));
|
||
},
|
||
);
|
||
}
|
||
}
|