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 a52935acb..1efbc4b81 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -194,22 +194,19 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun deleteItem(file: File) { - val needsPermissions = needsStupidWritePermissions(file.path) - if (needsPermissions && isShowingPermDialog(file)) { + if (needsStupidWritePermissions(file.path) && isShowingPermDialog(file)) { return } Thread({ if (!file.delete()) { - if (needsPermissions) { - val document = getFileDocument(file.absolutePath, config.treeUri) + 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") - val filename = URLDecoder.decode(file.absolutePath.getFilenameFromPath(), "UTF-8") - if (uri.endsWith(filename)) { - document.delete() - } + // 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(file.absolutePath.getFilenameFromPath(), "UTF-8") + if (uri.endsWith(filename)) { + document.delete() } } }).start() 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 8526d2a95..d8f0b9e9b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -27,7 +27,6 @@ import com.simplemobiletools.gallery.views.MyScalableRecyclerView import kotlinx.android.synthetic.main.activity_media.* import java.io.File import java.io.IOException -import java.net.URLDecoder import java.util.* class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { @@ -228,8 +227,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } override fun deleteFiles(files: ArrayList) { - val needsPermissions = needsStupidWritePermissions(files[0].path) - if (needsPermissions && isShowingPermDialog(files[0])) { + if (needsStupidWritePermissions(files[0].path) && isShowingPermDialog(files[0])) { return } @@ -237,18 +235,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { var hadSuccess = false files.filter { it.exists() && it.isImageVideoGif() } .forEach { - if (!it.delete()) { - 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") - val filename = URLDecoder.decode(it.absolutePath.getFilenameFromPath(), "UTF-8") - if (uri.endsWith(filename) && !document.isDirectory) { - if (document.delete()) - hadSuccess = true - } - } else { + if (it.delete() || tryFastDocumentDelete(it)) { + hadSuccess = true + } else { + val document = getFileDocument(it.absolutePath, config.treeUri) + if (document.isFile && document.delete()) { hadSuccess = true } } 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 e09131c1b..53463c55a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -36,7 +36,6 @@ import kotlinx.android.synthetic.main.activity_medium.* import java.io.File import java.io.FileOutputStream import java.io.OutputStream -import java.net.URLDecoder import java.util.* @@ -309,27 +308,19 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteFile() { val file = File(mMedia[mPos].path) - val needsPermissions = needsStupidWritePermissions(file.path) - if (needsPermissions && isShowingPermDialog(file)) { + if (needsStupidWritePermissions(file.path) && isShowingPermDialog(file)) { return } Thread { - if (!file.delete()) { - if (needsPermissions) { - val document = getFileDocument(file.absolutePath, config.treeUri) + if (!file.delete() && !tryFastDocumentDelete(file)) { + 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") - val filename = URLDecoder.decode(file.absolutePath.getFilenameFromPath(), "UTF-8") - if (uri.endsWith(filename) && !document.isDirectory) { - document.delete() - } else { - runOnUiThread { - toast(R.string.unknown_error_occurred) - } - return@Thread + if (!document.isFile || !document.delete()) { + runOnUiThread { + toast(R.string.unknown_error_occurred) } + return@Thread } }