From aca71be5d89b164eac2204cd6778d9b33c2dc7c7 Mon Sep 17 00:00:00 2001 From: darthpaul Date: Thu, 28 Apr 2022 01:09:11 +0100 Subject: [PATCH 1/2] handle mange media for renaming - move the launchMediaManagementIntent method to commons module --- .../gallery/pro/activities/ViewPagerActivity.kt | 10 ++++++++-- .../gallery/pro/adapters/MediaAdapter.kt | 8 +++++++- .../gallery/pro/extensions/Activity.kt | 11 ++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 96688e463..fc3226e6c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -245,7 +245,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View R.id.menu_unhide -> toggleFileVisibility(false) R.id.menu_share -> shareMediumPath(getCurrentPath()) R.id.menu_delete -> checkDeleteConfirmation() - R.id.menu_rename -> renameFile() + R.id.menu_rename -> checkMediaManagementAndRename() R.id.menu_print -> printFile() R.id.menu_edit -> openEditor(getCurrentPath()) R.id.menu_properties -> showProperties() @@ -900,7 +900,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View bottom_rename.beVisibleIf(visibleBottomActions and BOTTOM_ACTION_RENAME != 0 && currentMedium?.getIsInRecycleBin() == false) bottom_rename.setOnLongClickListener { toast(R.string.rename); true } bottom_rename.setOnClickListener { - renameFile() + checkMediaManagementAndRename() } bottom_set_as.beVisibleIf(visibleBottomActions and BOTTOM_ACTION_SET_AS != 0) @@ -1178,6 +1178,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } + private fun checkMediaManagementAndRename() { + handleMediaManagementPrompt { + renameFile() + } + } + private fun renameFile() { val oldPath = getCurrentPath() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index 47227f7ab..97275a268 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -161,7 +161,7 @@ class MediaAdapter( when (id) { R.id.cab_confirm_selection -> confirmSelection() R.id.cab_properties -> showProperties() - R.id.cab_rename -> renameFile() + R.id.cab_rename -> checkMediaManagementAndRename() R.id.cab_edit -> editFile() R.id.cab_hide -> toggleFileVisibility(true) R.id.cab_unhide -> toggleFileVisibility(false) @@ -234,6 +234,12 @@ class MediaAdapter( } } + private fun checkMediaManagementAndRename() { + activity.handleMediaManagementPrompt { + renameFile() + } + } + private fun renameFile() { val firstPath = getFirstSelectedItemPath() ?: return diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index bf9eda2b9..565c300de 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -125,18 +125,11 @@ fun SimpleActivity.launchAbout() { startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true) } -@RequiresApi(Build.VERSION_CODES.S) -fun AppCompatActivity.launchMediaManagementIntent() { - Intent(Settings.ACTION_REQUEST_MANAGE_MEDIA).apply { - data = Uri.parse("package:$packageName") - startActivity(this) - } -} -fun AppCompatActivity.handleMediaManagementPrompt(callback: () -> Unit) { +fun BaseSimpleActivity.handleMediaManagementPrompt(callback: () -> Unit) { if (isSPlus() && !MediaStore.canManageMedia(this)) { ConfirmationDialog(this, "", R.string.media_management_prompt, R.string.ok, 0) { - launchMediaManagementIntent() + launchMediaManagementIntent(callback) } } else { callback() From b4262111d7fbca11309adf9dd9e7f2119b5f453f Mon Sep 17 00:00:00 2001 From: darthpaul Date: Sat, 30 Apr 2022 23:58:21 +0100 Subject: [PATCH 2/2] use MediaStore request way of deleting files on SDK 31 - on SDK 31+, if the app can manange media, we want to use the MediaStore request way of deleting files - we use the new method added BaseSimpleActivity.checkManageMediaOrHandleSAFDialogSdk30 that performs the check and determines if we should show the SAF dialog or not --- .../gallery/pro/activities/ViewPagerActivity.kt | 8 ++++---- .../gallery/pro/adapters/MediaAdapter.kt | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index fc3226e6c..3f0e35bba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -1122,9 +1122,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) if (config.useRecycleBin && !getCurrentMedium()!!.getIsInRecycleBin()) { - handleSAFDialogSdk30(fileDirItem.path) { + checkManageMediaOrHandleSAFDialogSdk30(fileDirItem.path) { if (!it) { - return@handleSAFDialogSdk30 + return@checkManageMediaOrHandleSAFDialogSdk30 } mIgnoredPaths.add(fileDirItem.path) @@ -1150,9 +1150,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun handleDeletion(fileDirItem: FileDirItem) { - handleSAFDialogSdk30(fileDirItem.path) { + checkManageMediaOrHandleSAFDialogSdk30(fileDirItem.path) { if (!it) { - return@handleSAFDialogSdk30 + return@checkManageMediaOrHandleSAFDialogSdk30 } mIgnoredPaths.add(fileDirItem.path) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index 97275a268..eebb97e76 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -491,9 +491,9 @@ class MediaAdapter( } val sdk30SafPath = selectedPaths.firstOrNull { activity.isAccessibleWithSAFSdk30(it) } ?: getFirstSelectedItemPath() ?: return@handleSAFDialog - activity.handleSAFDialogSdk30(sdk30SafPath) { + activity.checkManageMediaOrHandleSAFDialogSdk30(sdk30SafPath) { if (!it) { - return@handleSAFDialogSdk30 + return@checkManageMediaOrHandleSAFDialogSdk30 } val fileDirItems = ArrayList(selectedKeys.size)