refactoring MediaFetcher once again
This commit is contained in:
parent
cfa2f82066
commit
8399b4fccd
|
@ -30,33 +30,36 @@ class MediaFetcher(val context: Context) {
|
||||||
return ArrayList()
|
return ArrayList()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val curMedia = ArrayList<Medium>()
|
||||||
if (curPath.startsWith(OTG_PATH)) {
|
if (curPath.startsWith(OTG_PATH)) {
|
||||||
val curMedia = ArrayList<Medium>()
|
|
||||||
getMediaOnOTG(curPath, curMedia, isPickImage, isPickVideo, filterMedia)
|
getMediaOnOTG(curPath, curMedia, isPickImage, isPickVideo, filterMedia)
|
||||||
return curMedia
|
|
||||||
} else {
|
} else {
|
||||||
val projection = arrayOf(MediaStore.Images.Media.DATA)
|
val foldersToScan = getFoldersToScan(curPath)
|
||||||
val uri = MediaStore.Files.getContentUri("external")
|
|
||||||
|
|
||||||
val selection = "${getSelectionQuery(curPath, filterMedia)} ${MediaStore.Images.ImageColumns.BUCKET_ID} IS NOT NULL) GROUP BY (${MediaStore.Images.ImageColumns.BUCKET_ID}"
|
|
||||||
val selectionArgs = getSelectionArgsQuery(curPath, filterMedia).toTypedArray()
|
|
||||||
|
|
||||||
return try {
|
|
||||||
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
|
||||||
val curMedia = ArrayList<Medium>()
|
|
||||||
val foldersToScan = getFoldersToScan(context, cursor, curPath)
|
|
||||||
foldersToScan.forEach {
|
foldersToScan.forEach {
|
||||||
fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia)
|
fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Medium.sorting = context.config.getFileSorting(curPath)
|
Medium.sorting = context.config.getFileSorting(curPath)
|
||||||
curMedia.sort()
|
curMedia.sort()
|
||||||
curMedia
|
return curMedia
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getFoldersToScan(path: String): ArrayList<String> {
|
||||||
|
val filterMedia = context.config.filterMedia
|
||||||
|
val projection = arrayOf(MediaStore.Images.Media.DATA)
|
||||||
|
val uri = MediaStore.Files.getContentUri("external")
|
||||||
|
|
||||||
|
val selection = "${getSelectionQuery(path, filterMedia)} ${MediaStore.Images.ImageColumns.BUCKET_ID} IS NOT NULL) GROUP BY (${MediaStore.Images.ImageColumns.BUCKET_ID}"
|
||||||
|
val selectionArgs = getSelectionArgsQuery(path, filterMedia).toTypedArray()
|
||||||
|
|
||||||
|
return try {
|
||||||
|
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
|
parseCursor(cursor, path)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
ArrayList()
|
ArrayList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun getSelectionQuery(path: String, filterMedia: Int): String {
|
private fun getSelectionQuery(path: String, filterMedia: Int): String {
|
||||||
val query = StringBuilder()
|
val query = StringBuilder()
|
||||||
|
@ -112,7 +115,7 @@ class MediaFetcher(val context: Context) {
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getFoldersToScan(context: Context, cursor: Cursor, curPath: String): ArrayList<String> {
|
private fun parseCursor(cursor: Cursor, curPath: String): ArrayList<String> {
|
||||||
val config = context.config
|
val config = context.config
|
||||||
val includedFolders = config.includedFolders
|
val includedFolders = config.includedFolders
|
||||||
var foldersToScan = ArrayList<String>()
|
var foldersToScan = ArrayList<String>()
|
||||||
|
|
Loading…
Reference in New Issue