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 mIsThirdPartyIntent = false
private var mIsGettingDirs = false private var mIsGettingDirs = false
private var mCurrAsyncTask: GetDirectoriesAsynctask? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
@ -98,6 +100,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
mCurrAsyncTask?.shouldStop = true
storeDirectories() storeDirectories()
} }
@ -146,9 +149,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
gotDirectories(dirs) gotDirectories(dirs)
} }
GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent) { mCurrAsyncTask = GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent) {
gotDirectories(it) gotDirectories(it)
}.execute() }
mCurrAsyncTask!!.execute()
} }
private fun showSortingDialog() { private fun showSortingDialog() {

View File

@ -35,6 +35,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private val SAVE_MEDIA_CNT = 40 private val SAVE_MEDIA_CNT = 40
private var mMedia = ArrayList<Medium>() private var mMedia = ArrayList<Medium>()
private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mPath = "" private var mPath = ""
private var mIsGetImageIntent = false private var mIsGetImageIntent = false
@ -65,6 +66,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
tryloadGallery() tryloadGallery()
} }
override fun onPause() {
super.onPause()
mCurrAsyncTask?.shouldStop = true
}
private fun tryloadGallery() { private fun tryloadGallery() {
if (hasWriteStoragePermission()) { if (hasWriteStoragePermission()) {
val dirName = getHumanizedFilename(mPath) val dirName = getHumanizedFilename(mPath)
@ -204,9 +210,10 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
mLoadedInitialPhotos = true mLoadedInitialPhotos = true
GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mShowAll) { mCurrAsyncTask = GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mShowAll) {
gotMedia(it) gotMedia(it)
}.execute() }
mCurrAsyncTask!!.execute()
} }
private fun isDirEmpty(): Boolean { private fun isDirEmpty(): Boolean {

View File

@ -38,11 +38,11 @@ import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
import java.util.* import java.util.*
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener { class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener {
private var mMedia = ArrayList<Medium>() private var mMedia = ArrayList<Medium>()
private var mPath = "" private var mPath = ""
private var mDirectory = "" private var mDirectory = ""
private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mIsFullScreen = false private var mIsFullScreen = false
private var mPos = -1 private var mPos = -1
@ -100,6 +100,11 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background)) supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background))
} }
override fun onPause() {
super.onPause()
mCurrAsyncTask?.shouldStop = true
}
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_viewpager, menu) menuInflater.inflate(R.menu.menu_viewpager, menu)
if (getCurrentMedium() == null) if (getCurrentMedium() == null)
@ -357,7 +362,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
private fun reloadViewPager() { private fun reloadViewPager() {
GetMediaAsynctask(applicationContext, mDirectory, false, false, mShowAll) { mCurrAsyncTask = GetMediaAsynctask(applicationContext, mDirectory, false, false, mShowAll) {
mMedia = it mMedia = it
if (isDirEmpty()) if (isDirEmpty())
return@GetMediaAsynctask return@GetMediaAsynctask
@ -371,7 +376,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
updateActionbarTitle() updateActionbarTitle()
updatePagerItems() updatePagerItems()
invalidateOptionsMenu() invalidateOptionsMenu()
}.execute() }
mCurrAsyncTask!!.execute()
} }
private fun getProperPosition(): Int { private fun getProperPosition(): Int {

View File

@ -17,6 +17,7 @@ import java.util.*
class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean, class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean,
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() { val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
var config = context.config var config = context.config
var shouldStop = false
override fun doInBackground(vararg params: Void): ArrayList<Directory> { override fun doInBackground(vararg params: Void): ArrayList<Directory> {
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
@ -29,6 +30,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val filenames = File(it).list() val filenames = File(it).list()
if (filenames?.size ?: 0 > 0) { if (filenames?.size ?: 0 > 0) {
for (filename in filenames) { for (filename in filenames) {
if (shouldStop)
cancel(true)
val isImage = filename.isImageFast() || filename.isGif() val isImage = filename.isImageFast() || filename.isGif()
val isVideo = if (isImage) false else filename.isVideoFast() 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 hidden = context.resources.getString(R.string.hidden)
val directories = LinkedHashMap<String, Directory>() val directories = LinkedHashMap<String, Directory>()
for ((name, path, isVideo, dateModified, dateTaken, size) in media) { for ((name, path, isVideo, dateModified, dateTaken, size) in media) {
if (shouldStop)
cancel(true)
val parentDir = File(path).parent ?: continue val parentDir = File(path).parent ?: continue
if (directories.containsKey(parentDir)) { if (directories.containsKey(parentDir)) {
val directory = directories[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 config = context.config
var showMedia = IMAGES_AND_VIDEOS var showMedia = IMAGES_AND_VIDEOS
var fileSorting = 0 var fileSorting = 0
var shouldStop = false
override fun onPreExecute() { override fun onPreExecute() {
super.onPreExecute() super.onPreExecute()
@ -49,6 +50,9 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
val dir = File(path) val dir = File(path)
val filenames = dir.list() ?: return media val filenames = dir.list() ?: return media
for (filename in filenames) { for (filename in filenames) {
if (shouldStop)
cancel(true)
val isImage = filename.isImageFast() || filename.isGif() val isImage = filename.isImageFast() || filename.isGif()
val isVideo = if (isImage) false else filename.isVideoFast() val isVideo = if (isImage) false else filename.isVideoFast()