stop media fetching asynctasks on pause

This commit is contained in:
tibbi 2017-02-26 21:52:53 +01:00
parent 6c11d71a9d
commit a198008c8e
5 changed files with 35 additions and 7 deletions

View File

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

View File

@ -35,6 +35,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private val SAVE_MEDIA_CNT = 40
private var mMedia = ArrayList<Medium>()
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 {

View File

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

View File

@ -17,6 +17,7 @@ import java.util.*
class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean,
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
var config = context.config
var shouldStop = false
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
val media = ArrayList<Medium>()
@ -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<String, Directory>()
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]!!

View File

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