From 8a31ad1b1c394c247aaaad71462b68152203a266 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 16 Jan 2018 16:58:58 +0100 Subject: [PATCH] store the medium type in it, to improve performance --- .../gallery/activities/PhotoVideoActivity.kt | 33 ++++++++++--------- .../gallery/adapters/DirectoryAdapter.kt | 11 ++++++- .../gallery/adapters/MediaAdapter.kt | 6 ++-- .../gallery/adapters/MyPagerAdapter.kt | 2 +- .../gallery/dialogs/PickMediumDialog.kt | 2 +- .../gallery/extensions/Activity.kt | 11 ++++--- .../gallery/helpers/Constants.kt | 5 +++ .../gallery/helpers/MediaFetcher.kt | 16 +++++++-- .../gallery/models/Medium.kt | 18 ++++++---- 9 files changed, 69 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt index ef52e77a2..aba3b7060 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -9,10 +9,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View import com.simplemobiletools.commons.dialogs.PropertiesDialog -import com.simplemobiletools.commons.extensions.getFilenameFromUri -import com.simplemobiletools.commons.extensions.getRealPathFromURI -import com.simplemobiletools.commons.extensions.scanPath -import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.IS_FROM_GALLERY import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.REAL_FILE_PATH @@ -21,9 +18,7 @@ import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.fragments.PhotoFragment import com.simplemobiletools.gallery.fragments.VideoFragment import com.simplemobiletools.gallery.fragments.ViewPagerFragment -import com.simplemobiletools.gallery.helpers.IS_VIEW_INTENT -import com.simplemobiletools.gallery.helpers.MEDIUM -import com.simplemobiletools.gallery.helpers.PATH +import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.fragment_holder.* import java.io.File @@ -52,6 +47,14 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList } } + override fun onResume() { + super.onResume() + supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background)) + if (config.blackBackground) { + updateStatusbarColor(Color.BLACK) + } + } + private fun checkIntent(savedInstanceState: Bundle? = null) { mUri = intent.data ?: return if (intent.extras?.containsKey(REAL_FILE_PATH) == true) { @@ -78,7 +81,13 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList showSystemUI() val bundle = Bundle() val file = File(mUri.toString()) - mMedium = Medium(getFilenameFromUri(mUri!!), mUri.toString(), mIsVideo, 0, 0, file.length()) + val type = when { + file.isImageFast() -> TYPE_IMAGE + file.isVideoFast() -> TYPE_VIDEO + else -> TYPE_GIF + } + + mMedium = Medium(getFilenameFromUri(mUri!!), mUri.toString(), 0, 0, file.length(), type) supportActionBar?.title = mMedium!!.name bundle.putSerializable(MEDIUM, mMedium) @@ -99,14 +108,6 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList } } - override fun onResume() { - super.onResume() - supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background)) - if (config.blackBackground) { - updateStatusbarColor(Color.BLACK) - } - } - private fun sendViewPagerIntent(path: String) { Intent(this, ViewPagerActivity::class.java).apply { putExtra(IS_VIEW_INTENT, true) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index e1e0def89..559a9bcc4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -17,6 +17,9 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog import com.simplemobiletools.gallery.dialogs.PickMediumDialog import com.simplemobiletools.gallery.extensions.* +import com.simplemobiletools.gallery.helpers.TYPE_GIF +import com.simplemobiletools.gallery.helpers.TYPE_IMAGE +import com.simplemobiletools.gallery.helpers.TYPE_VIDEO import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST import com.simplemobiletools.gallery.models.AlbumCover import com.simplemobiletools.gallery.models.Directory @@ -334,7 +337,13 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList TYPE_IMAGE + directory.tmb.isVideoFast() -> TYPE_VIDEO + else -> TYPE_GIF + } + + activity.loadImage(thumbnailType, directory.tmb, dir_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) dir_pin.beVisibleIf(pinnedFolders.contains(directory.path)) dir_sd_card.beVisibleIf(activity.isPathOnSD(directory.path)) photo_cnt.beVisibleIf(showMediaCount) 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 66df85b70..cb5dad83c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -282,20 +282,20 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, private fun setupView(view: View, medium: Medium) { view.apply { - play_outline.beVisibleIf(medium.video) + play_outline.beVisibleIf(medium.isVideo()) photo_name.beVisibleIf(displayFilenames || isListViewType) photo_name.text = medium.name photo_name.tag = medium.path if (loadImageInstantly) { - activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) + activity.loadImage(medium.type, medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) } else { medium_thumbnail.setImageDrawable(null) medium_thumbnail.isHorizontalScrolling = scrollHorizontally delayHandler.postDelayed({ val isVisible = visibleItemPaths.contains(medium.path) if (isVisible) { - activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) + activity.loadImage(medium.type, medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) } }, IMAGE_LOAD_DELAY) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt index 0f5d7fee3..0317a1b36 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt @@ -23,7 +23,7 @@ class MyPagerAdapter(val activity: ViewPagerActivity, fm: FragmentManager, val m bundle.putSerializable(MEDIUM, medium) val fragment: ViewPagerFragment - fragment = if (medium.video) { + fragment = if (medium.isVideo()) { VideoFragment() } else { PhotoFragment() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt index 61264f169..b94fd0aed 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt @@ -35,7 +35,7 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c activity.setupDialogStuff(view, this, R.string.select_photo) } - val media = activity.getCachedMedia(path).filter { !it.video } as ArrayList + val media = activity.getCachedMedia(path).filter { !it.isVideo() } as ArrayList if (media.isNotEmpty()) { gotMedia(media) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index 9d3cd9335..e2780eb71 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -22,6 +22,9 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.helpers.NOMEDIA +import com.simplemobiletools.gallery.helpers.TYPE_GIF +import com.simplemobiletools.gallery.helpers.TYPE_IMAGE +import com.simplemobiletools.gallery.helpers.TYPE_VIDEO import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.views.MySquareImageView @@ -139,15 +142,15 @@ fun BaseSimpleActivity.toggleFileVisibility(oldFile: File, hide: Boolean, callba } } -fun Activity.loadImage(path: String, target: MySquareImageView, horizontalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean) { +fun Activity.loadImage(type: Int, path: String, target: MySquareImageView, horizontalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean) { target.isHorizontalScrolling = horizontalScroll - if (path.isImageFast() || path.isVideoFast()) { - if (path.isPng()) { + if (type == TYPE_IMAGE || type == TYPE_VIDEO) { + if (type == TYPE_IMAGE && path.isPng()) { loadPng(path, target, cropThumbnails) } else { loadJpg(path, target, cropThumbnails) } - } else if (path.isGif()) { + } else if (type == TYPE_GIF) { try { val gifDrawable = GifDrawable(path) target.setImageDrawable(gifDrawable) 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 cf5bd0fa3..17e2dab26 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -101,3 +101,8 @@ val EXT_EXIF_PROPERTIES = 128 val EXT_DURATION = 256 val EXT_ARTIST = 512 val EXT_ALBUM = 1024 + +// media types +val TYPE_IMAGE = 1 +val TYPE_VIDEO = 2 +val TYPE_GIF = 3 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index e0e199df2..8f5eb9dec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -153,7 +153,13 @@ class MediaFetcher(val context: Context) { val dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN) val dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L - val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size) + val type = when { + isImage -> TYPE_IMAGE + isVideo -> TYPE_VIDEO + else -> TYPE_GIF + } + + val medium = Medium(filename, path, dateModified, dateTaken, size, type) curMedia.add(medium) } catch (e: Exception) { continue @@ -255,7 +261,13 @@ class MediaFetcher(val context: Context) { val dateTaken = file.lastModified() val dateModified = file.lastModified() - val medium = Medium(filename, file.absolutePath, isVideo, dateModified, dateTaken, size) + val type = when { + isImage -> TYPE_IMAGE + isVideo -> TYPE_VIDEO + else -> TYPE_GIF + } + + val medium = Medium(filename, file.absolutePath, dateModified, dateTaken, size, type) val isAlreadyAdded = curMedia.any { it.path == file.absolutePath } if (!isAlreadyAdded) { curMedia.add(medium) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt index 1d113e4aa..fb9e7a213 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt @@ -1,22 +1,26 @@ package com.simplemobiletools.gallery.models -import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.extensions.formatDate +import com.simplemobiletools.commons.extensions.formatSize +import com.simplemobiletools.commons.extensions.getMimeTypeFromPath +import com.simplemobiletools.commons.extensions.isDng import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.gallery.helpers.TYPE_GIF +import com.simplemobiletools.gallery.helpers.TYPE_IMAGE +import com.simplemobiletools.gallery.helpers.TYPE_VIDEO import java.io.Serializable -data class Medium(var name: String, var path: String, val video: Boolean, val modified: Long, val taken: Long, val size: Long) : Serializable, Comparable { +data class Medium(var name: String, var path: String, val modified: Long, val taken: Long, val size: Long, val type: Int) : Serializable, Comparable { companion object { private val serialVersionUID = -6553149366975455L var sorting: Int = 0 } - fun isPng() = path.isPng() + fun isGif() = type == TYPE_GIF - fun isGif() = path.isGif() + fun isImage() = type == TYPE_IMAGE - fun isJpg() = path.endsWith(".jpg", true) || path.endsWith(".jpeg", true) - - fun isImage() = !isGif() && !video + fun isVideo() = type == TYPE_VIDEO fun isDng() = path.isDng()