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

Update logic to handle calendar WRITE/FULL_ACCESS instead of READ/FULL_ACCESS #1194

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions permission_handler_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 12.0.0

* **BREAKING CHANGES:**
* Adds `Permission.calendarWriteOnly`.
* Removes `Permission.calendarReadOnly`.

## 11.1.0

* Implements the `Permission.calendarReadOnly` and `PermissionCalendarFullAccess` permissions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class PermissionConstants {

// PERMISSION_GROUP

// Deprecated in favor of PERMISSION_GROUP_CALENDAR_READ_ONLY and
// Deprecated in favor of PERMISSION_GROUP_CALENDAR_WRITE_ONLY and
// PERMISSION_GROUP_CALENDAR_FULL_ACCESS.
static final int PERMISSION_GROUP_CALENDAR = 0;
static final int PERMISSION_GROUP_CAMERA = 1;
Expand Down Expand Up @@ -58,7 +58,7 @@ final class PermissionConstants {
static final int PERMISSION_GROUP_AUDIO = 33;
static final int PERMISSION_GROUP_SCHEDULE_EXACT_ALARM = 34;
static final int PERMISSION_GROUP_SENSORS_ALWAYS = 35;
static final int PERMISSION_GROUP_CALENDAR_READ_ONLY = 36;
static final int PERMISSION_GROUP_CALENDAR_WRITE_ONLY = 36;
static final int PERMISSION_GROUP_CALENDAR_FULL_ACCESS = 37;

@Retention(RetentionPolicy.SOURCE)
Expand Down Expand Up @@ -96,7 +96,7 @@ final class PermissionConstants {
PERMISSION_GROUP_VIDEOS,
PERMISSION_GROUP_AUDIO,
PERMISSION_GROUP_SCHEDULE_EXACT_ALARM,
PERMISSION_GROUP_CALENDAR_READ_ONLY,
PERMISSION_GROUP_CALENDAR_WRITE_ONLY,
PERMISSION_GROUP_CALENDAR_FULL_ACCESS
})
@interface PermissionGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,24 @@ public boolean onRequestPermissionsResult(
return false;
}

// Calendar permissions are split between READ and WRITE in Android, and split between READ
// Calendar permissions are split between WRITE and READ in Android, and split between WRITE
// and FULL ACCESS in the plugin. We need special logic for this translation.
final List<String> permissionList = Arrays.asList(permissions);
final int calendarReadIndex = permissionList.indexOf(Manifest.permission.READ_CALENDAR);
final int calendarWriteIndex = permissionList.indexOf(Manifest.permission.WRITE_CALENDAR);
// READ -> READ.
if (calendarReadIndex >= 0) {
final int readGrantResult = grantResults[calendarReadIndex];
final @PermissionConstants.PermissionStatus int readStatus =
PermissionUtils.toPermissionStatus(this.activity, Manifest.permission.READ_CALENDAR, readGrantResult);
requestResults.put(PermissionConstants.PERMISSION_GROUP_CALENDAR_READ_ONLY, readStatus);

// READ + WRITE -> FULL ACCESS.
if (calendarWriteIndex >= 0) {
final int writeGrantResult = grantResults[calendarWriteIndex];
final @PermissionConstants.PermissionStatus int writeStatus =
PermissionUtils.toPermissionStatus(this.activity, Manifest.permission.WRITE_CALENDAR, writeGrantResult);
final @PermissionConstants.PermissionStatus int fullAccessStatus = strictestStatus(readStatus, writeStatus);
// WRITE -> WRITE.
if (calendarWriteIndex >= 0) {
final int writeGrantResult = grantResults[calendarWriteIndex];
final @PermissionConstants.PermissionStatus int writeStatus =
PermissionUtils.toPermissionStatus(this.activity, Manifest.permission.WRITE_CALENDAR, writeGrantResult);
requestResults.put(PermissionConstants.PERMISSION_GROUP_CALENDAR_WRITE_ONLY, writeStatus);

// WRITE + READ -> FULL ACCESS.
final int calendarReadIndex = permissionList.indexOf(Manifest.permission.READ_CALENDAR);
if (calendarReadIndex >= 0) {
final int readGrantResult = grantResults[calendarReadIndex];
final @PermissionConstants.PermissionStatus int readStatus =
PermissionUtils.toPermissionStatus(this.activity, Manifest.permission.READ_CALENDAR, readGrantResult);
final @PermissionConstants.PermissionStatus int fullAccessStatus = strictestStatus(writeStatus, readStatus);
requestResults.put(PermissionConstants.PERMISSION_GROUP_CALENDAR_FULL_ACCESS, fullAccessStatus);
// Support deprecated CALENDAR permission.
requestResults.put(PermissionConstants.PERMISSION_GROUP_CALENDAR, fullAccessStatus);
Expand All @@ -193,8 +193,8 @@ public boolean onRequestPermissionsResult(
for (int i = 0; i < permissions.length; i++) {
final String permissionName = permissions[i];

// READ_CALENDAR and WRITE_CALENDAR permission results have already been handled.
if (permissionName.equals(Manifest.permission.READ_CALENDAR) || permissionName.equals(Manifest.permission.WRITE_CALENDAR)) {
// WRITE_CALENDAR and READ_CALENDAR permission results have already been handled.
if (permissionName.equals(Manifest.permission.WRITE_CALENDAR) || permissionName.equals(Manifest.permission.READ_CALENDAR)) {
continue;
}

Expand Down Expand Up @@ -409,12 +409,11 @@ void requestPermissions(
Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
PermissionConstants.PERMISSION_CODE_SCHEDULE_EXACT_ALARM);
} else if (permission == PermissionConstants.PERMISSION_GROUP_CALENDAR_FULL_ACCESS || permission == PermissionConstants.PERMISSION_GROUP_CALENDAR) {
// Deny CALENDAR_FULL_ACCESS permission if manifest is not listing both read- and write permissions.
// Otherwise, we will only ask for READ permission and think full access is granted.
// Deny CALENDAR_FULL_ACCESS permission if manifest is not listing both write- and read permissions.
final boolean isValidManifest = isValidManifestForCalendarFullAccess();
if (isValidManifest) {
permissionsToRequest.add(Manifest.permission.READ_CALENDAR);
permissionsToRequest.add(Manifest.permission.WRITE_CALENDAR);
permissionsToRequest.add(Manifest.permission.READ_CALENDAR);
pendingRequestCount += 2;
} else {
requestResults.put(permission, PermissionConstants.PERMISSION_STATUS_DENIED);
Expand Down Expand Up @@ -650,19 +649,19 @@ private int checkBluetoothPermissionStatus() {
}

/**
* Checks if the manifest contains both {@link Manifest.permission#READ_CALENDAR} and
* {@link Manifest.permission#WRITE_CALENDAR} permission declarations.
* Checks if the manifest contains both {@link Manifest.permission#WRITE_CALENDAR} and
* {@link Manifest.permission#READ_CALENDAR} permission declarations.
*/
private boolean isValidManifestForCalendarFullAccess() {
List<String> names = PermissionUtils.getManifestNames(context, PermissionConstants.PERMISSION_GROUP_CALENDAR_FULL_ACCESS);
final boolean readInManifest = names != null && names.contains(Manifest.permission.READ_CALENDAR);
final boolean writeInManifest = names != null && names.contains(Manifest.permission.WRITE_CALENDAR);
final boolean validManifest = readInManifest && writeInManifest;
final boolean readInManifest = names != null && names.contains(Manifest.permission.READ_CALENDAR);
final boolean validManifest = writeInManifest && readInManifest;
if (!validManifest) {
if (!readInManifest)
Log.d(PermissionConstants.LOG_TAG, Manifest.permission.READ_CALENDAR + " missing in manifest");
if (!writeInManifest)
Log.d(PermissionConstants.LOG_TAG, Manifest.permission.WRITE_CALENDAR + " missing in manifest");
if (!readInManifest)
Log.d(PermissionConstants.LOG_TAG, Manifest.permission.READ_CALENDAR + " missing in manifest");
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class PermissionUtils {
@PermissionConstants.PermissionGroup
static int parseManifestName(String permission) {
switch (permission) {
case Manifest.permission.READ_CALENDAR:
case Manifest.permission.WRITE_CALENDAR:
case Manifest.permission.READ_CALENDAR:
return PermissionConstants.PERMISSION_GROUP_CALENDAR;
case Manifest.permission.CAMERA:
return PermissionConstants.PERMISSION_GROUP_CAMERA;
Expand Down Expand Up @@ -103,17 +103,17 @@ static List<String> getManifestNames(Context context, @PermissionConstants.Permi
final ArrayList<String> permissionNames = new ArrayList<>();

switch (permission) {
case PermissionConstants.PERMISSION_GROUP_CALENDAR_READ_ONLY:
if (hasPermissionInManifest(context, permissionNames, Manifest.permission.READ_CALENDAR))
permissionNames.add(Manifest.permission.READ_CALENDAR);
case PermissionConstants.PERMISSION_GROUP_CALENDAR_WRITE_ONLY:
if (hasPermissionInManifest(context, permissionNames, Manifest.permission.WRITE_CALENDAR))
permissionNames.add(Manifest.permission.WRITE_CALENDAR);
break;

case PermissionConstants.PERMISSION_GROUP_CALENDAR_FULL_ACCESS:
case PermissionConstants.PERMISSION_GROUP_CALENDAR:
if (hasPermissionInManifest(context, permissionNames, Manifest.permission.READ_CALENDAR))
permissionNames.add(Manifest.permission.READ_CALENDAR);
if (hasPermissionInManifest(context, permissionNames, Manifest.permission.WRITE_CALENDAR))
permissionNames.add(Manifest.permission.WRITE_CALENDAR);
if (hasPermissionInManifest(context, permissionNames, Manifest.permission.READ_CALENDAR))
permissionNames.add(Manifest.permission.READ_CALENDAR);
break;

case PermissionConstants.PERMISSION_GROUP_CAMERA:
Expand Down
4 changes: 2 additions & 2 deletions permission_handler_android/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: permission_handler_android
description: Permission plugin for Flutter. This plugin provides the Android API to request and check permissions.
homepage: https://github.com/baseflow/flutter-permission-handler
version: 11.1.0
version: 12.0.0

environment:
sdk: ">=2.15.0 <4.0.0"
Expand All @@ -18,7 +18,7 @@ flutter:
dependencies:
flutter:
sdk: flutter
permission_handler_platform_interface: ^3.12.0
permission_handler_platform_interface: ^4.0.0

dev_dependencies:
flutter_lints: ^1.0.4
Expand Down