diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index dd1ed3ac1..46240a664 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -147,6 +147,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } updateWidgets() + registerFileUpdateListener() } override fun onStart() { @@ -239,6 +240,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { config.tempSkipDeleteConfirmation = false mTempShowHiddenHandler.removeCallbacksAndMessages(null) removeTempFolder() + unregisterFileUpdateListener() if (!config.showAll) { GalleryDatabase.destroyInstance() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt index 11ba3692b..dff45ff8f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt @@ -108,6 +108,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { if (mShowAll) { supportActionBar?.setDisplayHomeAsUpEnabled(false) + registerFileUpdateListener() } media_empty_text.setOnClickListener { @@ -193,6 +194,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { if (config.showAll && !isChangingConfigurations) { config.temporarilyShowHidden = false config.tempSkipDeleteConfirmation = false + unregisterFileUpdateListener() GalleryDatabase.destroyInstance() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt index 8d0017ff8..2be7d90fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt @@ -1,13 +1,28 @@ package com.simplemobiletools.gallery.pro.activities import android.annotation.SuppressLint +import android.database.ContentObserver +import android.net.Uri +import android.provider.MediaStore import android.view.WindowManager import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.getRealPathFromURI import com.simplemobiletools.commons.helpers.isPiePlus import com.simplemobiletools.gallery.pro.R +import com.simplemobiletools.gallery.pro.extensions.addPathToDB import com.simplemobiletools.gallery.pro.extensions.config open class SimpleActivity : BaseSimpleActivity() { + val observer = object : ContentObserver(null) { + override fun onChange(selfChange: Boolean, uri: Uri) { + super.onChange(selfChange, uri) + val path = getRealPathFromURI(uri) + if (path != null) { + addPathToDB(path) + } + } + } + override fun getAppIconIDs() = arrayListOf( R.mipmap.ic_launcher_red, R.mipmap.ic_launcher_pink, @@ -46,4 +61,19 @@ open class SimpleActivity : BaseSimpleActivity() { } } } + + protected fun registerFileUpdateListener() { + try { + contentResolver.registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, observer) + contentResolver.registerContentObserver(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true, observer) + } catch (ignored: Exception) { + } + } + + protected fun unregisterFileUpdateListener() { + try { + contentResolver.unregisterContentObserver(observer) + } catch (ignored: Exception) { + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index fb2d6f48d..15f51a3de 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -698,3 +698,19 @@ fun Context.parseFileChannel(path: String, fc: FileChannel, level: Int, start: L } catch (ignored: Exception) { } } + +fun Context.addPathToDB(path: String) { + Thread { + val type = when { + path.isVideoFast() -> TYPE_VIDEOS + path.isGif() -> TYPE_GIFS + path.isRawFast() -> TYPE_RAWS + path.isSvg() -> TYPE_SVGS + else -> TYPE_IMAGES + } + + val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), + File(path).length(), type, 0, false, 0L) + galleryDB.MediumDao().insert(medium) + }.start() +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/receivers/RefreshMediaReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/receivers/RefreshMediaReceiver.kt index 3e3f35ec9..3aa81b2f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/receivers/RefreshMediaReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/receivers/RefreshMediaReceiver.kt @@ -3,29 +3,12 @@ package com.simplemobiletools.gallery.pro.receivers import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.REFRESH_PATH -import com.simplemobiletools.gallery.pro.extensions.galleryDB -import com.simplemobiletools.gallery.pro.helpers.* -import com.simplemobiletools.gallery.pro.models.Medium -import java.io.File +import com.simplemobiletools.gallery.pro.extensions.addPathToDB class RefreshMediaReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val path = intent.getStringExtra(REFRESH_PATH) ?: return - - Thread { - val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), - File(path).length(), getFileType(path), 0, false, 0L) - context.galleryDB.MediumDao().insert(medium) - }.start() - } - - private fun getFileType(path: String) = when { - path.isVideoFast() -> TYPE_VIDEOS - path.isGif() -> TYPE_GIFS - path.isRawFast() -> TYPE_RAWS - path.isSvg() -> TYPE_SVGS - else -> TYPE_IMAGES + context.addPathToDB(path) } }