Skip to content

Commit f4a4e67

Browse files
authored
Merge pull request #875 from wordpress-mobile/anitaa/update-glide-version
Update glide to the latest version
2 parents d417331 + d878dee commit f4a4e67

File tree

4 files changed

+140
-94
lines changed

4 files changed

+140
-94
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
kotlinCoroutinesVersion = '1.1.0'
66
supportLibVersion = '27.1.1'
77
tagSoupVersion = '1.2.1'
8-
glideVersion = '3.7.0'
8+
glideVersion = '4.10.0'
99
picassoVersion = '2.5.2'
1010
robolectricVersion = '3.5.1'
1111
jUnitVersion = '4.12'

glide-loader/src/main/java/org/wordpress/aztec/glideloader/GlideImageLoader.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import android.graphics.drawable.Drawable
77
import android.util.DisplayMetrics
88
import com.bumptech.glide.Glide
99
import com.bumptech.glide.request.Request
10-
import com.bumptech.glide.request.animation.GlideAnimation
1110
import com.bumptech.glide.request.target.SizeReadyCallback
1211
import com.bumptech.glide.request.target.Target
12+
import com.bumptech.glide.request.transition.Transition
1313
import org.wordpress.aztec.Html
1414
import org.wordpress.aztec.glideloader.extensions.upscaleTo
1515

@@ -20,32 +20,35 @@ class GlideImageLoader(private val context: Context) : Html.ImageGetter {
2020
}
2121

2222
override fun loadImage(source: String, callbacks: Html.ImageGetter.Callbacks, maxWidth: Int, minWidth: Int) {
23-
Glide.with(context).load(source).asBitmap().into(object : Target<Bitmap> {
23+
Glide.with(context).asBitmap().load(source).into(object : Target<Bitmap> {
2424
override fun onLoadStarted(placeholder: Drawable?) {
2525
callbacks.onImageLoading(placeholder)
2626
}
2727

28-
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
28+
override fun onLoadFailed(errorDrawable: Drawable?) {
2929
callbacks.onImageFailed()
3030
}
3131

32-
override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
32+
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
3333
//Upscaling bitmap only for demonstration purposes.
3434
//This should probably be done somewhere more appropriate for Glide (?).
35-
if (resource != null && resource.width < minWidth) {
35+
if (resource.width < minWidth) {
3636
return callbacks.onImageLoaded(BitmapDrawable(context.resources, resource.upscaleTo(minWidth)))
3737
}
3838

3939
// By default, BitmapFactory.decodeFile sets the bitmap's density to the device default so, we need
4040
// to correctly set the input density to 160 ourselves.
41-
resource?.density = DisplayMetrics.DENSITY_DEFAULT
41+
resource.density = DisplayMetrics.DENSITY_DEFAULT
4242
callbacks.onImageLoaded(BitmapDrawable(context.resources, resource))
4343
}
4444

4545
override fun onLoadCleared(placeholder: Drawable?) {}
4646

47-
override fun getSize(cb: SizeReadyCallback?) {
48-
cb?.onSizeReady(maxWidth, Target.SIZE_ORIGINAL)
47+
override fun getSize(cb: SizeReadyCallback) {
48+
cb.onSizeReady(maxWidth, Target.SIZE_ORIGINAL)
49+
}
50+
51+
override fun removeCallback(cb: SizeReadyCallback) {
4952
}
5053

5154
override fun setRequest(request: Request?) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.wordpress.aztec.glideloader
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.media.MediaMetadataRetriever
6+
import android.net.Uri
7+
import com.bumptech.glide.Glide
8+
import com.bumptech.glide.Priority
9+
import com.bumptech.glide.Registry
10+
import com.bumptech.glide.annotation.GlideModule
11+
import com.bumptech.glide.load.DataSource
12+
import com.bumptech.glide.load.Options
13+
import com.bumptech.glide.load.data.DataFetcher
14+
import com.bumptech.glide.load.model.ModelLoader
15+
import com.bumptech.glide.load.model.ModelLoaderFactory
16+
import com.bumptech.glide.load.model.MultiModelLoaderFactory
17+
import com.bumptech.glide.module.LibraryGlideModule
18+
import com.bumptech.glide.signature.ObjectKey
19+
import java.io.ByteArrayInputStream
20+
import java.io.ByteArrayOutputStream
21+
import java.io.IOException
22+
import java.io.InputStream
23+
24+
@GlideModule
25+
class GlideLoaderModule : LibraryGlideModule() {
26+
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
27+
registry.append(String::class.java, InputStream::class.java, ThumbnailLoader.Factory(context))
28+
super.registerComponents(context, glide, registry)
29+
}
30+
31+
internal class ThumbnailLoader(private val context: Context) : ModelLoader<String, InputStream> {
32+
override fun buildLoadData(
33+
model: String,
34+
width: Int,
35+
height: Int,
36+
options: Options
37+
): ModelLoader.LoadData<InputStream>? {
38+
return ModelLoader.LoadData<InputStream>(ObjectKey(model), VideoThumbnailFetcher(model, context))
39+
}
40+
41+
override fun handles(model: String): Boolean {
42+
return true
43+
}
44+
45+
internal class Factory(private val context: Context) : ModelLoaderFactory<String, InputStream> {
46+
override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<String, InputStream> {
47+
return ThumbnailLoader(context)
48+
}
49+
50+
override fun teardown() = Unit
51+
}
52+
53+
class VideoThumbnailFetcher(val source: String, private val context: Context) : DataFetcher<InputStream> {
54+
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
55+
val retriever = MediaMetadataRetriever()
56+
try {
57+
58+
val uri = Uri.parse(source)
59+
val isRemote = uri?.scheme?.startsWith("http", true) ?: false
60+
if (isRemote) {
61+
retriever.setDataSource(source, emptyMap())
62+
} else {
63+
retriever.setDataSource(context, uri)
64+
}
65+
66+
if (cancelled) return
67+
val picture = retriever.frameAtTime
68+
if (cancelled) return
69+
if (picture != null) {
70+
val bitmapData = ByteArrayOutputStream().use { bos ->
71+
picture.compress(Bitmap.CompressFormat.JPEG, 90, bos)
72+
bos.toByteArray()
73+
}
74+
if (cancelled) return
75+
stream = ByteArrayInputStream(bitmapData)
76+
return callback.onDataReady(stream)
77+
}
78+
} finally {
79+
retriever.release()
80+
}
81+
}
82+
83+
override fun getDataClass(): Class<InputStream> {
84+
return InputStream::class.java
85+
}
86+
87+
override fun getDataSource(): DataSource {
88+
return DataSource.REMOTE
89+
}
90+
91+
var stream: InputStream? = null
92+
@Volatile
93+
var cancelled = false
94+
95+
override fun cleanup() = try {
96+
stream?.close()
97+
} catch (e: IOException) {
98+
// Just Ignore it
99+
} ?: Unit
100+
101+
override fun cancel() {
102+
cancelled = true
103+
}
104+
}
105+
}
106+
}

glide-loader/src/main/java/org/wordpress/aztec/glideloader/GlideVideoThumbnailLoader.kt

Lines changed: 22 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,62 @@ import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.drawable.BitmapDrawable
66
import android.graphics.drawable.Drawable
7-
import android.media.MediaMetadataRetriever
8-
import android.net.Uri
97
import android.util.DisplayMetrics
108
import com.bumptech.glide.Glide
11-
import com.bumptech.glide.Priority
12-
import com.bumptech.glide.load.data.DataFetcher
13-
import com.bumptech.glide.load.model.GenericLoaderFactory
14-
import com.bumptech.glide.load.model.ModelLoaderFactory
15-
import com.bumptech.glide.load.model.stream.StreamModelLoader
169
import com.bumptech.glide.request.Request
17-
import com.bumptech.glide.request.animation.GlideAnimation
1810
import com.bumptech.glide.request.target.SizeReadyCallback
1911
import com.bumptech.glide.request.target.Target
12+
import com.bumptech.glide.request.transition.Transition
2013
import org.wordpress.aztec.Html
2114
import org.wordpress.aztec.glideloader.extensions.upscaleTo
22-
import java.io.ByteArrayInputStream
23-
import java.io.ByteArrayOutputStream
24-
import java.io.IOException
25-
import java.io.InputStream
2615

2716
class GlideVideoThumbnailLoader(private val context: Context) : Html.VideoThumbnailGetter {
2817

2918
override fun loadVideoThumbnail(source: String, callbacks: Html.VideoThumbnailGetter.Callbacks, maxWidth: Int) {
3019
loadVideoThumbnail(source, callbacks, maxWidth, 0)
3120
}
3221

33-
override fun loadVideoThumbnail(source: String, callbacks: Html.VideoThumbnailGetter.Callbacks, maxWidth: Int, minWidth: Int) {
34-
22+
override fun loadVideoThumbnail(
23+
source: String,
24+
callbacks: Html.VideoThumbnailGetter.Callbacks,
25+
maxWidth: Int,
26+
minWidth: Int
27+
) {
3528
Glide.with(context)
36-
.using(ThumbnailLoader(context))
37-
.load(source)
3829
.asBitmap()
30+
.load(source)
3931
.fitCenter()
4032
.into(object : Target<Bitmap> {
4133
override fun onLoadStarted(placeholder: Drawable?) {
4234
callbacks.onThumbnailLoading(placeholder)
4335
}
4436

45-
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
37+
override fun onLoadFailed(errorDrawable: Drawable?) {
4638
callbacks.onThumbnailFailed()
4739
}
4840

49-
override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
41+
override fun onResourceReady(resource: Bitmap, glideAnimation: Transition<in Bitmap>?) {
5042
//Upscaling bitmap only for demonstration purposes.
5143
//This should probably be done somewhere more appropriate for Glide (?).
52-
if (resource != null && resource.width < minWidth) {
53-
return callbacks.onThumbnailLoaded(BitmapDrawable(context.resources, resource.upscaleTo(minWidth)))
44+
if (resource.width < minWidth) {
45+
return callbacks.onThumbnailLoaded(
46+
BitmapDrawable(context.resources, resource.upscaleTo(minWidth))
47+
)
5448
}
5549

56-
// By default, BitmapFactory.decodeFile sets the bitmap's density to the device default so, we need
57-
// to correctly set the input density to 160 ourselves.
58-
resource?.density = DisplayMetrics.DENSITY_DEFAULT
50+
// By default, BitmapFactory.decodeFile sets the bitmap's density to the device default so,
51+
// we need to correctly set the input density to 160 ourselves.
52+
resource.density = DisplayMetrics.DENSITY_DEFAULT
5953
callbacks.onThumbnailLoaded(BitmapDrawable(context.resources, resource))
6054
}
6155

6256
override fun onLoadCleared(placeholder: Drawable?) {}
6357

64-
override fun getSize(cb: SizeReadyCallback?) {
65-
cb?.onSizeReady(maxWidth, maxWidth)
58+
override fun getSize(cb: SizeReadyCallback) {
59+
cb.onSizeReady(maxWidth, maxWidth)
60+
}
61+
62+
override fun removeCallback(cb: SizeReadyCallback) {
6663
}
6764

6865
override fun setRequest(request: Request?) {
@@ -82,64 +79,4 @@ class GlideVideoThumbnailLoader(private val context: Context) : Html.VideoThumbn
8279
}
8380
})
8481
}
85-
86-
// Based on a Gist from Stepan Goncharov (https://gist.github.com/stepango/5edcbdb408b0ba87f8383f868961c257)
87-
internal class ThumbnailLoader(val context: Context) : StreamModelLoader<String> {
88-
89-
override fun getResourceFetcher(src: String, width: Int, height: Int) = VideoThumbnailFetcher(src, context)
90-
91-
internal class Factory : ModelLoaderFactory<String, InputStream> {
92-
override fun build(context: Context, factories: GenericLoaderFactory) = ThumbnailLoader(context)
93-
94-
override fun teardown() = Unit
95-
}
96-
97-
class VideoThumbnailFetcher(val source: String, val context: Context) : DataFetcher<InputStream> {
98-
var stream: InputStream? = null
99-
@Volatile
100-
var cancelled = false
101-
102-
override fun getId(): String = source
103-
104-
override fun loadData(priority: Priority): InputStream? {
105-
val retriever = MediaMetadataRetriever()
106-
try {
107-
108-
val uri = Uri.parse(source)
109-
val isRemote = uri?.scheme?.startsWith("http", true) ?: false
110-
if (isRemote) {
111-
retriever.setDataSource(source, emptyMap())
112-
} else {
113-
retriever.setDataSource(context, uri)
114-
}
115-
116-
if (cancelled) return null
117-
val picture = retriever.frameAtTime
118-
if (cancelled) return null
119-
if (picture != null) {
120-
val bitmapData = ByteArrayOutputStream().use { bos ->
121-
picture.compress(Bitmap.CompressFormat.JPEG, 90, bos)
122-
bos.toByteArray()
123-
}
124-
if (cancelled) return null
125-
stream = ByteArrayInputStream(bitmapData)
126-
return stream
127-
}
128-
} finally {
129-
retriever.release()
130-
}
131-
return null
132-
}
133-
134-
override fun cleanup() = try {
135-
stream?.close()
136-
} catch (e: IOException) {
137-
// Just Ignore it
138-
} ?: Unit
139-
140-
override fun cancel() {
141-
cancelled = true
142-
}
143-
}
144-
}
14582
}

0 commit comments

Comments
 (0)