check last media change at media too, but in a background thread

This commit is contained in:
tibbi 2017-05-25 22:16:23 +02:00
parent 2cc9d1051c
commit 448fa72017
2 changed files with 37 additions and 9 deletions

View file

@ -362,13 +362,17 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
private fun checkLastMediaChanged() { private fun checkLastMediaChanged() {
mLastMediaHandler.removeCallbacksAndMessages(null) mLastMediaHandler.removeCallbacksAndMessages(null)
mLastMediaHandler.postDelayed({ mLastMediaHandler.postDelayed({
Thread({
val lastModified = getLastMediaModified() val lastModified = getLastMediaModified()
if (mLastMediaModified != lastModified) { if (mLastMediaModified != lastModified) {
getDirectories()
mLastMediaModified = lastModified mLastMediaModified = lastModified
runOnUiThread {
getDirectories()
}
} else { } else {
checkLastMediaChanged() checkLastMediaChanged()
} }
}).start()
}, LAST_MEDIA_CHECK_PERIOD) }, LAST_MEDIA_CHECK_PERIOD)
} }

View file

@ -6,6 +6,7 @@ import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.GridLayoutManager
import android.util.Log import android.util.Log
import android.view.Menu import android.view.Menu
@ -33,6 +34,7 @@ import java.io.IOException
class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private val TAG = MediaActivity::class.java.simpleName private val TAG = MediaActivity::class.java.simpleName
private val SAVE_MEDIA_CNT = 40 private val SAVE_MEDIA_CNT = 40
private val LAST_MEDIA_CHECK_PERIOD = 3000L
private var mMedia = ArrayList<Medium>() private var mMedia = ArrayList<Medium>()
@ -45,6 +47,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mLoadedInitialPhotos = false private var mLoadedInitialPhotos = false
private var mStoredAnimateGifs = true private var mStoredAnimateGifs = true
private var mStoredCropThumbnails = true private var mStoredCropThumbnails = true
private var mLastMediaModified = 0
private var mLastMediaHandler = Handler()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -84,6 +88,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
mStoredAnimateGifs = config.animateGifs mStoredAnimateGifs = config.animateGifs
mStoredCropThumbnails = config.cropThumbnails mStoredCropThumbnails = config.cropThumbnails
MyScalableRecyclerView.mListener = null MyScalableRecyclerView.mListener = null
mLastMediaHandler.removeCallbacksAndMessages(null)
} }
override fun onStop() { override fun onStop() {
@ -110,7 +115,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
} }
private fun initializeGallery() { private fun setupAdapter() {
if (isDirEmpty()) if (isDirEmpty())
return return
@ -127,6 +132,23 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
media_fastscroller.setViews(media_grid, media_refresh_layout) media_fastscroller.setViews(media_grid, media_refresh_layout)
} }
private fun checkLastMediaChanged() {
mLastMediaHandler.removeCallbacksAndMessages(null)
mLastMediaHandler.postDelayed({
Thread({
val lastModified = getLastMediaModified()
if (mLastMediaModified != lastModified) {
mLastMediaModified = lastModified
runOnUiThread {
getMedia()
}
} else {
checkLastMediaChanged()
}
}).start()
}, LAST_MEDIA_CHECK_PERIOD)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_media, menu) menuInflater.inflate(R.menu.menu_media, menu)
@ -359,14 +381,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
private fun gotMedia(media: ArrayList<Medium>) { private fun gotMedia(media: ArrayList<Medium>) {
mLastMediaModified = getLastMediaModified()
mIsGettingMedia = false mIsGettingMedia = false
media_refresh_layout.isRefreshing = false media_refresh_layout.isRefreshing = false
checkLastMediaChanged()
if (media.hashCode() == mMedia.hashCode()) if (media.hashCode() == mMedia.hashCode())
return return
mMedia = media mMedia = media
initializeGallery() setupAdapter()
storeFolder() storeFolder()
} }