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
17e8c554
authored
Dec 07, 2018
by
eguven
Committed by
Andrew Lewis
Dec 10, 2018
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Extract DownloadThread class from DownloadManager.Task
PiperOrigin-RevId: 224492268
parent
c48b77d1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
13 deletions
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
View file @
17e8c554
...
@@ -544,7 +544,7 @@ public final class DownloadManager {
...
@@ -544,7 +544,7 @@ public final class DownloadManager {
}
}
private
static
final
class
Task
implements
Runnable
{
private
static
final
class
Task
{
/** Target states for the download thread. */
/** Target states for the download thread. */
@Documented
@Documented
...
@@ -566,7 +566,7 @@ public final class DownloadManager {
...
@@ -566,7 +566,7 @@ public final class DownloadManager {
@TargetState
private
volatile
int
targetState
;
@TargetState
private
volatile
int
targetState
;
@MonotonicNonNull
private
Downloader
downloader
;
@MonotonicNonNull
private
Downloader
downloader
;
@MonotonicNonNull
private
Thread
t
hread
;
@MonotonicNonNull
private
DownloadThread
downloadT
hread
;
@MonotonicNonNull
private
Throwable
error
;
@MonotonicNonNull
private
Throwable
error
;
private
Task
(
private
Task
(
...
@@ -625,8 +625,9 @@ public final class DownloadManager {
...
@@ -625,8 +625,9 @@ public final class DownloadManager {
targetState
=
STATE_COMPLETED
;
targetState
=
STATE_COMPLETED
;
downloadManager
.
onTaskStateChange
(
this
);
downloadManager
.
onTaskStateChange
(
this
);
downloader
=
downloaderFactory
.
createDownloader
(
action
);
downloader
=
downloaderFactory
.
createDownloader
(
action
);
thread
=
new
Thread
(
this
);
downloadThread
=
thread
.
start
();
new
DownloadThread
(
this
,
downloader
,
action
.
isRemoveAction
,
minRetryCount
,
downloadManager
.
handler
);
}
}
}
}
...
@@ -649,8 +650,7 @@ public final class DownloadManager {
...
@@ -649,8 +650,7 @@ public final class DownloadManager {
private
void
stopDownloadThread
(
@TargetState
int
targetState
)
{
private
void
stopDownloadThread
(
@TargetState
int
targetState
)
{
this
.
targetState
=
targetState
;
this
.
targetState
=
targetState
;
Assertions
.
checkNotNull
(
downloader
).
cancel
();
Assertions
.
checkNotNull
(
downloadThread
).
cancel
();
Assertions
.
checkNotNull
(
thread
).
interrupt
();
}
}
private
void
onDownloadThreadStopped
(
@Nullable
Throwable
finalError
)
{
private
void
onDownloadThreadStopped
(
@Nullable
Throwable
finalError
)
{
...
@@ -664,35 +664,67 @@ public final class DownloadManager {
...
@@ -664,35 +664,67 @@ public final class DownloadManager {
error
=
finalError
;
error
=
finalError
;
downloadManager
.
onTaskStateChange
(
this
);
downloadManager
.
onTaskStateChange
(
this
);
}
}
}
private
static
class
DownloadThread
implements
Runnable
{
private
final
Task
task
;
private
final
Downloader
downloader
;
private
final
boolean
remove
;
private
final
int
minRetryCount
;
private
final
Handler
callbackHandler
;
private
final
Thread
thread
;
private
volatile
boolean
isCanceled
;
private
DownloadThread
(
Task
task
,
Downloader
downloader
,
boolean
remove
,
int
minRetryCount
,
Handler
callbackHandler
)
{
this
.
task
=
task
;
this
.
downloader
=
downloader
;
this
.
remove
=
remove
;
this
.
minRetryCount
=
minRetryCount
;
this
.
callbackHandler
=
callbackHandler
;
thread
=
new
Thread
(
this
);
thread
.
start
();
}
public
void
cancel
()
{
isCanceled
=
true
;
downloader
.
cancel
();
thread
.
interrupt
();
}
// Methods running on download thread.
// Methods running on download thread.
@Override
@Override
public
void
run
()
{
public
void
run
()
{
logd
(
"Task is started"
,
t
his
);
logd
(
"Task is started"
,
t
ask
);
Throwable
error
=
null
;
Throwable
error
=
null
;
try
{
try
{
if
(
action
.
isRemoveAction
)
{
if
(
remove
)
{
downloader
.
remove
();
downloader
.
remove
();
}
else
{
}
else
{
int
errorCount
=
0
;
int
errorCount
=
0
;
long
errorPosition
=
C
.
LENGTH_UNSET
;
long
errorPosition
=
C
.
LENGTH_UNSET
;
while
(
targetState
==
STATE_COMPLETED
)
{
while
(
!
isCanceled
)
{
try
{
try
{
downloader
.
download
();
downloader
.
download
();
break
;
break
;
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
if
(
targetState
==
STATE_COMPLETED
)
{
if
(
!
isCanceled
)
{
long
downloadedBytes
=
downloader
.
getDownloadedBytes
();
long
downloadedBytes
=
downloader
.
getDownloadedBytes
();
if
(
downloadedBytes
!=
errorPosition
)
{
if
(
downloadedBytes
!=
errorPosition
)
{
logd
(
"Reset error count. downloadedBytes = "
+
downloadedBytes
,
t
his
);
logd
(
"Reset error count. downloadedBytes = "
+
downloadedBytes
,
t
ask
);
errorPosition
=
downloadedBytes
;
errorPosition
=
downloadedBytes
;
errorCount
=
0
;
errorCount
=
0
;
}
}
if
(++
errorCount
>
minRetryCount
)
{
if
(++
errorCount
>
minRetryCount
)
{
throw
e
;
throw
e
;
}
}
logd
(
"Download error. Retry "
+
errorCount
,
t
his
);
logd
(
"Download error. Retry "
+
errorCount
,
t
ask
);
Thread
.
sleep
(
getRetryDelayMillis
(
errorCount
));
Thread
.
sleep
(
getRetryDelayMillis
(
errorCount
));
}
}
}
}
...
@@ -702,7 +734,7 @@ public final class DownloadManager {
...
@@ -702,7 +734,7 @@ public final class DownloadManager {
error
=
e
;
error
=
e
;
}
}
final
Throwable
finalError
=
error
;
final
Throwable
finalError
=
error
;
downloadManager
.
handler
.
post
(()
->
onDownloadThreadStopped
(
finalError
));
callbackHandler
.
post
(()
->
task
.
onDownloadThreadStopped
(
isCanceled
?
null
:
finalError
));
}
}
private
int
getRetryDelayMillis
(
int
errorCount
)
{
private
int
getRetryDelayMillis
(
int
errorCount
)
{
...
...
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