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 9522b8bc3..e43b63b56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -25,6 +25,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.DirectoryAdapter +import com.simplemobiletools.gallery.databases.GalleryDataBase import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* @@ -184,6 +185,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { config.temporarilyShowHidden = false mTempShowHiddenHandler.removeCallbacksAndMessages(null) removeTempFolder() + GalleryDataBase.destroyInstance() } override fun onCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt deleted file mode 100644 index 64b8d9756..000000000 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ /dev/null @@ -1,80 +0,0 @@ -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.commons.helpers.sumByLong -import com.simplemobiletools.gallery.R -import com.simplemobiletools.gallery.extensions.checkAppendingHidden -import com.simplemobiletools.gallery.extensions.config -import com.simplemobiletools.gallery.extensions.getDirMediaTypes -import com.simplemobiletools.gallery.helpers.MediaFetcher -import com.simplemobiletools.gallery.models.Directory -import com.simplemobiletools.gallery.models.Medium -import java.io.File - -class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean, - val callback: (dirs: ArrayList) -> Unit) : AsyncTask>() { - private val mediaFetcher = MediaFetcher(context) - - override fun doInBackground(vararg params: Void): ArrayList { - if (!context.hasPermission(PERMISSION_WRITE_STORAGE)) { - return ArrayList() - } - - val config = context.config - val groupedMedia = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage) - val directories = ArrayList() - val hidden = context.getString(R.string.hidden) - val albumCovers = config.parseAlbumCovers() - val hasOTG = context.hasOTGConnected() && context.config.OTGBasePath.isNotEmpty() - val includedFolders = config.includedFolders - - for ((path, curMedia) in groupedMedia) { - Medium.sorting = config.getFileSorting(path) - curMedia.sort() - - val firstItem = curMedia.first() - val lastItem = curMedia.last() - val parentDir = if (hasOTG && firstItem.path.startsWith(OTG_PATH)) { - firstItem.parentPath - } else { - File(firstItem.path).parent - } ?: continue - - var thumbnail = curMedia.firstOrNull { context.getDoesFilePathExist(it.path) }?.path ?: "" - if (thumbnail.startsWith(OTG_PATH)) { - thumbnail = thumbnail.getOTGPublicPath(context) - } - - albumCovers.forEach { - if (it.path == parentDir && context.getDoesFilePathExist(it.tmb)) { - thumbnail = it.tmb - } - } - - val mediaTypes = curMedia.getDirMediaTypes() - val dirName = context.checkAppendingHidden(parentDir, hidden, includedFolders) - val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified) - val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken) - val size = curMedia.sumByLong { it.size } - val directory = Directory(null, parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, context.isPathOnSD(parentDir), mediaTypes) - directories.add(directory) - } - - return directories - } - - override fun onPostExecute(dirs: ArrayList) { - super.onPostExecute(dirs) - callback(dirs) - } - - fun stopFetching() { - mediaFetcher.shouldStop = true - cancel(true) - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index a3b8a2b5d..d59ee1850 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -14,11 +14,11 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo override fun doInBackground(vararg params: Void): ArrayList { return if (showAll) { - val mediaMap = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage) + val foldersToScan = mediaFetcher.getFoldersToScan("") val media = ArrayList() - - mediaMap.values.forEach { - media.addAll(it) + for (folder in foldersToScan) { + val newMedia = mediaFetcher.getFilesFrom(folder, isPickImage, isPickVideo) + media.addAll(newMedia) } Medium.sorting = context.config.getFileSorting("") diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt index 1ee74ba96..372a0483c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt @@ -11,7 +11,6 @@ import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.views.MyGridLayoutManager import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.DirectoryAdapter -import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.extensions.addTempFolderIfNeeded import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.getCachedDirectories @@ -47,10 +46,6 @@ class PickDirectoryDialog(val activity: BaseSimpleActivity, val sourcePath: Stri } } } - - GetDirectoriesAsynctask(activity, false, false) { - gotDirectories(activity.addTempFolderIfNeeded(it)) - }.execute() } private fun showOtherFolder() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index 0ce4332a5..179262e30 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -12,18 +12,10 @@ import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible import com.simplemobiletools.gallery.models.Medium import java.io.File -import java.util.LinkedHashMap -import kotlin.collections.ArrayList -import kotlin.collections.set class MediaFetcher(val context: Context) { var shouldStop = false - fun getMediaByDirectories(isPickVideo: Boolean, isPickImage: Boolean): HashMap> { - val media = getFilesFrom("", isPickImage, isPickVideo) - return groupDirectories(media) - } - fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { @@ -167,23 +159,6 @@ class MediaFetcher(val context: Context) { } } - private fun groupDirectories(media: ArrayList): HashMap> { - val directories = LinkedHashMap>() - for (medium in media) { - if (shouldStop) { - break - } - - val parentDir = medium.parentPath.toLowerCase() - if (directories.containsKey(parentDir)) { - directories[parentDir]!!.add(medium) - } else { - directories[parentDir] = arrayListOf(medium) - } - } - return directories - } - private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int): ArrayList { val media = ArrayList() val files = File(folder).listFiles() ?: return media