properly scan OTG folders if manually included

This commit is contained in:
tibbi 2018-02-20 00:17:06 +01:00
parent 93ba5d67de
commit a8e87bafcc
2 changed files with 64 additions and 9 deletions

View file

@ -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<Directory>()
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()
}

View file

@ -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<Medium>): HashMap<String, ArrayList<Medium>> {
val directories = LinkedHashMap<String, ArrayList<Medium>>()
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<Medium>, 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<Medium>, 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 {