Skip to content

Commit

Permalink
Merge pull request #3894 from TeamAmaze/intent-permission-issue
Browse files Browse the repository at this point in the history
Intent permission issue
  • Loading branch information
VishalNehra authored Aug 6, 2023
2 parents fcfc2e0 + 489087a commit 37db3ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ class AppsRecyclerAdapter(
) {
uri, mimeType, useNewStack ->
val intent = buildIntent(
fragment.requireContext(),
uri,
mimeType,
useNewStack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class OpenFileDialogFragment : BaseBottomSheetFragment(), AdjustListViewForTv<Ap
* Builds an intent which necessary permission flags for external apps to open uri file
*/
fun buildIntent(
context: Context,
uri: Uri,
mimeType: String,
useNewStack: Boolean,
Expand All @@ -152,6 +153,18 @@ class OpenFileDialogFragment : BaseBottomSheetFragment(), AdjustListViewForTv<Ap
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
chooserIntent.setDataAndType(uri, mimeType)

for (
resolveInfo in context.packageManager
.queryIntentActivities(
chooserIntent,
PackageManager.MATCH_DEFAULT_ONLY
)
) context.grantUriPermission(
resolveInfo.activityInfo.packageName,
uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)

if (useNewStack) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
Expand Down Expand Up @@ -187,6 +200,7 @@ class OpenFileDialogFragment : BaseBottomSheetFragment(), AdjustListViewForTv<Ap
try {
val classNameAndPackageName = classAndPackageRaw.split(" ")
val intent = buildIntent(
activity,
uri,
mimeType,
useNewStack,
Expand Down Expand Up @@ -305,6 +319,7 @@ class OpenFileDialogFragment : BaseBottomSheetFragment(), AdjustListViewForTv<Ap
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())

val intent = buildIntent(
requireContext(),
uri!!,
mimeType!!,
useNewStack!!,
Expand Down Expand Up @@ -340,6 +355,7 @@ class OpenFileDialogFragment : BaseBottomSheetFragment(), AdjustListViewForTv<Ap
private fun loadViews(lastAppData: AppDataParcelable) {
lastAppData.let {
val lastAppIntent = buildIntent(
requireContext(),
it.openFileParcelable?.uri!!,
it.openFileParcelable?.mimeType!!,
it.openFileParcelable?.useNewStack!!,
Expand Down Expand Up @@ -417,6 +433,7 @@ class OpenFileDialogFragment : BaseBottomSheetFragment(), AdjustListViewForTv<Ap
if (appDataParcelableList.size == 1) {
requireContext().startActivityCatchingSecurityException(
buildIntent(
requireContext(),
appDataParcelableList[0].openFileParcelable?.uri!!,
appDataParcelableList[0].openFileParcelable?.mimeType!!,
appDataParcelableList[0].openFileParcelable?.useNewStack!!,
Expand Down

0 comments on commit 37db3ba

Please sign in to comment.