From 52a8cf84a9c10123777db805b235bf47e18ec234 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Wed, 5 Jan 2022 17:11:08 -0800 Subject: [PATCH] Change asset uri data fetcher to return AssetFileDescriptor rather than ParcelFileDescriptor. PiperOrigin-RevId: 419944523 --- .../com/bumptech/glide/LoadAssetUriTest.java | 6 ------ .../src/main/java/com/bumptech/glide/Glide.java | 2 +- .../data/FileDescriptorAssetPathFetcher.java | 16 ++++++++-------- .../glide/load/model/AssetUriLoader.java | 12 ++++++------ .../data/FileDescriptorAssetPathFetcherTest.java | 15 +++++---------- 5 files changed, 20 insertions(+), 31 deletions(-) diff --git a/instrumentation/src/androidTest/java/com/bumptech/glide/LoadAssetUriTest.java b/instrumentation/src/androidTest/java/com/bumptech/glide/LoadAssetUriTest.java index e89c68939e..d7b5573e0a 100644 --- a/instrumentation/src/androidTest/java/com/bumptech/glide/LoadAssetUriTest.java +++ b/instrumentation/src/androidTest/java/com/bumptech/glide/LoadAssetUriTest.java @@ -14,7 +14,6 @@ import java.io.IOException; import java.util.concurrent.TimeUnit; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,7 +39,6 @@ public void setUp() throws IOException { } @Test - @Ignore("b/213227559") public void loadVideoAssetUri_decodesFrame() { Uri uri = Uri.parse(assetNameToUri(VIDEO_ASSET_NAME)); @@ -50,7 +48,6 @@ public void loadVideoAssetUri_decodesFrame() { } @Test - @Ignore("b/213227559") public void loadVideoAssetUri_asBitmap_decodesFrame() { Uri uri = Uri.parse(assetNameToUri(VIDEO_ASSET_NAME)); @@ -60,7 +57,6 @@ public void loadVideoAssetUri_asBitmap_decodesFrame() { } @Test - @Ignore("b/213227559") public void loadVideoAssetUri_withFrame_decodesFrame() { Uri uri = Uri.parse(assetNameToUri(VIDEO_ASSET_NAME)); @@ -76,7 +72,6 @@ public void loadVideoAssetUri_withFrame_decodesFrame() { } @Test - @Ignore("b/213227559") public void loadVideoAssetUriString_decodesFrame() { Uri uri = Uri.parse(assetNameToUri(VIDEO_ASSET_NAME)); @@ -86,7 +81,6 @@ public void loadVideoAssetUriString_decodesFrame() { } @Test - @Ignore("b/213227559") public void loadVideoAssetUriString_withFrame_decodesFrame() { Uri uri = Uri.parse(assetNameToUri(VIDEO_ASSET_NAME)); diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java index 4951007c00..dd4c02e897 100644 --- a/library/src/main/java/com/bumptech/glide/Glide.java +++ b/library/src/main/java/com/bumptech/glide/Glide.java @@ -542,7 +542,7 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm .append(Uri.class, InputStream.class, new AssetUriLoader.StreamFactory(context.getAssets())) .append( Uri.class, - ParcelFileDescriptor.class, + AssetFileDescriptor.class, new AssetUriLoader.FileDescriptorFactory(context.getAssets())) .append(Uri.class, InputStream.class, new MediaStoreImageThumbLoader.Factory(context)) .append(Uri.class, InputStream.class, new MediaStoreVideoThumbLoader.Factory(context)); diff --git a/library/src/main/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcher.java b/library/src/main/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcher.java index 50adeba32e..43c69300a1 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcher.java +++ b/library/src/main/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcher.java @@ -1,30 +1,30 @@ package com.bumptech.glide.load.data; +import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; -import android.os.ParcelFileDescriptor; import androidx.annotation.NonNull; import java.io.IOException; -/** Fetches an {@link android.os.ParcelFileDescriptor} for an asset path. */ -public class FileDescriptorAssetPathFetcher extends AssetPathFetcher { +/** Fetches an {@link android.content.res.AssetFileDescriptor} for an asset path. */ +public class FileDescriptorAssetPathFetcher extends AssetPathFetcher { public FileDescriptorAssetPathFetcher(AssetManager assetManager, String assetPath) { super(assetManager, assetPath); } @Override - protected ParcelFileDescriptor loadResource(AssetManager assetManager, String path) + protected AssetFileDescriptor loadResource(AssetManager assetManager, String path) throws IOException { - return assetManager.openFd(path).getParcelFileDescriptor(); + return assetManager.openFd(path); } @Override - protected void close(ParcelFileDescriptor data) throws IOException { + protected void close(AssetFileDescriptor data) throws IOException { data.close(); } @NonNull @Override - public Class getDataClass() { - return ParcelFileDescriptor.class; + public Class getDataClass() { + return AssetFileDescriptor.class; } } diff --git a/library/src/main/java/com/bumptech/glide/load/model/AssetUriLoader.java b/library/src/main/java/com/bumptech/glide/load/model/AssetUriLoader.java index 3da79ce851..321cec12c1 100644 --- a/library/src/main/java/com/bumptech/glide/load/model/AssetUriLoader.java +++ b/library/src/main/java/com/bumptech/glide/load/model/AssetUriLoader.java @@ -1,9 +1,9 @@ package com.bumptech.glide.load.model; import android.content.ContentResolver; +import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.net.Uri; -import android.os.ParcelFileDescriptor; import androidx.annotation.NonNull; import com.bumptech.glide.load.Options; import com.bumptech.glide.load.data.DataFetcher; @@ -83,10 +83,10 @@ public DataFetcher buildFetcher(AssetManager assetManager, String a } } - /** Factory for loading {@link ParcelFileDescriptor}s from asset manager Uris. */ + /** Factory for loading {@link AssetFileDescriptor}s from asset manager Uris. */ public static class FileDescriptorFactory - implements ModelLoaderFactory, - AssetFetcherFactory { + implements ModelLoaderFactory, + AssetFetcherFactory { private final AssetManager assetManager; @@ -96,7 +96,7 @@ public FileDescriptorFactory(AssetManager assetManager) { @NonNull @Override - public ModelLoader build(MultiModelLoaderFactory multiFactory) { + public ModelLoader build(MultiModelLoaderFactory multiFactory) { return new AssetUriLoader<>(assetManager, this); } @@ -106,7 +106,7 @@ public void teardown() { } @Override - public DataFetcher buildFetcher( + public DataFetcher buildFetcher( AssetManager assetManager, String assetPath) { return new FileDescriptorAssetPathFetcher(assetManager, assetPath); } diff --git a/library/test/src/test/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcherTest.java b/library/test/src/test/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcherTest.java index 3204525d8f..8939938d9c 100644 --- a/library/test/src/test/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcherTest.java +++ b/library/test/src/test/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcherTest.java @@ -1,14 +1,12 @@ package com.bumptech.glide.load.data; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; -import android.os.ParcelFileDescriptor; import com.bumptech.glide.Priority; import java.io.IOException; import org.junit.Before; @@ -25,25 +23,22 @@ public class FileDescriptorAssetPathFetcherTest { @Mock private AssetManager assetManager; @Mock private AssetFileDescriptor assetFileDescriptor; - @Mock private DataFetcher.DataCallback callback; + @Mock private DataFetcher.DataCallback callback; private FileDescriptorAssetPathFetcher fetcher; - private ParcelFileDescriptor expected; @Before public void setUp() throws IOException { MockitoAnnotations.initMocks(this); String assetPath = "/some/asset/path"; fetcher = new FileDescriptorAssetPathFetcher(assetManager, assetPath); - expected = mock(ParcelFileDescriptor.class); - when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(expected); when(assetManager.openFd(eq(assetPath))).thenReturn(assetFileDescriptor); } @Test public void testOpensInputStreamForPathWithAssetManager() throws Exception { fetcher.loadData(Priority.NORMAL, callback); - verify(callback).onDataReady(eq(expected)); + verify(callback).onDataReady(eq(assetFileDescriptor)); } @Test @@ -51,19 +46,19 @@ public void testClosesOpenedInputStreamOnCleanup() throws Exception { fetcher.loadData(Priority.NORMAL, callback); fetcher.cleanup(); - verify(expected).close(); + verify(assetFileDescriptor).close(); } @Test public void testDoesNothingOnCleanupIfNoDataLoaded() throws IOException { fetcher.cleanup(); - verify(expected, never()).close(); + verify(assetFileDescriptor, never()).close(); } @Test public void testDoesNothingOnCancel() throws Exception { fetcher.loadData(Priority.NORMAL, callback); fetcher.cancel(); - verify(expected, never()).close(); + verify(assetFileDescriptor, never()).close(); } }