Skip to content

Commit

Permalink
Change asset uri data fetcher to return AssetFileDescriptor rather th…
Browse files Browse the repository at this point in the history
…an ParcelFileDescriptor.

PiperOrigin-RevId: 419944523
  • Loading branch information
sjudd authored and glide-copybara-robot committed Jan 6, 2022
1 parent a6d84a8 commit 52a8cf8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ParcelFileDescriptor> {
/** Fetches an {@link android.content.res.AssetFileDescriptor} for an asset path. */
public class FileDescriptorAssetPathFetcher extends AssetPathFetcher<AssetFileDescriptor> {
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<ParcelFileDescriptor> getDataClass() {
return ParcelFileDescriptor.class;
public Class<AssetFileDescriptor> getDataClass() {
return AssetFileDescriptor.class;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -83,10 +83,10 @@ public DataFetcher<InputStream> 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<Uri, ParcelFileDescriptor>,
AssetFetcherFactory<ParcelFileDescriptor> {
implements ModelLoaderFactory<Uri, AssetFileDescriptor>,
AssetFetcherFactory<AssetFileDescriptor> {

private final AssetManager assetManager;

Expand All @@ -96,7 +96,7 @@ public FileDescriptorFactory(AssetManager assetManager) {

@NonNull
@Override
public ModelLoader<Uri, ParcelFileDescriptor> build(MultiModelLoaderFactory multiFactory) {
public ModelLoader<Uri, AssetFileDescriptor> build(MultiModelLoaderFactory multiFactory) {
return new AssetUriLoader<>(assetManager, this);
}

Expand All @@ -106,7 +106,7 @@ public void teardown() {
}

@Override
public DataFetcher<ParcelFileDescriptor> buildFetcher(
public DataFetcher<AssetFileDescriptor> buildFetcher(
AssetManager assetManager, String assetPath) {
return new FileDescriptorAssetPathFetcher(assetManager, assetPath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,45 +23,42 @@ public class FileDescriptorAssetPathFetcherTest {

@Mock private AssetManager assetManager;
@Mock private AssetFileDescriptor assetFileDescriptor;
@Mock private DataFetcher.DataCallback<ParcelFileDescriptor> callback;
@Mock private DataFetcher.DataCallback<AssetFileDescriptor> 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
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();
}
}

0 comments on commit 52a8cf8

Please sign in to comment.