scan exlicitly included OTG folders recursively

This commit is contained in:
tibbi 2018-02-20 17:41:22 +01:00
parent 04d42d6772
commit 6ce9ea4b8d
2 changed files with 18 additions and 13 deletions

View file

@ -25,7 +25,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
media.sort() media.sort()
media media
} else { } else {
mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo) mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, false)
} }
} }

View file

@ -19,7 +19,7 @@ class MediaFetcher(val context: Context) {
var shouldStop = false var shouldStop = false
fun getMediaByDirectories(isPickVideo: Boolean, isPickImage: Boolean): HashMap<String, ArrayList<Medium>> { fun getMediaByDirectories(isPickVideo: Boolean, isPickImage: Boolean): HashMap<String, ArrayList<Medium>> {
val media = getFilesFrom("", isPickImage, isPickVideo) val media = getFilesFrom("", isPickImage, isPickVideo, true)
val excludedPaths = context.config.excludedFolders val excludedPaths = context.config.excludedFolders
val includedPaths = context.config.includedFolders val includedPaths = context.config.includedFolders
val showHidden = context.config.shouldShowHidden val showHidden = context.config.shouldShowHidden
@ -74,10 +74,10 @@ class MediaFetcher(val context: Context) {
}.start() }.start()
} }
fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList<Medium> { fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, allowRecursion: Boolean): ArrayList<Medium> {
if (curPath.startsWith(OTG_PATH)) { if (curPath.startsWith(OTG_PATH)) {
val curMedia = ArrayList<Medium>() val curMedia = ArrayList<Medium>()
getMediaOnOTG(curPath, curMedia, isPickImage, isPickVideo, context.config.filterMedia) getMediaOnOTG(curPath, curMedia, isPickImage, isPickVideo, context.config.filterMedia, allowRecursion)
return curMedia return curMedia
} else { } else {
val projection = arrayOf(MediaStore.Images.Media._ID, val projection = arrayOf(MediaStore.Images.Media._ID,
@ -92,7 +92,7 @@ class MediaFetcher(val context: Context) {
return try { return try {
val cur = context.contentResolver.query(uri, projection, selection, selectionArgs, getSortingForFolder(curPath)) val cur = context.contentResolver.query(uri, projection, selection, selectionArgs, getSortingForFolder(curPath))
parseCursor(context, cur, isPickImage, isPickVideo, curPath) parseCursor(context, cur, isPickImage, isPickVideo, curPath, allowRecursion)
} catch (e: Exception) { } catch (e: Exception) {
ArrayList() ArrayList()
} }
@ -131,7 +131,7 @@ class MediaFetcher(val context: Context) {
} }
} }
private fun parseCursor(context: Context, cur: Cursor, isPickImage: Boolean, isPickVideo: Boolean, curPath: String): ArrayList<Medium> { private fun parseCursor(context: Context, cur: Cursor, isPickImage: Boolean, isPickVideo: Boolean, curPath: String, allowRecursion: Boolean): ArrayList<Medium> {
val curMedia = ArrayList<Medium>() val curMedia = ArrayList<Medium>()
val config = context.config val config = context.config
val filterMedia = config.filterMedia val filterMedia = config.filterMedia
@ -200,14 +200,14 @@ class MediaFetcher(val context: Context) {
config.includedFolders.filter { it.isNotEmpty() && (curPath.isEmpty() || it == curPath) }.forEach { config.includedFolders.filter { it.isNotEmpty() && (curPath.isEmpty() || it == curPath) }.forEach {
if (it.startsWith(OTG_PATH)) { if (it.startsWith(OTG_PATH)) {
getMediaOnOTG(it, curMedia, isPickImage, isPickVideo, filterMedia) getMediaOnOTG(it, curMedia, isPickImage, isPickVideo, filterMedia, allowRecursion)
} else { } else {
getMediaInFolder(it, curMedia, isPickImage, isPickVideo, filterMedia) getMediaInFolder(it, curMedia, isPickImage, isPickVideo, filterMedia, allowRecursion)
} }
} }
if (isThirdPartyIntent && curPath.isNotEmpty() && curMedia.isEmpty()) { if (isThirdPartyIntent && curPath.isNotEmpty() && curMedia.isEmpty()) {
getMediaInFolder(curPath, curMedia, isPickImage, isPickVideo, filterMedia) getMediaInFolder(curPath, curMedia, isPickImage, isPickVideo, filterMedia, allowRecursion)
} }
Medium.sorting = config.getFileSorting(curPath) Medium.sorting = config.getFileSorting(curPath)
@ -268,15 +268,15 @@ class MediaFetcher(val context: Context) {
private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>) = private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>) =
includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) } includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) }
private fun getMediaInFolder(folder: String, curMedia: ArrayList<Medium>, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) { private fun getMediaInFolder(folder: String, curMedia: ArrayList<Medium>, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, allowRecursion: Boolean) {
val files = File(folder).listFiles() ?: return val files = File(folder).listFiles() ?: return
for (file in files) { for (file in files) {
if (shouldStop) { if (shouldStop) {
break break
} }
if (file.isDirectory) { if (file.isDirectory && allowRecursion) {
getMediaInFolder(file.absolutePath, curMedia, isPickImage, isPickVideo, filterMedia) getMediaInFolder(file.absolutePath, curMedia, isPickImage, isPickVideo, filterMedia, allowRecursion)
continue continue
} }
@ -319,13 +319,18 @@ class MediaFetcher(val context: Context) {
} }
} }
private fun getMediaOnOTG(folder: String, curMedia: ArrayList<Medium>, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) { private fun getMediaOnOTG(folder: String, curMedia: ArrayList<Medium>, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, allowRecursion: Boolean) {
val files = context.getDocumentFile(folder)?.listFiles() ?: return val files = context.getDocumentFile(folder)?.listFiles() ?: return
for (file in files) { for (file in files) {
if (shouldStop) { if (shouldStop) {
return return
} }
if (file.isDirectory && allowRecursion) {
getMediaOnOTG("$folder${file.name}", curMedia, isPickImage, isPickVideo, filterMedia, allowRecursion)
continue
}
val filename = file.name val filename = file.name
val isImage = filename.isImageFast() val isImage = filename.isImageFast()
val isVideo = if (isImage) false else filename.isVideoFast() val isVideo = if (isImage) false else filename.isVideoFast()