Commit 4ae79105 by olly Committed by Oliver Woodman

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
parent be239951
...@@ -28,7 +28,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; ...@@ -28,7 +28,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; 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.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
...@@ -165,7 +165,7 @@ public class DemoApplication extends Application { ...@@ -165,7 +165,7 @@ public class DemoApplication extends Application {
return new CacheDataSourceFactory( return new CacheDataSourceFactory(
cache, cache,
upstreamFactory, upstreamFactory,
new FileDataSourceFactory(), new FileDataSource.Factory(),
/* cacheWriteDataSinkFactory= */ null, /* cacheWriteDataSinkFactory= */ null,
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
/* eventListener= */ null); /* eventListener= */ null);
......
...@@ -20,6 +20,7 @@ import com.google.android.exoplayer2.C; ...@@ -20,6 +20,7 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSink; import com.google.android.exoplayer2.upstream.DataSink;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DummyDataSource; 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.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.PriorityDataSourceFactory; import com.google.android.exoplayer2.upstream.PriorityDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.Cache;
...@@ -108,7 +109,7 @@ public final class DownloaderConstructorHelper { ...@@ -108,7 +109,7 @@ public final class DownloaderConstructorHelper {
DataSource.Factory readDataSourceFactory = DataSource.Factory readDataSourceFactory =
cacheReadDataSourceFactory != null cacheReadDataSourceFactory != null
? cacheReadDataSourceFactory ? cacheReadDataSourceFactory
: new FileDataSourceFactory(); : new FileDataSource.Factory();
if (cacheWriteDataSinkFactory == null) { if (cacheWriteDataSinkFactory == null) {
cacheWriteDataSinkFactory = cacheWriteDataSinkFactory =
new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE); new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE);
......
...@@ -30,9 +30,7 @@ import java.io.RandomAccessFile; ...@@ -30,9 +30,7 @@ import java.io.RandomAccessFile;
/** A {@link DataSource} for reading local files. */ /** A {@link DataSource} for reading local files. */
public final class FileDataSource extends BaseDataSource { public final class FileDataSource extends BaseDataSource {
/** /** Thrown when a {@link FileDataSource} encounters an error reading a file. */
* Thrown when IOException is encountered during local file read operation.
*/
public static class FileDataSourceException extends IOException { public static class FileDataSourceException extends IOException {
public FileDataSourceException(IOException cause) { public FileDataSourceException(IOException cause) {
...@@ -44,6 +42,32 @@ public final class FileDataSource extends BaseDataSource { ...@@ -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 RandomAccessFile file;
@Nullable private Uri uri; @Nullable private Uri uri;
private long bytesRemaining; private long bytesRemaining;
......
...@@ -17,28 +17,22 @@ package com.google.android.exoplayer2.upstream; ...@@ -17,28 +17,22 @@ package com.google.android.exoplayer2.upstream;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
/** /** @deprecated Use {@link FileDataSource.Factory}. */
* A {@link DataSource.Factory} that produces {@link FileDataSource}. @Deprecated
*/
public final class FileDataSourceFactory implements DataSource.Factory { public final class FileDataSourceFactory implements DataSource.Factory {
@Nullable private final TransferListener listener; private final FileDataSource.Factory wrappedFactory;
public FileDataSourceFactory() { public FileDataSourceFactory() {
this(null); this(/* listener= */ null);
} }
public FileDataSourceFactory(@Nullable TransferListener listener) { public FileDataSourceFactory(@Nullable TransferListener listener) {
this.listener = listener; wrappedFactory = new FileDataSource.Factory().setListener(listener);
} }
@Override @Override
public FileDataSource createDataSource() { public FileDataSource createDataSource() {
FileDataSource dataSource = new FileDataSource(); return wrappedFactory.createDataSource();
if (listener != null) {
dataSource.addTransferListener(listener);
}
return dataSource;
} }
} }
...@@ -18,7 +18,7 @@ package com.google.android.exoplayer2.upstream.cache; ...@@ -18,7 +18,7 @@ package com.google.android.exoplayer2.upstream.cache;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.upstream.DataSink; import com.google.android.exoplayer2.upstream.DataSink;
import com.google.android.exoplayer2.upstream.DataSource; 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}. */ /** A {@link DataSource.Factory} that produces {@link CacheDataSource}. */
public final class CacheDataSourceFactory implements DataSource.Factory { public final class CacheDataSourceFactory implements DataSource.Factory {
...@@ -49,7 +49,7 @@ public final class CacheDataSourceFactory implements DataSource.Factory { ...@@ -49,7 +49,7 @@ public final class CacheDataSourceFactory implements DataSource.Factory {
this( this(
cache, cache,
upstreamFactory, upstreamFactory,
new FileDataSourceFactory(), new FileDataSource.Factory(),
new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE), new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE),
flags, flags,
/* eventListener= */ null); /* eventListener= */ null);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment