improve the performance of fetching Android 11+ files, use the MediaStore

This commit is contained in:
tibbi 2021-12-04 22:24:50 +01:00
parent 0224dc56c1
commit dae13c0261
4 changed files with 30 additions and 17 deletions

View file

@ -873,12 +873,6 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent
val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent
val favoritePaths = getFavoritePaths() val favoritePaths = getFavoritePaths()
/*if (isRPlus()) {
mLastMediaFetcher!!.getAndroid11FolderMedia(getImagesOnly, getVideosOnly, favoritePaths)
return
}*/
val hiddenString = getString(R.string.hidden) val hiddenString = getString(R.string.hidden)
val albumCovers = config.parseAlbumCovers() val albumCovers = config.parseAlbumCovers()
val includedFolders = config.includedFolders val includedFolders = config.includedFolders
@ -913,6 +907,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
} }
} }
val android11Files = mLastMediaFetcher?.getAndroid11FolderMedia(getImagesOnly, getVideosOnly, favoritePaths)
try { try {
for (directory in dirs) { for (directory in dirs) {
if (mShouldStopFetching || isDestroyed || isFinishing) { if (mShouldStopFetching || isDestroyed || isFinishing) {
@ -933,7 +928,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val curMedia = mLastMediaFetcher!!.getFilesFrom( val curMedia = mLastMediaFetcher!!.getFilesFrom(
directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified,
getProperFileSize, favoritePaths, false, lastModifieds, dateTakens getProperFileSize, favoritePaths, false, lastModifieds, dateTakens, android11Files
) )
val newDir = if (curMedia.isEmpty()) { val newDir = if (curMedia.isEmpty()) {
@ -1040,7 +1035,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val newMedia = mLastMediaFetcher!!.getFilesFrom( val newMedia = mLastMediaFetcher!!.getFilesFrom(
folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified,
getProperFileSize, favoritePaths, false, lastModifieds, dateTakens getProperFileSize, favoritePaths, false, lastModifieds, dateTakens, android11Files
) )
if (newMedia.isEmpty()) { if (newMedia.isEmpty()) {

View file

@ -38,7 +38,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
foldersToScan.forEach { foldersToScan.forEach {
val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize,
favoritePaths, getVideoDurations, lastModifieds, dateTakens) favoritePaths, getVideoDurations, lastModifieds, dateTakens, null)
media.addAll(newMedia) media.addAll(newMedia)
} }
@ -46,7 +46,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage
media media
} else { } else {
mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths,
getVideoDurations, lastModifieds, dateTakens) getVideoDurations, lastModifieds, dateTakens, null)
} }
return mediaFetcher.groupMedia(media, pathToUse) return mediaFetcher.groupMedia(media, pathToUse)

View file

@ -1071,7 +1071,7 @@ fun Context.updateDirectoryPath(path: String) {
val favoritePaths = getFavoritePaths() val favoritePaths = getFavoritePaths()
val curMedia = mediaFetcher.getFilesFrom( val curMedia = mediaFetcher.getFilesFrom(
path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize,
favoritePaths, false, lastModifieds, dateTakens favoritePaths, false, lastModifieds, dateTakens, null
) )
val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, getProperFileSize, noMediaFolders) val directory = createDirectoryFromMedia(path, curMedia, albumCovers, hiddenString, includedFolders, getProperFileSize, noMediaFolders)
updateDBDirectory(directory) updateDBDirectory(directory)

View file

@ -25,10 +25,11 @@ import java.util.*
class MediaFetcher(val context: Context) { class MediaFetcher(val context: Context) {
var shouldStop = false var shouldStop = false
// on Android 11 we fetch all files at once from MediaStore and have it split by folder, use it if available
fun getFilesFrom( fun getFilesFrom(
curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean,
getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean, getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean,
lastModifieds: HashMap<String, Long>, dateTakens: HashMap<String, Long> lastModifieds: HashMap<String, Long>, dateTakens: HashMap<String, Long>, android11Files: HashMap<String, ArrayList<Medium>>?
): ArrayList<Medium> { ): ArrayList<Medium> {
val filterMedia = context.config.filterMedia val filterMedia = context.config.filterMedia
if (filterMedia == 0) { if (filterMedia == 0) {
@ -42,12 +43,25 @@ class MediaFetcher(val context: Context) {
curMedia.addAll(newMedia) curMedia.addAll(newMedia)
} }
} else { } else {
if (isRPlus() && curPath != FAVORITES && curPath != RECYCLE_BIN) {
if (android11Files?.containsKey(curPath.toLowerCase()) == true) {
curMedia.addAll(android11Files[curPath.toLowerCase()]!!)
} else {
val files = getAndroid11FolderMedia(isPickImage, isPickVideo, favoritePaths)
if (files.containsKey(curPath.toLowerCase())) {
curMedia.addAll(files[curPath.toLowerCase()]!!)
}
}
}
if (curMedia.isEmpty()) {
val newMedia = getMediaInFolder( val newMedia = getMediaInFolder(
curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize, curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperLastModified, getProperFileSize,
favoritePaths, getVideoDurations, lastModifieds.clone() as HashMap<String, Long>, dateTakens.clone() as HashMap<String, Long> favoritePaths, getVideoDurations, lastModifieds.clone() as HashMap<String, Long>, dateTakens.clone() as HashMap<String, Long>
) )
curMedia.addAll(newMedia) curMedia.addAll(newMedia)
} }
}
sortMedia(curMedia, context.config.getFolderSorting(curPath)) sortMedia(curMedia, context.config.getFolderSorting(curPath))
@ -399,6 +413,10 @@ class MediaFetcher(val context: Context) {
isPickImage: Boolean, isPickVideo: Boolean, favoritePaths: ArrayList<String> isPickImage: Boolean, isPickVideo: Boolean, favoritePaths: ArrayList<String>
): HashMap<String, ArrayList<Medium>> { ): HashMap<String, ArrayList<Medium>> {
val media = HashMap<String, ArrayList<Medium>>() val media = HashMap<String, ArrayList<Medium>>()
if (!isRPlus()) {
return media
}
val filterMedia = context.config.filterMedia val filterMedia = context.config.filterMedia
val showHidden = context.config.shouldShowHidden val showHidden = context.config.shouldShowHidden