mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2024-11-23 04:57:59 +01:00
more attempt to optimize the media fetching process
This commit is contained in:
parent
513694c117
commit
19093b989f
2 changed files with 30 additions and 47 deletions
|
@ -2,9 +2,6 @@ package com.simplemobiletools.gallery.asynctasks
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.AsyncTask
|
import android.os.AsyncTask
|
||||||
import com.simplemobiletools.commons.extensions.isGif
|
|
||||||
import com.simplemobiletools.commons.extensions.isImageFast
|
|
||||||
import com.simplemobiletools.commons.extensions.isVideoFast
|
|
||||||
import com.simplemobiletools.gallery.R
|
import com.simplemobiletools.gallery.R
|
||||||
import com.simplemobiletools.gallery.extensions.config
|
import com.simplemobiletools.gallery.extensions.config
|
||||||
import com.simplemobiletools.gallery.extensions.containsNoMedia
|
import com.simplemobiletools.gallery.extensions.containsNoMedia
|
||||||
|
@ -21,6 +18,13 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
|
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
|
||||||
var config = context.config
|
var config = context.config
|
||||||
|
|
||||||
|
val photoExtensions = arrayOf("jpg", "png", "jpeg", "bmp", "webp", "tiff")
|
||||||
|
val videoExtensions = arrayOf("webm", "mkv", "flv", "vob", "avi", "wmv", "mp4", "ogv", "qt", "m4p", "mpg", "m4v", "mp2", "mpeg", "3gp")
|
||||||
|
|
||||||
|
private fun localIsImage(path: String) = photoExtensions.any { path.endsWith(".$it", true) }
|
||||||
|
private fun localIsGif(path: String) = path.endsWith(".gif", true)
|
||||||
|
private fun localIsVideo(path: String) = videoExtensions.any { path.endsWith(".$it", true) }
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
||||||
val directories = LinkedHashMap<String, Directory>()
|
val directories = LinkedHashMap<String, Directory>()
|
||||||
val media = ArrayList<Medium>()
|
val media = ArrayList<Medium>()
|
||||||
|
@ -28,11 +32,12 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
val fileSorting = config.fileSorting
|
val fileSorting = config.fileSorting
|
||||||
val parents = context.getParents()
|
val parents = context.getParents()
|
||||||
|
|
||||||
parents.mapNotNull { File(it).listFiles() }
|
parents.map {
|
||||||
.forEach {
|
val paths = File(it).list()
|
||||||
for (file in it) {
|
if (paths?.size ?: 0 > 0) {
|
||||||
val isImage = file.isImageFast() || file.isGif()
|
for (path in paths) {
|
||||||
val isVideo = file.isVideoFast()
|
val isImage = localIsGif(path) or localIsImage(path)
|
||||||
|
val isVideo = if (isImage) false else localIsVideo(path)
|
||||||
|
|
||||||
if (!isImage && !isVideo)
|
if (!isImage && !isVideo)
|
||||||
continue
|
continue
|
||||||
|
@ -43,14 +48,16 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
if (isImage && (isPickVideo || showMedia == VIDEOS))
|
if (isImage && (isPickVideo || showMedia == VIDEOS))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
val file = File(it, path)
|
||||||
val size = file.length()
|
val size = file.length()
|
||||||
if (size == 0L)
|
if (size == 0L)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
val name = file.name
|
val name = file.name
|
||||||
val path = file.absolutePath
|
|
||||||
val dateModified = file.lastModified()
|
val dateModified = file.lastModified()
|
||||||
media.add(Medium(name, path, isVideo, dateModified, dateModified, size))
|
val medium = Medium(name, file.absolutePath, false, dateModified, dateModified, size)
|
||||||
|
media.add(medium)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,29 +49,6 @@ fun Context.launchSettings() {
|
||||||
startActivity(Intent(this, SettingsActivity::class.java))
|
startActivity(Intent(this, SettingsActivity::class.java))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getMediaFolders(): Set<String> {
|
|
||||||
val uri = MediaStore.Files.getContentUri("external")
|
|
||||||
val where = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? OR ${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?"
|
|
||||||
val args = arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(), MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
|
|
||||||
val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA)
|
|
||||||
val parentsSet = HashSet<String>()
|
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
|
||||||
try {
|
|
||||||
cursor = contentResolver.query(uri, columns, where, args, null)
|
|
||||||
if (cursor?.moveToFirst() == true) {
|
|
||||||
do {
|
|
||||||
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: ""
|
|
||||||
parentsSet.add(File(curPath).parent)
|
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
return parentsSet
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Context.getParents(): ArrayList<String> {
|
fun Context.getParents(): ArrayList<String> {
|
||||||
val uri = MediaStore.Files.getContentUri("external")
|
val uri = MediaStore.Files.getContentUri("external")
|
||||||
val columns = arrayOf(MediaStore.Images.Media.DATA)
|
val columns = arrayOf(MediaStore.Images.Media.DATA)
|
||||||
|
@ -92,8 +69,7 @@ fun Context.getParents(): ArrayList<String> {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
val mediaFolders = getMediaFolders()
|
parentsSet.mapTo(parents, { it })
|
||||||
parentsSet.filterTo(parents, { mediaFolders.contains(it) || hasImageVideoGif(File(it)) })
|
|
||||||
|
|
||||||
if (config.showHiddenFolders) {
|
if (config.showHiddenFolders) {
|
||||||
parents.addAll(getNoMediaFolders())
|
parents.addAll(getNoMediaFolders())
|
||||||
|
|
Loading…
Reference in a new issue