From fb493459dea8cee32d5afe7be70fbe14d5d8417e Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 19 Feb 2018 20:24:32 +0100 Subject: [PATCH] update commons to 3.12.6 --- app/build.gradle | 2 +- .../gallery/activities/EditActivity.kt | 6 +- .../gallery/activities/MainActivity.kt | 8 ++- .../gallery/activities/MediaActivity.kt | 16 +++--- .../gallery/activities/PhotoVideoActivity.kt | 13 +++-- .../gallery/activities/ViewPagerActivity.kt | 53 +++++++++--------- .../gallery/adapters/DirectoryAdapter.kt | 21 ++++--- .../adapters/ManageHiddenFoldersAdapter.kt | 3 +- .../gallery/adapters/MediaAdapter.kt | 44 ++++++++------- .../gallery/dialogs/ChangeSortingDialog.kt | 2 +- .../gallery/extensions/Activity.kt | 56 +++++++++---------- .../gallery/extensions/FileDirItem.kt | 6 ++ .../gallery/fragments/ViewPagerFragment.kt | 2 +- .../gallery/helpers/Config.kt | 8 +-- .../gallery/helpers/Constants.kt | 1 - .../gallery/helpers/MediaFetcher.kt | 3 +- .../gallery/models/Medium.kt | 4 +- app/src/main/res/values/integers.xml | 2 + 18 files changed, 130 insertions(+), 120 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/extensions/FileDirItem.kt diff --git a/app/build.gradle b/app/build.gradle index 31a0c64e4..40875409d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,7 +46,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.11.15' + implementation 'com.simplemobiletools:commons:3.12.6' implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0' implementation 'com.android.support:multidex:1.0.2' implementation 'com.google.code.gson:gson:2.8.2' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index c1408b2c9..628f66457 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -12,6 +12,7 @@ import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.dialogs.ResizeDialog import com.simplemobiletools.gallery.dialogs.SaveAsDialog @@ -191,7 +192,8 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener try { Thread { val file = File(path) - getFileOutputStream(file) { + val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) + getFileOutputStream(fileDirItem) { if (it != null) { saveBitmap(file, bitmap, it) } else { @@ -227,7 +229,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } private fun editWith() { - openEditor(uri) + openEditor(uri.toString()) isEditingWithThirdParty = true } 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 ecbf4c525..e3361fff0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -20,6 +20,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.views.MyGridLayoutManager @@ -233,7 +234,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val newFolder = File(config.tempFolderPath) if (newFolder.exists() && newFolder.isDirectory) { if (newFolder.list()?.isEmpty() == true) { - deleteFile(newFolder, true) + deleteFile(newFolder.toFileDirItem(applicationContext), true) } } config.tempFolderPath = "" @@ -352,7 +353,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } override fun deleteFolders(folders: ArrayList) { - deleteFolders(folders) { + val fileDirItems = folders.map { FileDirItem(it.absolutePath, it.name, true) } as ArrayList + deleteFolders(fileDirItems) { runOnUiThread { refreshItems() } @@ -522,7 +524,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun fillIntentPath(resultData: Intent, resultIntent: Intent) { val path = resultData.data.path val uri = getFilePublicUri(File(path), BuildConfig.APPLICATION_ID) - val type = path.getMimeTypeFromPath() + val type = path.getMimeType() resultIntent.setDataAndTypeAndNormalize(uri, type) resultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } 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 9ce048b91..87b35c151 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -26,6 +26,7 @@ import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.REQUEST_EDIT_IMAGE +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.views.MyGridLayoutManager import com.simplemobiletools.commons.views.MyRecyclerView @@ -442,9 +443,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun deleteDirectoryIfEmpty() { - val file = File(mPath) - if (config.deleteEmptyFolders && !file.isDownloadsFolder() && file.isDirectory && file.list()?.isEmpty() == true) { - deleteFile(file, true) + val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath()) + if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { + deleteFile(fileDirItem, true) } } @@ -610,10 +611,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } finish() } else { - val file = File(path) - val isVideo = file.isVideoFast() + val isVideo = path.isVideoFast() if (isVideo) { - openFile(Uri.fromFile(file), false) + openPath(path, false) } else { Intent(this, ViewPagerActivity::class.java).apply { putExtra(PATH, path) @@ -666,8 +666,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } - override fun deleteFiles(files: ArrayList) { - val filtered = files.filter { it.isImageVideoGif() } as ArrayList + override fun deleteFiles(fileDirItems: ArrayList) { + val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList deleteFiles(filtered) { if (!it) { toast(R.string.unknown_error_occurred) 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 602154896..1eee71390 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -59,7 +59,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList private fun checkIntent(savedInstanceState: Bundle? = null) { mUri = intent.data ?: return if (intent.extras?.containsKey(REAL_FILE_PATH) == true) { - mUri = intent.extras.get(REAL_FILE_PATH) as Uri + mUri = Uri.parse(intent.extras.get(REAL_FILE_PATH) as String) } mIsFromGallery = intent.getBooleanExtra(IS_FROM_GALLERY, false) @@ -131,14 +131,15 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList } override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (mMedium == null) + if (mMedium == null) { return true + } when (item.itemId) { - R.id.menu_set_as -> setAs(mUri!!) - R.id.menu_open_with -> openFile(mUri!!, true) - R.id.menu_share -> shareUri(mUri!!) - R.id.menu_edit -> openEditor(mUri!!) + R.id.menu_set_as -> setAs(mUri!!.toString()) + R.id.menu_open_with -> openPath(mUri!!.toString(), true) + R.id.menu_share -> sharePath(mUri!!.toString()) + R.id.menu_edit -> openEditor(mUri!!.toString()) R.id.menu_properties -> showProperties() else -> return super.onOptionsItemSelected(item) } 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 80e41d7c5..89e25594d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -33,6 +33,7 @@ import com.simplemobiletools.commons.helpers.IS_FROM_GALLERY import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.REQUEST_EDIT_IMAGE import com.simplemobiletools.commons.helpers.REQUEST_SET_AS +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MyPagerAdapter import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask @@ -180,10 +181,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View return } - val file = File(mPath) - if (!file.exists() && file.length() == 0L) { + if (!getDoesFilePathExist(mPath)) { Thread { - deleteFromMediaStore(file) + deleteFromMediaStore(mPath) }.start() finish() return @@ -201,7 +201,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View showSystemUI() - mDirectory = file.parent + mDirectory = mPath.getParentPath().trimEnd('/') supportActionBar?.title = mPath.getFilenameFromPath() view_pager.onGlobalLayout { @@ -272,18 +272,18 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View return true when (item.itemId) { - R.id.menu_set_as -> setAs(Uri.fromFile(getCurrentFile())) + R.id.menu_set_as -> setAs(getCurrentPath()) R.id.menu_slideshow -> initSlideshow() R.id.menu_copy_to -> copyMoveTo(true) R.id.menu_move_to -> copyMoveTo(false) - R.id.menu_open_with -> openFile(Uri.fromFile(getCurrentFile()), true) + R.id.menu_open_with -> openPath(getCurrentPath(), true) R.id.menu_hide -> toggleFileVisibility(true) R.id.menu_unhide -> toggleFileVisibility(false) R.id.menu_share_1 -> shareMedium(getCurrentMedium()!!) R.id.menu_share_2 -> shareMedium(getCurrentMedium()!!) R.id.menu_delete -> checkDeleteConfirmation() R.id.menu_rename -> renameFile() - R.id.menu_edit -> openEditor(Uri.fromFile(getCurrentFile())) + R.id.menu_edit -> openEditor(getCurrentPath()) R.id.menu_properties -> showProperties() R.id.menu_show_on_map -> showOnMap() R.id.menu_rotate_right -> rotateImage(90) @@ -462,8 +462,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun copyMoveTo(isCopyOperation: Boolean) { - val files = ArrayList(1).apply { add(getCurrentFile()) } - tryCopyMoveFilesTo(files, isCopyOperation) { + val currPath = getCurrentPath() + val fileDirItems = arrayListOf(FileDirItem(currPath, currPath.getFilenameFromPath())) + tryCopyMoveFilesTo(fileDirItems, isCopyOperation) { config.tempFolderPath = "" if (!isCopyOperation) { refreshViewPager() @@ -472,13 +473,13 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun toggleFileVisibility(hide: Boolean) { - toggleFileVisibility(getCurrentFile(), hide) { - val newFileName = it.absolutePath.getFilenameFromPath() + toggleFileVisibility(getCurrentPath(), hide) { + val newFileName = it.getFilenameFromPath() supportActionBar?.title = newFileName getCurrentMedium()!!.apply { name = newFileName - path = it.absolutePath + path = it getCurrentMedia()[mPos] = this } invalidateOptionsMenu() @@ -508,8 +509,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun saveImageAs() { val currPath = getCurrentPath() SaveAsDialog(this, currPath, false) { - val selectedFile = File(it) - handleSAFDialog(selectedFile) { + handleSAFDialog(it) { Thread { saveImageToFile(currPath, it) }.start() @@ -523,7 +523,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val tmpFile = File(filesDir, ".tmp_${newPath.getFilenameFromPath()}") try { val bitmap = BitmapFactory.decodeFile(oldPath) - getFileOutputStream(tmpFile) { + getFileOutputStream(tmpFile.toFileDirItem(applicationContext)) { if (it == null) { toast(R.string.unknown_error_occurred) return@getFileOutputStream @@ -536,16 +536,16 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View saveFile(tmpFile, bitmap, it as FileOutputStream) } - if (tmpFile.length() > 0 && newFile.exists()) { - deleteFile(newFile) + if (tmpFile.length() > 0 && getDoesFilePathExist(newPath)) { + deleteFile(FileDirItem(newPath, newPath.getFilenameFromPath())) } copyFile(tmpFile, newFile) - scanFile(newFile) + scanPath(newPath) toast(R.string.file_saved) if (config.keepLastModified) { newFile.setLastModified(oldLastModified) - updateLastModified(newFile, oldLastModified) + updateLastModified(newPath, oldLastModified) } it.flush() @@ -565,7 +565,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } catch (e: Exception) { showErrorToast(e) } finally { - deleteFile(tmpFile) + deleteFile(FileDirItem(newPath, newPath.getFilenameFromPath())) } } @@ -573,7 +573,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View var inputStream: InputStream? = null var out: OutputStream? = null try { - val fileDocument = if (isPathOnSD(destination.absolutePath)) getFileDocument(destination.parent) else null + val fileDocument = if (isPathOnSD(destination.absolutePath)) getDocumentFile(destination.parent) else null out = getFileOutputStreamSync(destination.absolutePath, source.getMimeType(), fileDocument) inputStream = FileInputStream(source) inputStream.copyTo(out!!) @@ -735,7 +735,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun deleteConfirmed() { - deleteFile(File(getCurrentMedia()[mPos].path)) { + val path = getCurrentMedia()[mPos].path + deleteFile(FileDirItem(path, path.getFilenameFromPath())) { refreshViewPager() } } @@ -811,9 +812,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun deleteDirectoryIfEmpty() { - val file = File(mDirectory) - if (config.deleteEmptyFolders && !file.isDownloadsFolder() && file.isDirectory && file.list()?.isEmpty() == true) { - deleteFile(file, true) + val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory)) + if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { + deleteFile(fileDirItem, true) } scanPath(mDirectory) @@ -821,7 +822,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun checkOrientation() { if (!mIsOrientationLocked && config.screenRotation == ROTATE_BY_ASPECT_RATIO) { - val res = getCurrentFile().getResolution() + val res = getCurrentPath().getResolution() ?: return if (res.x > res.y) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE } else if (res.x < res.y) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index f6efe8972..cc3bde953 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.R @@ -76,6 +77,10 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList showProperties() R.id.cab_rename -> renameDir() @@ -213,16 +218,14 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList() + val paths = ArrayList() selectedPositions.forEach { val dir = File(dirs[it].path) - files.addAll(dir.listFiles().filter { it.isFile && it.isImageVideoGif() }) + paths.addAll(dir.list().filter { !activity.getIsPathDirectory(it) && it.isImageVideoGif() }) } - activity.tryCopyMoveFilesTo(files, isCopyOperation) { + val fileDirItems = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList + activity.tryCopyMoveFilesTo(fileDirItems, isCopyOperation) { config.tempFolderPath = "" listener?.refreshItems() finishActMode() @@ -236,10 +239,6 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList(selectedPositions.size) val removeFolders = ArrayList(selectedPositions.size) @@ -253,7 +252,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList, val listener: RefreshRecyclerViewListener?, @@ -70,7 +69,7 @@ class ManageHiddenFoldersAdapter(activity: BaseSimpleActivity, var folders: Arra } if (sdCardPaths.isNotEmpty()) { - activity.handleSAFDialog(File(sdCardPaths.first())) { + activity.handleSAFDialog(sdCardPaths.first()) { unhideFolders(removeFolders) } } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index e54fd102c..fc07bcca2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.gallery.adapters -import android.net.Uri import android.os.Handler import android.os.Looper import android.view.Menu @@ -13,7 +12,9 @@ import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.beVisibleIf +import com.simplemobiletools.commons.extensions.getFilenameFromPath import com.simplemobiletools.commons.extensions.isActivityDestroyed +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.R @@ -23,7 +24,6 @@ import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.photo_video_item_grid.view.* import java.io.File -import java.util.* class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, fastScroller: FastScroller? = null, @@ -86,6 +86,10 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, } override fun actionItemPressed(id: Int) { + if (selectedPositions.isEmpty()) { + return + } + when (id) { R.id.cab_confirm_selection -> confirmSelection() R.id.cab_properties -> showProperties() @@ -97,8 +101,8 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, R.id.cab_copy_to -> copyMoveTo(true) R.id.cab_move_to -> copyMoveTo(false) R.id.cab_select_all -> selectAll() - R.id.cab_open_with -> activity.openFile(Uri.fromFile(getCurrentFile()), true) - R.id.cab_set_as -> activity.setAs(Uri.fromFile(getCurrentFile())) + R.id.cab_open_with -> activity.openPath(getCurrentPath(), true) + R.id.cab_set_as -> activity.setAs(getCurrentPath()) R.id.cab_delete -> checkDeleteConfirmation() } } @@ -145,7 +149,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, } private fun renameFile() { - RenameItemDialog(activity, getCurrentFile().absolutePath) { + RenameItemDialog(activity, getCurrentPath()) { activity.runOnUiThread { listener?.refreshItems() finishActMode() @@ -154,15 +158,14 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, } private fun editFile() { - activity.openEditor(Uri.fromFile(getCurrentFile())) + activity.openEditor(getCurrentPath()) finishActMode() } private fun toggleFileVisibility(hide: Boolean) { Thread { getSelectedMedia().forEach { - val oldFile = File(it.path) - activity.toggleFileVisibility(oldFile, hide) + activity.toggleFileVisibility(it.path, hide) } activity.runOnUiThread { listener?.refreshItems() @@ -180,10 +183,11 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, } private fun copyMoveTo(isCopyOperation: Boolean) { - val files = ArrayList() - selectedPositions.forEach { files.add(File(media[it].path)) } + val paths = ArrayList() + selectedPositions.forEach { paths.add(media[it].path) } - activity.tryCopyMoveFilesTo(files, isCopyOperation) { + val fileDirItems = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList + activity.tryCopyMoveFilesTo(fileDirItems, isCopyOperation) { config.tempFolderPath = "" if (!isCopyOperation) { listener?.refreshItems() @@ -211,14 +215,12 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, deleteFiles() } - private fun getCurrentFile() = File(media[selectedPositions.first()].path) + private fun getCurrentPath() = media[selectedPositions.first()].path + + private fun getCurrentFile() = File(getCurrentPath()) private fun deleteFiles() { - if (selectedPositions.isEmpty()) { - return - } - - val files = ArrayList(selectedPositions.size) + val fileDirItems = ArrayList(selectedPositions.size) val removeMedia = ArrayList(selectedPositions.size) if (media.size <= selectedPositions.first()) { @@ -227,15 +229,15 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, } val SAFPath = media[selectedPositions.first()].path - activity.handleSAFDialog(File(SAFPath)) { + activity.handleSAFDialog(SAFPath) { selectedPositions.sortedDescending().forEach { val medium = media[it] - files.add(File(medium.path)) + fileDirItems.add(FileDirItem(medium.path, medium.name)) removeMedia.add(medium) } media.removeAll(removeMedia) - listener?.deleteFiles(files) + listener?.deleteFiles(fileDirItems) removeSelectedItems() } } @@ -314,7 +316,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, interface MediaOperationsListener { fun refreshItems() - fun deleteFiles(files: ArrayList) + fun deleteFiles(fileDirItems: ArrayList) fun selectedPaths(paths: ArrayList) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt index d83d10237..efbf4fe0e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt @@ -81,7 +81,7 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, val isDirectorySorti config.saveFileSorting(path, sorting) } else { config.removeFileSorting(path) - config.fileSorting = sorting + config.sorting = sorting } } callback() 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 dd8630889..77ad06b56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -2,7 +2,6 @@ package com.simplemobiletools.gallery.extensions import android.app.Activity import android.content.Intent -import android.net.Uri import android.provider.MediaStore import android.support.v7.app.AppCompatActivity import android.view.View @@ -17,6 +16,7 @@ import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity @@ -32,34 +32,33 @@ import pl.droidsonroids.gif.GifDrawable import java.io.File import java.util.* -fun Activity.shareUri(uri: Uri) { - shareUri(uri, BuildConfig.APPLICATION_ID) +fun Activity.sharePath(path: String) { + sharePathIntent(path, BuildConfig.APPLICATION_ID) } -fun Activity.shareUris(uris: ArrayList) { - shareUris(uris, BuildConfig.APPLICATION_ID) +fun Activity.sharePaths(paths: ArrayList) { + sharePathsIntent(paths, BuildConfig.APPLICATION_ID) } fun Activity.shareMedium(medium: Medium) { - val file = File(medium.path) - shareUri(Uri.fromFile(file)) + sharePath(medium.path) } fun Activity.shareMedia(media: List) { - val uris = media.map { Uri.fromFile(File(it.path)) } as ArrayList - shareUris(uris) + val paths = media.map { it.path } as ArrayList + sharePaths(paths) } -fun Activity.setAs(uri: Uri) { - setAs(uri, BuildConfig.APPLICATION_ID) +fun Activity.setAs(path: String) { + setAsIntent(path, BuildConfig.APPLICATION_ID) } -fun Activity.openFile(uri: Uri, forceChooser: Boolean) { - openFile(uri, forceChooser, BuildConfig.APPLICATION_ID) +fun Activity.openPath(path: String, forceChooser: Boolean) { + openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID) } -fun Activity.openEditor(uri: Uri) { - openEditor(uri, BuildConfig.APPLICATION_ID) +fun Activity.openEditor(path: String) { + openEditorIntent(path, BuildConfig.APPLICATION_ID) } fun Activity.launchCamera() { @@ -100,8 +99,8 @@ fun BaseSimpleActivity.addNoMedia(path: String, callback: () -> Unit) { return if (needsStupidWritePermissions(path)) { - handleSAFDialog(file) { - val fileDocument = getFileDocument(path) + handleSAFDialog(file.absolutePath) { + val fileDocument = getDocumentFile(path) if (fileDocument?.exists() == true && fileDocument.isDirectory) { fileDocument.createFile("", NOMEDIA) } else { @@ -123,22 +122,23 @@ fun BaseSimpleActivity.addNoMedia(path: String, callback: () -> Unit) { fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = null) { val file = File(path, NOMEDIA) - deleteFile(file) { + deleteFile(file.toFileDirItem(applicationContext)) { callback?.invoke() } } -fun BaseSimpleActivity.toggleFileVisibility(oldFile: File, hide: Boolean, callback: ((newFile: File) -> Unit)? = null) { - val path = oldFile.parent - var filename = oldFile.name +fun BaseSimpleActivity.toggleFileVisibility(oldPath: String, hide: Boolean, callback: ((newPath: String) -> Unit)? = null) { + val path = oldPath.getParentPath() + var filename = oldPath.getFilenameFromPath() filename = if (hide) { ".${filename.trimStart('.')}" } else { filename.substring(1, filename.length) } - val newFile = File(path, filename) - renameFile(oldFile, newFile) { - callback?.invoke(newFile) + + val newPath = "$path/$filename" + renameFile(oldPath, newPath) { + callback?.invoke(newPath) } } @@ -169,15 +169,15 @@ fun Activity.loadImage(type: Int, path: String, target: MySquareImageView, horiz } } -fun BaseSimpleActivity.tryCopyMoveFilesTo(files: ArrayList, isCopyOperation: Boolean, callback: () -> Unit) { - if (files.isEmpty()) { +fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList, isCopyOperation: Boolean, callback: () -> Unit) { + if (fileDirItems.isEmpty()) { toast(R.string.unknown_error_occurred) return } - val source = if (files[0].isFile) files[0].parent else files[0].absolutePath + val source = fileDirItems[0].getParentPath() PickDirectoryDialog(this, source) { - copyMoveFilesTo(files, source.trimEnd('/'), it, isCopyOperation, true, config.shouldShowHidden, callback) + copyMoveFilesTo(fileDirItems, source.trimEnd('/'), it, isCopyOperation, true, config.shouldShowHidden, callback) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/FileDirItem.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/FileDirItem.kt new file mode 100644 index 000000000..b882a9bb7 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/FileDirItem.kt @@ -0,0 +1,6 @@ +package com.simplemobiletools.gallery.extensions + +import android.os.Environment +import com.simplemobiletools.commons.models.FileDirItem + +fun FileDirItem.isDownloadsFolder() = path == Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/ViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/ViewPagerFragment.kt index 8cb977537..fecbcc23d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/ViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/ViewPagerFragment.kt @@ -45,7 +45,7 @@ abstract class ViewPagerFragment : Fragment() { } if (detailsFlag and EXT_RESOLUTION != 0) { - file.getResolution().formatAsResolution().let { if (it.isNotEmpty()) details.appendln(it) } + file.absolutePath.getResolution()?.formatAsResolution().let { if (it?.isNotEmpty() == true) details.appendln(it) } } if (detailsFlag and EXT_LAST_MODIFIED != 0) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index 333937de6..e8a691f53 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -16,23 +16,19 @@ class Config(context: Context) : BaseConfig(context) { fun newInstance(context: Context) = Config(context) } - var fileSorting: Int - get() = prefs.getInt(SORT_ORDER, SORT_BY_DATE_MODIFIED or SORT_DESCENDING) - set(order) = prefs.edit().putInt(SORT_ORDER, order).apply() - var directorySorting: Int get() = prefs.getInt(DIRECTORY_SORT_ORDER, SORT_BY_DATE_MODIFIED or SORT_DESCENDING) set(order) = prefs.edit().putInt(DIRECTORY_SORT_ORDER, order).apply() fun saveFileSorting(path: String, value: Int) { if (path.isEmpty()) { - fileSorting = value + sorting = value } else { prefs.edit().putInt(SORT_FOLDER_PREFIX + path.toLowerCase(), value).apply() } } - fun getFileSorting(path: String) = prefs.getInt(SORT_FOLDER_PREFIX + path.toLowerCase(), fileSorting) + fun getFileSorting(path: String) = prefs.getInt(SORT_FOLDER_PREFIX + path.toLowerCase(), sorting) fun removeFileSorting(path: String) { prefs.edit().remove(SORT_FOLDER_PREFIX + path.toLowerCase()).apply() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index b4ecd1a6d..9d30dd61f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.gallery.helpers // shared preferences -const val SORT_ORDER = "sort_order" const val DIRECTORY_SORT_ORDER = "directory_sort_order" const val SORT_FOLDER_PREFIX = "sort_folder_" const val SHOW_HIDDEN_MEDIA = "show_hidden_media" 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 6df8fc016..3621bb8b6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -138,8 +138,9 @@ class MediaFetcher(val context: Context) { if (cur.moveToFirst()) { do { try { - if (shouldStop) + if (shouldStop) { break + } val path = cur.getStringValue(MediaStore.Images.Media.DATA).trim() var filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME)?.trim() ?: "" 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 fb9e7a213..f00aedda9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.gallery.models import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatSize -import com.simplemobiletools.commons.extensions.getMimeTypeFromPath +import com.simplemobiletools.commons.extensions.getMimeType import com.simplemobiletools.commons.extensions.isDng import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.gallery.helpers.TYPE_GIF @@ -24,7 +24,7 @@ data class Medium(var name: String, var path: String, val modified: Long, val ta fun isDng() = path.isDng() - fun getMimeType() = path.getMimeTypeFromPath() + fun getMimeType() = path.getMimeType() override fun compareTo(other: Medium): Int { var result: Int diff --git a/app/src/main/res/values/integers.xml b/app/src/main/res/values/integers.xml index e27e3e8f5..1ba77d374 100644 --- a/app/src/main/res/values/integers.xml +++ b/app/src/main/res/values/integers.xml @@ -3,4 +3,6 @@ 3 3 4 + + 1026