diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 1b94c07b2..771328743 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -189,23 +189,25 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun deleteItem(file: File) { - if (needsStupidWritePermissions(file.absolutePath)) { - if (!isShowingPermDialog(file)) { + val needsPermissions = needsStupidWritePermissions(file.path) + if (needsPermissions && isShowingPermDialog(file)) { + return + } + + Thread({ + if (needsPermissions) { val document = getFileDocument(file.absolutePath, config.treeUri) // double check we have the uri to the proper file path, not some parent folder val uri = URLDecoder.decode(document.uri.toString(), "UTF-8") - if (uri.endsWith(file.absolutePath.getFilenameFromPath())) { - Thread({ - document.delete() - }).start() + val filename = URLDecoder.decode(file.absolutePath.getFilenameFromPath(), "UTF-8") + if (uri.endsWith(filename)) { + document.delete() } - } - } else { - Thread({ + } else { file.delete() - }).start() - } + } + }).start() } private fun handleZooming() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index eb646b38d..341d8b997 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -214,29 +214,29 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } override fun deleteFiles(files: ArrayList) { - files.filter { it.exists() && it.isImageVideoGif() } - .forEach { - if (needsStupidWritePermissions(it.absolutePath)) { - if (isShowingPermDialog(it)) - return + val needsPermissions = needsStupidWritePermissions(files[0].path) + if (needsPermissions && isShowingPermDialog(files[0])) { + return + } - val document = getFileDocument(it.absolutePath, config.treeUri) + Thread({ + files.filter { it.exists() && it.isImageVideoGif() } + .forEach { + if (needsPermissions) { + val document = getFileDocument(it.absolutePath, config.treeUri) - // double check we have the uri to the proper file path, not some parent folder - val uri = URLDecoder.decode(document.uri.toString(), "UTF-8") - if (uri.endsWith(it.absolutePath.getFilenameFromPath()) && !document.isDirectory) { - Thread({ + // double check we have the uri to the proper file path, not some parent folder + val uri = URLDecoder.decode(document.uri.toString(), "UTF-8") + val filename = URLDecoder.decode(it.absolutePath.getFilenameFromPath(), "UTF-8") + if (uri.endsWith(filename) && !document.isDirectory) { document.delete() - }).start() - } - } else { - Thread({ + } + } else { it.delete() - }).start() + } + deleteFromMediaStore(it) } - - deleteFromMediaStore(it) - } + }).start() if (mMedia.isEmpty()) { finish() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 05a80a764..f2670380d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -232,34 +232,33 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteFile() { val file = File(mMedia[mPos].path) - if (isShowingPermDialog(file)) + val needsPermissions = needsStupidWritePermissions(file.path) + if (needsPermissions && isShowingPermDialog(file)) { return + } - if (needsStupidWritePermissions(mPath)) { - if (!isShowingPermDialog(file)) { + Thread({ + if (needsPermissions) { val document = getFileDocument(mPath, config.treeUri) // double check we have the uri to the proper file path, not some parent folder val uri = URLDecoder.decode(document.uri.toString(), "UTF-8") - if (uri.endsWith(file.absolutePath.getFilenameFromPath()) && !document.isDirectory) { - Thread({ - document.delete() - }).start() + val filename = URLDecoder.decode(file.absolutePath.getFilenameFromPath(), "UTF-8") + if (uri.endsWith(filename) && !document.isDirectory) { + document.delete() + } + } else { + file.delete() + } + + if (deleteFromMediaStore(file)) { + reloadViewPager() + } else { + scanFile(file) { + reloadViewPager() } } - } else { - Thread({ - file.delete() - }).start() - } - - if (deleteFromMediaStore(file)) { - reloadViewPager() - } else { - scanFile(file) { - reloadViewPager() - } - } + }).start() } private fun isDirEmpty(): Boolean {