From 69addc428ad8d5d7a1655746c6c9227a83038044 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 4 May 2020 11:04:34 +0200 Subject: [PATCH] improving the way video durations are fetched, should be a lot quicker --- app/build.gradle | 2 +- .../pro/activities/ViewPagerActivity.kt | 2 +- .../gallery/pro/extensions/Context.kt | 80 +++++++++---------- .../gallery/pro/extensions/String.kt | 12 --- .../gallery/pro/fragments/VideoFragment.kt | 4 +- .../gallery/pro/helpers/MediaFetcher.kt | 18 ++--- 6 files changed, 53 insertions(+), 65 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e5960e784..d5dd6293e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -78,7 +78,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.27.2' + implementation 'com.simplemobiletools:commons:5.27.3' implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' implementation 'androidx.multidex:multidex:2.0.1' implementation 'it.sephiroth.android.exif:library:1.0.1' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 75d4f965d..06e97a770 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -389,7 +389,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } val isFavorite = favoritesDB.isFavorite(mPath) - val duration = if (type == TYPE_VIDEOS) mPath.getVideoDuration() else 0 + val duration = if (type == TYPE_VIDEOS) getVideoDuration(mPath) ?: 0 else 0 val ts = System.currentTimeMillis() val medium = Medium(null, mPath.getFilenameFromPath(), mPath, mPath.getParentPath(), ts, ts, File(mPath).length(), type, duration, isFavorite, 0) mediaDB.insert(medium) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 99674593e..559e949a5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -216,16 +216,16 @@ fun Context.getDirectParentSubfolders(dirs: ArrayList, currentPathPre } val directory = Directory(newDirId++, - parent, - subDirs.first().tmb, - getFolderNameFromPath(parent), - subDirs.sumBy { it.mediaCnt }, - lastModified, - dateTaken, - subDirs.sumByLong { it.size }, - getPathLocation(parent), - mediaTypes, - "") + parent, + subDirs.first().tmb, + getFolderNameFromPath(parent), + subDirs.sumBy { it.mediaCnt }, + lastModified, + dateTaken, + subDirs.sumByLong { it.size }, + getPathLocation(parent), + mediaTypes, + "") directory.containsMediaFilesDirectly = false dirs.add(directory) @@ -442,15 +442,15 @@ fun Context.getPathLocation(path: String): Int { fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean, skipMemoryCacheAtPaths: ArrayList? = null) { val options = RequestOptions() - .signature(path.getFileSignature()) - .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .priority(Priority.LOW) - .format(DecodeFormat.PREFER_ARGB_8888) + .signature(path.getFileSignature()) + .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .priority(Priority.LOW) + .format(DecodeFormat.PREFER_ARGB_8888) val builder = Glide.with(applicationContext) - .asBitmap() - .load(path) + .asBitmap() + .load(path) if (cropThumbnails) options.centerCrop() else options.fitCenter() builder.apply(options).into(target) @@ -458,34 +458,34 @@ fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boo fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean, skipMemoryCacheAtPaths: ArrayList? = null) { val options = RequestOptions() - .signature(path.getFileSignature()) - .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) - .priority(Priority.LOW) - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .signature(path.getFileSignature()) + .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) + .priority(Priority.LOW) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) val builder = Glide.with(applicationContext) - .load(path) + .load(path) if (cropThumbnails) options.centerCrop() else options.fitCenter() builder.apply(options) - .transition(DrawableTransitionOptions.withCrossFade()) - .into(target) + .transition(DrawableTransitionOptions.withCrossFade()) + .into(target) } fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnails: Boolean, skipMemoryCacheAtPaths: ArrayList? = null) { val options = RequestOptions() - .signature(path.getFileSignature()) - .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) - .priority(Priority.LOW) - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .signature(path.getFileSignature()) + .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) + .priority(Priority.LOW) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) val builder = Glide.with(applicationContext) - .asBitmap() // make sure the GIF wont animate - .load(path) + .asBitmap() // make sure the GIF wont animate + .load(path) if (cropThumbnails) options.centerCrop() else options.fitCenter() builder.apply(options) - .into(target) + .into(target) } fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boolean) { @@ -493,12 +493,12 @@ fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boo val options = RequestOptions().signature(path.getFileSignature()) Glide.with(applicationContext) - .`as`(PictureDrawable::class.java) - .listener(SvgSoftwareLayerSetter()) - .load(path) - .apply(options) - .transition(DrawableTransitionOptions.withCrossFade()) - .into(target) + .`as`(PictureDrawable::class.java) + .listener(SvgSoftwareLayerSetter()) + .load(path) + .apply(options) + .transition(DrawableTransitionOptions.withCrossFade()) + .into(target) } fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, forceShowHidden: Boolean = false, callback: (ArrayList) -> Unit) { @@ -789,9 +789,9 @@ fun Context.addPathToDB(path: String) { try { val isFavorite = favoritesDB.isFavorite(path) - val videoDuration = if (type == TYPE_VIDEOS) path.getVideoDuration() else 0 + val videoDuration = if (type == TYPE_VIDEOS) getVideoDuration(path) ?: 0 else 0 val medium = Medium(null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), - File(path).length(), type, videoDuration, isFavorite, 0L) + File(path).length(), type, videoDuration, isFavorite, 0L) mediaDB.insert(medium) } catch (ignored: Exception) { @@ -888,7 +888,7 @@ fun Context.updateDirectoryPath(path: String) { fun Context.getFileDateTaken(path: String): Long { val projection = arrayOf( - Images.Media.DATE_TAKEN + Images.Media.DATE_TAKEN ) val uri = Files.getContentUri("external") diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt index 0aa790df9..3f36fa99e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.gallery.pro.extensions import android.content.Context -import android.media.MediaMetadataRetriever import android.os.Environment import com.simplemobiletools.commons.extensions.containsNoMedia import com.simplemobiletools.commons.extensions.doesParentHaveNoMedia @@ -67,15 +66,4 @@ fun String.getDistinctPath(): String { } } -fun String.getVideoDuration(): Int { - var seconds = 0 - try { - val retriever = MediaMetadataRetriever() - retriever.setDataSource(this) - seconds = Math.round(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toInt() / 1000f) - } catch (e: Exception) { - } - return seconds -} - fun String.isDownloadsFolder() = equals(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), true) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt index ed7a61842..4122e2941 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt @@ -29,7 +29,6 @@ import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.PanoramaVideoActivity import com.simplemobiletools.gallery.pro.activities.VideoActivity import com.simplemobiletools.gallery.pro.extensions.config -import com.simplemobiletools.gallery.pro.extensions.getVideoDuration import com.simplemobiletools.gallery.pro.extensions.hasNavBar import com.simplemobiletools.gallery.pro.extensions.parseFileChannel import com.simplemobiletools.gallery.pro.helpers.* @@ -688,7 +687,8 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S private fun setupVideoDuration() { ensureBackgroundThread { - mDuration = mMedium.path.getVideoDuration() + mDuration = context?.getVideoDuration(mMedium.path) ?: 0 + activity?.runOnUiThread { setupTimeHolder() setPosition(0) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 4145ba453..4cbef5ad3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -49,9 +49,9 @@ class MediaFetcher(val context: Context) { val OTGPath = context.config.OTGPath val folders = getLatestFileFolders() folders.addAll(arrayListOf( - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString(), - "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)}/Camera", - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString(), + "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)}/Camera", + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() ).filter { context.getDoesFilePathExist(it, OTGPath) }) val filterMedia = context.config.filterMedia @@ -193,7 +193,7 @@ class MediaFetcher(val context: Context) { val showHidden = config.shouldShowHidden val excludedFolders = config.excludedFolders return foldersToScan.distinctBy { it.getDistinctPath() } - .filter { it.shouldFolderBeVisible(excludedFolders, includedFolders, showHidden, context) }.toMutableSet() as LinkedHashSet + .filter { it.shouldFolderBeVisible(excludedFolders, includedFolders, showHidden, context) }.toMutableSet() as LinkedHashSet } private fun addFolder(curFolders: HashSet, folder: String) { @@ -294,7 +294,7 @@ class MediaFetcher(val context: Context) { } else { val lastModified = if (getProperLastModified) file.lastModified() else 0L var dateTaken = lastModified - val videoDuration = if (getVideoDurations && isVideo) path.getVideoDuration() else 0 + val videoDuration = if (getVideoDurations && isVideo) context.getVideoDuration(path) ?: 0 else 0 if (getProperDateTaken) { var newDateTaken = dateTakens.remove(path) @@ -383,7 +383,7 @@ class MediaFetcher(val context: Context) { } val path = Uri.decode(file.uri.toString().replaceFirst("${context.config.OTGTreeUri}/document/${context.config.OTGPartition}%3A", "${context.config.OTGPath}/")) - val videoDuration = if (getVideoDurations) path.getVideoDuration() else 0 + val videoDuration = if (getVideoDurations) context.getVideoDuration(path) ?: 0 else 0 val isFavorite = favoritePaths.contains(path) val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, videoDuration, isFavorite, 0L) media.add(medium) @@ -396,8 +396,8 @@ class MediaFetcher(val context: Context) { val dateTakens = HashMap() if (folder != FAVORITES) { val projection = arrayOf( - Images.Media.DISPLAY_NAME, - Images.Media.DATE_TAKEN + Images.Media.DISPLAY_NAME, + Images.Media.DATE_TAKEN ) val uri = Files.getContentUri("external") @@ -494,7 +494,7 @@ class MediaFetcher(val context: Context) { val sortDescending = currentGrouping and GROUP_DESCENDING != 0 val sorted = if (currentGrouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || currentGrouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 || - currentGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 || currentGrouping and GROUP_BY_DATE_TAKEN_MONTHLY != 0) { + currentGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 || currentGrouping and GROUP_BY_DATE_TAKEN_MONTHLY != 0) { mediumGroups.toSortedMap(if (sortDescending) compareByDescending { it.toLongOrNull() ?: 0L } else {