fix #1293, rely on ContentObserver for showing new files quicker

This commit is contained in:
tibbi 2019-03-04 21:26:03 +01:00
parent d425083e57
commit abe061cf05
5 changed files with 52 additions and 19 deletions

View File

@ -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()

View File

@ -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()
}

View File

@ -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) {
}
}
}

View File

@ -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()
}

View File

@ -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)
}
}