From 3571fcd3abd7869bb2f32454b89099c8603b78cd Mon Sep 17 00:00:00 2001 From: darthpaul Date: Sat, 14 May 2022 03:51:52 +0100 Subject: [PATCH] fix media store crash on deletion on android 12 - MediaStore crashed on deletion because the ViewPagerActivity got finish before the MediaStore.createDeleteRequest executes - To fix, we apply the same approach in the MediaActivity and finish explicitly after deletion completes, if the media list is empty - also, only refresh the UI when the media list is not empty to avoid UI glitches - fix slideshow bug by resetting mAreSlideShowMediaVisible back to false when the slideshow ends --- .../pro/activities/ViewPagerActivity.kt | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 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 3f0e35bba..9b782bf4e 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 @@ -591,6 +591,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View showSystemUI(true) mSlideshowHandler.removeCallbacksAndMessages(null) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + mAreSlideShowMediaVisible = false } } @@ -1128,16 +1129,21 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } mIgnoredPaths.add(fileDirItem.path) - val media = mMediaFiles.filter { !mIgnoredPaths.contains(it.path) } as ArrayList - runOnUiThread { - gotMedia(media, true, false) + val media = mMediaFiles.filter { !mIgnoredPaths.contains(it.path) } as ArrayList + if (media.isNotEmpty()) { + runOnUiThread { + refreshUI(media, true) + } } movePathsInRecycleBin(arrayListOf(path)) { if (it) { tryDeleteFileDirItem(fileDirItem, false, false) { mIgnoredPaths.remove(fileDirItem.path) - deleteDirectoryIfEmpty() + if (media.isEmpty()) { + deleteDirectoryIfEmpty() + finish() + } } } else { toast(R.string.unknown_error_occurred) @@ -1156,14 +1162,19 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } mIgnoredPaths.add(fileDirItem.path) - val media = mMediaFiles.filter { !mIgnoredPaths.contains(it.path) } as ArrayList - runOnUiThread { - gotMedia(media, true, false) + val media = mMediaFiles.filter { !mIgnoredPaths.contains(it.path) } as ArrayList + if (media.isNotEmpty()) { + runOnUiThread { + refreshUI(media, true) + } } tryDeleteFileDirItem(fileDirItem, false, true) { mIgnoredPaths.remove(fileDirItem.path) - deleteDirectoryIfEmpty() + if (media.isEmpty()) { + deleteDirectoryIfEmpty() + finish() + } } } } @@ -1227,6 +1238,10 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View return } + refreshUI(media, refetchViewPagerPosition) + } + + private fun refreshUI(media: ArrayList, refetchViewPagerPosition: Boolean) { mPrevHashcode = media.hashCode() mMediaFiles = media