Simplify DownloadManager constructors

PiperOrigin-RevId: 244223870
This commit is contained in:
olly 2019-04-18 19:19:38 +01:00 committed by Oliver Woodman
parent 62964026b9
commit e9413b0d41
5 changed files with 46 additions and 91 deletions

View File

@ -49,7 +49,6 @@ public class DemoApplication extends Application {
private static final String DOWNLOAD_ACTION_FILE = "actions"; private static final String DOWNLOAD_ACTION_FILE = "actions";
private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions"; private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions";
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads"; private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
private static final int MAX_SIMULTANEOUS_DOWNLOADS = 2;
protected String userAgent; protected String userAgent;
@ -122,12 +121,7 @@ public class DemoApplication extends Application {
new DownloaderConstructorHelper(getDownloadCache(), buildHttpDataSourceFactory()); new DownloaderConstructorHelper(getDownloadCache(), buildHttpDataSourceFactory());
downloadManager = downloadManager =
new DownloadManager( new DownloadManager(
this, this, downloadIndex, new DefaultDownloaderFactory(downloaderConstructorHelper));
downloadIndex,
new DefaultDownloaderFactory(downloaderConstructorHelper),
MAX_SIMULTANEOUS_DOWNLOADS,
DownloadManager.DEFAULT_MIN_RETRY_COUNT,
DownloadManager.DEFAULT_REQUIREMENTS);
downloadTracker = downloadTracker =
new DownloadTracker(/* context= */ this, buildDataSourceFactory(), downloadManager); new DownloadTracker(/* context= */ this, buildDataSourceFactory(), downloadManager);
} }

View File

@ -34,7 +34,6 @@ import android.os.Message;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.database.DatabaseProvider;
import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.scheduler.RequirementsWatcher; import com.google.android.exoplayer2.scheduler.RequirementsWatcher;
import com.google.android.exoplayer2.upstream.cache.CacheUtil.CachingCounters; import com.google.android.exoplayer2.upstream.cache.CacheUtil.CachingCounters;
@ -111,8 +110,8 @@ public final class DownloadManager {
@Requirements.RequirementFlags int notMetRequirements) {} @Requirements.RequirementFlags int notMetRequirements) {}
} }
/** The default maximum number of simultaneous downloads. */ /** The default maximum number of parallel downloads. */
public static final int DEFAULT_MAX_SIMULTANEOUS_DOWNLOADS = 1; public static final int DEFAULT_MAX_PARALLEL_DOWNLOADS = 3;
/** The default minimum number of times a download must be retried before failing. */ /** The default minimum number of times a download must be retried before failing. */
public static final int DEFAULT_MIN_RETRY_COUNT = 5; public static final int DEFAULT_MIN_RETRY_COUNT = 5;
/** The default requirement is that the device has network connectivity. */ /** The default requirement is that the device has network connectivity. */
@ -151,8 +150,6 @@ public final class DownloadManager {
private static final String TAG = "DownloadManager"; private static final String TAG = "DownloadManager";
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private final int maxSimultaneousDownloads;
private final int minRetryCount;
private final Context context; private final Context context;
private final WritableDownloadIndex downloadIndex; private final WritableDownloadIndex downloadIndex;
private final DownloaderFactory downloaderFactory; private final DownloaderFactory downloaderFactory;
@ -180,51 +177,11 @@ public final class DownloadManager {
// Mutable fields that are accessed on the internal thread. // Mutable fields that are accessed on the internal thread.
@Requirements.RequirementFlags private int notMetRequirements; @Requirements.RequirementFlags private int notMetRequirements;
private boolean downloadsResumed; private boolean downloadsResumed;
private int simultaneousDownloads; private int parallelDownloads;
/** // TODO: Fix these to properly support changes at runtime.
* Constructs a {@link DownloadManager}. private volatile int maxParallelDownloads;
* private volatile int minRetryCount;
* @param context Any context.
* @param databaseProvider Provides the database that holds the downloads.
* @param downloaderFactory A factory for creating {@link Downloader}s.
*/
public DownloadManager(
Context context, DatabaseProvider databaseProvider, DownloaderFactory downloaderFactory) {
this(
context,
databaseProvider,
downloaderFactory,
DEFAULT_MAX_SIMULTANEOUS_DOWNLOADS,
DEFAULT_MIN_RETRY_COUNT,
DEFAULT_REQUIREMENTS);
}
/**
* Constructs a {@link DownloadManager}.
*
* @param context Any context.
* @param databaseProvider Provides the database that holds the downloads.
* @param downloaderFactory A factory for creating {@link Downloader}s.
* @param maxSimultaneousDownloads The maximum number of simultaneous downloads.
* @param minRetryCount The minimum number of times a download must be retried before failing.
* @param requirements The requirements needed to be met to start downloads.
*/
public DownloadManager(
Context context,
DatabaseProvider databaseProvider,
DownloaderFactory downloaderFactory,
int maxSimultaneousDownloads,
int minRetryCount,
Requirements requirements) {
this(
context,
new DefaultDownloadIndex(databaseProvider),
downloaderFactory,
maxSimultaneousDownloads,
minRetryCount,
requirements);
}
/** /**
* Constructs a {@link DownloadManager}. * Constructs a {@link DownloadManager}.
@ -232,22 +189,14 @@ public final class DownloadManager {
* @param context Any context. * @param context Any context.
* @param downloadIndex The download index used to hold the download information. * @param downloadIndex The download index used to hold the download information.
* @param downloaderFactory A factory for creating {@link Downloader}s. * @param downloaderFactory A factory for creating {@link Downloader}s.
* @param maxSimultaneousDownloads The maximum number of simultaneous downloads.
* @param minRetryCount The minimum number of times a download must be retried before failing.
* @param requirements The requirements needed to be met to start downloads.
*/ */
public DownloadManager( public DownloadManager(
Context context, Context context, WritableDownloadIndex downloadIndex, DownloaderFactory downloaderFactory) {
WritableDownloadIndex downloadIndex,
DownloaderFactory downloaderFactory,
int maxSimultaneousDownloads,
int minRetryCount,
Requirements requirements) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
this.downloadIndex = downloadIndex; this.downloadIndex = downloadIndex;
this.downloaderFactory = downloaderFactory; this.downloaderFactory = downloaderFactory;
this.maxSimultaneousDownloads = maxSimultaneousDownloads; maxParallelDownloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
this.minRetryCount = minRetryCount; minRetryCount = DEFAULT_MIN_RETRY_COUNT;
downloadInternals = new ArrayList<>(); downloadInternals = new ArrayList<>();
downloads = new ArrayList<>(); downloads = new ArrayList<>();
@ -262,7 +211,8 @@ public final class DownloadManager {
internalThread.start(); internalThread.start();
internalHandler = new Handler(internalThread.getLooper(), this::handleInternalMessage); internalHandler = new Handler(internalThread.getLooper(), this::handleInternalMessage);
requirementsWatcher = new RequirementsWatcher(context, requirementsListener, requirements); requirementsWatcher =
new RequirementsWatcher(context, requirementsListener, DEFAULT_REQUIREMENTS);
int notMetRequirements = requirementsWatcher.start(); int notMetRequirements = requirementsWatcher.start();
pendingMessages = 1; pendingMessages = 1;
@ -332,6 +282,27 @@ public final class DownloadManager {
onRequirementsStateChanged(requirementsWatcher, notMetRequirements); onRequirementsStateChanged(requirementsWatcher, notMetRequirements);
} }
/**
* Sets the maximum number of parallel downloads.
*
* @param maxParallelDownloads The maximum number of parallel downloads.
*/
// TODO: Fix to properly support changes at runtime.
public void setMaxParallelDownloads(int maxParallelDownloads) {
this.maxParallelDownloads = maxParallelDownloads;
}
/**
* Sets the minimum number of times that a download will be retried. A download will fail if the
* specified number of retries is exceeded without any progress being made.
*
* @param minRetryCount The minimum number of times that a download will be retried.
*/
// TODO: Fix to properly support changes at runtime.
public void setMinRetryCount(int minRetryCount) {
this.minRetryCount = minRetryCount;
}
/** Returns the used {@link DownloadIndex}. */ /** Returns the used {@link DownloadIndex}. */
public DownloadIndex getDownloadIndex() { public DownloadIndex getDownloadIndex() {
return downloadIndex; return downloadIndex;
@ -696,15 +667,15 @@ public final class DownloadManager {
downloadThreads.remove(downloadId); downloadThreads.remove(downloadId);
boolean tryToStartDownloads = false; boolean tryToStartDownloads = false;
if (!downloadThread.isRemove) { if (!downloadThread.isRemove) {
// If maxSimultaneousDownloads was hit, there might be a download waiting for a slot. // If maxParallelDownloads was hit, there might be a download waiting for a slot.
tryToStartDownloads = simultaneousDownloads == maxSimultaneousDownloads; tryToStartDownloads = parallelDownloads == maxParallelDownloads;
simultaneousDownloads--; parallelDownloads--;
} }
getDownload(downloadId) getDownload(downloadId)
.onDownloadThreadStopped(downloadThread.isCanceled, downloadThread.finalError); .onDownloadThreadStopped(downloadThread.isCanceled, downloadThread.finalError);
if (tryToStartDownloads) { if (tryToStartDownloads) {
for (int i = 0; for (int i = 0;
simultaneousDownloads < maxSimultaneousDownloads && i < downloadInternals.size(); parallelDownloads < maxParallelDownloads && i < downloadInternals.size();
i++) { i++) {
downloadInternals.get(i).start(); downloadInternals.get(i).start();
} }
@ -760,10 +731,10 @@ public final class DownloadManager {
} }
boolean isRemove = downloadInternal.isInRemoveState(); boolean isRemove = downloadInternal.isInRemoveState();
if (!isRemove) { if (!isRemove) {
if (simultaneousDownloads == maxSimultaneousDownloads) { if (parallelDownloads == maxParallelDownloads) {
return START_THREAD_TOO_MANY_DOWNLOADS; return START_THREAD_TOO_MANY_DOWNLOADS;
} }
simultaneousDownloads++; parallelDownloads++;
} }
Downloader downloader = downloaderFactory.createDownloader(request); Downloader downloader = downloaderFactory.createDownloader(request);
DownloadThread downloadThread = DownloadThread downloadThread =

View File

@ -517,7 +517,7 @@ public class DownloadManagerTest {
assertEqualIgnoringTimeFields(mergedDownload, expectedDownload); assertEqualIgnoringTimeFields(mergedDownload, expectedDownload);
} }
private void setUpDownloadManager(final int maxActiveDownloadTasks) throws Exception { private void setUpDownloadManager(final int maxParallelDownloads) throws Exception {
if (downloadManager != null) { if (downloadManager != null) {
releaseDownloadManager(); releaseDownloadManager();
} }
@ -526,12 +526,10 @@ public class DownloadManagerTest {
() -> { () -> {
downloadManager = downloadManager =
new DownloadManager( new DownloadManager(
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
downloadIndex, downloadManager.setMaxParallelDownloads(maxParallelDownloads);
downloaderFactory, downloadManager.setMinRetryCount(MIN_RETRY_COUNT);
maxActiveDownloadTasks, downloadManager.setRequirements(new Requirements(0));
MIN_RETRY_COUNT,
new Requirements(0));
downloadManager.resumeDownloads(); downloadManager.resumeDownloads();
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread); new TestDownloadManagerListener(downloadManager, dummyMainThread);

View File

@ -32,7 +32,6 @@ import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadRequest; import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.testutil.DummyMainThread; import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable; import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
@ -259,10 +258,7 @@ public class DownloadManagerDashTest {
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(),
downloadIndex, downloadIndex,
new DefaultDownloaderFactory( new DefaultDownloaderFactory(
new DownloaderConstructorHelper(cache, fakeDataSourceFactory)), new DownloaderConstructorHelper(cache, fakeDataSourceFactory)));
/* maxSimultaneousDownloads= */ 1,
/* minRetryCount= */ 3,
new Requirements(0));
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener( new TestDownloadManagerListener(

View File

@ -35,7 +35,6 @@ import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.scheduler.Scheduler; import com.google.android.exoplayer2.scheduler.Scheduler;
import com.google.android.exoplayer2.testutil.DummyMainThread; import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
@ -119,10 +118,7 @@ public class DownloadServiceDashTest {
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(),
downloadIndex, downloadIndex,
new DefaultDownloaderFactory( new DefaultDownloaderFactory(
new DownloaderConstructorHelper(cache, fakeDataSourceFactory)), new DownloaderConstructorHelper(cache, fakeDataSourceFactory)));
/* maxSimultaneousDownloads= */ 1,
/* minRetryCount= */ 3,
new Requirements(0));
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener(dashDownloadManager, dummyMainThread); new TestDownloadManagerListener(dashDownloadManager, dummyMainThread);
dashDownloadManager.resumeDownloads(); dashDownloadManager.resumeDownloads();