Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement proposal]: Split out service status checks from Permissions #1095

Open
2 tasks done
wujek-srujek opened this issue Jul 10, 2023 · 0 comments
Open
2 tasks done
Labels
P3 Issues that we currently consider unimportant. refactor Issues marked with refactor should be considered when refactoring the plugin. type: enhancement New feature or request

Comments

@wujek-srujek
Copy link
Contributor

wujek-srujek commented Jul 10, 2023

Please check the following before submitting a new issue.

Please select affected platform(s)

All of them, the API changes for the better ;).

Proposal

Currently service checks use permissions as input, and this sometimes is strange to use.

Bluetooth on Android is a good example. Until API level 30 there was Permissions.BLUETOOTH, and this is also used to check if BT is enabled on the device (almost, see #773). With API level 31, Permissions.BLUETOOTH is removed and others (.BLUETOOTH_SCAN, .BLUETOOTH_CONNECT etc.) are introduced instead. This means that on phones > API level 30 we will request the new permissions, but still need to use the old / legacy one for checking if BT is enabled on the device. IMHO this sounds clumsy.

An idea: service checks should not use permissions; instead, a type ServiceType is introduces with values like location, bluetooth, telephony etc. There is an extension on ServiceType with a member status, which allows for checking the status without using permissions, e.g.

extension ServiceTypeActions on ServiceType {
...
  Future<ServiceStatus> get status => _handler.checkServiceStatus(this);
...
}

This allows users to just do:

await ServiceType.bluetooth.status

If you want to additionally keep the current way and let the user check service status based on the permissions, PermissionWithStatus gets a new member service and the extension PermissionWithService.serviceStatus gets updated to

extension ServicePermissionsActions on PermissionWithService {
...
  Future<ServiceStatus> get serviceStatus => _handler.checkServiceStatus(service); // not _checkServiceStatus(this)
...

or even just

extension ServicePermissionsActions on PermissionWithService {
...
  Future<ServiceStatus> get serviceStatus => service.status;
...

In the BT example, Permissions.bluetooth, .bluetoothScan, .bluetoothConnect etc. all are a PermissionWithService whose service member returns ServiceType.bluetooth.

Pitch

I think the code will become easier to maintain as there will be fewer conditions with OR, e.g. https://github.com/Baseflow/flutter-permission-handler/blob/permission_handler_v10.4.2/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/ServiceManager.java#L40-L42 and easier to understand, as currently permissions are used for two purposes.

I think it will be easier for end users because they would be able to check a service status without having to think about which permission they should use for the check (but they could still do it due to the facts that the extension PermissionsWithService.serviceStatus would be updated.). This nicely separates the concerns of checking services vs checking permissions.

Finally, I think it would make the API more robust against changes like the one for Bluetooth described above, where it is currently necessary to use a legacy permissions on new phones.

@wujek-srujek wujek-srujek changed the title [Enhancement proposal]: Split Permission from services [Enhancement proposal]: Split out service status checks from Permissions Jul 10, 2023
@JeroenWeener JeroenWeener added type: enhancement New feature or request P3 Issues that we currently consider unimportant. labels Jul 13, 2023
@JeroenWeener JeroenWeener added the refactor Issues marked with refactor should be considered when refactoring the plugin. label Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 Issues that we currently consider unimportant. refactor Issues marked with refactor should be considered when refactoring the plugin. type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants