recheck cached folders at startup one by one

This commit is contained in:
tibbi 2018-04-23 16:05:54 +02:00
parent 4f724786b2
commit a4f62ffcd9
7 changed files with 123 additions and 49 deletions

View file

@ -16,10 +16,7 @@ import com.simplemobiletools.commons.dialogs.CreateNewFolderDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_STORAGE import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
@ -34,6 +31,7 @@ import com.simplemobiletools.gallery.dialogs.FilterMediaDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.io.* import java.io.*
@ -587,7 +585,69 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
setupAdapter() setupAdapter()
} }
if (!isFromCache) { if (isFromCache) {
Thread {
val mediaFetcher = MediaFetcher(applicationContext)
val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent
val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent
val hidden = getString(R.string.hidden)
val albumCovers = config.parseAlbumCovers()
val hasOTG = hasOTGConnected() && config.OTGBasePath.isNotEmpty()
val includedFolders = config.includedFolders
for (directory in dirs) {
val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly)
Medium.sorting = config.getFileSorting(directory.path)
curMedia.sort()
val firstItem = curMedia.first()
val lastItem = curMedia.last()
val parentDir = if (hasOTG && firstItem.path.startsWith(OTG_PATH)) {
firstItem.parentPath
} else {
File(firstItem.path).parent
} ?: continue
var thumbnail = curMedia.firstOrNull { getDoesFilePathExist(it.path) }?.path ?: ""
if (thumbnail.startsWith(OTG_PATH)) {
thumbnail = thumbnail.getOTGPublicPath(applicationContext)
}
albumCovers.forEach {
if (it.path == parentDir && getDoesFilePathExist(it.tmb)) {
thumbnail = it.tmb
}
}
val mediaTypes = curMedia.getDirMediaTypes()
val dirName = checkAppendingHidden(parentDir, hidden, includedFolders)
val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified)
val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken)
val size = curMedia.sumByLong { it.size }
val newDir = Directory(null, parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, isPathOnSD(parentDir), mediaTypes)
directory.mediaCnt = curMedia.size
if (directory == newDir) {
continue
}
directory.apply {
tmb = thumbnail
mediaCnt = curMedia.size
modified = lastModified
taken = dateTaken
this@apply.size = size
types = mediaTypes
}
updateDirectory(directory)
rescanFolderMediaSync(directory.path)
val sortedDirs = getSortedDirectories(dirs).clone() as ArrayList<Directory>
runOnUiThread {
(directories_grid.adapter as DirectoryAdapter).updateDirs(sortedDirs)
}
}
}.start()
} else {
storeDirectories() storeDirectories()
} }
} }

View file

@ -10,10 +10,8 @@ import com.simplemobiletools.commons.helpers.sumByLong
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.checkAppendingHidden import com.simplemobiletools.gallery.extensions.checkAppendingHidden
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getDirMediaTypes
import com.simplemobiletools.gallery.helpers.MediaFetcher import com.simplemobiletools.gallery.helpers.MediaFetcher
import com.simplemobiletools.gallery.helpers.TYPE_GIFS
import com.simplemobiletools.gallery.helpers.TYPE_IMAGES
import com.simplemobiletools.gallery.helpers.TYPE_VIDEOS
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
@ -58,8 +56,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
} }
} }
val mediaTypes = getDirMediaTypes(curMedia) val mediaTypes = curMedia.getDirMediaTypes()
val dirName = context.checkAppendingHidden(parentDir, hidden, includedFolders) val dirName = context.checkAppendingHidden(parentDir, hidden, includedFolders)
val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified) val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified)
val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken) val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken)
@ -71,23 +68,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
return directories return directories
} }
private fun getDirMediaTypes(media: ArrayList<Medium>): Int {
var types = 0
if (media.any { it.isImage() }) {
types += TYPE_IMAGES
}
if (media.any { it.isVideo() }) {
types += TYPE_VIDEOS
}
if (media.any { it.isGif() }) {
types += TYPE_GIFS
}
return types
}
override fun onPostExecute(dirs: ArrayList<Directory>) { override fun onPostExecute(dirs: ArrayList<Directory>) {
super.onPostExecute(dirs) super.onPostExecute(dirs)
callback(dirs) callback(dirs)

View file

@ -0,0 +1,23 @@
package com.simplemobiletools.gallery.extensions
import com.simplemobiletools.gallery.helpers.TYPE_GIFS
import com.simplemobiletools.gallery.helpers.TYPE_IMAGES
import com.simplemobiletools.gallery.helpers.TYPE_VIDEOS
import com.simplemobiletools.gallery.models.Medium
fun ArrayList<Medium>.getDirMediaTypes(): Int {
var types = 0
if (any { it.isImage() }) {
types += TYPE_IMAGES
}
if (any { it.isVideo() }) {
types += TYPE_VIDEOS
}
if (any { it.isGif() }) {
types += TYPE_GIFS
}
return types
}

View file

@ -135,25 +135,29 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
fun Context.rescanFolderMedia(path: String) { fun Context.rescanFolderMedia(path: String) {
Thread { Thread {
getCachedMedia(path) { rescanFolderMediaSync(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)
}
}
}.start()
}.execute()
}
}.start() }.start()
} }
fun Context.rescanFolderMediaSync(path: String) {
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)
}
}
}.start()
}.execute()
}
}
fun Context.updateStoredDirectories() { fun Context.updateStoredDirectories() {
GetDirectoriesAsynctask(this, false, false) { GetDirectoriesAsynctask(this, false, false) {
if (!config.temporarilyShowHidden) { if (!config.temporarilyShowHidden) {
@ -317,5 +321,9 @@ fun Context.removeInvalidDirectories(dirs: ArrayList<Directory>? = null, directo
fun Context.updateMediaPath(oldPath: String, newPath: String) { fun Context.updateMediaPath(oldPath: String, newPath: String) {
val newFilename = newPath.getFilenameFromPath() val newFilename = newPath.getFilenameFromPath()
val newParentPath = newPath.getParentPath() val newParentPath = newPath.getParentPath()
galleryDB.MediumDao().updateMedia(oldPath, newParentPath, newFilename, newPath) galleryDB.MediumDao().updateMedium(oldPath, newParentPath, newFilename, newPath)
}
fun Context.updateDirectory(directory: Directory) {
galleryDB.DirectoryDao().updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types)
} }

View file

@ -23,4 +23,7 @@ interface DirectoryDao {
@Query("DELETE FROM directories WHERE path = :path") @Query("DELETE FROM directories WHERE path = :path")
fun deleteDirPath(path: String) fun deleteDirPath(path: String)
@Query("UPDATE OR REPLACE directories SET thumbnail = :thumbnail, media_count = :mediaCnt, last_modified = :lastModified, date_taken = :dateTaken, size = :size, media_types = :mediaTypes WHERE path = :path")
fun updateDirectory(path: String, thumbnail: String, mediaCnt: Int, lastModified: Long, dateTaken: Long, size: Long, mediaTypes: Int)
} }

View file

@ -24,5 +24,5 @@ interface MediumDao {
fun deleteMediumPath(path: String) fun deleteMediumPath(path: String)
@Query("UPDATE OR REPLACE media SET filename = :newFilename, full_path = :newFullPath, parent_path = :newParentPath WHERE full_path = :oldPath") @Query("UPDATE OR REPLACE media SET filename = :newFilename, full_path = :newFullPath, parent_path = :newParentPath WHERE full_path = :oldPath")
fun updateMedia(oldPath: String, newParentPath: String, newFilename: String, newFullPath: String) fun updateMedium(oldPath: String, newParentPath: String, newFilename: String, newFullPath: String)
} }

View file

@ -16,11 +16,11 @@ data class Directory(
@ColumnInfo(name = "thumbnail") var tmb: String, @ColumnInfo(name = "thumbnail") var tmb: String,
@ColumnInfo(name = "filename") var name: String, @ColumnInfo(name = "filename") var name: String,
@ColumnInfo(name = "media_count") var mediaCnt: Int, @ColumnInfo(name = "media_count") var mediaCnt: Int,
@ColumnInfo(name = "last_modified") val modified: Long, @ColumnInfo(name = "last_modified") var modified: Long,
@ColumnInfo(name = "date_taken") val taken: Long, @ColumnInfo(name = "date_taken") var taken: Long,
@ColumnInfo(name = "size") val size: Long, @ColumnInfo(name = "size") var size: Long,
@ColumnInfo(name = "is_on_sd_card") val isOnSDCard: Boolean, @ColumnInfo(name = "is_on_sd_card") val isOnSDCard: Boolean,
@ColumnInfo(name = "media_types") val types: Int) : Serializable, Comparable<Directory> { @ColumnInfo(name = "media_types") var types: Int) : Serializable, Comparable<Directory> {
companion object { companion object {
private const val serialVersionUID = -6553345863555455L private const val serialVersionUID = -6553345863555455L