From 4923d5e4afbb016bc65bed354691aa59caff8b60 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 27 Jun 2018 20:25:12 +0200 Subject: [PATCH] allow restoring files from the recycle bin at the fullscreen view --- .../gallery/activities/ViewPagerActivity.kt | 8 +++++ .../gallery/extensions/Activity.kt | 31 +++++++++++++++++++ app/src/main/res/menu/menu_viewpager.xml | 4 +++ 3 files changed, 43 insertions(+) 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 a7640970b..78ae00d35 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -298,6 +298,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View findItem(R.id.menu_unhide).isVisible = currentMedium.name.startsWith('.') findItem(R.id.menu_add_to_favorites).isVisible = !currentMedium.isFavorite && !config.bottomActions findItem(R.id.menu_remove_from_favorites).isVisible = currentMedium.isFavorite && !config.bottomActions + findItem(R.id.menu_restore_file).isVisible = currentMedium.path.startsWith(filesDir.toString()) findItem(R.id.menu_lock_orientation).isVisible = mRotationDegrees == 0 findItem(R.id.menu_lock_orientation).title = getString(if (mIsOrientationLocked) R.string.unlock_orientation else R.string.lock_orientation) findItem(R.id.menu_rotate).setShowAsAction( @@ -336,6 +337,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View R.id.menu_rotate_left -> rotateImage(270) R.id.menu_add_to_favorites -> toggleFavorite() R.id.menu_remove_from_favorites -> toggleFavorite() + R.id.menu_restore_file -> restoreFile() R.id.menu_rotate_one_eighty -> rotateImage(180) R.id.menu_lock_orientation -> toggleLockOrientation() R.id.menu_save_as -> saveImageAs() @@ -809,6 +811,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View }.start() } + private fun restoreFile() { + restoreRecycleBinPath(getCurrentPath()) { + refreshViewPager() + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { if (requestCode == REQUEST_EDIT_IMAGE) { if (resultCode == Activity.RESULT_OK && resultData != null) { 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 4d4938fb6..00293ff61 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -16,6 +16,8 @@ import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.helpers.NOMEDIA import java.io.File +import java.io.InputStream +import java.io.OutputStream import java.util.* fun Activity.sharePath(path: String) { @@ -205,6 +207,35 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, callback: }.start() } +fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) { + restoreRecycleBinPaths(arrayListOf(path), callback) +} + +fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, callback: () -> Unit) { + Thread { + val mediumDao = galleryDB.MediumDao() + paths.forEach { + val source = it + val destination = it.removePrefix(filesDir.toString()) + + var inputStream: InputStream? = null + var out: OutputStream? = null + try { + out = getFileOutputStreamSync(destination, source.getMimeType()) + inputStream = getFileInputStreamSync(source)!! + inputStream.copyTo(out!!) + mediumDao.updateDeleted(destination, 0) + } catch (e: Exception) { + showErrorToast(e) + } finally { + inputStream?.close() + out?.close() + } + } + callback() + }.start() +} + fun BaseSimpleActivity.emptyTheRecycleBin(callback: (() -> Unit)? = null) { Thread { filesDir.deleteRecursively() diff --git a/app/src/main/res/menu/menu_viewpager.xml b/app/src/main/res/menu/menu_viewpager.xml index ce071ed9c..5ec3e8338 100644 --- a/app/src/main/res/menu/menu_viewpager.xml +++ b/app/src/main/res/menu/menu_viewpager.xml @@ -44,6 +44,10 @@ android:icon="@drawable/ic_star_on" android:title="@string/remove_from_favorites" app:showAsAction="ifRoom"/> +