Skip to content

Commit

Permalink
Add support for POST_NOTIFICATIONS permission (#845)
Browse files Browse the repository at this point in the history
* Add support for POST_NOTIFICATIONS permission (Introduced with Android 13)

* increase compileSdkVersion to 33 and fix deprecated methods in API 33

* bump package version and update changelog

* bump example app compile and target sdk versions

* fix: crash on Android < 13

* revert version updates

* revert  example app's

* bump permission_handler_android to 10.0.0
  • Loading branch information
alex-sandri authored Jun 15, 2022
1 parent 3574726 commit 3d161ec
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 15 deletions.
5 changes: 5 additions & 0 deletions permission_handler_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 10.0.0

* __BREAKING CHANGE__: Updated Android `compileSdkVersion` to `33` to handle the new `POST_NOTIFICATIONS` permission.
> When updating to version 10.0.0 make sure to update the `android/app/build.gradle` file and set the `compileSdkVersion` to `33`.
## 9.0.2+1

* Undoes PR [#765](https://github.com/baseflow/flutter-permission-handler/pull/765) which by mistake requests write_external_storage permission based on the target SDK instead of the actual SDK of the Android device.
Expand Down
2 changes: 1 addition & 1 deletion permission_handler_android/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ project.getTasks().withType(JavaCompile){
apply plugin: 'com.android.library'

android {
compileSdkVersion 31
compileSdkVersion 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.baseflow.permissionhandler;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.app.Notification;
Expand Down Expand Up @@ -465,12 +466,18 @@ void shouldShowRequestPermissionRationale(
}

private int checkNotificationPermissionStatus(Context context) {
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
boolean isGranted = manager.areNotificationsEnabled();
if (isGranted) {
return PermissionConstants.PERMISSION_STATUS_GRANTED;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
boolean isGranted = manager.areNotificationsEnabled();
if (isGranted) {
return PermissionConstants.PERMISSION_STATUS_GRANTED;
}
return PermissionConstants.PERMISSION_STATUS_DENIED;
}
return PermissionConstants.PERMISSION_STATUS_DENIED;

return context.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
? PermissionConstants.PERMISSION_STATUS_GRANTED
: PermissionConstants.PERMISSION_STATUS_DENIED;
}

private int checkBluetoothPermissionStatus(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ static int parseManifestName(String permission) {
return PermissionConstants.PERMISSION_GROUP_BLUETOOTH_ADVERTISE;
case Manifest.permission.BLUETOOTH_CONNECT:
return PermissionConstants.PERMISSION_GROUP_BLUETOOTH_CONNECT;
case Manifest.permission.POST_NOTIFICATIONS:
return PermissionConstants.PERMISSION_GROUP_NOTIFICATION;
default:
return PermissionConstants.PERMISSION_GROUP_UNKNOWN;
}
Expand Down Expand Up @@ -288,6 +290,11 @@ static List<String> getManifestNames(Context context, @PermissionConstants.Permi
break;
}
case PermissionConstants.PERMISSION_GROUP_NOTIFICATION:
// The POST_NOTIFICATIONS permission is introduced in Android 13, meaning we should
// not handle permissions on pre Android 13 devices.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && hasPermissionInManifest(context, permissionNames, Manifest.permission.POST_NOTIFICATIONS ))
permissionNames.add(Manifest.permission.POST_NOTIFICATIONS);
break;
case PermissionConstants.PERMISSION_GROUP_MEDIA_LIBRARY:
case PermissionConstants.PERMISSION_GROUP_PHOTOS:
case PermissionConstants.PERMISSION_GROUP_REMINDERS:
Expand All @@ -313,9 +320,7 @@ private static boolean hasPermissionInManifest(Context context, ArrayList<String
return false;
}

PackageInfo info = context
.getPackageManager()
.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
PackageInfo info = getPackageInfo(context);

if (info == null) {
Log.d(PermissionConstants.LOG_TAG, "Unable to get Package info, will not be able to determine permissions to request.");
Expand Down Expand Up @@ -384,4 +389,17 @@ private static String determineBluetoothPermission(Context context, String permi

return null;
}

// Suppress deprecation warnings since its purpose is to support to be backwards compatible with
// pre TIRAMISU versions of Android
@SuppressWarnings("deprecation")
private static PackageInfo getPackageInfo(Context context) throws PackageManager.NameNotFoundException {
final PackageManager pm = context.getPackageManager();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return pm.getPackageInfo(context.getPackageName(), PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS));
} else {
return pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ void checkServiceStatus(
return;
}

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123123"));
List<ResolveInfo> callAppsList = pm.queryIntentActivities(callIntent, 0);
List<ResolveInfo> callAppsList = getCallAppsList(pm);

if (callAppsList.isEmpty()) {
successCallback.onSuccess(PermissionConstants.SERVICE_STATUS_NOT_APPLICABLE);
Expand Down Expand Up @@ -162,4 +160,18 @@ private boolean isBluetoothServiceEnabled(Context context) {
final BluetoothAdapter adapter = manager.getAdapter();
return adapter.isEnabled();
}

// Suppress deprecation warnings since its purpose is to support to be backwards compatible with
// pre TIRAMISU versions of Android
@SuppressWarnings("deprecation")
private List<ResolveInfo> getCallAppsList(PackageManager pm) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123123"));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return pm.queryIntentActivities(callIntent, PackageManager.ResolveInfoFlags.of(0));
} else {
return pm.queryIntentActivities(callIntent, 0);
}
}
}
4 changes: 2 additions & 2 deletions permission_handler_android/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -36,7 +36,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.baseflow.permissionhandler.example"
minSdkVersion 16
targetSdkVersion 31
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
2 changes: 1 addition & 1 deletion permission_handler_android/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: permission_handler_android
description: Permission plugin for Flutter. This plugin provides the Android API to request and check permissions.
version: 9.0.2+1
version: 10.0.0
homepage: https://github.com/baseflow/flutter-permission-handler

environment:
Expand Down

0 comments on commit 3d161ec

Please sign in to comment.