From 5b5f81fda585bb00424ccbb7f101384499857f46 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 1 Jul 2017 00:04:31 +0200 Subject: [PATCH] update Commons and Glide --- app/build.gradle | 2 +- .../gallery/activities/MediaActivity.kt | 22 ++++--- .../gallery/adapters/DirectoryAdapter.kt | 2 +- .../gallery/adapters/MediaAdapter.kt | 2 +- .../gallery/extensions/activity.kt | 63 ++++++++++--------- .../gallery/extensions/string.kt | 6 ++ .../gallery/fragments/PhotoFragment.kt | 54 ++++++++++------ .../helpers/GlideRotateTransformation.kt | 5 +- 8 files changed, 94 insertions(+), 62 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/extensions/string.kt diff --git a/app/build.gradle b/app/build.gradle index 72121121c..c35fa18e8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.21.17' + compile 'com.simplemobiletools:commons:2.22.0' compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0' compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' 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 d2d33fb8f..0a5a34bab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -9,14 +9,14 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.support.v7.widget.GridLayoutManager -import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.ViewGroup import android.widget.FrameLayout import com.bumptech.glide.Glide -import com.bumptech.glide.request.animation.GlideAnimation +import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.SimpleTarget +import com.bumptech.glide.request.transition.Transition import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.dialogs.ConfirmationDialog @@ -390,18 +390,22 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { val wantedWidth = wallpaperDesiredMinimumWidth val wantedHeight = wallpaperDesiredMinimumHeight val ratio = wantedWidth.toFloat() / wantedHeight - Glide.with(this) - .load(File(path)) - .asBitmap() + + val options = RequestOptions() .override((wantedWidth * ratio).toInt(), wantedHeight) .fitCenter() + + Glide.with(this) + .asBitmap() + .load(File(path)) + .apply(options) .into(object : SimpleTarget() { - override fun onResourceReady(bitmap: Bitmap?, glideAnimation: GlideAnimation?) { + override fun onResourceReady(resource: Bitmap?, transition: Transition?) { try { - WallpaperManager.getInstance(applicationContext).setBitmap(bitmap) + WallpaperManager.getInstance(applicationContext).setBitmap(resource) setResult(Activity.RESULT_OK) - } catch (e: IOException) { - Log.e(TAG, "item click $e") + } catch (ignored: IOException) { + } finish() 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 3d437756c..646954bcf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -455,7 +455,7 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList, } fun stopLoad() { - Glide.clear(view.medium_thumbnail) + Glide.with(activity).clear(view.medium_thumbnail) } } 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 cc9125a8e..451e2bdff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -15,7 +15,8 @@ import android.view.ViewConfiguration import com.bumptech.glide.Glide import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.signature.StringSignature +import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.bumptech.glide.request.RequestOptions import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.* @@ -262,8 +263,6 @@ fun SimpleActivity.toggleFileVisibility(oldFile: File, hide: Boolean, callback: } } -fun Activity.getFileSignature(path: String) = StringSignature(File(path).lastModified().toString()) - fun Activity.loadImage(path: String, target: MySquareImageView, verticalScroll: Boolean) { target.isVerticalScrolling = verticalScroll if (path.isImageFast() || path.isVideoFast()) { @@ -282,49 +281,55 @@ fun Activity.loadImage(path: String, target: MySquareImageView, verticalScroll: } fun Activity.loadPng(path: String, target: MySquareImageView) { - val builder = Glide.with(applicationContext) - .load(path) - .asBitmap() - .signature(getFileSignature(path)) - .diskCacheStrategy(DiskCacheStrategy.RESULT) + val options = RequestOptions() + .signature(path.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) .format(DecodeFormat.PREFER_ARGB_8888) - if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter() - builder.into(target) + val builder = Glide.with(applicationContext) + .asBitmap() + .load(path) + + if (config.cropThumbnails) options.centerCrop() else options.fitCenter() + builder.apply(options).into(target) } fun Activity.loadJpg(path: String, target: MySquareImageView) { + val options = RequestOptions() + .signature(path.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + val builder = Glide.with(applicationContext) .load(path) - .signature(getFileSignature(path)) - .diskCacheStrategy(DiskCacheStrategy.RESULT) - .crossFade() - if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter() - builder.into(target) + if (config.cropThumbnails) options.centerCrop() else options.fitCenter() + builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) } fun Activity.loadAnimatedGif(path: String, target: MySquareImageView) { - val builder = Glide.with(applicationContext) - .load(path) - .asGif() - .signature(getFileSignature(path)) + val options = RequestOptions() + .signature(path.getFileSignature()) .diskCacheStrategy(DiskCacheStrategy.NONE) - .crossFade() - if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter() - builder.into(target) + val builder = Glide.with(applicationContext) + .asGif() + .load(path) + + if (config.cropThumbnails) options.centerCrop() else options.fitCenter() + builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) } fun Activity.loadStaticGif(path: String, target: MySquareImageView) { - val builder = Glide.with(applicationContext) - .load(path) - .asBitmap() - .signature(getFileSignature(path)) - .diskCacheStrategy(DiskCacheStrategy.SOURCE) + val options = RequestOptions() + .signature(path.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.DATA) - if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter() - builder.into(target) + val builder = Glide.with(applicationContext) + .asBitmap() + .load(path) + + if (config.cropThumbnails) options.centerCrop() else options.fitCenter() + builder.apply(options).into(target) } fun Activity.getCachedDirectories(): ArrayList { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/string.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/string.kt new file mode 100644 index 000000000..47837c672 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/string.kt @@ -0,0 +1,6 @@ +package com.simplemobiletools.gallery.extensions + +import com.bumptech.glide.signature.ObjectKey +import java.io.File + +fun String.getFileSignature() = ObjectKey(File(this).lastModified().toString()) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt index f6d8b1a80..134af8f40 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt @@ -7,15 +7,20 @@ import android.graphics.Color import android.graphics.Matrix import android.graphics.drawable.ColorDrawable import android.net.Uri +import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.bumptech.glide.Glide import com.bumptech.glide.Priority +import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.load.engine.GlideException +import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestListener +import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.Target import com.davemorrissey.labs.subscaleview.ImageSource import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView @@ -143,12 +148,15 @@ class PhotoFragment : ViewPagerFragment() { private fun loadImage() { if (medium.isGif()) { - Glide.with(this) - .load(medium.path) - .asGif() - .crossFade() + val options = RequestOptions() .priority(if (isFragmentVisible) Priority.IMMEDIATE else Priority.LOW) - .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .diskCacheStrategy(DiskCacheStrategy.DATA) + + Glide.with(this) + .asGif() + .load(medium.path) + .transition(DrawableTransitionOptions.withCrossFade()) + .apply(options) .into(view.photo_view) } else { loadBitmap() @@ -160,31 +168,37 @@ class PhotoFragment : ViewPagerFragment() { val targetWidth = if (ViewPagerActivity.screenWidth == 0) Target.SIZE_ORIGINAL else ViewPagerActivity.screenWidth val targetHeight = if (ViewPagerActivity.screenHeight == 0) Target.SIZE_ORIGINAL else ViewPagerActivity.screenHeight - Glide.with(this) - .load(medium.path) - .asBitmap() - .signature(activity.getFileSignature(medium.path)) - .format(if (medium.isPng()) DecodeFormat.PREFER_ARGB_8888 else DecodeFormat.PREFER_RGB_565) + val options = RequestOptions() + .signature(medium.path.getFileSignature()) + .format(DecodeFormat.PREFER_ARGB_8888) .diskCacheStrategy(DiskCacheStrategy.NONE) .override(targetWidth, targetHeight) - .listener(object : RequestListener { - override fun onException(e: Exception?, model: String?, target: Target?, isFirstResource: Boolean): Boolean { + + Glide.with(this) + .asBitmap() + .load(medium.path) + .apply(options) + .listener(object : RequestListener { + override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { return false } - override fun onResourceReady(resource: Bitmap, model: String?, target: Target?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean { + override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { if (isFragmentVisible) addZoomableView() return false } }).into(view.photo_view) } else { - Glide.with(this) - .load(medium.path) - .asBitmap() - .transform(GlideRotateTransformation(context, degrees)) - .thumbnail(0.2f) + val options = RequestOptions() .diskCacheStrategy(DiskCacheStrategy.NONE) + .transform(GlideRotateTransformation(context, degrees)) + + Glide.with(this) + .asBitmap() + .load(medium.path) + .thumbnail(0.2f) + .apply(options) .into(view.photo_view) } } @@ -257,7 +271,9 @@ class PhotoFragment : ViewPagerFragment() { override fun onDestroyView() { super.onDestroyView() - Glide.clear(view.photo_view) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && !activity.isDestroyed) { + Glide.with(context).clear(view.photo_view) + } } override fun onConfigurationChanged(newConfig: Configuration?) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/GlideRotateTransformation.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/GlideRotateTransformation.kt index 4e347dd9e..8a48b23f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/GlideRotateTransformation.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/GlideRotateTransformation.kt @@ -5,9 +5,9 @@ import android.graphics.Bitmap import android.graphics.Matrix import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool import com.bumptech.glide.load.resource.bitmap.BitmapTransformation +import java.security.MessageDigest class GlideRotateTransformation(context: Context, val rotateRotationAngle: Float) : BitmapTransformation(context) { - override fun transform(pool: BitmapPool, bitmap: Bitmap, outWidth: Int, outHeight: Int): Bitmap { if (rotateRotationAngle % 360 == 0f) return bitmap @@ -17,5 +17,6 @@ class GlideRotateTransformation(context: Context, val rotateRotationAngle: Float return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) } - override fun getId() = "GlideRotateTransformation $rotateRotationAngle" + override fun updateDiskCacheKey(messageDigest: MessageDigest?) { + } }