From 4ae79105ded962c3c5f476a70db0cc71c3e8d701 Mon Sep 17 00:00:00 2001 From: olly Date: Fri, 11 Oct 2019 14:39:23 +0100 Subject: [PATCH] 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 --- .../exoplayer2/demo/DemoApplication.java | 4 +-- .../offline/DownloaderConstructorHelper.java | 3 +- .../exoplayer2/upstream/FileDataSource.java | 30 +++++++++++++++++-- .../upstream/FileDataSourceFactory.java | 18 ++++------- .../cache/CacheDataSourceFactory.java | 4 +-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java index 6985d42b36..d83d7076c5 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java @@ -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); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java index 65dcd187a9..cd090c2c5e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java @@ -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); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java index 38b4a1da03..2661469efd 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java @@ -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; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSourceFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSourceFactory.java index e0630c7989..004a68fdaf 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSourceFactory.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSourceFactory.java @@ -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(); } - } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceFactory.java index 2f0f6caa2c..21758bdceb 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceFactory.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceFactory.java @@ -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);