From a198008c8e3385b52e2ba87886422e5ed7c69d33 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 26 Feb 2017 21:52:53 +0100 Subject: [PATCH] stop media fetching asynctasks on pause --- .../gallery/activities/MainActivity.kt | 8 ++++++-- .../gallery/activities/MediaActivity.kt | 11 +++++++++-- .../gallery/activities/ViewPagerActivity.kt | 12 +++++++++--- .../gallery/asynctasks/GetDirectoriesAsynctask.kt | 7 +++++++ .../gallery/asynctasks/GetMediaAsynctask.kt | 4 ++++ 5 files changed, 35 insertions(+), 7 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 412d5f768..1a29c1a59 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -48,6 +48,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private var mIsThirdPartyIntent = false private var mIsGettingDirs = false + private var mCurrAsyncTask: GetDirectoriesAsynctask? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -98,6 +100,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { override fun onPause() { super.onPause() + mCurrAsyncTask?.shouldStop = true storeDirectories() } @@ -146,9 +149,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { gotDirectories(dirs) } - GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent) { + mCurrAsyncTask = GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent) { gotDirectories(it) - }.execute() + } + mCurrAsyncTask!!.execute() } private fun showSortingDialog() { 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 2dcf02033..bb5174ba1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -35,6 +35,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private val SAVE_MEDIA_CNT = 40 private var mMedia = ArrayList() + private var mCurrAsyncTask: GetMediaAsynctask? = null private var mPath = "" private var mIsGetImageIntent = false @@ -65,6 +66,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { tryloadGallery() } + override fun onPause() { + super.onPause() + mCurrAsyncTask?.shouldStop = true + } + private fun tryloadGallery() { if (hasWriteStoragePermission()) { val dirName = getHumanizedFilename(mPath) @@ -204,9 +210,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/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index a4b7c910e..e65ee5bec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -38,11 +38,11 @@ import java.io.FileOutputStream import java.io.OutputStream import java.util.* - class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener { private var mMedia = ArrayList() private var mPath = "" private var mDirectory = "" + private var mCurrAsyncTask: GetMediaAsynctask? = null private var mIsFullScreen = false private var mPos = -1 @@ -100,6 +100,11 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background)) } + override fun onPause() { + super.onPause() + mCurrAsyncTask?.shouldStop = true + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_viewpager, menu) if (getCurrentMedium() == null) @@ -357,7 +362,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun reloadViewPager() { - GetMediaAsynctask(applicationContext, mDirectory, false, false, mShowAll) { + mCurrAsyncTask = GetMediaAsynctask(applicationContext, mDirectory, false, false, mShowAll) { mMedia = it if (isDirEmpty()) return@GetMediaAsynctask @@ -371,7 +376,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View updateActionbarTitle() updatePagerItems() invalidateOptionsMenu() - }.execute() + } + mCurrAsyncTask!!.execute() } private fun getProperPosition(): Int { 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 49229ce96..04188f1fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -17,6 +17,7 @@ import java.util.* class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean, val callback: (dirs: ArrayList) -> Unit) : AsyncTask>() { var config = context.config + var shouldStop = false override fun doInBackground(vararg params: Void): ArrayList { val media = ArrayList() @@ -29,6 +30,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val filenames = File(it).list() if (filenames?.size ?: 0 > 0) { for (filename in filenames) { + if (shouldStop) + cancel(true) + val isImage = filename.isImageFast() || filename.isGif() val isVideo = if (isImage) false else filename.isVideoFast() @@ -68,6 +72,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val hidden = context.resources.getString(R.string.hidden) val directories = LinkedHashMap() for ((name, path, isVideo, dateModified, dateTaken, size) in media) { + if (shouldStop) + cancel(true) + val parentDir = File(path).parent ?: continue if (directories.containsKey(parentDir)) { val directory = directories[parentDir]!! 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 c6483cce1..50d8c8020 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -20,6 +20,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo var config = context.config var showMedia = IMAGES_AND_VIDEOS var fileSorting = 0 + var shouldStop = false override fun onPreExecute() { super.onPreExecute() @@ -49,6 +50,9 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo val dir = File(path) val filenames = dir.list() ?: return media for (filename in filenames) { + if (shouldStop) + cancel(true) + val isImage = filename.isImageFast() || filename.isGif() val isVideo = if (isImage) false else filename.isVideoFast()