remove the old way of caching media files

This commit is contained in:
tibbi 2018-04-20 22:21:00 +02:00
parent cb5ac8510f
commit af7108fd12
5 changed files with 24 additions and 62 deletions

View file

@ -652,18 +652,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
setupAdapter()
}
if (!isFromCache) {
storeFolder()
}
}
private fun storeFolder() {
if (!config.temporarilyShowHidden) {
Thread {
storeFolderItems(mPath, mMedia)
}.start()
}
}
override fun deleteFiles(fileDirItems: ArrayList<FileDirItem>) {

View file

@ -187,7 +187,8 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
val fileDirItems = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList
activity.tryCopyMoveFilesTo(fileDirItems, isCopyOperation) {
config.tempFolderPath = ""
activity.applicationContext.updateStoredFolderItems(it)
activity.applicationContext.rescanFolderMedia(it)
activity.applicationContext.rescanFolderMedia(fileDirItems.first().getParentPath())
if (!isCopyOperation) {
listener?.refreshItems()
}
@ -237,7 +238,6 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
media.removeAll(removeMedia)
listener?.deleteFiles(fileDirItems)
removeSelectedItems()
updateStoredFolderItems()
}
}
@ -247,14 +247,6 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
return selectedMedia
}
private fun updateStoredFolderItems() {
Thread {
if (media.isNotEmpty()) {
activity.applicationContext.storeFolderItems(media.first().parentPath, media as ArrayList<Medium>)
}
}.start()
}
fun updateMedia(newMedia: ArrayList<Medium>) {
if (newMedia.hashCode() != currentMediaHash) {
currentMediaHash = newMedia.hashCode()

View file

@ -15,7 +15,6 @@ import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.google.gson.Gson
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.gallery.R
@ -106,7 +105,6 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
val sortOrder = "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
if (cursor?.moveToFirst() == true) {
@ -126,37 +124,25 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
}.start()
}
fun Context.isPathInMediaStore(path: String): Boolean {
if (path.startsWith(OTG_PATH)) {
return false
fun Context.rescanFolderMedia(path: String) {
Thread {
getCachedMedia(path) {
val cached = it
GetMediaAsynctask(applicationContext, path, false, false, false) {
Thread {
val newMedia = it
val mediumDao = galleryDB.MediumDao()
mediumDao.insertAll(newMedia)
cached.forEach {
if (!newMedia.contains(it)) {
mediumDao.deleteMediumPath(it.path)
}
val projection = arrayOf(MediaStore.Images.Media.DATE_MODIFIED)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.MediaColumns.DATA} = ?"
val selectionArgs = arrayOf(path)
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
return cursor.moveToFirst()
}
return false
}
fun Context.updateStoredFolderItems(path: String) {
GetMediaAsynctask(this, path, false, false, false) {
storeFolderItems(path, it)
}.start()
}.execute()
}
fun Context.storeFolderItems(path: String, items: ArrayList<Medium>) {
try {
val subList = items.subList(0, Math.min(SAVE_MEDIA_CNT, items.size))
val json = Gson().toJson(subList)
config.saveFolderMedia(path, json)
} catch (ignored: Exception) {
} catch (ignored: OutOfMemoryError) {
}
}.start()
}
fun Context.updateStoredDirectories() {
@ -275,5 +261,9 @@ fun Context.getCachedMedia(path: String, callback: (ArrayList<Medium>) -> Unit)
val mediumDao = galleryDB.MediumDao()
val media = mediumDao.getMediaFromPath(path) as ArrayList<Medium>
callback(media)
media.filter { !File(it.path).exists() }.forEach {
mediumDao.deleteMediumPath(it.path)
}
}.start()
}

View file

@ -131,12 +131,6 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getStringSet(INCLUDED_FOLDERS, HashSet<String>())
set(includedFolders) = prefs.edit().remove(INCLUDED_FOLDERS).putStringSet(INCLUDED_FOLDERS, includedFolders).apply()
fun saveFolderMedia(path: String, json: String) {
prefs.edit().putString(SAVE_FOLDER_PREFIX + path, json).apply()
}
fun loadFolderMedia(path: String) = prefs.getString(SAVE_FOLDER_PREFIX + path, "")
var autoplayVideos: Boolean
get() = prefs.getBoolean(AUTOPLAY_VIDEOS, false)
set(autoplay) = prefs.edit().putBoolean(AUTOPLAY_VIDEOS, autoplay).apply()

View file

@ -25,7 +25,6 @@ const val MEDIA_LANDSCAPE_COLUMN_CNT = "media_landscape_column_cnt"
const val MEDIA_HORIZONTAL_COLUMN_CNT = "media_horizontal_column_cnt"
const val MEDIA_LANDSCAPE_HORIZONTAL_COLUMN_CNT = "media_landscape_horizontal_column_cnt"
const val SHOW_ALL = "show_all" // display images and videos from all folders together
const val SAVE_FOLDER_PREFIX = "folder2_"
const val HIDE_FOLDER_TOOLTIP_SHOWN = "hide_folder_tooltip_shown"
const val EXCLUDED_FOLDERS = "excluded_folders"
const val INCLUDED_FOLDERS = "included_folders"
@ -66,7 +65,6 @@ const val MAX_COLUMN_COUNT = 20
const val SHOW_TEMP_HIDDEN_DURATION = 600000L
const val CLICK_MAX_DURATION = 150
const val DRAG_THRESHOLD = 8
const val SAVE_MEDIA_CNT = 80
const val DIRECTORY = "directory"
const val MEDIUM = "medium"