From 87fde0f813a90c5609fa141145eeed2bd223c95e Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 27 Jun 2018 10:40:22 +0200 Subject: [PATCH] lets just move the deleted file into the recycle bin when appropriate --- .../gallery/activities/MainActivity.kt | 2 +- .../gallery/activities/MediaActivity.kt | 2 +- .../gallery/activities/PhotoVideoActivity.kt | 2 +- .../gallery/activities/ViewPagerActivity.kt | 26 +++++++++++++---- .../gallery/extensions/Activity.kt | 29 +++++++++++++++---- .../gallery/helpers/MediaFetcher.kt | 4 +-- .../gallery/interfaces/MediumDao.kt | 3 ++ .../gallery/models/Medium.kt | 2 +- .../gallery/receivers/RefreshMediaReceiver.kt | 2 +- 9 files changed, 54 insertions(+), 18 deletions(-) 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 5f9bf1867..580599a23 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -275,7 +275,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { if (newFolder.exists() && newFolder.isDirectory) { if (newFolder.list()?.isEmpty() == true) { toast(String.format(getString(R.string.deleting_folder), config.tempFolderPath), Toast.LENGTH_LONG) - tryDeleteFileDirItem(newFolder.toFileDirItem(applicationContext), true) + tryDeleteFileDirItem(newFolder.toFileDirItem(applicationContext), true, true) } } config.tempFolderPath = "" 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 c15bb52f0..e3ba78a58 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -490,7 +490,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun deleteDirectoryIfEmpty() { val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath()) if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { - tryDeleteFileDirItem(fileDirItem, true) + tryDeleteFileDirItem(fileDirItem, true, true) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt index b3c139420..764f3ff82 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -95,7 +95,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList else -> TYPE_RAWS } - mMedium = Medium(null, getFilenameFromUri(mUri!!), mUri.toString(), mUri!!.path.getParentPath(), 0, 0, file.length(), type, false, 0) + mMedium = Medium(null, getFilenameFromUri(mUri!!), mUri.toString(), mUri!!.path.getParentPath(), 0, 0, file.length(), type, false, 0L) supportActionBar?.title = mMedium!!.name bundle.putSerializable(MEDIUM, mMedium) 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 07c0e0be4..d7a194288 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -589,7 +589,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } if (getDoesFilePathExist(newPath)) { - tryDeleteFileDirItem(FileDirItem(newPath, newPath.getFilenameFromPath())) + tryDeleteFileDirItem(FileDirItem(newPath, newPath.getFilenameFromPath()), false, true) } copyFile(tmpPath, newPath) @@ -618,7 +618,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } catch (e: Exception) { showErrorToast(e) } finally { - tryDeleteFileDirItem(tmpFileDirItem) + tryDeleteFileDirItem(tmpFileDirItem, false, true) } } @@ -837,9 +837,23 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun deleteConfirmed() { - val path = getCurrentMedia()[mPos].path - tryDeleteFileDirItem(FileDirItem(path, path.getFilenameFromPath())) { - refreshViewPager() + val path = getCurrentMedia().getOrNull(mPos)?.path ?: return + if (config.useRecycleBin) { + Thread { + movePathInRecycleBin(path) { + if (it) { + galleryDB.MediumDao().updateDeleted(path, System.currentTimeMillis()) + refreshViewPager() + } else { + toast(R.string.unknown_error_occurred) + } + } + }.start() + } else { + val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) + tryDeleteFileDirItem(fileDirItem, false, true) { + refreshViewPager() + } } } @@ -926,7 +940,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteDirectoryIfEmpty() { val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory)) if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { - tryDeleteFileDirItem(fileDirItem, true) + tryDeleteFileDirItem(fileDirItem, true, true) } scanPathRecursively(mDirectory) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index 8ae591cb2..c138baa31 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -133,7 +133,7 @@ fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = nul return } - tryDeleteFileDirItem(file.toFileDirItem(applicationContext)) { + tryDeleteFileDirItem(file.toFileDirItem(applicationContext), false, false) { callback?.invoke() } } @@ -168,12 +168,31 @@ fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList, } } -fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) { +fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, deleteFromDatabase: Boolean, + callback: ((wasSuccess: Boolean) -> Unit)? = null) { deleteFile(fileDirItem, allowDeleteFolder) { callback?.invoke(it) - Thread { - galleryDB.MediumDao().deleteMediumPath(fileDirItem.path) - }.start() + if (deleteFromDatabase) { + Thread { + galleryDB.MediumDao().deleteMediumPath(fileDirItem.path) + }.start() + } + } +} + +fun BaseSimpleActivity.movePathInRecycleBin(path: String, callback: (success: Boolean) -> Unit) { + val file = File(path) + val internalFile = File(filesDir, path) + return try { + file.copyRecursively(internalFile, true) + val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) + tryDeleteFileDirItem(fileDirItem, getIsPathDirectory(path), false) { + Thread { + callback(it) + }.start() + } + } catch (ignored: Exception) { + callback(false) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index 349cb9aee..3e495cb7c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -224,7 +224,7 @@ class MediaFetcher(val context: Context) { val path = file.absolutePath val isFavorite = favoritePaths.contains(path) - val medium = Medium(null, filename, path, file.parent, lastModified, dateTaken, size, type, isFavorite, 0) + val medium = Medium(null, filename, path, file.parent, lastModified, dateTaken, size, type, isFavorite, 0L) media.add(medium) } return media @@ -281,7 +281,7 @@ class MediaFetcher(val context: Context) { val path = Uri.decode(file.uri.toString().replaceFirst("${context.config.OTGTreeUri}/document/${context.config.OTGPartition}%3A", OTG_PATH)) val isFavorite = favoritePaths.contains(path) - val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, isFavorite, 0) + val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, isFavorite, 0L) media.add(medium) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt index 955513380..31ea1835e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -34,4 +34,7 @@ interface MediumDao { @Query("UPDATE media SET is_favorite = :isFavorite WHERE full_path = :path COLLATE NOCASE") fun updateFavorite(path: String, isFavorite: Boolean) + + @Query("UPDATE media SET deleted_ts = :deletedTS WHERE full_path = :path COLLATE NOCASE") + fun updateDeleted(path: String, deletedTS: Long) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt index a942039cf..1a7313d52 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt @@ -26,7 +26,7 @@ data class Medium( @ColumnInfo(name = "size") val size: Long, @ColumnInfo(name = "type") val type: Int, @ColumnInfo(name = "is_favorite") var isFavorite: Boolean, - @ColumnInfo(name = "deleted_ts") var deletedTS: Int) : Serializable, ThumbnailItem() { + @ColumnInfo(name = "deleted_ts") var deletedTS: Long) : Serializable, ThumbnailItem() { companion object { private const val serialVersionUID = -6553149366975655L diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt index 8ec3929c5..25dc4aa5f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt @@ -19,7 +19,7 @@ class RefreshMediaReceiver : BroadcastReceiver() { Thread { val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), - File(path).length(), getFileType(path), false, 0) + File(path).length(), getFileType(path), false, 0L) context.galleryDB.MediumDao().insert(medium) }.start() }