From 04ecb6e0b4b3ebf356f5acefafd470b7e100159a Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 4 Nov 2019 23:36:08 +0100 Subject: [PATCH] fix moving OTG files in the recycle bin --- .../pro/activities/ViewPagerActivity.kt | 2 +- .../gallery/pro/extensions/Activity.kt | 61 +++++++++++++++---- 2 files changed, 49 insertions(+), 14 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 cbcc9e4e4..f0eec2073 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 @@ -945,7 +945,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteConfirmed() { val path = getCurrentMedia().getOrNull(mPos)?.path ?: return - if (File(path).isDirectory || !path.isMediaFile()) { + if (getIsPathDirectory(path) || !path.isMediaFile()) { 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 9b3faf538..0f6db3a75 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 @@ -228,22 +228,57 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) { ensureBackgroundThread { var pathsCnt = paths.size - paths.forEach { - val file = File(it) - val internalFile = File(recycleBinPath, it) - val lastModified = file.lastModified() - try { - if (file.copyRecursively(internalFile, true)) { - mediumDao.updateDeleted("$RECYCLE_BIN$it", System.currentTimeMillis(), it) - pathsCnt-- + val OTGPath = config.OTGPath - if (config.keepLastModified) { - internalFile.setLastModified(lastModified) + for (source in paths) { + if (OTGPath.isNotEmpty() && source.startsWith(OTGPath)) { + var inputStream: InputStream? = null + var out: OutputStream? = null + try { + val destination = "$recycleBinPath/$source" + val fileDocument = getSomeDocumentFile(source) + inputStream = applicationContext.contentResolver.openInputStream(fileDocument?.uri) + out = getFileOutputStreamSync(destination, source.getMimeType()) + + var copiedSize = 0L + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + var bytes = inputStream.read(buffer) + while (bytes >= 0) { + out!!.write(buffer, 0, bytes) + copiedSize += bytes + bytes = inputStream.read(buffer) } + + out?.flush() + + if (fileDocument?.getItemSize(false) == copiedSize && getDoesFilePathExist(destination)) { + mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) + pathsCnt-- + } + } catch (e: Exception) { + showErrorToast(e) + return@ensureBackgroundThread + } finally { + inputStream?.close() + out?.close() + } + } else { + val file = File(source) + val internalFile = File(recycleBinPath, source) + val lastModified = file.lastModified() + try { + if (file.copyRecursively(internalFile, true)) { + mediumDao.updateDeleted("$RECYCLE_BIN$source", System.currentTimeMillis(), source) + pathsCnt-- + + if (config.keepLastModified) { + internalFile.setLastModified(lastModified) + } + } + } catch (e: Exception) { + showErrorToast(e) + return@ensureBackgroundThread } - } catch (e: Exception) { - showErrorToast(e) - return@forEach } } callback?.invoke(pathsCnt == 0)