From a8e87bafcc896df4154e7497886d04ad3b4ba51f Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 20 Feb 2018 00:17:06 +0100 Subject: [PATCH] properly scan OTG folders if manually included --- .../asynctasks/GetDirectoriesAsynctask.kt | 6 +- .../gallery/helpers/MediaFetcher.kt | 67 ++++++++++++++++--- 2 files changed, 64 insertions(+), 9 deletions(-) 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 b6c6bae21..0869a976d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.asynctasks import android.content.Context import android.os.AsyncTask import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.OTG_PATH import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.gallery.R @@ -28,13 +29,15 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val directories = ArrayList() val hidden = context.resources.getString(R.string.hidden) val albumCovers = config.parseAlbumCovers() + val hasOTG = context.hasOTGConnected() && context.config.OTGBasePath.isNotEmpty() + for ((path, curMedia) in groupedMedia) { Medium.sorting = config.getFileSorting(path) curMedia.sort() val firstItem = curMedia.first() val lastItem = curMedia.last() - val parentDir = File(firstItem.path).parent + val parentDir = if (hasOTG && context.isPathOnOTG(firstItem.path)) firstItem.path.getParentPath() else File(firstItem.path).parent var thumbnail = firstItem.path albumCovers.forEach { if (it.path == parentDir && File(it.tmb).exists()) { @@ -45,6 +48,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va var dirName = when (parentDir) { context.internalStoragePath -> context.getString(R.string.internal) context.sdCardPath -> context.getString(R.string.sd_card) + OTG_PATH -> context.getString(R.string.otg) else -> parentDir.getFilenameFromPath() } 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 adb3702e1..b89a58565 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -4,10 +4,7 @@ import android.content.Context import android.database.Cursor import android.provider.MediaStore import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED -import com.simplemobiletools.commons.helpers.SORT_BY_NAME -import com.simplemobiletools.commons.helpers.SORT_BY_SIZE -import com.simplemobiletools.commons.helpers.SORT_DESCENDING +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.containsNoMedia import com.simplemobiletools.gallery.models.Medium @@ -195,7 +192,11 @@ class MediaFetcher(val context: Context) { } config.includedFolders.filter { it.isNotEmpty() && (curPath.isEmpty() || it == curPath) }.forEach { - getMediaInFolder(it, curMedia, isPickImage, isPickVideo, filterMedia) + if (context.isPathOnOTG(it)) { + getMediaOnOTG(it, curMedia, isPickImage, isPickVideo, filterMedia) + } else { + getMediaInFolder(it, curMedia, isPickImage, isPickVideo, filterMedia) + } } if (isThirdPartyIntent && curPath.isNotEmpty() && curMedia.isEmpty()) { @@ -210,11 +211,14 @@ class MediaFetcher(val context: Context) { private fun groupDirectories(media: ArrayList): HashMap> { val directories = LinkedHashMap>() + val hasOTG = context.hasOTGConnected() && context.config.OTGBasePath.isNotEmpty() for (medium in media) { - if (shouldStop) + if (shouldStop) { break + } - val parentDir = File(medium.path).parent?.toLowerCase() ?: continue + val parentDir = (if (hasOTG && medium.path.startsWith(OTG_PATH)) medium.path.getParentPath().toLowerCase() else File(medium.path).parent?.toLowerCase()) + ?: continue if (directories.containsKey(parentDir)) { directories[parentDir]!!.add(medium) } else { @@ -260,8 +264,9 @@ class MediaFetcher(val context: Context) { private fun getMediaInFolder(folder: String, curMedia: ArrayList, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) { val files = File(folder).listFiles() ?: return for (file in files) { - if (shouldStop) + if (shouldStop) { break + } val filename = file.name val isImage = filename.isImageFast() @@ -302,6 +307,52 @@ class MediaFetcher(val context: Context) { } } + private fun getMediaOnOTG(folder: String, curMedia: ArrayList, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) { + val files = context.getDocumentFile(folder)?.listFiles() ?: return + for (file in files) { + if (shouldStop) { + return + } + + val filename = file.name + val isImage = filename.isImageFast() + val isVideo = if (isImage) false else filename.isVideoFast() + val isGif = if (isImage || isVideo) false else filename.isGif() + + if (!isImage && !isVideo && !isGif) + continue + + if (isVideo && (isPickImage || filterMedia and VIDEOS == 0)) + continue + + if (isImage && (isPickVideo || filterMedia and IMAGES == 0)) + continue + + if (isGif && filterMedia and GIFS == 0) + continue + + val size = file.length() + if (size <= 0L && !file.exists()) + continue + + val dateTaken = file.lastModified() + val dateModified = file.lastModified() + + val type = when { + isImage -> TYPE_IMAGE + isVideo -> TYPE_VIDEO + else -> TYPE_GIF + } + + val path = file.uri.toString().replaceFirst("${context.config.OTGBasePath}%3A", OTG_PATH) + val medium = Medium(filename, path, dateModified, dateTaken, size, type) + val isAlreadyAdded = curMedia.any { it.path == path } + if (!isAlreadyAdded) { + curMedia.add(medium) + } + } + } + private fun getSortingForFolder(path: String): String { val sorting = context.config.getFileSorting(path) val sortValue = when {