From 5c7dfb87be8b893201a5a03d9b21b39987f28165 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 30 Sep 2017 22:23:28 +0200 Subject: [PATCH] stop media fetching on the calling activity pause --- .../gallery/activities/MainActivity.kt | 2 +- .../gallery/activities/MediaActivity.kt | 7 +++++-- .../gallery/asynctasks/GetDirectoriesAsynctask.kt | 8 +++++++- .../gallery/asynctasks/GetMediaAsynctask.kt | 10 ++++++++-- .../gallery/helpers/MediaFetcher.kt | 14 +++++++++++--- 5 files changed, 32 insertions(+), 9 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 d9997a500..0694bbc53 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -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() { 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 feda91d45..678db7f7f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -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() @@ -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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 44a1921dc..676b45f8b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -18,13 +18,14 @@ import java.io.File class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean, val callback: (dirs: ArrayList) -> Unit) : AsyncTask>() { + val mediaFetcher = MediaFetcher(context) override fun doInBackground(vararg params: Void): ArrayList { 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() 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) + } } 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 f0016b7f1..2c99f54d6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -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) -> Unit) : AsyncTask>() { + val mediaFetcher = MediaFetcher(context) override fun doInBackground(vararg params: Void): ArrayList { return if (showAll) { - val mediaMap = MediaFetcher(context).getMediaByDirectories(isPickVideo, isPickImage) + val mediaMap = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage) val media = ArrayList() 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) + } } 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 7bb0d0e8f..210e44258 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -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> { 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): HashMap> { val directories = LinkedHashMap>() 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, 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()