remove GetDirectoriesAsynctask

This commit is contained in:
tibbi 2018-04-23 23:35:07 +02:00
parent cde57f35b8
commit 24f14fffc4
5 changed files with 6 additions and 114 deletions

View File

@ -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 {

View File

@ -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<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
private val mediaFetcher = MediaFetcher(context)
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
if (!context.hasPermission(PERMISSION_WRITE_STORAGE)) {
return ArrayList()
}
val config = context.config
val groupedMedia = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage)
val directories = ArrayList<Directory>()
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<Directory>) {
super.onPostExecute(dirs)
callback(dirs)
}
fun stopFetching() {
mediaFetcher.shouldStop = true
cancel(true)
}
}

View File

@ -14,11 +14,11 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
override fun doInBackground(vararg params: Void): ArrayList<Medium> {
return if (showAll) {
val mediaMap = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage)
val foldersToScan = mediaFetcher.getFoldersToScan("")
val media = ArrayList<Medium>()
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("")

View File

@ -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() {

View File

@ -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<String, ArrayList<Medium>> {
val media = getFilesFrom("", isPickImage, isPickVideo)
return groupDirectories(media)
}
fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList<Medium> {
val filterMedia = context.config.filterMedia
if (filterMedia == 0) {
@ -167,23 +159,6 @@ class MediaFetcher(val context: Context) {
}
}
private fun groupDirectories(media: ArrayList<Medium>): HashMap<String, ArrayList<Medium>> {
val directories = LinkedHashMap<String, ArrayList<Medium>>()
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<Medium> {
val media = ArrayList<Medium>()
val files = File(folder).listFiles() ?: return media