Fixed: Fix `Settings.ACTION_*` permission requests for non-activity contexts

This was caused by ce12b8ad

Closes #2769
This commit is contained in:
agnostic-apollo 2022-05-29 07:26:53 +05:00
parent a50387b553
commit 37f08c4fcc
1 changed files with 15 additions and 0 deletions

View File

@ -357,6 +357,11 @@ public class PermissionUtils {
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + context.getPackageName()));
// Flag must not be passed for activity contexts, otherwise onActivityResult() will not be called with permission grant result.
// Flag must be passed for non-activity contexts like services, otherwise "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" exception will be raised.
if (!(context instanceof Activity))
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Error error;
if (requestCode >=0)
error = ActivityUtils.startActivityForResult(context, requestCode, intent, true, false);
@ -474,6 +479,11 @@ public class PermissionUtils {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + context.getPackageName()));
// Flag must not be passed for activity contexts, otherwise onActivityResult() will not be called with permission grant result.
// Flag must be passed for non-activity contexts like services, otherwise "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" exception will be raised.
if (!(context instanceof Activity))
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (requestCode >=0)
return ActivityUtils.startActivityForResult(context, requestCode, intent);
else
@ -549,6 +559,11 @@ public class PermissionUtils {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
// Flag must not be passed for activity contexts, otherwise onActivityResult() will not be called with permission grant result.
// Flag must be passed for non-activity contexts like services, otherwise "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" exception will be raised.
if (!(context instanceof Activity))
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (requestCode >=0)
return ActivityUtils.startActivityForResult(context, requestCode, intent);
else