try a faster way of deleting SD card files

This commit is contained in:
tibbi 2017-02-20 21:57:41 +01:00
parent acaaa28806
commit 07b2371b9c
3 changed files with 20 additions and 41 deletions

View File

@ -194,14 +194,12 @@ 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)
// double check we have the uri to the proper file path, not some parent folder
@ -211,7 +209,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
document.delete()
}
}
}
}).start()
}

View File

@ -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<File>) {
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())
if (it.delete() || tryFastDocumentDelete(it)) {
hadSuccess = true
}
} else {
val document = getFileDocument(it.absolutePath, config.treeUri)
if (document.isFile && document.delete()) {
hadSuccess = true
}
}

View File

@ -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,29 +308,21 @@ 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) {
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 {
if (!document.isFile || !document.delete()) {
runOnUiThread {
toast(R.string.unknown_error_occurred)
}
return@Thread
}
}
}
if (deleteFromMediaStore(file)) {
reloadViewPager()