From d8811d66b80310769680c210e2c9381acf7fbe3f Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 25 Sep 2017 23:19:17 +0200 Subject: [PATCH] move some media getting functions in Context extensions --- .../gallery/activities/MainActivity.kt | 1 - .../asynctasks/GetDirectoriesAsynctask.kt | 83 ++----------------- .../gallery/extensions/context.kt | 73 ++++++++++++++++ 3 files changed, 79 insertions(+), 78 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 220d5e4b9..35deec3c1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -146,7 +146,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { override fun onPause() { super.onPause() - mCurrAsyncTask?.shouldStop = true storeDirectories() directories_refresh_layout.isRefreshing = false mIsGettingDirs = false diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 80d629d32..a6450aac1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -10,45 +10,25 @@ import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.containsNoMedia -import com.simplemobiletools.gallery.extensions.getFilesFrom +import com.simplemobiletools.gallery.extensions.getMediaByDirectories import com.simplemobiletools.gallery.extensions.sumByLong import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File -import java.util.* class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean, val callback: (dirs: ArrayList) -> Unit) : AsyncTask>() { - var config = context.config - var shouldStop = false - private val showHidden = config.shouldShowHidden override fun doInBackground(vararg params: Void): ArrayList { if (!context.hasWriteStoragePermission()) return ArrayList() - val media = context.getFilesFrom("", isPickImage, isPickVideo) - val excludedPaths = config.excludedFolders - val includedPaths = config.includedFolders + val config = context.config + val groupedMedia = context.getMediaByDirectories(isPickVideo, isPickImage) + val directories = ArrayList() val hidden = context.resources.getString(R.string.hidden) - val directories = groupDirectories(media) - - val removePaths = ArrayList() - for ((path, curMedia) in directories) { - // make sure the path has uppercase letters wherever appropriate - val groupPath = File(curMedia.first().path).parent - if (!File(groupPath).exists() || !shouldFolderBeVisible(groupPath, excludedPaths, includedPaths)) { - removePaths.add(groupPath.toLowerCase()) - } - } - - removePaths.forEach { - directories.remove(it) - } - - val dirs = ArrayList() val albumCovers = config.parseAlbumCovers() - for ((path, curMedia) in directories) { + for ((path, curMedia) in groupedMedia) { Medium.sorting = config.getFileSorting(path) curMedia.sort() @@ -76,63 +56,12 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken) val size = curMedia.sumByLong { it.size } val directory = Directory(parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size) - dirs.add(directory) + directories.add(directory) } - return dirs - } - - private fun groupDirectories(media: ArrayList): HashMap> { - val directories = LinkedHashMap>() - for (medium in media) { - if (shouldStop) { - cancel(true) - break - } - - val parentDir = File(medium.path).parent?.toLowerCase() ?: continue - if (directories.containsKey(parentDir)) { - directories[parentDir]!!.add(medium) - } else { - directories.put(parentDir, arrayListOf(medium)) - } - } return directories } - private fun shouldFolderBeVisible(path: String, excludedPaths: MutableSet, includedPaths: MutableSet): Boolean { - val file = File(path) - return if (includedPaths.contains(path)) { - true - } else if (isThisOrParentExcluded(path, excludedPaths, includedPaths)) { - false - } else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) { - var containsNoMediaOrDot = file.containsNoMedia() || path.contains("/.") - if (!containsNoMediaOrDot) { - containsNoMediaOrDot = checkParentHasNoMedia(file.parentFile) - } - !containsNoMediaOrDot - } else { - true - } - } - - private fun checkParentHasNoMedia(file: File): Boolean { - var curFile = file - while (true) { - if (curFile.containsNoMedia()) { - return true - } - curFile = curFile.parentFile - if (curFile.absolutePath == "/") - break - } - return false - } - - private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet, includedPaths: MutableSet) = - includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) } - override fun onPostExecute(dirs: ArrayList) { super.onPostExecute(dirs) callback(dirs) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index e6ae6d99e..095d8f348 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -20,6 +20,10 @@ import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File +import java.util.LinkedHashMap +import kotlin.collections.ArrayList +import kotlin.collections.component1 +import kotlin.collections.component2 val Context.portrait get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager @@ -301,6 +305,75 @@ fun Context.movePinnedDirectoriesToFront(dirs: ArrayList): ArrayList< return dirs } +fun Context.getMediaByDirectories(isPickVideo: Boolean, isPickImage: Boolean): HashMap> { + val media = getFilesFrom("", isPickImage, isPickVideo) + val excludedPaths = config.excludedFolders + val includedPaths = config.includedFolders + val showHidden = config.shouldShowHidden + val directories = groupDirectories(media) + + val removePaths = ArrayList() + for ((path, curMedia) in directories) { + // make sure the path has uppercase letters wherever appropriate + val groupPath = File(curMedia.first().path).parent + if (!File(groupPath).exists() || !shouldFolderBeVisible(groupPath, excludedPaths, includedPaths, showHidden)) { + removePaths.add(groupPath.toLowerCase()) + } + } + + removePaths.forEach { + directories.remove(it) + } + + return directories +} + +private fun groupDirectories(media: ArrayList): HashMap> { + val directories = LinkedHashMap>() + for (medium in media) { + val parentDir = File(medium.path).parent?.toLowerCase() ?: continue + if (directories.containsKey(parentDir)) { + directories[parentDir]!!.add(medium) + } else { + directories.put(parentDir, arrayListOf(medium)) + } + } + return directories +} + +private fun shouldFolderBeVisible(path: String, excludedPaths: MutableSet, includedPaths: MutableSet, showHidden: Boolean): Boolean { + val file = File(path) + return if (includedPaths.contains(path)) { + true + } else if (isThisOrParentExcluded(path, excludedPaths, includedPaths)) { + false + } else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) { + var containsNoMediaOrDot = file.containsNoMedia() || path.contains("/.") + if (!containsNoMediaOrDot) { + containsNoMediaOrDot = checkParentHasNoMedia(file.parentFile) + } + !containsNoMediaOrDot + } else { + true + } +} + +private fun checkParentHasNoMedia(file: File): Boolean { + var curFile = file + while (true) { + if (curFile.containsNoMedia()) { + return true + } + curFile = curFile.parentFile + if (curFile.absolutePath == "/") + break + } + return false +} + +private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet, includedPaths: MutableSet) = + includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) } + @Suppress("UNCHECKED_CAST") fun Context.getSortedDirectories(source: ArrayList): ArrayList { Directory.sorting = config.directorySorting