From 2cc9d1051c9abf715f13ef7c51d263fc3eb7fedd Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 25 May 2017 22:04:19 +0200 Subject: [PATCH] recheck every 3 seconds if some media changed at folders view --- .../gallery/activities/MainActivity.kt | 20 +++++++++++++++++++ .../gallery/extensions/context.kt | 17 ++++++++++++++++ 2 files changed, 37 insertions(+) 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 e3017393e..88e6e24e6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -7,6 +7,7 @@ import android.content.pm.PackageManager import android.net.Uri import android.os.AsyncTask import android.os.Bundle +import android.os.Handler import android.provider.MediaStore import android.support.v4.app.ActivityCompat import android.support.v7.widget.GridLayoutManager @@ -32,6 +33,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private val STORAGE_PERMISSION = 1 private val PICK_MEDIA = 2 private val PICK_WALLPAPER = 3 + private val LAST_MEDIA_CHECK_PERIOD = 3000L lateinit var mDirs: ArrayList @@ -46,6 +48,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private var mStoredAnimateGifs = true private var mStoredCropThumbnails = true private var mLoadedInitialPhotos = false + private var mLastMediaModified = 0 + private var mLastMediaHandler = Handler() private var mCurrAsyncTask: GetDirectoriesAsynctask? = null @@ -119,6 +123,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { mStoredAnimateGifs = config.animateGifs mStoredCropThumbnails = config.cropThumbnails MyScalableRecyclerView.mListener = null + mLastMediaHandler.removeCallbacksAndMessages(null) } override fun onStop() { @@ -321,9 +326,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun gotDirectories(dirs: ArrayList) { + mLastMediaModified = getLastMediaModified() directories_refresh_layout.isRefreshing = false mIsGettingDirs = false + checkLastMediaChanged() if (dirs.hashCode() == mDirs.hashCode()) return @@ -352,6 +359,19 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { directories_fastscroller.setViews(directories_grid, directories_refresh_layout) } + private fun checkLastMediaChanged() { + mLastMediaHandler.removeCallbacksAndMessages(null) + mLastMediaHandler.postDelayed({ + val lastModified = getLastMediaModified() + if (mLastMediaModified != lastModified) { + getDirectories() + mLastMediaModified = lastModified + } else { + checkLastMediaChanged() + } + }, LAST_MEDIA_CHECK_PERIOD) + } + override fun refreshItems() { getDirectories() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index dd3039c87..094879fac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -194,3 +194,20 @@ fun Context.getNoMediaFolders(): ArrayList { return folders } + + +fun Context.getLastMediaModified(): Int { + val uri = MediaStore.Files.getContentUri("external") + val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATE_MODIFIED) + val order = "${MediaStore.Images.Media.DATE_MODIFIED} DESC" + var cursor: Cursor? = null + try { + cursor = contentResolver.query(uri, projection, null, null, order) + if (cursor?.moveToFirst() == true) { + return cursor.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) + } + } finally { + cursor?.close() + } + return 0 +}