From 448fa72017d92016ab0b625a6923449515fef9db Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 25 May 2017 22:16:23 +0200 Subject: [PATCH] check last media change at media too, but in a background thread --- .../gallery/activities/MainActivity.kt | 18 +++++++----- .../gallery/activities/MediaActivity.kt | 28 +++++++++++++++++-- 2 files changed, 37 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 88e6e24e6..ff6d79604 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -362,13 +362,17 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun checkLastMediaChanged() { mLastMediaHandler.removeCallbacksAndMessages(null) mLastMediaHandler.postDelayed({ - val lastModified = getLastMediaModified() - if (mLastMediaModified != lastModified) { - getDirectories() - mLastMediaModified = lastModified - } else { - checkLastMediaChanged() - } + Thread({ + val lastModified = getLastMediaModified() + if (mLastMediaModified != lastModified) { + mLastMediaModified = lastModified + runOnUiThread { + getDirectories() + } + } else { + checkLastMediaChanged() + } + }).start() }, LAST_MEDIA_CHECK_PERIOD) } 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 d2dbba586..16d166fa2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.graphics.Bitmap import android.net.Uri import android.os.Bundle +import android.os.Handler import android.support.v7.widget.GridLayoutManager import android.util.Log import android.view.Menu @@ -33,6 +34,7 @@ import java.io.IOException class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private val TAG = MediaActivity::class.java.simpleName private val SAVE_MEDIA_CNT = 40 + private val LAST_MEDIA_CHECK_PERIOD = 3000L private var mMedia = ArrayList() @@ -45,6 +47,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private var mLoadedInitialPhotos = false private var mStoredAnimateGifs = true private var mStoredCropThumbnails = true + private var mLastMediaModified = 0 + private var mLastMediaHandler = Handler() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -84,6 +88,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mStoredAnimateGifs = config.animateGifs mStoredCropThumbnails = config.cropThumbnails MyScalableRecyclerView.mListener = null + mLastMediaHandler.removeCallbacksAndMessages(null) } override fun onStop() { @@ -110,7 +115,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } - private fun initializeGallery() { + private fun setupAdapter() { if (isDirEmpty()) return @@ -127,6 +132,23 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { 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 { menuInflater.inflate(R.menu.menu_media, menu) @@ -359,14 +381,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun gotMedia(media: ArrayList) { + mLastMediaModified = getLastMediaModified() mIsGettingMedia = false media_refresh_layout.isRefreshing = false + checkLastMediaChanged() if (media.hashCode() == mMedia.hashCode()) return mMedia = media - initializeGallery() + setupAdapter() storeFolder() }