From af7108fd127521a59b152ba02780be3772e11f6f Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 20 Apr 2018 22:21:00 +0200 Subject: [PATCH] remove the old way of caching media files --- .../gallery/activities/MediaActivity.kt | 12 ----- .../gallery/adapters/MediaAdapter.kt | 12 +---- .../gallery/extensions/Context.kt | 54 ++++++++----------- .../gallery/helpers/Config.kt | 6 --- .../gallery/helpers/Constants.kt | 2 - 5 files changed, 24 insertions(+), 62 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 3833627b2..7ee09f1c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -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) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 6485ca21e..af8cc80e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -187,7 +187,8 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, 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, media.removeAll(removeMedia) listener?.deleteFiles(fileDirItems) removeSelectedItems() - updateStoredFolderItems() } } @@ -247,14 +247,6 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, return selectedMedia } - private fun updateStoredFolderItems() { - Thread { - if (media.isNotEmpty()) { - activity.applicationContext.storeFolderItems(media.first().parentPath, media as ArrayList) - } - }.start() - } - fun updateMedia(newMedia: ArrayList) { if (newMedia.hashCode() != currentMediaHash) { currentMediaHash = newMedia.hashCode() 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 07cbaa91b..1794b40f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -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) -> 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) -> 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) - 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) - }.execute() -} - -fun Context.storeFolderItems(path: String, items: ArrayList) { - 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) { - } + cached.forEach { + if (!newMedia.contains(it)) { + mediumDao.deleteMediumPath(it.path) + } + } + }.start() + }.execute() + } + }.start() } fun Context.updateStoredDirectories() { @@ -275,5 +261,9 @@ fun Context.getCachedMedia(path: String, callback: (ArrayList) -> Unit) val mediumDao = galleryDB.MediumDao() val media = mediumDao.getMediaFromPath(path) as ArrayList callback(media) + + media.filter { !File(it.path).exists() }.forEach { + mediumDao.deleteMediumPath(it.path) + } }.start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index edb79a028..cc9d07b0f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -131,12 +131,6 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getStringSet(INCLUDED_FOLDERS, HashSet()) 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() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index 5dcbc4305..adff9c62b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -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"