Commit 3d161ec8 by Alex Sandri Committed by GitHub

Add support for POST_NOTIFICATIONS permission (#845)

* 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
parent 35747269
## 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.
......
......@@ -27,7 +27,7 @@ project.getTasks().withType(JavaCompile){
apply plugin: 'com.android.library'
android {
compileSdkVersion 31
compileSdkVersion 33
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
......
package com.baseflow.permissionhandler;
import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.app.Notification;
......@@ -465,12 +466,18 @@ final class PermissionManager implements PluginRegistry.ActivityResultListener,
}
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) {
......
......@@ -75,6 +75,8 @@ public class PermissionUtils {
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;
}
......@@ -288,6 +290,11 @@ public class PermissionUtils {
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:
......@@ -313,9 +320,7 @@ public class PermissionUtils {
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.");
......@@ -384,4 +389,17 @@ public class PermissionUtils {
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);
}
}
}
......@@ -70,9 +70,7 @@ final class ServiceManager {
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);
......@@ -162,4 +160,18 @@ final class ServiceManager {
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);
}
}
}
......@@ -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
......@@ -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
}
......
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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment