From 37c31f813b63f0c0b487e56fc5afb322486d2f5c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 22 Apr 2018 22:01:05 +0200 Subject: [PATCH] make sure only the appropriate media files are shown at third party intents --- .../gallery/activities/MainActivity.kt | 7 ++- .../gallery/activities/MediaActivity.kt | 2 +- .../gallery/extensions/Context.kt | 63 ++++++++++--------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index c2f714519..e24681ea3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -274,8 +274,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } mIsGettingDirs = true + val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent + val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent + if (!mLoadedInitialPhotos) { - getCachedDirectories { + getCachedDirectories(getVideosOnly, getImagesOnly) { if (it.isNotEmpty()) { gotDirectories(it, true) } @@ -288,7 +291,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { mLoadedInitialPhotos = true mCurrAsyncTask?.stopFetching() - mCurrAsyncTask = GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent) { + mCurrAsyncTask = GetDirectoriesAsynctask(applicationContext, getVideosOnly, getImagesOnly) { mCurrAsyncTask = null gotDirectories(addTempFolderIfNeeded(it), false) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 498a25994..2d9becbeb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -456,7 +456,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mIsGettingMedia = true if (!mLoadedInitialPhotos) { - getCachedMedia(mPath) { + getCachedMedia(mPath, mIsGetVideoIntent, mIsGetImageIntent) { if (it.isEmpty()) { media_refresh_layout.isRefreshing = true } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 6d8c64bb7..944051624 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -245,7 +245,7 @@ fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boo builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) } -fun Context.getCachedDirectories(callback: (ArrayList) -> Unit) { +fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) { Thread { val directoryDao = galleryDB.DirectoryDao() val directories = directoryDao.getAll() as ArrayList @@ -254,11 +254,16 @@ fun Context.getCachedDirectories(callback: (ArrayList) -> Unit) { val includedPaths = config.includedFolders var filteredDirectories = directories.filter { it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) } as ArrayList val filterMedia = config.filterMedia - filteredDirectories = filteredDirectories.filter { - (filterMedia and TYPE_IMAGES != 0 && it.types and TYPE_IMAGES != 0) || - (filterMedia and TYPE_VIDEOS != 0 && it.types and TYPE_VIDEOS != 0) || - (filterMedia and TYPE_GIFS != 0 && it.types and TYPE_GIFS != 0) - } as ArrayList + + filteredDirectories = (when { + getVideosOnly -> filteredDirectories.filter { it.types and TYPE_VIDEOS != 0 } + getImagesOnly -> filteredDirectories.filter { it.types and TYPE_IMAGES != 0 } + else -> filteredDirectories.filter { + (filterMedia and TYPE_IMAGES != 0 && it.types and TYPE_IMAGES != 0) || + (filterMedia and TYPE_VIDEOS != 0 && it.types and TYPE_VIDEOS != 0) || + (filterMedia and TYPE_GIFS != 0 && it.types and TYPE_GIFS != 0) + } + }) as ArrayList callback(filteredDirectories) @@ -266,30 +271,32 @@ fun Context.getCachedDirectories(callback: (ArrayList) -> Unit) { }.start() } -fun Context.getCachedMedia(path: String, callback: (ArrayList) -> Unit) { - Thread { - val mediumDao = galleryDB.MediumDao() - val media = mediumDao.getMediaFromPath(path) as ArrayList - val shouldShowHidden = config.shouldShowHidden - var filteredMedia = media - if (!shouldShowHidden) { - filteredMedia = media.filter { !it.name.startsWith('.') } as ArrayList - } +fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) = + Thread { + val mediumDao = galleryDB.MediumDao() + val media = mediumDao.getMediaFromPath(path) as ArrayList + val shouldShowHidden = config.shouldShowHidden + var filteredMedia = media + if (!shouldShowHidden) { + filteredMedia = media.filter { !it.name.startsWith('.') } as ArrayList + } - val filterMedia = config.filterMedia - filteredMedia = filteredMedia.filter { - (filterMedia and TYPE_IMAGES != 0 && it.type == TYPE_IMAGES) || - (filterMedia and TYPE_VIDEOS != 0 && it.type == TYPE_VIDEOS) || - (filterMedia and TYPE_GIFS != 0 && it.type == TYPE_GIFS) - } as ArrayList + val filterMedia = config.filterMedia + filteredMedia = (when { + getVideosOnly -> filteredMedia.filter { it.type == TYPE_VIDEOS } + getImagesOnly -> filteredMedia.filter { it.type == TYPE_IMAGES } + else -> filteredMedia.filter { + (filterMedia and TYPE_IMAGES != 0 && it.type == TYPE_IMAGES) || + (filterMedia and TYPE_VIDEOS != 0 && it.type == TYPE_VIDEOS) || + (filterMedia and TYPE_GIFS != 0 && it.type == TYPE_GIFS) + } + }) as ArrayList - callback(filteredMedia) - - media.filter { !File(it.path).exists() }.forEach { - mediumDao.deleteMediumPath(it.path) - } - }.start() -} + callback(filteredMedia) + media.filter { !File(it.path).exists() }.forEach { + mediumDao.deleteMediumPath(it.path) + } + }.start() fun Context.removeInvalidDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { val dirsToCheck = dirs ?: directoryDao.getAll()