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
3d6707e2
authored
Dec 17, 2018
by
eguven
Committed by
Oliver Woodman
Dec 18, 2018
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Don't create Downloads until DownloadManager is initialized
PiperOrigin-RevId: 225824428
parent
f4fd3b12
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
36 additions
and
14 deletions
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
testutils/src/main/java/com/google/android/exoplayer2/testutil/DummyMainThread.java
testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
View file @
3d6707e2
...
...
@@ -38,7 +38,6 @@ import java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy
;
import
java.util.ArrayDeque
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.CopyOnWriteArraySet
;
import
org.checkerframework.checker.nullness.qual.MonotonicNonNull
;
...
...
@@ -93,6 +92,7 @@ public final class DownloadManager {
private
final
HandlerThread
fileIOThread
;
private
final
Handler
fileIOHandler
;
private
final
CopyOnWriteArraySet
<
Listener
>
listeners
;
private
final
ArrayDeque
<
DownloadAction
>
actionQueue
;
private
boolean
initialized
;
private
boolean
released
;
...
...
@@ -142,6 +142,7 @@ public final class DownloadManager {
fileIOHandler
=
new
Handler
(
fileIOThread
.
getLooper
());
listeners
=
new
CopyOnWriteArraySet
<>();
actionQueue
=
new
ArrayDeque
<>();
loadActions
();
logd
(
"Created"
);
...
...
@@ -194,8 +195,8 @@ public final class DownloadManager {
*/
public
void
handleAction
(
DownloadAction
action
)
{
Assertions
.
checkState
(!
released
);
Download
download
=
getDownloadForAction
(
action
);
if
(
initialized
)
{
Download
download
=
getOrAddDownloadForAction
(
action
);
saveActions
();
maybeStartDownloads
();
if
(
download
.
state
==
STATE_QUEUED
)
{
...
...
@@ -204,6 +205,8 @@ public final class DownloadManager {
// reported to listeners. Do so now.
notifyListenersDownloadStateChange
(
download
);
}
}
else
{
actionQueue
.
add
(
action
);
}
}
...
...
@@ -281,7 +284,7 @@ public final class DownloadManager {
logd
(
"Released"
);
}
private
Download
getDownloadForAction
(
DownloadAction
action
)
{
private
Download
get
OrAdd
DownloadForAction
(
DownloadAction
action
)
{
for
(
int
i
=
0
;
i
<
downloads
.
size
();
i
++)
{
Download
download
=
downloads
.
get
(
i
);
if
(
download
.
action
.
isSameMedia
(
action
))
{
...
...
@@ -380,18 +383,18 @@ public final class DownloadManager {
if
(
released
)
{
return
;
}
List
<
Download
>
pendingDownloads
=
new
ArrayList
<>(
downloads
);
downloads
.
clear
();
for
(
DownloadAction
action
:
actions
)
{
getDownloadForAction
(
action
);
get
OrAdd
DownloadForAction
(
action
);
}
logd
(
"Downloads are created."
);
initialized
=
true
;
for
(
Listener
listener
:
listeners
)
{
listener
.
onInitialized
(
DownloadManager
.
this
);
}
if
(!
pendingDownloads
.
isEmpty
())
{
downloads
.
addAll
(
pendingDownloads
);
if
(!
actionQueue
.
isEmpty
())
{
while
(!
actionQueue
.
isEmpty
())
{
getOrAddDownloadForAction
(
actionQueue
.
remove
());
}
saveActions
();
}
maybeStartDownloads
();
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
View file @
3d6707e2
...
...
@@ -426,9 +426,9 @@ public class DownloadManagerTest {
actionFile
,
downloaderFactory
,
maxActiveDownloadTasks
,
MIN_RETRY_COUNT
);
downloadManagerListener
=
new
TestDownloadManagerListener
(
downloadManager
,
dummyMainThread
);
downloadManager
.
addListener
(
downloadManagerListener
);
downloadManager
.
startDownloads
();
});
downloadManagerListener
.
waitUntilInitialized
();
}
catch
(
Throwable
throwable
)
{
throw
new
Exception
(
throwable
);
}
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java
View file @
3d6707e2
...
...
@@ -247,7 +247,6 @@ public class DownloadManagerDashTest {
downloadManagerListener
=
new
TestDownloadManagerListener
(
downloadManager
,
dummyMainThread
);
downloadManager
.
addListener
(
downloadManagerListener
);
downloadManager
.
startDownloads
();
});
}
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java
View file @
3d6707e2
...
...
@@ -124,7 +124,6 @@ public class DownloadServiceDashTest {
/* minRetryCount= */
3
);
downloadManagerListener
=
new
TestDownloadManagerListener
(
dashDownloadManager
,
dummyMainThread
);
dashDownloadManager
.
addListener
(
downloadManagerListener
);
dashDownloadManager
.
startDownloads
();
dashDownloadService
=
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/DummyMainThread.java
View file @
3d6707e2
...
...
@@ -21,6 +21,8 @@ import android.os.ConditionVariable;
import
android.os.Handler
;
import
android.os.HandlerThread
;
import
android.os.Looper
;
import
com.google.android.exoplayer2.util.Util
;
import
java.util.concurrent.atomic.AtomicReference
;
/** Helper class to simulate main/UI thread in tests. */
public
final
class
DummyMainThread
{
...
...
@@ -58,13 +60,21 @@ public final class DummyMainThread {
if
(
Looper
.
myLooper
()
==
handler
.
getLooper
())
{
runnable
.
run
();
}
else
{
final
ConditionVariable
finishedCondition
=
new
ConditionVariable
();
ConditionVariable
finishedCondition
=
new
ConditionVariable
();
AtomicReference
<
Throwable
>
thrown
=
new
AtomicReference
<>();
handler
.
post
(
()
->
{
runnable
.
run
();
try
{
runnable
.
run
();
}
catch
(
Throwable
t
)
{
thrown
.
set
(
t
);
}
finishedCondition
.
open
();
});
assertThat
(
finishedCondition
.
block
(
timeoutMs
)).
isTrue
();
if
(
thrown
.
get
()
!=
null
)
{
Util
.
sneakyThrow
(
thrown
.
get
());
}
}
}
...
...
testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java
View file @
3d6707e2
...
...
@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.testutil;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
android.os.ConditionVariable
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.offline.DownloadManager.DownloadState
;
import
java.util.HashMap
;
...
...
@@ -28,10 +29,12 @@ import java.util.concurrent.TimeUnit;
public
final
class
TestDownloadManagerListener
implements
DownloadManager
.
Listener
{
private
static
final
int
TIMEOUT
=
1000
;
private
static
final
int
INITIALIZATION_TIMEOUT
=
10000
;
private
final
DownloadManager
downloadManager
;
private
final
DummyMainThread
dummyMainThread
;
private
final
HashMap
<
String
,
ArrayBlockingQueue
<
Integer
>>
actionStates
;
private
final
ConditionVariable
initializedCondition
;
private
CountDownLatch
downloadFinishedCondition
;
@DownloadState
.
FailureReason
private
int
failureReason
;
...
...
@@ -41,6 +44,8 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
this
.
downloadManager
=
downloadManager
;
this
.
dummyMainThread
=
dummyMainThread
;
actionStates
=
new
HashMap
<>();
initializedCondition
=
new
ConditionVariable
();
downloadManager
.
addListener
(
this
);
}
public
Integer
pollStateChange
(
String
taskId
,
long
timeoutMs
)
throws
InterruptedException
{
...
...
@@ -53,7 +58,13 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
@Override
public
void
onInitialized
(
DownloadManager
downloadManager
)
{
// Do nothing.
initializedCondition
.
open
();
}
public
void
waitUntilInitialized
()
{
if
(!
downloadManager
.
isInitialized
())
{
assertThat
(
initializedCondition
.
block
(
INITIALIZATION_TIMEOUT
)).
isTrue
();
}
}
@Override
...
...
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