mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2024-11-26 14:37:59 +01:00
move some media getting functions in Context extensions
This commit is contained in:
parent
fcad1f6fa5
commit
d8811d66b8
3 changed files with 79 additions and 78 deletions
|
@ -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
|
||||
|
|
|
@ -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<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
|
||||
var config = context.config
|
||||
var shouldStop = false
|
||||
private val showHidden = config.shouldShowHidden
|
||||
|
||||
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
||||
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<Directory>()
|
||||
val hidden = context.resources.getString(R.string.hidden)
|
||||
val directories = groupDirectories(media)
|
||||
|
||||
val removePaths = ArrayList<String>()
|
||||
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<Directory>()
|
||||
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<Medium>): HashMap<String, ArrayList<Medium>> {
|
||||
val directories = LinkedHashMap<String, ArrayList<Medium>>()
|
||||
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<String>, includedPaths: MutableSet<String>): 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<String>, includedPaths: MutableSet<String>) =
|
||||
includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) }
|
||||
|
||||
override fun onPostExecute(dirs: ArrayList<Directory>) {
|
||||
super.onPostExecute(dirs)
|
||||
callback(dirs)
|
||||
|
|
|
@ -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<Directory>): ArrayList<
|
|||
return dirs
|
||||
}
|
||||
|
||||
fun Context.getMediaByDirectories(isPickVideo: Boolean, isPickImage: Boolean): HashMap<String, ArrayList<Medium>> {
|
||||
val media = getFilesFrom("", isPickImage, isPickVideo)
|
||||
val excludedPaths = config.excludedFolders
|
||||
val includedPaths = config.includedFolders
|
||||
val showHidden = config.shouldShowHidden
|
||||
val directories = groupDirectories(media)
|
||||
|
||||
val removePaths = ArrayList<String>()
|
||||
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<Medium>): HashMap<String, ArrayList<Medium>> {
|
||||
val directories = LinkedHashMap<String, ArrayList<Medium>>()
|
||||
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<String>, includedPaths: MutableSet<String>, 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<String>, includedPaths: MutableSet<String>) =
|
||||
includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) }
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun Context.getSortedDirectories(source: ArrayList<Directory>): ArrayList<Directory> {
|
||||
Directory.sorting = config.directorySorting
|
||||
|
|
Loading…
Reference in a new issue