Skip to content

Commit f73f003

Browse files
sjuddglide-copybara-robot
authored andcommitted
Enable DirectResourceLoader in Glide
The new loaders respect themes, dark mode, screen sizes and other resource attributes that are not supported by contentResolver.openInputStream. We can only use them reliably with resource ids and uris that are owned by the same package as the resource, so the legacy loaders will continue to live for other packages. There are also some edge cases, like videos in raw resources, that are not supported by the direct resource loaders. This will result in a behavior change for applications. Resources that were previously loaded with default attributes will now be loaded instead with the appropriate theme / dark mode / size etc. PiperOrigin-RevId: 511612517
1 parent 62654be commit f73f003

File tree

9 files changed

+82
-163
lines changed

9 files changed

+82
-163
lines changed

instrumentation/src/androidTest/java/com/bumptech/glide/DarkModeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class DarkModeTest {
4646

4747
@Rule
4848
public final IdlingGlideRule idlingGlideRule =
49-
IdlingGlideRule.newGlideRule(glideBuilder -> glideBuilder.useDirectResourceLoader(true));
49+
IdlingGlideRule.newGlideRule(glideBuilder -> glideBuilder);
5050

5151
@Before
5252
public void before() {

instrumentation/src/androidTest/java/com/bumptech/glide/NonBitmapDrawableResourcesTest.java

+3-22
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,25 @@
1717
import android.graphics.drawable.Drawable;
1818
import android.net.Uri;
1919
import androidx.test.core.app.ApplicationProvider;
20+
import androidx.test.ext.junit.runners.AndroidJUnit4;
2021
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
2122
import com.bumptech.glide.test.ResourceIds;
2223
import com.bumptech.glide.testutil.TearDownGlide;
23-
import com.google.common.collect.ImmutableList;
2424
import java.util.HashSet;
2525
import java.util.List;
2626
import java.util.Set;
2727
import java.util.concurrent.ExecutionException;
28-
import org.junit.Before;
2928
import org.junit.Rule;
3029
import org.junit.Test;
3130
import org.junit.function.ThrowingRunnable;
3231
import org.junit.rules.TestName;
3332
import org.junit.runner.RunWith;
34-
import org.junit.runners.Parameterized;
35-
import org.junit.runners.Parameterized.Parameter;
36-
import org.junit.runners.Parameterized.Parameters;
3733

38-
@RunWith(Parameterized.class)
34+
@RunWith(AndroidJUnit4.class)
3935
public class NonBitmapDrawableResourcesTest {
4036
@Rule public final TestName testName = new TestName();
4137
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide();
42-
43-
@Parameter public boolean useDirectResourceLoader;
44-
45-
@Parameters(name = "useDirectResourceLoader = {0}")
46-
public static ImmutableList<Boolean> parameters() {
47-
return ImmutableList.of(true, false);
48-
}
49-
50-
private Context context;
51-
52-
@Before
53-
public void setUp() {
54-
context = ApplicationProvider.getApplicationContext();
55-
56-
Glide.init(context, new GlideBuilder().useDirectResourceLoader(useDirectResourceLoader));
57-
}
38+
private final Context context = ApplicationProvider.getApplicationContext();
5839

5940
@Test
6041
public void load_withBitmapResourceId_asDrawable_producesNonNullDrawable()

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

-17
Original file line numberDiff line numberDiff line change
@@ -487,23 +487,6 @@ public GlideBuilder setImageDecoderEnabledForBitmaps(boolean isEnabled) {
487487
return this;
488488
}
489489

490-
/**
491-
* Use {@link com.bumptech.glide.load.model.DirectResourceLoader} instead of {@link
492-
* com.bumptech.glide.load.model.ResourceLoader} so that we load drawables asynchronously with the
493-
* correc theme (ie light / dark mode).
494-
*
495-
* <p>This also removes support for loading resources as resource Uris and for loading {@link
496-
* android.os.ParcelFileDescriptor}s from resource ids. I think neither of those are useful but if
497-
* you have a use case or seem to find some test failing with this experiment enabled, please file
498-
* an issue so we can investigate.
499-
*
500-
* <p>This flag is experimental and will be removed without notice in a future version.
501-
*/
502-
public GlideBuilder useDirectResourceLoader(boolean isEnabled) {
503-
glideExperimentsBuilder.update(new UseDirectResourceLoader(), isEnabled);
504-
return this;
505-
}
506-
507490
void setRequestManagerFactory(@Nullable RequestManagerFactory factory) {
508491
this.requestManagerFactory = factory;
509492
}

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

+39-42
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import androidx.annotation.Nullable;
1414
import androidx.tracing.Trace;
1515
import com.bumptech.glide.GlideBuilder.EnableImageDecoderForBitmaps;
16-
import com.bumptech.glide.GlideBuilder.UseDirectResourceLoader;
1716
import com.bumptech.glide.gifdecoder.GifDecoder;
1817
import com.bumptech.glide.load.ImageHeaderParser;
1918
import com.bumptech.glide.load.ResourceDecoder;
@@ -274,48 +273,47 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
274273
registry.register(new ParcelFileDescriptorRewinder.Factory());
275274
}
276275

277-
if (experiments.isEnabled(UseDirectResourceLoader.class)) {
278-
ModelLoaderFactory<Integer, InputStream> inputStreamFactory =
279-
DirectResourceLoader.inputStreamFactory(context);
280-
ModelLoaderFactory<Integer, AssetFileDescriptor> assetFileDescriptorFactory =
281-
DirectResourceLoader.assetFileDescriptorFactory(context);
282-
ModelLoaderFactory<Integer, Drawable> drawableFactory =
283-
DirectResourceLoader.drawableFactory(context);
284-
registry
285-
.append(int.class, InputStream.class, inputStreamFactory)
286-
.append(Integer.class, InputStream.class, inputStreamFactory)
287-
.append(int.class, AssetFileDescriptor.class, assetFileDescriptorFactory)
288-
.append(Integer.class, AssetFileDescriptor.class, assetFileDescriptorFactory)
289-
.append(int.class, Drawable.class, drawableFactory)
290-
.append(Integer.class, Drawable.class, drawableFactory)
291-
.append(Uri.class, InputStream.class, ResourceUriLoader.newStreamFactory(context))
292-
.append(
293-
Uri.class,
294-
AssetFileDescriptor.class,
295-
ResourceUriLoader.newAssetFileDescriptorFactory(context));
296-
} else {
297-
ResourceLoader.StreamFactory resourceLoaderStreamFactory =
298-
new ResourceLoader.StreamFactory(resources);
299-
ResourceLoader.FileDescriptorFactory resourceLoaderFileDescriptorFactory =
300-
new ResourceLoader.FileDescriptorFactory(resources);
301-
ResourceLoader.AssetFileDescriptorFactory resourceLoaderAssetFileDescriptorFactory =
302-
new ResourceLoader.AssetFileDescriptorFactory(resources);
303-
304-
registry
305-
.append(int.class, InputStream.class, resourceLoaderStreamFactory)
306-
.append(Integer.class, InputStream.class, resourceLoaderStreamFactory)
307-
.append(int.class, ParcelFileDescriptor.class, resourceLoaderFileDescriptorFactory)
308-
.append(Integer.class, ParcelFileDescriptor.class, resourceLoaderFileDescriptorFactory)
309-
.append(int.class, AssetFileDescriptor.class, resourceLoaderAssetFileDescriptorFactory)
310-
.append(
311-
Integer.class, AssetFileDescriptor.class, resourceLoaderAssetFileDescriptorFactory);
312-
}
276+
// DirectResourceLoader and ResourceUriLoader handle resource IDs and Uris owned by this
277+
// package.
278+
ModelLoaderFactory<Integer, InputStream> directResourceLoaderStreamFactory =
279+
DirectResourceLoader.inputStreamFactory(context);
280+
ModelLoaderFactory<Integer, AssetFileDescriptor>
281+
directResourceLoaderAssetFileDescriptorFactory =
282+
DirectResourceLoader.assetFileDescriptorFactory(context);
283+
ModelLoaderFactory<Integer, Drawable> directResourceLaoderDrawableFactory =
284+
DirectResourceLoader.drawableFactory(context);
285+
registry
286+
.append(int.class, InputStream.class, directResourceLoaderStreamFactory)
287+
.append(Integer.class, InputStream.class, directResourceLoaderStreamFactory)
288+
.append(
289+
int.class, AssetFileDescriptor.class, directResourceLoaderAssetFileDescriptorFactory)
290+
.append(
291+
Integer.class,
292+
AssetFileDescriptor.class,
293+
directResourceLoaderAssetFileDescriptorFactory)
294+
.append(int.class, Drawable.class, directResourceLaoderDrawableFactory)
295+
.append(Integer.class, Drawable.class, directResourceLaoderDrawableFactory)
296+
.append(Uri.class, InputStream.class, ResourceUriLoader.newStreamFactory(context))
297+
.append(
298+
Uri.class,
299+
AssetFileDescriptor.class,
300+
ResourceUriLoader.newAssetFileDescriptorFactory(context));
313301

314-
// Handles resources from other applications or converting Drawable resource types to Bitmaps.
302+
// ResourceLoader and UriLoader handle resource IDs and Uris owned by other packages.
315303
ResourceLoader.UriFactory resourceLoaderUriFactory = new ResourceLoader.UriFactory(resources);
304+
ResourceLoader.AssetFileDescriptorFactory resourceLoaderAssetFileDescriptorFactory =
305+
new ResourceLoader.AssetFileDescriptorFactory(resources);
306+
ResourceLoader.StreamFactory resourceLoaderStreamFactory =
307+
new ResourceLoader.StreamFactory(resources);
316308
registry
317309
.append(Integer.class, Uri.class, resourceLoaderUriFactory)
318310
.append(int.class, Uri.class, resourceLoaderUriFactory)
311+
.append(Integer.class, AssetFileDescriptor.class, resourceLoaderAssetFileDescriptorFactory)
312+
.append(int.class, AssetFileDescriptor.class, resourceLoaderAssetFileDescriptorFactory)
313+
.append(Integer.class, InputStream.class, resourceLoaderStreamFactory)
314+
.append(int.class, InputStream.class, resourceLoaderStreamFactory);
315+
316+
registry
319317
.append(String.class, InputStream.class, new DataUrlLoader.StreamFactory<String>())
320318
.append(Uri.class, InputStream.class, new DataUrlLoader.StreamFactory<Uri>())
321319
.append(String.class, InputStream.class, new StringLoader.StreamFactory())
@@ -338,16 +336,15 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
338336
new QMediaStoreUriLoader.FileDescriptorFactory(context));
339337
}
340338
registry
341-
.append(
342-
Uri.class, InputStream.class, new UriLoader.StreamFactory(contentResolver, experiments))
339+
.append(Uri.class, InputStream.class, new UriLoader.StreamFactory(contentResolver))
343340
.append(
344341
Uri.class,
345342
ParcelFileDescriptor.class,
346-
new UriLoader.FileDescriptorFactory(contentResolver, experiments))
343+
new UriLoader.FileDescriptorFactory(contentResolver))
347344
.append(
348345
Uri.class,
349346
AssetFileDescriptor.class,
350-
new UriLoader.AssetFileDescriptorFactory(contentResolver, experiments))
347+
new UriLoader.AssetFileDescriptorFactory(contentResolver))
351348
.append(Uri.class, InputStream.class, new UrlUriLoader.StreamFactory())
352349
.append(URL.class, InputStream.class, new UrlLoader.StreamFactory())
353350
.append(Uri.class, File.class, new MediaStoreFileLoader.Factory(context))

library/src/main/java/com/bumptech/glide/load/model/DirectResourceLoader.java

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
* Resources#openRawResourceFd(int)} using the theme from {@link ResourceDrawableDecoder#THEME} when
2525
* it's available.
2626
*
27+
* <p>Resource ids from other packages are handled by {@link ResourceLoader} via {@link
28+
* ResourceDrawableDecoder} and {@link
29+
* com.bumptech.glide.load.resource.bitmap.ResourceBitmapDecoder}.
30+
*
2731
* @param <DataT> The type of data this {@code ModelLoader} will produce (e.g. {@link InputStream},
2832
* {@link AssetFileDescriptor} etc).
2933
*/

library/src/main/java/com/bumptech/glide/load/model/ResourceLoader.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
* A model loader for handling Android resource files. Model must be an Android resource id in the
1616
* package of the given context.
1717
*
18+
* <p>This class should always be less preferred than {@link DirectResourceLoader} because {@link
19+
* DirectResourceLoader} is more efficient for {@code Drawables} owned by this package. This class
20+
* only handles passing through {@link Uri}s to {@link
21+
* com.bumptech.glide.load.resource.drawable.ResourceDrawableDecoder} and {@link
22+
* com.bumptech.glide.load.resource.bitmap.ResourceBitmapDecoder}. Those classes can handle assets
23+
* from other applications, but are not as efficient as {@link DirectResourceLoader} for assets
24+
* owned by this package.
25+
*
1826
* @param <Data> The type of data that will be loaded for the given android resource.
1927
*/
2028
public class ResourceLoader<Data> implements ModelLoader<Integer, Data> {
@@ -82,7 +90,14 @@ public void teardown() {
8290
}
8391
}
8492

85-
/** Factory for loading {@link ParcelFileDescriptor}s from Android resource ids. */
93+
/**
94+
* Factory for loading {@link ParcelFileDescriptor}s from Android resource ids.
95+
*
96+
* @deprecated This class is unused by Glide. {@link AssetFileDescriptorFactory} should be
97+
* preferred because it's not possible to reliably load a simple {@link
98+
* java.io.FileDescriptor} for resources.
99+
*/
100+
@Deprecated
86101
public static class FileDescriptorFactory
87102
implements ModelLoaderFactory<Integer, ParcelFileDescriptor> {
88103

library/src/main/java/com/bumptech/glide/load/model/ResourceUriLoader.java

+8
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@
1515
/**
1616
* Converts Resource Uris to resource ids if the resource Uri points to a resource in this package.
1717
*
18+
* <p>This class works by parsing Uris into resource ids, then delegating the resource ID load to
19+
* other {@link ModelLoader}s, typically {@link DirectResourceLoader}.
20+
*
1821
* <p>This class really shouldn't need to exist. If you need to load resources, just pass in the
1922
* integer resource id directly using {@link com.bumptech.glide.RequestManager#load(Integer)}
2023
* instead. It'll be more correct in terms of caching and more efficient to load. The only reason
2124
* we're supporting this case is for backwards compatibility.
2225
*
26+
* <p>Because this class explicitly only handles resource Uris that are from the application's
27+
* package, resource uris from other packages are handled by {@link UriLoader}. {@link UriLoader} is
28+
* even less preferred because it can only handle certain resources from raw resources and it will
29+
* not apply appropriate theming, RTL or night mode attributes.
30+
*
2331
* @param <DataT> The type of data produced, e.g. {@link InputStream} or {@link
2432
* AssetFileDescriptor}.
2533
*/

0 commit comments

Comments
 (0)