Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e9413b0d
authored
Apr 18, 2019
by
olly
Committed by
Oliver Woodman
Apr 18, 2019
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Simplify DownloadManager constructors
PiperOrigin-RevId: 244223870
parent
62964026
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
91 deletions
demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java
demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java
View file @
e9413b0d
...
@@ -49,7 +49,6 @@ public class DemoApplication extends Application {
...
@@ -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 {
...
@@ -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
);
}
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
View file @
e9413b0d
...
@@ -34,7 +34,6 @@ import android.os.Message;
...
@@ -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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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
simultaneous
Downloads
;
private
int
parallel
Downloads
;
/**
// 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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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 max
Simultaneous
Downloads was hit, there might be a download waiting for a slot.
// If max
Parallel
Downloads was hit, there might be a download waiting for a slot.
tryToStartDownloads
=
simultaneousDownloads
==
maxSimultaneous
Downloads
;
tryToStartDownloads
=
parallelDownloads
==
maxParallel
Downloads
;
simultaneous
Downloads
--;
parallel
Downloads
--;
}
}
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
<
maxSimultaneous
Downloads
&&
i
<
downloadInternals
.
size
();
parallelDownloads
<
maxParallel
Downloads
&&
i
<
downloadInternals
.
size
();
i
++)
{
i
++)
{
downloadInternals
.
get
(
i
).
start
();
downloadInternals
.
get
(
i
).
start
();
}
}
...
@@ -760,10 +731,10 @@ public final class DownloadManager {
...
@@ -760,10 +731,10 @@ public final class DownloadManager {
}
}
boolean
isRemove
=
downloadInternal
.
isInRemoveState
();
boolean
isRemove
=
downloadInternal
.
isInRemoveState
();
if
(!
isRemove
)
{
if
(!
isRemove
)
{
if
(
simultaneousDownloads
==
maxSimultaneous
Downloads
)
{
if
(
parallelDownloads
==
maxParallel
Downloads
)
{
return
START_THREAD_TOO_MANY_DOWNLOADS
;
return
START_THREAD_TOO_MANY_DOWNLOADS
;
}
}
simultaneous
Downloads
++;
parallel
Downloads
++;
}
}
Downloader
downloader
=
downloaderFactory
.
createDownloader
(
request
);
Downloader
downloader
=
downloaderFactory
.
createDownloader
(
request
);
DownloadThread
downloadThread
=
DownloadThread
downloadThread
=
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
View file @
e9413b0d
...
@@ -517,7 +517,7 @@ public class DownloadManagerTest {
...
@@ -517,7 +517,7 @@ public class DownloadManagerTest {
assertEqualIgnoringTimeFields
(
mergedDownload
,
expectedDownload
);
assertEqualIgnoringTimeFields
(
mergedDownload
,
expectedDownload
);
}
}
private
void
setUpDownloadManager
(
final
int
max
ActiveDownloadTask
s
)
throws
Exception
{
private
void
setUpDownloadManager
(
final
int
max
ParallelDownload
s
)
throws
Exception
{
if
(
downloadManager
!=
null
)
{
if
(
downloadManager
!=
null
)
{
releaseDownloadManager
();
releaseDownloadManager
();
}
}
...
@@ -526,12 +526,10 @@ public class DownloadManagerTest {
...
@@ -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
);
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java
View file @
e9413b0d
...
@@ -32,7 +32,6 @@ import com.google.android.exoplayer2.offline.DownloadManager;
...
@@ -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 {
...
@@ -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
(
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java
View file @
e9413b0d
...
@@ -35,7 +35,6 @@ import com.google.android.exoplayer2.offline.DownloadRequest;
...
@@ -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 {
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment