Add Builder to DataSourceBitmapLoader

There are too many parameters to handle via constructors
and there will be another one added soon.

PiperOrigin-RevId: 820632572
This commit is contained in:
tonihei 2025-10-17 04:20:56 -07:00 committed by Copybara-Service
parent fe2f062b7f
commit 3d4b0610ea
17 changed files with 235 additions and 88 deletions

View file

@ -827,7 +827,8 @@ public final class TransformerActivity extends AppCompatActivity {
inputImageView.setVisibility(View.VISIBLE);
inputTextView.setText(getString(R.string.input_image));
BitmapLoader bitmapLoader = new DataSourceBitmapLoader(getApplicationContext());
BitmapLoader bitmapLoader =
new DataSourceBitmapLoader.Builder(getApplicationContext()).build();
ListenableFuture<Bitmap> future = bitmapLoader.loadBitmap(uri);
try {
Bitmap bitmap = future.get();

View file

@ -16,6 +16,7 @@
package androidx.media3.datasource;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
import static org.junit.Assert.assertThrows;
import android.content.Context;
@ -31,7 +32,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SdkSuppress;
import com.google.common.io.Files;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.File;
import java.util.concurrent.ExecutionException;
import okhttp3.mockwebserver.MockResponse;
@ -74,7 +74,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void decodeBitmap_withValidData_loadsCorrectData() throws Exception {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Bitmap bitmap = bitmapLoader.decodeBitmap(imageData).get();
@ -88,7 +91,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void decodeBitmap_withExifRotation_loadsCorrectData() throws Exception {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
byte[] imageData =
TestUtil.getByteArray(context, TEST_IMAGE_FOLDER + "non-motion-photo-shortened.jpg");
Bitmap bitmapWithoutRotation =
@ -113,7 +119,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void decodeBitmap_withInvalidData_throws() {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
ListenableFuture<Bitmap> future = bitmapLoader.decodeBitmap(new byte[0]);
@ -124,7 +133,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void loadBitmap_withHttpUri_loadsCorrectData() throws Exception {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Buffer responseBody = new Buffer().write(imageData);
Bitmap bitmap;
@ -143,7 +155,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void loadBitmap_httpUriAndServerError_throws() throws Exception {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
ListenableFuture<Bitmap> future;
try (MockWebServer mockWebServer = new MockWebServer()) {
mockWebServer.enqueue(new MockResponse().setResponseCode(404));
@ -158,7 +173,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void loadBitmap_assetUri_loadsCorrectData() throws Exception {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Bitmap bitmap = bitmapLoader.loadBitmap(Uri.parse("asset:///" + TEST_IMAGE_PATH)).get();
@ -172,7 +190,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void loadBitmap_assetUriWithAssetNotExisting_throws() {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
assertException(
() -> bitmapLoader.loadBitmap(Uri.parse("asset:///not_valid/path/image.bmp")).get(),
@ -187,7 +208,10 @@ public class DataSourceBitmapLoaderTest {
Files.write(imageData, file);
Uri uri = Uri.fromFile(file);
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
Bitmap bitmap = bitmapLoader.loadBitmap(uri).get();
@ -206,8 +230,11 @@ public class DataSourceBitmapLoaderTest {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(
MoreExecutors.newDirectExecutorService(), dataSourceFactory, options);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.setBitmapFactoryOptions(options)
.build();
Bitmap bitmap = bitmapLoader.loadBitmap(uri).get();
@ -223,11 +250,11 @@ public class DataSourceBitmapLoaderTest {
Uri uri = Uri.fromFile(file);
int maximumOutputDimension = 2000;
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(
MoreExecutors.newDirectExecutorService(),
dataSourceFactory,
/* options= */ null,
maximumOutputDimension);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.setMaximumOutputDimension(maximumOutputDimension)
.build();
Bitmap bitmap = bitmapLoader.loadBitmap(uri).get();
@ -245,7 +272,9 @@ public class DataSourceBitmapLoaderTest {
int maximumOutputDimension = 720;
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(context, maximumOutputDimension);
new DataSourceBitmapLoader.Builder(context)
.setMaximumOutputDimension(maximumOutputDimension)
.build();
Bitmap bitmap = bitmapLoader.loadBitmap(uri).get();
@ -256,7 +285,10 @@ public class DataSourceBitmapLoaderTest {
@Test
public void loadBitmap_fileUriWithFileNotExisting_throws() {
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
assertException(
() -> bitmapLoader.loadBitmap(Uri.parse("file:///not_valid/path/image.bmp")).get(),
@ -269,7 +301,10 @@ public class DataSourceBitmapLoaderTest {
throws Exception {
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
try (MockWebServer mockWebServer = new MockWebServer()) {
Uri uri = Uri.parse(mockWebServer.url("test_path").toString());
MediaMetadata metadata =
@ -293,7 +328,10 @@ public class DataSourceBitmapLoaderTest {
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Buffer responseBody = new Buffer().write(imageData);
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
try (MockWebServer mockWebServer = new MockWebServer()) {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseBody));
Uri uri = Uri.parse(mockWebServer.url("test_path").toString());
@ -313,7 +351,10 @@ public class DataSourceBitmapLoaderTest {
public void loadBitmapFromMetadata_withArtworkDataAndArtworkUriUnset_returnNull() {
MediaMetadata metadata = new MediaMetadata.Builder().build();
DataSourceBitmapLoader bitmapLoader =
new DataSourceBitmapLoader(MoreExecutors.newDirectExecutorService(), dataSourceFactory);
new DataSourceBitmapLoader.Builder(context)
.setExecutorService(newDirectExecutorService())
.setDataSourceFactory(dataSourceFactory)
.build();
ListenableFuture<Bitmap> bitmapFuture = bitmapLoader.loadBitmapFromMetadata(metadata);

View file

@ -31,6 +31,7 @@ import com.google.common.base.Suppliers;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.util.concurrent.Executors;
@ -49,55 +50,121 @@ public final class DataSourceBitmapLoader implements BitmapLoader {
Suppliers.memoize(
() -> MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
/** A builder for {@link DataSourceBitmapLoader} instances. */
public static final class Builder {
private final Context context;
@Nullable private ListeningExecutorService listeningExecutorService;
@Nullable private DataSource.Factory dataSourceFactory;
@Nullable private BitmapFactory.Options options;
private int maximumOutputDimension;
/**
* Creates a builder.
*
* @param context The context.
*/
public Builder(Context context) {
this.context = context;
this.maximumOutputDimension = C.LENGTH_UNSET;
}
/**
* Sets the {@link DataSource.Factory} to be used to create {@link DataSource} instances for
* loading bitmaps.
*
* <p>If not set, a {@link DefaultDataSource.Factory} will be used.
*
* @param dataSourceFactory A {@link DataSource.Factory}.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setDataSourceFactory(DataSource.Factory dataSourceFactory) {
this.dataSourceFactory = dataSourceFactory;
return this;
}
/**
* Sets the {@link ListeningExecutorService} to be used for loading bitmaps.
*
* <p>If not set, {@link #DEFAULT_EXECUTOR_SERVICE} will be used.
*
* @param listeningExecutorService A {@link ListeningExecutorService}.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setExecutorService(ListeningExecutorService listeningExecutorService) {
this.listeningExecutorService = listeningExecutorService;
return this;
}
/**
* Sets the {@link BitmapFactory.Options} to be used for decoding bitmaps.
*
* @param options A {@link BitmapFactory.Options}.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setBitmapFactoryOptions(@Nullable BitmapFactory.Options options) {
this.options = options;
return this;
}
/**
* Sets the maximum output dimension for decoded bitmaps.
*
* @param maximumOutputDimension The maximum output dimension in pixels.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setMaximumOutputDimension(int maximumOutputDimension) {
this.maximumOutputDimension = maximumOutputDimension;
return this;
}
/** Builds a {@link DataSourceBitmapLoader}. */
public DataSourceBitmapLoader build() {
return new DataSourceBitmapLoader(this);
}
}
private final ListeningExecutorService listeningExecutorService;
private final DataSource.Factory dataSourceFactory;
@Nullable private final BitmapFactory.Options options;
private final int maximumOutputDimension;
/**
* Creates an instance that uses a {@link DefaultHttpDataSource} for image loading and delegates
* loading tasks to a {@link Executors#newSingleThreadExecutor()}.
* @deprecated Use {@link Builder} instead.
*/
@Deprecated
public DataSourceBitmapLoader(Context context) {
this(checkNotNull(DEFAULT_EXECUTOR_SERVICE.get()), new DefaultDataSource.Factory(context));
this(new Builder(context));
}
/**
* Creates an instance that uses a {@link DefaultHttpDataSource} for image loading and delegates
* loading tasks to a {@link Executors#newSingleThreadExecutor()} with specified maximum output
* dimension.
*
* @param context The context
* @param maximumOutputDimension The maximum dimension of the output Bitmap.
* @deprecated Use {@link Builder} instead.
*/
@Deprecated
public DataSourceBitmapLoader(Context context, int maximumOutputDimension) {
this(
checkNotNull(DEFAULT_EXECUTOR_SERVICE.get()),
new DefaultDataSource.Factory(context),
/* options= */ null,
maximumOutputDimension);
this(new Builder(context).setMaximumOutputDimension(maximumOutputDimension));
}
/**
* Creates an instance that delegates loading tasks to the {@link ListeningExecutorService}.
*
* @param listeningExecutorService The {@link ListeningExecutorService}.
* @param dataSourceFactory The {@link DataSource.Factory} that creates the {@link DataSource}
* used to load the image.
* @deprecated Use {@link Builder} instead.
*/
@SuppressWarnings("deprecation") // Calling deprecated constructor.
@Deprecated
public DataSourceBitmapLoader(
ListeningExecutorService listeningExecutorService, DataSource.Factory dataSourceFactory) {
this(listeningExecutorService, dataSourceFactory, /* options= */ null);
}
/**
* Creates an instance that delegates loading tasks to the {@link ListeningExecutorService}.
*
* @param listeningExecutorService The {@link ListeningExecutorService}.
* @param dataSourceFactory The {@link DataSource.Factory} that creates the {@link DataSource}
* used to load the image.
* @param options The {@link BitmapFactory.Options} the image should be loaded with.
* @deprecated Use {@link Builder} instead.
*/
@SuppressWarnings("deprecation") // Calling deprecated constructor.
@Deprecated
public DataSourceBitmapLoader(
ListeningExecutorService listeningExecutorService,
DataSource.Factory dataSourceFactory,
@ -110,16 +177,9 @@ public final class DataSourceBitmapLoader implements BitmapLoader {
}
/**
* Creates an instance that delegates loading tasks to the {@link ListeningExecutorService}.
*
* <p>Use {@code maximumOutputDimension} to limit memory usage when loading large Bitmaps.
*
* @param listeningExecutorService The {@link ListeningExecutorService}.
* @param dataSourceFactory The {@link DataSource.Factory} that creates the {@link DataSource}
* used to load the image.
* @param options The {@link BitmapFactory.Options} the image should be loaded with.
* @param maximumOutputDimension The maximum dimension of the output Bitmap.
* @deprecated Use {@link Builder} instead.
*/
@Deprecated
public DataSourceBitmapLoader(
ListeningExecutorService listeningExecutorService,
DataSource.Factory dataSourceFactory,
@ -131,6 +191,19 @@ public final class DataSourceBitmapLoader implements BitmapLoader {
this.maximumOutputDimension = maximumOutputDimension;
}
private DataSourceBitmapLoader(Builder builder) {
this.dataSourceFactory =
builder.dataSourceFactory != null
? builder.dataSourceFactory
: new DefaultDataSource.Factory(builder.context);
this.listeningExecutorService =
builder.listeningExecutorService != null
? builder.listeningExecutorService
: checkNotNull(DEFAULT_EXECUTOR_SERVICE.get());
this.options = builder.options;
this.maximumOutputDimension = builder.maximumOutputDimension;
}
@Override
public boolean supportsMimeType(String mimeType) {
return isBitmapFactorySupportedMimeType(mimeType);

View file

@ -111,7 +111,7 @@ public abstract class BitmapOverlay extends TextureOverlay {
@Override
public Bitmap getBitmap(long presentationTimeUs) throws VideoFrameProcessingException {
if (lastBitmap == null) {
BitmapLoader bitmapLoader = new DataSourceBitmapLoader(context);
BitmapLoader bitmapLoader = new DataSourceBitmapLoader.Builder(context).build();
ListenableFuture<Bitmap> future = bitmapLoader.loadBitmap(overlayBitmapUri);
try {
lastBitmap = future.get();

View file

@ -206,7 +206,7 @@ public final class MediaBrowser extends MediaController {
public ListenableFuture<MediaBrowser> buildAsync() {
MediaControllerHolder<MediaBrowser> holder = new MediaControllerHolder<>(applicationLooper);
if (token.isLegacySession() && bitmapLoader == null) {
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(context));
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
}
MediaBrowser browser =
new MediaBrowser(

View file

@ -375,7 +375,7 @@ public class MediaController implements Player {
MediaControllerHolder<MediaController> holder =
new MediaControllerHolder<>(applicationLooper);
if (token.isLegacySession() && bitmapLoader == null) {
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(context));
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
}
MediaController controller =
new MediaController(

View file

@ -2588,7 +2588,11 @@ public class MediaSession {
protected final void ensureBitmapLoaderIsSizeLimited() {
int dimensionLimit = MediaSession.getBitmapDimensionLimit(context);
if (bitmapLoader == null) {
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(context, dimensionLimit));
bitmapLoader =
new CacheBitmapLoader(
new DataSourceBitmapLoader.Builder(context)
.setMaximumOutputDimension(dimensionLimit)
.build());
} else {
bitmapLoader = new SizeLimitedBitmapLoader(bitmapLoader, dimensionLimit);
}

View file

@ -78,7 +78,7 @@ public class CacheBitmapLoaderTest {
@Test
public void decodeBitmap_requestWithSameDataTwice_returnsCachedFuture() throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(new DataSourceBitmapLoader(context));
new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Bitmap expectedBitmap =
apply90DegreeExifRotation(
@ -98,7 +98,7 @@ public class CacheBitmapLoaderTest {
@Test
public void decodeBitmap_requestWithDifferentData_returnsNewFuture() throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(new DataSourceBitmapLoader(context));
new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
byte[] imageData1 = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
byte[] imageData2 = TestUtil.getByteArray(context, SECOND_TEST_IMAGE_PATH);
Bitmap expectedBitmap1 =
@ -123,7 +123,7 @@ public class CacheBitmapLoaderTest {
@Test
public void decodeBitmap_requestWithSameDataTwice_throwsException() {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(new DataSourceBitmapLoader(context));
new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
// First request, no cached bitmap load request.
ListenableFuture<Bitmap> future1 = cacheBitmapLoader.decodeBitmap(new byte[0]);
@ -141,7 +141,7 @@ public class CacheBitmapLoaderTest {
@Test
public void loadBitmap_httpUri_requestWithSameUriTwice_returnsCachedFuture() throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(new DataSourceBitmapLoader(context));
new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Buffer responseBody = new Buffer().write(imageData);
MockWebServer mockWebServer = new MockWebServer();
@ -166,7 +166,7 @@ public class CacheBitmapLoaderTest {
@Test
public void loadBitmap_httpUri_requestWithDifferentUri_returnsNewFuture() throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(new DataSourceBitmapLoader(context));
new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
byte[] imageData1 = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
byte[] imageData2 = TestUtil.getByteArray(context, SECOND_TEST_IMAGE_PATH);
Buffer responseBody1 = new Buffer().write(imageData1);
@ -198,7 +198,7 @@ public class CacheBitmapLoaderTest {
@Test
public void loadBitmap_httpUri_requestWithSameUriTwice_throwsException() {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(new DataSourceBitmapLoader(context));
new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setResponseCode(404));
Uri uri = Uri.parse(mockWebServer.url("test_path").toString());
@ -227,7 +227,8 @@ public class CacheBitmapLoaderTest {
throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context)));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build()));
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Bitmap expectedBitmap =
apply90DegreeExifRotation(
@ -250,7 +251,8 @@ public class CacheBitmapLoaderTest {
public void loadBitmapFromMetadata_requestWithDifferentData_returnsNewFuture() throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context)));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build()));
byte[] imageData1 = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
byte[] imageData2 = TestUtil.getByteArray(context, SECOND_TEST_IMAGE_PATH);
Bitmap expectedBitmap1 =
@ -280,7 +282,8 @@ public class CacheBitmapLoaderTest {
public void loadBitmapFromMetadata_requestWithSameDataTwice_throwsException() {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context)));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build()));
MediaMetadata metadata =
new MediaMetadata.Builder().setArtworkData(new byte[0], PICTURE_TYPE_MEDIA).build();
@ -302,7 +305,8 @@ public class CacheBitmapLoaderTest {
throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context)));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build()));
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Buffer responseBody = new Buffer().write(imageData);
MockWebServer mockWebServer = new MockWebServer();
@ -329,7 +333,8 @@ public class CacheBitmapLoaderTest {
public void loadBitmapFromMetadata_requestWithDifferentUri_returnsNewFuture() throws Exception {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context)));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build()));
byte[] imageData1 = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
byte[] imageData2 = TestUtil.getByteArray(context, SECOND_TEST_IMAGE_PATH);
Buffer responseBody1 = new Buffer().write(imageData1);
@ -364,7 +369,8 @@ public class CacheBitmapLoaderTest {
public void loadBitmapFromMetadata_requestWithSameUriTwice_throwsException() {
CacheBitmapLoader cacheBitmapLoader =
new CacheBitmapLoader(
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context)));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build()));
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setResponseCode(404));
Uri uri = Uri.parse(mockWebServer.url("test_path").toString());

View file

@ -78,7 +78,7 @@ public final class LegacyConversionsTest {
@Before
public void setUp() {
context = ApplicationProvider.getApplicationContext();
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(context));
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
}
@Test

View file

@ -72,7 +72,7 @@ public class SizeLimitedBitmapLoaderTest {
public void decodeBitmapWithLimit() throws Exception {
int limit = MediaSession.getBitmapDimensionLimit(context);
SizeLimitedBitmapLoader sizeLimitedBitmapLoader =
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader(context), limit);
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader.Builder(context).build(), limit);
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Bitmap expectedBitmap = getExpectedBitmap(imageData, limit);
@ -85,7 +85,7 @@ public class SizeLimitedBitmapLoaderTest {
public void loadBitmapWithLimit() throws Exception {
int limit = MediaSession.getBitmapDimensionLimit(context);
SizeLimitedBitmapLoader sizeLimitedBitmapLoader =
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader(context), limit);
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader.Builder(context).build(), limit);
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
Buffer responseBody = new Buffer().write(imageData);
MockWebServer mockWebServer = new MockWebServer();
@ -103,7 +103,7 @@ public class SizeLimitedBitmapLoaderTest {
public void loadBitmapWithLimitWithDifferentUris() throws Exception {
int limit = MediaSession.getBitmapDimensionLimit(context);
SizeLimitedBitmapLoader sizeLimitedBitmapLoader =
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader(context), limit);
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader.Builder(context).build(), limit);
byte[] imageData1 = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
byte[] imageData2 = TestUtil.getByteArray(context, SECOND_TEST_IMAGE_PATH);
Buffer responseBody1 = new Buffer().write(imageData1);
@ -132,7 +132,7 @@ public class SizeLimitedBitmapLoaderTest {
public void loadBitmapWithLimitWithInvalidUri() {
int limit = MediaSession.getBitmapDimensionLimit(context);
SizeLimitedBitmapLoader sizeLimitedBitmapLoader =
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader(context), limit);
new SizeLimitedBitmapLoader(new DataSourceBitmapLoader.Builder(context).build(), limit);
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setResponseCode(404));
Uri uri = Uri.parse(mockWebServer.url("test_path").toString());
@ -152,7 +152,8 @@ public class SizeLimitedBitmapLoaderTest {
int limit = MediaSession.getBitmapDimensionLimit(context);
byte[] imageData = TestUtil.getByteArray(context, TEST_IMAGE_PATH);
LoadBitmapFromMetadataOnlyBitmapLoader testBitmapLoader =
new LoadBitmapFromMetadataOnlyBitmapLoader(new DataSourceBitmapLoader(context));
new LoadBitmapFromMetadataOnlyBitmapLoader(
new DataSourceBitmapLoader.Builder(context).build());
SizeLimitedBitmapLoader sizeLimitedBitmapLoader =
new SizeLimitedBitmapLoader(testBitmapLoader, limit);
Buffer responseBody = new Buffer().write(imageData);

View file

@ -88,7 +88,7 @@ public class MediaControllerMediaSessionCompatCallbackAggregationTest {
public void setUp() throws Exception {
context = ApplicationProvider.getApplicationContext();
session = new RemoteMediaSessionCompat(DEFAULT_TEST_NAME, context);
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(context));
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
}
@After

View file

@ -135,7 +135,7 @@ public class MediaControllerWithMediaSessionCompatTest {
public void setUp() throws Exception {
context = ApplicationProvider.getApplicationContext();
session = new RemoteMediaSessionCompat(DEFAULT_TEST_NAME, context);
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(context));
bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader.Builder(context).build());
}
@After

View file

@ -146,7 +146,11 @@ public class RawAssetLoaderAndroidTest {
@Test
public void videoTranscoding_withTextureInput_completesWithCorrectFrameCountAndDuration()
throws Exception {
Bitmap bitmap = new DataSourceBitmapLoader(context).loadBitmap(Uri.parse(PNG_ASSET.uri)).get();
Bitmap bitmap =
new DataSourceBitmapLoader.Builder(context)
.build()
.loadBitmap(Uri.parse(PNG_ASSET.uri))
.get();
DefaultVideoFrameProcessor.Factory videoFrameProcessorFactory =
new DefaultVideoFrameProcessor.Factory.Builder()
.setGlObjectsProvider(new DefaultGlObjectsProvider(createOpenGlObjects()))
@ -189,7 +193,11 @@ public class RawAssetLoaderAndroidTest {
@Test
public void videoEditing_withTextureInput_completesWithCorrectFrameCountAndDuration()
throws Exception {
Bitmap bitmap = new DataSourceBitmapLoader(context).loadBitmap(Uri.parse(PNG_ASSET.uri)).get();
Bitmap bitmap =
new DataSourceBitmapLoader.Builder(context)
.build()
.loadBitmap(Uri.parse(PNG_ASSET.uri))
.get();
EGLContext currentContext = createOpenGlObjects();
DefaultVideoFrameProcessor.Factory videoFrameProcessorFactory =
new DefaultVideoFrameProcessor.Factory.Builder()
@ -235,7 +243,11 @@ public class RawAssetLoaderAndroidTest {
@Test
public void audioAndVideoTranscoding_withRawData_completesWithCorrectFrameCountAndDuration()
throws Exception {
Bitmap bitmap = new DataSourceBitmapLoader(context).loadBitmap(Uri.parse(PNG_ASSET.uri)).get();
Bitmap bitmap =
new DataSourceBitmapLoader.Builder(context)
.build()
.loadBitmap(Uri.parse(PNG_ASSET.uri))
.get();
DefaultVideoFrameProcessor.Factory videoFrameProcessorFactory =
new DefaultVideoFrameProcessor.Factory.Builder()
.setGlObjectsProvider(new DefaultGlObjectsProvider(createOpenGlObjects()))

View file

@ -355,7 +355,11 @@ public class TransformerEndToEndTest {
@Test
public void videoEditing_withTextureInput_completesWithCorrectFrameCountAndDuration()
throws Exception {
Bitmap bitmap = new DataSourceBitmapLoader(context).loadBitmap(Uri.parse(PNG_ASSET.uri)).get();
Bitmap bitmap =
new DataSourceBitmapLoader.Builder(context)
.build()
.loadBitmap(Uri.parse(PNG_ASSET.uri))
.get();
int expectedFrameCount = 2;
EGLContext currentContext = createOpenGlObjects();
DefaultVideoFrameProcessor.Factory videoFrameProcessorFactory =
@ -409,7 +413,11 @@ public class TransformerEndToEndTest {
@Test
public void videoTranscoding_withTextureInput_completesWithCorrectFrameCountAndDuration()
throws Exception {
Bitmap bitmap = new DataSourceBitmapLoader(context).loadBitmap(Uri.parse(PNG_ASSET.uri)).get();
Bitmap bitmap =
new DataSourceBitmapLoader.Builder(context)
.build()
.loadBitmap(Uri.parse(PNG_ASSET.uri))
.get();
int expectedFrameCount = 2;
EGLContext currentContext = createOpenGlObjects();
DefaultVideoFrameProcessor.Factory videoFrameProcessorFactory =

View file

@ -190,7 +190,7 @@ public final class TransformerUltraHdrTest {
final BitmapLoader bitmapLoader;
{
bitmapLoader = new DataSourceBitmapLoader(context);
bitmapLoader = new DataSourceBitmapLoader.Builder(context).build();
}
@Override

View file

@ -172,7 +172,8 @@ public class ImageAssetLoaderTest {
.setFrameRate(30)
.build();
return new ImageAssetLoader.Factory(
context, new DataSourceBitmapLoader(ApplicationProvider.getApplicationContext()))
context,
new DataSourceBitmapLoader.Builder(ApplicationProvider.getApplicationContext()).build())
.createAssetLoader(
editedMediaItem,
Looper.myLooper(),

View file

@ -107,7 +107,7 @@ class MediaAppControllerActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener { finish() }
bitmapLoader = CacheBitmapLoader(DataSourceBitmapLoader(this))
bitmapLoader = CacheBitmapLoader(DataSourceBitmapLoader.Builder(this).build())
viewPager = findViewById(R.id.view_pager)
ratingViewGroup = findViewById(R.id.rating)
mediaInfoText = findViewById(R.id.media_info)