Make FileDataSourceFactory an inner class

This is a proof of concept for cleanup we should do for all of
our DataSource implementations as we move toward stabilizing
parts of the API.

- Move all XDataSourceFactory classes to be inner classes.
- Remove chained constructors for XDataSourceFactory classes. Keep
  required args going through constructors. Use setters for the
  rest.
- Not applicable in this case, but we probably want to deprecate
  all but the no-arg method for instantiating eac XDataSource
  instance (with the all-arg method kept but with the intention
  of making it package private).

PiperOrigin-RevId: 274162076
This commit is contained in:
olly 2019-10-11 14:39:23 +01:00 committed by Oliver Woodman
parent be23995100
commit 4ae79105de
5 changed files with 39 additions and 20 deletions

View file

@ -28,7 +28,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
@ -165,7 +165,7 @@ public class DemoApplication extends Application {
return new CacheDataSourceFactory(
cache,
upstreamFactory,
new FileDataSourceFactory(),
new FileDataSource.Factory(),
/* cacheWriteDataSinkFactory= */ null,
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
/* eventListener= */ null);

View file

@ -20,6 +20,7 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSink;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DummyDataSource;
import com.google.android.exoplayer2.upstream.FileDataSource;
import com.google.android.exoplayer2.upstream.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.PriorityDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.Cache;
@ -108,7 +109,7 @@ public final class DownloaderConstructorHelper {
DataSource.Factory readDataSourceFactory =
cacheReadDataSourceFactory != null
? cacheReadDataSourceFactory
: new FileDataSourceFactory();
: new FileDataSource.Factory();
if (cacheWriteDataSinkFactory == null) {
cacheWriteDataSinkFactory =
new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE);

View file

@ -30,9 +30,7 @@ import java.io.RandomAccessFile;
/** A {@link DataSource} for reading local files. */
public final class FileDataSource extends BaseDataSource {
/**
* Thrown when IOException is encountered during local file read operation.
*/
/** Thrown when a {@link FileDataSource} encounters an error reading a file. */
public static class FileDataSourceException extends IOException {
public FileDataSourceException(IOException cause) {
@ -44,6 +42,32 @@ public final class FileDataSource extends BaseDataSource {
}
}
/** {@link DataSource.Factory} for {@link FileDataSource} instances. */
public static final class Factory implements DataSource.Factory {
@Nullable private TransferListener listener;
/**
* Sets a {@link TransferListener} for {@link FileDataSource} instances created by this factory.
*
* @param listener The {@link TransferListener}.
* @return This factory.
*/
public Factory setListener(@Nullable TransferListener listener) {
this.listener = listener;
return this;
}
@Override
public FileDataSource createDataSource() {
FileDataSource dataSource = new FileDataSource();
if (listener != null) {
dataSource.addTransferListener(listener);
}
return dataSource;
}
}
@Nullable private RandomAccessFile file;
@Nullable private Uri uri;
private long bytesRemaining;

View file

@ -17,28 +17,22 @@ package com.google.android.exoplayer2.upstream;
import androidx.annotation.Nullable;
/**
* A {@link DataSource.Factory} that produces {@link FileDataSource}.
*/
/** @deprecated Use {@link FileDataSource.Factory}. */
@Deprecated
public final class FileDataSourceFactory implements DataSource.Factory {
@Nullable private final TransferListener listener;
private final FileDataSource.Factory wrappedFactory;
public FileDataSourceFactory() {
this(null);
this(/* listener= */ null);
}
public FileDataSourceFactory(@Nullable TransferListener listener) {
this.listener = listener;
wrappedFactory = new FileDataSource.Factory().setListener(listener);
}
@Override
public FileDataSource createDataSource() {
FileDataSource dataSource = new FileDataSource();
if (listener != null) {
dataSource.addTransferListener(listener);
}
return dataSource;
return wrappedFactory.createDataSource();
}
}

View file

@ -18,7 +18,7 @@ package com.google.android.exoplayer2.upstream.cache;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.upstream.DataSink;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSource;
/** A {@link DataSource.Factory} that produces {@link CacheDataSource}. */
public final class CacheDataSourceFactory implements DataSource.Factory {
@ -49,7 +49,7 @@ public final class CacheDataSourceFactory implements DataSource.Factory {
this(
cache,
upstreamFactory,
new FileDataSourceFactory(),
new FileDataSource.Factory(),
new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE),
flags,
/* eventListener= */ null);