From 13a3f90595dc76c13ba50c2becf5dd41117d430d Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 10 Jan 2020 22:30:21 +0100 Subject: [PATCH] optimize Portrait photo checking for better performance --- .../gallery/pro/helpers/MediaFetcher.kt | 62 +++++++------------ 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 965771e9c..c9cada42d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -214,7 +214,6 @@ class MediaFetcher(val context: Context) { private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean): ArrayList { val media = ArrayList() - val isRecycleBin = folder == RECYCLE_BIN val deletedMedia = if (isRecycleBin) { context.getUpdatedDeletedMedia(context.galleryDB.MediumDao()) @@ -229,54 +228,39 @@ class MediaFetcher(val context: Context) { val showPortraits = filterMedia and TYPE_PORTRAITS != 0 val dateTakens = if (getProperDateTaken && folder != FAVORITES && !isRecycleBin) getFolderDateTakens(folder) else HashMap() - // used only for Portrait photos starting with "IMG_" for now - val subdirs = ArrayList() - val covers = ArrayList() - val files = when (folder) { FAVORITES -> favoritePaths.filter { showHidden || !it.contains("/.") }.map { File(it) }.toMutableList() as ArrayList RECYCLE_BIN -> deletedMedia.map { File(it.path) }.toMutableList() as ArrayList - else -> { - val allFiles = File(folder).listFiles() ?: return media - val notDirs = ArrayList() - allFiles.forEach { - if (it.isDirectory) { - if (showPortraits && it.name.startsWith("img_", true)) { - subdirs.add(it) - } - } else { - notDirs.add(it) - } - } - - notDirs - } + else -> File(folder).listFiles()?.toMutableList() ?: return media } - for (subdir in subdirs) { - val portraitFiles = subdir.listFiles() ?: continue - val cover = portraitFiles.firstOrNull { it.name.contains("cover", true) } ?: portraitFiles.firstOrNull() - if (cover != null) { - files.add(cover) - covers.add(cover.absolutePath) - } - } - - for (file in files) { + for (curFile in files) { + var file = curFile if (shouldStop) { break } - val path = file.absolutePath - val isPortrait = covers.contains(path) - val isImage = if (isPortrait) false else path.isImageFast() - val isVideo = if (isPortrait || isImage) false else path.isVideoFast() - val isGif = if (isPortrait || isImage || isVideo) false else path.isGif() - val isRaw = if (isPortrait || isImage || isVideo || isGif) false else path.isRawFast() - val isSvg = if (isPortrait || isImage || isVideo || isGif || isRaw) false else path.isSvg() + var path = file.absolutePath + var isPortrait = false + val isImage = path.isImageFast() + val isVideo = if (isImage) false else path.isVideoFast() + val isGif = if (isImage || isVideo) false else path.isGif() + val isRaw = if (isImage || isVideo || isGif) false else path.isRawFast() + val isSvg = if (isImage || isVideo || isGif || isRaw) false else path.isSvg() - if (!isPortrait && !isImage && !isVideo && !isGif && !isRaw && !isSvg) - continue + if (!isImage && !isVideo && !isGif && !isRaw && !isSvg) { + if (showPortraits && file.isDirectory && file.name.startsWith("img_", true)) { + val portraitFiles = file.listFiles() ?: continue + val cover = portraitFiles.firstOrNull { it.name.contains("cover", true) } ?: portraitFiles.firstOrNull() + if (cover != null && !files.contains(cover)) { + file = cover + path = cover.absolutePath + isPortrait = true + } + } else { + continue + } + } if (isVideo && (isPickImage || filterMedia and TYPE_VIDEOS == 0)) continue