From 37f08c4fccb79c9e8e911af9407168b566e890d9 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Sun, 29 May 2022 07:26:53 +0500 Subject: [PATCH] Fixed: Fix `Settings.ACTION_*` permission requests for non-activity contexts This was caused by ce12b8ad Closes #2769 --- .../termux/shared/android/PermissionUtils.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java b/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java index 60aeed07..4a15649d 100644 --- a/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java @@ -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