stop media fetching on the calling activity pause

This commit is contained in:
tibbi 2017-09-30 22:23:28 +02:00
parent 7cc1a9ce17
commit 5c7dfb87be
5 changed files with 32 additions and 9 deletions

View file

@ -61,7 +61,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
private var mLoadedInitialPhotos = false
private var mLatestMediaId = 0L
private var mLastMediaHandler = Handler()
private var mCurrAsyncTask: GetDirectoriesAsynctask? = null
override fun onCreate(savedInstanceState: Bundle?) {
@ -164,6 +163,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
mStoredTextColor = config.textColor
directories_grid.listener = null
mLastMediaHandler.removeCallbacksAndMessages(null)
mCurrAsyncTask?.stopFetching()
}
override fun onDestroy() {

View file

@ -55,6 +55,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mLastDrawnHashCode = 0
private var mLatestMediaId = 0L
private var mLastMediaHandler = Handler()
private var mCurrAsyncTask: GetMediaAsynctask? = null
companion object {
var mMedia = ArrayList<Medium>()
@ -122,6 +123,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
mStoredTextColor = config.textColor
media_grid.listener = null
mLastMediaHandler.removeCallbacksAndMessages(null)
mCurrAsyncTask?.stopFetching()
}
override fun onDestroy() {
@ -344,9 +346,10 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}
mLoadedInitialPhotos = true
GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mShowAll) {
mCurrAsyncTask = GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mShowAll) {
gotMedia(it)
}.execute()
}
mCurrAsyncTask!!.execute()
}
private fun isDirEmpty(): Boolean {

View file

@ -18,13 +18,14 @@ 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>>() {
val mediaFetcher = MediaFetcher(context)
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
if (!context.hasWriteStoragePermission())
return ArrayList()
val config = context.config
val groupedMedia = MediaFetcher(context).getMediaByDirectories(isPickVideo, isPickImage)
val groupedMedia = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage)
val directories = ArrayList<Directory>()
val hidden = context.resources.getString(R.string.hidden)
val albumCovers = config.parseAlbumCovers()
@ -66,4 +67,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
super.onPostExecute(dirs)
callback(dirs)
}
fun stopFetching() {
mediaFetcher.shouldStop = true
cancel(true)
}
}

View file

@ -10,10 +10,11 @@ import java.util.*
class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean = false, val isPickImage: Boolean = false,
val showAll: Boolean, val callback: (media: ArrayList<Medium>) -> Unit) :
AsyncTask<Void, Void, ArrayList<Medium>>() {
val mediaFetcher = MediaFetcher(context)
override fun doInBackground(vararg params: Void): ArrayList<Medium> {
return if (showAll) {
val mediaMap = MediaFetcher(context).getMediaByDirectories(isPickVideo, isPickImage)
val mediaMap = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage)
val media = ArrayList<Medium>()
for ((path, curMedia) in mediaMap) {
media.addAll(curMedia)
@ -23,7 +24,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
media.sort()
media
} else {
MediaFetcher(context).getFilesFrom(mPath, isPickImage, isPickVideo)
mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo)
}
}
@ -31,4 +32,9 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
super.onPostExecute(media)
callback(media)
}
fun stopFetching() {
mediaFetcher.shouldStop = true
cancel(true)
}
}

View file

@ -3,7 +3,6 @@ package com.simplemobiletools.gallery.helpers
import android.content.Context
import android.database.Cursor
import android.provider.MediaStore
import android.util.Log
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
@ -19,6 +18,8 @@ import kotlin.collections.component1
import kotlin.collections.component2
class MediaFetcher(val context: Context) {
var shouldStop = false
fun getMediaByDirectories(isPickVideo: Boolean, isPickImage: Boolean): HashMap<String, ArrayList<Medium>> {
val media = getFilesFrom("", isPickImage, isPickVideo)
val excludedPaths = context.config.excludedFolders
@ -96,9 +97,10 @@ class MediaFetcher(val context: Context) {
if (cur.moveToFirst()) {
do {
try {
val path = cur.getStringValue(MediaStore.Images.Media.DATA)
if (shouldStop)
break
Log.e("DEBUG", "checking $path")
val path = cur.getStringValue(MediaStore.Images.Media.DATA)
var filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
if (filename.isEmpty())
filename = path.getFilenameFromPath()
@ -189,6 +191,9 @@ 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 = File(medium.path).parent?.toLowerCase() ?: continue
if (directories.containsKey(parentDir)) {
directories[parentDir]!!.add(medium)
@ -236,6 +241,9 @@ class MediaFetcher(val context: Context) {
private fun getMediaInFolder(folder: String, curMedia: ArrayList<Medium>, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) {
val files = File(folder).listFiles() ?: return
for (file in files) {
if (shouldStop)
break
val filename = file.name
val isImage = filename.isImageFast()
val isVideo = if (isImage) false else filename.isVideoFast()