Skip to content

Commit 03f5bd4

Browse files
MistaGreensjudd
authored andcommitted
Override default Encoders (#2349)
* Add prepend methods for EncoderRegistry and ResourceEncoderRegistry classes
1 parent 73a8054 commit 03f5bd4

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

library/src/main/java/com/bumptech/glide/Registry.java

+38-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.bumptech.glide.provider.ResourceDecoderRegistry;
2424
import com.bumptech.glide.provider.ResourceEncoderRegistry;
2525
import com.bumptech.glide.util.pool.FactoryPools;
26+
2627
import java.util.ArrayList;
2728
import java.util.Collections;
2829
import java.util.List;
@@ -65,11 +66,24 @@ public Registry() {
6566
* {@link java.io.FileInputStream} and any other subclass.
6667
*
6768
* <p>If multiple {@link Encoder}s are registered for the same type or super type, the
68-
* {@link Encoder} that is registered first will be used. As a result, it's not currently possible
69-
* to replace Glide's default {@link Encoder}s.
69+
* {@link Encoder} that is registered first will be used.
7070
*/
7171
public <Data> Registry register(Class<Data> dataClass, Encoder<Data> encoder) {
72-
encoderRegistry.add(dataClass, encoder);
72+
encoderRegistry.append(dataClass, encoder);
73+
return this;
74+
}
75+
76+
/**
77+
* Prepends the given {@link Encoder} into the list of available {@link Encoder}s
78+
* so that it is attempted before all later and default {@link Encoder}s for the given
79+
* data class.
80+
*
81+
* <p>This method allows you to replace the default {@link Encoder} because it ensures
82+
* the registered {@link Encoder} will run first. If multiple {@link Encoder}s are registered for
83+
* the same type or super type, the {@link Encoder} that is registered first will be used.
84+
*/
85+
public <Data> Registry prepend(Class<Data> dataClass, Encoder<Data> encoder) {
86+
encoderRegistry.prepend(dataClass, encoder);
7387
return this;
7488
}
7589

@@ -139,12 +153,30 @@ public <Data, TResource> Registry prepend(
139153
* {@link com.bumptech.glide.load.resource.gif.GifDrawable} and any other subclass.
140154
*
141155
* <p>If multiple {@link ResourceEncoder}s are registered for the same type or super type, the
142-
* {@link ResourceEncoder} that is registered first will be used. As a result, it's not currently
143-
* possible to replace Glide's default {@link ResourceEncoder}s.
156+
* {@link ResourceEncoder} that is registered first will be used.
144157
*/
145158
public <TResource> Registry register(Class<TResource> resourceClass,
146159
ResourceEncoder<TResource> encoder) {
147-
resourceEncoderRegistry.add(resourceClass, encoder);
160+
resourceEncoderRegistry.append(resourceClass, encoder);
161+
return this;
162+
}
163+
164+
/**
165+
* Registers a new {@link com.bumptech.glide.load.data.DataRewinder.Factory} to handle a
166+
* non-default data type that can be rewind to allow for efficient reads of file headers.
167+
*
168+
* Prepends the given {@link ResourceEncoder} into the list of available {@link ResourceEncoder}s
169+
* so that it is attempted before all later and default {@link ResourceEncoder}s for the given
170+
* data type.
171+
*
172+
* <p>This method allows you to replace the default {@link ResourceEncoder} because it ensures
173+
* the registered {@link ResourceEncoder} will run first. If multiple {@link ResourceEncoder}s are
174+
* registered for the same type or super type, the {@link ResourceEncoder} that is registered
175+
* first will be used.
176+
*/
177+
public <TResource> Registry prepend(Class<TResource> resourceClass,
178+
ResourceEncoder<TResource> encoder) {
179+
resourceEncoderRegistry.prepend(resourceClass, encoder);
148180
return this;
149181
}
150182

library/src/main/java/com/bumptech/glide/provider/EncoderRegistry.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.List;
88

99
/**
10-
* Contains an unordered list of {@link Encoder}s capable of encoding arbitrary data types.
10+
* Contains an ordered list of {@link Encoder}s capable of encoding arbitrary data types.
1111
*/
1212
public class EncoderRegistry {
1313
// TODO: This registry should probably contain a put, rather than a list.
@@ -24,10 +24,14 @@ public synchronized <T> Encoder<T> getEncoder(Class<T> dataClass) {
2424
return null;
2525
}
2626

27-
public synchronized <T> void add(Class<T> dataClass, Encoder<T> encoder) {
27+
public synchronized <T> void append(Class<T> dataClass, Encoder<T> encoder) {
2828
encoders.add(new Entry<>(dataClass, encoder));
2929
}
3030

31+
public synchronized <T> void prepend(Class<T> dataClass, Encoder<T> encoder) {
32+
encoders.add(0, new Entry<>(dataClass, encoder));
33+
}
34+
3135
private static final class Entry<T> {
3236
private final Class<T> dataClass;
3337
@Synthetic final Encoder<T> encoder;

library/src/main/java/com/bumptech/glide/provider/ResourceEncoderRegistry.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package com.bumptech.glide.provider;
22

33
import android.support.annotation.Nullable;
4+
45
import com.bumptech.glide.load.ResourceEncoder;
56
import com.bumptech.glide.util.Synthetic;
7+
68
import java.util.ArrayList;
79
import java.util.List;
810

911
/**
10-
* Contains an unordered list of {@link ResourceEncoder}s capable of encoding arbitrary resource
12+
* Contains an ordered list of {@link ResourceEncoder}s capable of encoding arbitrary resource
1113
* types.
1214
*/
1315
public class ResourceEncoderRegistry {
1416
// TODO: this should probably be a put.
1517
final List<Entry<?>> encoders = new ArrayList<>();
1618

17-
public synchronized <Z> void add(Class<Z> resourceClass, ResourceEncoder<Z> encoder) {
19+
public synchronized <Z> void append(Class<Z> resourceClass, ResourceEncoder<Z> encoder) {
1820
encoders.add(new Entry<>(resourceClass, encoder));
1921
}
2022

23+
public synchronized <Z> void prepend(Class<Z> resourceClass, ResourceEncoder<Z> encoder) {
24+
encoders.add(0, new Entry<>(resourceClass, encoder));
25+
}
26+
2127
@SuppressWarnings("unchecked")
2228
@Nullable
2329
public synchronized <Z> ResourceEncoder<Z> get(Class<Z> resourceClass) {

0 commit comments

Comments
 (0)