Skip to content

Commit

Permalink
Added "REQUEST_INSTALL_PACKAGES" permission.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Derk committed May 19, 2021
1 parent 25b10ca commit bea04e2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions permission_handler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 7.2.0

* Added support for the "REQUEST_INSTALL_PACKAGES" permission on Android.

## 7.1.1

* Improved the example app by using the Baseflow Plugin Template and move all the functionality to the `main.dart` file.
Expand Down
1 change: 1 addition & 0 deletions permission_handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ The following permissions will show no dialog, but will open the corrseponding s

- manageExternalStorage
- systemAlertWindow
- requestInstallPackages

## Issues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class PermissionConstants {
static final int PERMISSION_CODE_IGNORE_BATTERY_OPTIMIZATIONS = 209;
static final int PERMISSION_CODE_MANAGE_EXTERNAL_STORAGE = 210;
static final int PERMISSION_CODE_SYSTEM_ALERT_WINDOW = 211;
static final int PERMISSION_CODE_REQUEST_INSTALL_PACKAGES = 212;

//PERMISSION_GROUP
static final int PERMISSION_GROUP_CALENDAR = 0;
Expand All @@ -37,6 +38,7 @@ final class PermissionConstants {
static final int PERMISSION_GROUP_BLUETOOTH = 21;
static final int PERMISSION_GROUP_MANAGE_EXTERNAL_STORAGE = 22;
static final int PERMISSION_GROUP_SYSTEM_ALERT_WINDOW = 23;
static final int PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES = 24;

@Retention(RetentionPolicy.SOURCE)
@IntDef({
Expand All @@ -62,7 +64,8 @@ final class PermissionConstants {
PERMISSION_GROUP_UNKNOWN,
PERMISSION_GROUP_BLUETOOTH,
PERMISSION_GROUP_MANAGE_EXTERNAL_STORAGE,
PERMISSION_GROUP_SYSTEM_ALERT_WINDOW
PERMISSION_GROUP_SYSTEM_ALERT_WINDOW,
PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES
})
@interface PermissionGroup {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ final class PermissionManager implements PluginRegistry.ActivityResultListener,
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PermissionConstants.PERMISSION_CODE_IGNORE_BATTERY_OPTIMIZATIONS &&
requestCode != PermissionConstants.PERMISSION_CODE_MANAGE_EXTERNAL_STORAGE &&
requestCode != PermissionConstants.PERMISSION_CODE_SYSTEM_ALERT_WINDOW) {
requestCode != PermissionConstants.PERMISSION_CODE_SYSTEM_ALERT_WINDOW &&
requestCode != PermissionConstants.PERMISSION_CODE_REQUEST_INSTALL_PACKAGES) {
return false;
}

Expand All @@ -63,6 +64,15 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
} else {
return false;
}
} else if (requestCode == PermissionConstants.PERMISSION_CODE_REQUEST_INSTALL_PACKAGES) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
status = activity.getPackageManager().canRequestPackageInstalls()
? PermissionConstants.PERMISSION_STATUS_GRANTED
: PermissionConstants.PERMISSION_STATUS_DENIED;
permission = PermissionConstants.PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES;
} else {
return false;
}
} else {
return false;
}
Expand Down Expand Up @@ -241,6 +251,10 @@ void requestPermissions(
executeIntent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
PermissionConstants.PERMISSION_CODE_SYSTEM_ALERT_WINDOW);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permission == PermissionConstants.PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES) {
executeIntent(
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
PermissionConstants.PERMISSION_CODE_REQUEST_INSTALL_PACKAGES);
} else {
permissionsToRequest.addAll(names);
}
Expand Down Expand Up @@ -343,6 +357,14 @@ private int determinePermissionStatus(
}
}

if (permission == PermissionConstants.PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return context.getPackageManager().canRequestPackageInstalls()
? PermissionConstants.PERMISSION_STATUS_GRANTED
: PermissionConstants.PERMISSION_STATUS_DENIED;
}
}

final int permissionStatus = ContextCompat.checkSelfPermission(context, name);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
return PermissionConstants.PERMISSION_STATUS_DENIED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static int parseManifestName(String permission) {
return PermissionConstants.PERMISSION_GROUP_MANAGE_EXTERNAL_STORAGE;
case Manifest.permission.SYSTEM_ALERT_WINDOW:
return PermissionConstants.PERMISSION_GROUP_SYSTEM_ALERT_WINDOW;
case Manifest.permission.REQUEST_INSTALL_PACKAGES:
return PermissionConstants.PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES;
default:
return PermissionConstants.PERMISSION_GROUP_UNKNOWN;
}
Expand Down Expand Up @@ -231,6 +233,13 @@ static List<String> getManifestNames(Context context, @PermissionConstants.Permi
permissionNames.add(Manifest.permission.SYSTEM_ALERT_WINDOW);
break;

case PermissionConstants.PERMISSION_GROUP_REQUEST_INSTALL_PACKAGES:
// The REQUEST_INSTALL_PACKAGES permission is introduced in Android M, meaning we should
// not handle permissions on pre Android M devices.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && hasPermissionInManifest(context, permissionNames, Manifest.permission.REQUEST_INSTALL_PACKAGES ))
permissionNames.add(Manifest.permission.REQUEST_INSTALL_PACKAGES);
break;

case PermissionConstants.PERMISSION_GROUP_NOTIFICATION:
case PermissionConstants.PERMISSION_GROUP_MEDIA_LIBRARY:
case PermissionConstants.PERMISSION_GROUP_PHOTOS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<!-- Permissions options for the `system alert windows` group -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<!-- Permissions options for the `request install packages` group -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
Expand Down
4 changes: 3 additions & 1 deletion permission_handler/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ class _PermissionHandlerWidgetState extends State<PermissionHandlerWidget> {
permission != Permission.accessMediaLocation &&
permission != Permission.activityRecognition &&
permission != Permission.manageExternalStorage &&
permission != Permission.systemAlertWindow;
permission != Permission.systemAlertWindow &&
permission != Permission.requestInstallPackages;
} else {
return permission != Permission.unknown &&
permission != Permission.mediaLibrary &&
permission != Permission.photos &&
permission != Permission.photosAddOnly &&
permission != Permission.reminders;
}
})
Expand Down
4 changes: 2 additions & 2 deletions permission_handler/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: permission_handler
description: Permission plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API to request and check permissions.
version: 7.1.1
version: 7.2.0
homepage: https://github.com/baseflowit/flutter-permission-handler

flutter:
Expand All @@ -16,7 +16,7 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.3.0
permission_handler_platform_interface: ^3.3.0
permission_handler_platform_interface: ^3.4.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit bea04e2

Please sign in to comment.