mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2024-11-27 06:57:59 +01:00
fix #1293, rely on ContentObserver for showing new files quicker
This commit is contained in:
parent
d425083e57
commit
abe061cf05
5 changed files with 52 additions and 19 deletions
|
@ -147,6 +147,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateWidgets()
|
updateWidgets()
|
||||||
|
registerFileUpdateListener()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
|
@ -239,6 +240,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
|
||||||
config.tempSkipDeleteConfirmation = false
|
config.tempSkipDeleteConfirmation = false
|
||||||
mTempShowHiddenHandler.removeCallbacksAndMessages(null)
|
mTempShowHiddenHandler.removeCallbacksAndMessages(null)
|
||||||
removeTempFolder()
|
removeTempFolder()
|
||||||
|
unregisterFileUpdateListener()
|
||||||
|
|
||||||
if (!config.showAll) {
|
if (!config.showAll) {
|
||||||
GalleryDatabase.destroyInstance()
|
GalleryDatabase.destroyInstance()
|
||||||
|
|
|
@ -108,6 +108,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
|
||||||
|
|
||||||
if (mShowAll) {
|
if (mShowAll) {
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(false)
|
supportActionBar?.setDisplayHomeAsUpEnabled(false)
|
||||||
|
registerFileUpdateListener()
|
||||||
}
|
}
|
||||||
|
|
||||||
media_empty_text.setOnClickListener {
|
media_empty_text.setOnClickListener {
|
||||||
|
@ -193,6 +194,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
|
||||||
if (config.showAll && !isChangingConfigurations) {
|
if (config.showAll && !isChangingConfigurations) {
|
||||||
config.temporarilyShowHidden = false
|
config.temporarilyShowHidden = false
|
||||||
config.tempSkipDeleteConfirmation = false
|
config.tempSkipDeleteConfirmation = false
|
||||||
|
unregisterFileUpdateListener()
|
||||||
GalleryDatabase.destroyInstance()
|
GalleryDatabase.destroyInstance()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,28 @@
|
||||||
package com.simplemobiletools.gallery.pro.activities
|
package com.simplemobiletools.gallery.pro.activities
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.database.ContentObserver
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.MediaStore
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||||
|
import com.simplemobiletools.commons.extensions.getRealPathFromURI
|
||||||
import com.simplemobiletools.commons.helpers.isPiePlus
|
import com.simplemobiletools.commons.helpers.isPiePlus
|
||||||
import com.simplemobiletools.gallery.pro.R
|
import com.simplemobiletools.gallery.pro.R
|
||||||
|
import com.simplemobiletools.gallery.pro.extensions.addPathToDB
|
||||||
import com.simplemobiletools.gallery.pro.extensions.config
|
import com.simplemobiletools.gallery.pro.extensions.config
|
||||||
|
|
||||||
open class SimpleActivity : BaseSimpleActivity() {
|
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(
|
override fun getAppIconIDs() = arrayListOf(
|
||||||
R.mipmap.ic_launcher_red,
|
R.mipmap.ic_launcher_red,
|
||||||
R.mipmap.ic_launcher_pink,
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -698,3 +698,19 @@ fun Context.parseFileChannel(path: String, fc: FileChannel, level: Int, start: L
|
||||||
} catch (ignored: Exception) {
|
} 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()
|
||||||
|
}
|
||||||
|
|
|
@ -3,29 +3,12 @@ package com.simplemobiletools.gallery.pro.receivers
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.simplemobiletools.commons.extensions.*
|
|
||||||
import com.simplemobiletools.commons.helpers.REFRESH_PATH
|
import com.simplemobiletools.commons.helpers.REFRESH_PATH
|
||||||
import com.simplemobiletools.gallery.pro.extensions.galleryDB
|
import com.simplemobiletools.gallery.pro.extensions.addPathToDB
|
||||||
import com.simplemobiletools.gallery.pro.helpers.*
|
|
||||||
import com.simplemobiletools.gallery.pro.models.Medium
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
class RefreshMediaReceiver : BroadcastReceiver() {
|
class RefreshMediaReceiver : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
val path = intent.getStringExtra(REFRESH_PATH) ?: return
|
val path = intent.getStringExtra(REFRESH_PATH) ?: return
|
||||||
|
context.addPathToDB(path)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue