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
5856e757
authored
Apr 16, 2019
by
eguven
Committed by
Oliver Woodman
Apr 16, 2019
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Rename DownloadAction to DownloadRequest
PiperOrigin-RevId: 243806888
parent
9fc3ea79
Hide whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
489 additions
and
488 deletions
demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java
demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadTracker.java
library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java
library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactory.java
library/core/src/main/java/com/google/android/exoplayer2/offline/Download.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java → library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadRequest.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderFactory.java
library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactoryTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadBuilder.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadHelperTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java → library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadRequestTest.java
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.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
library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java
library/smoothstreaming/src/test/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloaderTest.java
testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java
demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java
View file @
5856e757
...
...
@@ -75,13 +75,13 @@ public class DemoDownloadService extends DownloadService {
notificationHelper
.
buildDownloadCompletedNotification
(
R
.
drawable
.
ic_download_done
,
/* contentIntent= */
null
,
Util
.
fromUtf8Bytes
(
download
.
action
.
data
));
Util
.
fromUtf8Bytes
(
download
.
request
.
data
));
}
else
if
(
download
.
state
==
Download
.
STATE_FAILED
)
{
notification
=
notificationHelper
.
buildDownloadFailedNotification
(
R
.
drawable
.
ic_download_done
,
/* contentIntent= */
null
,
Util
.
fromUtf8Bytes
(
download
.
action
.
data
));
Util
.
fromUtf8Bytes
(
download
.
request
.
data
));
}
else
{
return
;
}
...
...
demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadTracker.java
View file @
5856e757
...
...
@@ -24,11 +24,11 @@ import android.widget.Toast;
import
com.google.android.exoplayer2.C
;
import
com.google.android.exoplayer2.RenderersFactory
;
import
com.google.android.exoplayer2.offline.Download
;
import
com.google.android.exoplayer2.offline.DownloadAction
;
import
com.google.android.exoplayer2.offline.DownloadCursor
;
import
com.google.android.exoplayer2.offline.DownloadHelper
;
import
com.google.android.exoplayer2.offline.DownloadIndex
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.offline.DownloadRequest
;
import
com.google.android.exoplayer2.offline.DownloadService
;
import
com.google.android.exoplayer2.offline.StreamKey
;
import
com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo
;
...
...
@@ -89,7 +89,7 @@ public class DownloadTracker {
public
List
<
StreamKey
>
getOfflineStreamKeys
(
Uri
uri
)
{
Download
download
=
downloads
.
get
(
uri
);
return
download
!=
null
&&
download
.
state
!=
Download
.
STATE_FAILED
?
download
.
action
.
streamKeys
?
download
.
request
.
streamKeys
:
Collections
.
emptyList
();
}
...
...
@@ -102,7 +102,7 @@ public class DownloadTracker {
Download
download
=
downloads
.
get
(
uri
);
if
(
download
!=
null
)
{
DownloadService
.
startWithRemoveDownload
(
context
,
DemoDownloadService
.
class
,
download
.
action
.
id
,
/* foreground= */
false
);
context
,
DemoDownloadService
.
class
,
download
.
request
.
id
,
/* foreground= */
false
);
}
else
{
if
(
startDownloadDialogHelper
!=
null
)
{
startDownloadDialogHelper
.
release
();
...
...
@@ -117,18 +117,13 @@ public class DownloadTracker {
try
(
DownloadCursor
loadedDownloads
=
downloadIndex
.
getDownloads
())
{
while
(
loadedDownloads
.
moveToNext
())
{
Download
download
=
loadedDownloads
.
getDownload
();
downloads
.
put
(
download
.
action
.
uri
,
download
);
downloads
.
put
(
download
.
request
.
uri
,
download
);
}
}
catch
(
IOException
e
)
{
Log
.
w
(
TAG
,
"Failed to query downloads"
,
e
);
}
}
private
void
startServiceWithAction
(
DownloadAction
action
)
{
DownloadService
.
startWithAction
(
context
,
DemoDownloadService
.
class
,
action
,
/* foreground= */
false
);
}
private
DownloadHelper
getDownloadHelper
(
Uri
uri
,
String
extension
,
RenderersFactory
renderersFactory
)
{
int
type
=
Util
.
inferContentType
(
uri
,
extension
);
...
...
@@ -150,7 +145,7 @@ public class DownloadTracker {
@Override
public
void
onDownloadChanged
(
DownloadManager
downloadManager
,
Download
download
)
{
downloads
.
put
(
download
.
action
.
uri
,
download
);
downloads
.
put
(
download
.
request
.
uri
,
download
);
for
(
Listener
listener
:
listeners
)
{
listener
.
onDownloadsChanged
();
}
...
...
@@ -158,7 +153,7 @@ public class DownloadTracker {
@Override
public
void
onDownloadRemoved
(
DownloadManager
downloadManager
,
Download
download
)
{
downloads
.
remove
(
download
.
action
.
uri
);
downloads
.
remove
(
download
.
request
.
uri
);
for
(
Listener
listener
:
listeners
)
{
listener
.
onDownloadsChanged
();
}
...
...
@@ -259,8 +254,9 @@ public class DownloadTracker {
// Internal methods.
private
void
startDownload
()
{
DownloadAction
downloadAction
=
downloadHelper
.
getDownloadAction
(
Util
.
getUtf8Bytes
(
name
));
startServiceWithAction
(
downloadAction
);
DownloadRequest
downloadRequest
=
downloadHelper
.
getDownloadRequest
(
Util
.
getUtf8Bytes
(
name
));
DownloadService
.
startWithNewDownload
(
context
,
DemoDownloadService
.
class
,
downloadRequest
,
/* foreground= */
false
);
}
}
}
library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java
View file @
5856e757
...
...
@@ -17,7 +17,7 @@ package com.google.android.exoplayer2.offline;
import
android.net.Uri
;
import
androidx.annotation.Nullable
;
import
com.google.android.exoplayer2.offline.Download
Action.UnsupportedAction
Exception
;
import
com.google.android.exoplayer2.offline.Download
Request.UnsupportedRequest
Exception
;
import
com.google.android.exoplayer2.util.AtomicFile
;
import
com.google.android.exoplayer2.util.Util
;
import
java.io.DataInputStream
;
...
...
@@ -28,7 +28,7 @@ import java.util.ArrayList;
import
java.util.List
;
/**
* Loads {@link Download
Action DownloadAction
s} from legacy action files.
* Loads {@link Download
Request DownloadRequest
s} from legacy action files.
*
* @deprecated Legacy action files should be merged into download indices using {@link
* ActionFileUpgradeUtil}.
...
...
@@ -41,7 +41,7 @@ import java.util.List;
private
final
AtomicFile
atomicFile
;
/**
* @param actionFile The file from which {@link Download
Action DownloadAction
s} will be loaded.
* @param actionFile The file from which {@link Download
Request DownloadRequest
s} will be loaded.
*/
public
ActionFile
(
File
actionFile
)
{
atomicFile
=
new
AtomicFile
(
actionFile
);
...
...
@@ -58,15 +58,15 @@ import java.util.List;
}
/**
* Loads {@link Download
Action DownloadAction
s} from the file.
* Loads {@link Download
Request DownloadRequest
s} from the file.
*
* @return The loaded {@link Download
Action DownloadAction
s}, or an empty array if the file does
* @return The loaded {@link Download
Request DownloadRequest
s}, or an empty array if the file does
* not exist.
* @throws IOException If there is an error reading the file.
*/
public
Download
Action
[]
load
()
throws
IOException
{
public
Download
Request
[]
load
()
throws
IOException
{
if
(!
exists
())
{
return
new
Download
Action
[
0
];
return
new
Download
Request
[
0
];
}
InputStream
inputStream
=
null
;
try
{
...
...
@@ -77,21 +77,21 @@ import java.util.List;
throw
new
IOException
(
"Unsupported action file version: "
+
version
);
}
int
actionCount
=
dataInputStream
.
readInt
();
ArrayList
<
Download
Action
>
actions
=
new
ArrayList
<>();
ArrayList
<
Download
Request
>
actions
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
actionCount
;
i
++)
{
try
{
actions
.
add
(
readDownload
Action
(
dataInputStream
));
}
catch
(
Unsupported
Action
Exception
e
)
{
// remove Download
Action is not supported. Ignore the exception
and continue loading rest.
actions
.
add
(
readDownload
Request
(
dataInputStream
));
}
catch
(
Unsupported
Request
Exception
e
)
{
// remove Download
Request is not supported. Ignore
and continue loading rest.
}
}
return
actions
.
toArray
(
new
Download
Action
[
0
]);
return
actions
.
toArray
(
new
Download
Request
[
0
]);
}
finally
{
Util
.
closeQuietly
(
inputStream
);
}
}
private
static
Download
Action
readDownloadAction
(
DataInputStream
input
)
throws
IOException
{
private
static
Download
Request
readDownloadRequest
(
DataInputStream
input
)
throws
IOException
{
String
type
=
input
.
readUTF
();
int
version
=
input
.
readInt
();
...
...
@@ -108,7 +108,7 @@ import java.util.List;
}
// Serialized version 0 progressive actions did not contain keys.
boolean
isLegacyProgressive
=
version
==
0
&&
Download
Action
.
TYPE_PROGRESSIVE
.
equals
(
type
);
boolean
isLegacyProgressive
=
version
==
0
&&
Download
Request
.
TYPE_PROGRESSIVE
.
equals
(
type
);
List
<
StreamKey
>
keys
=
new
ArrayList
<>();
if
(!
isLegacyProgressive
)
{
int
keyCount
=
input
.
readInt
();
...
...
@@ -120,22 +120,22 @@ import java.util.List;
// Serialized version 0 and 1 DASH/HLS/SS actions did not contain a custom cache key.
boolean
isLegacySegmented
=
version
<
2
&&
(
Download
Action
.
TYPE_DASH
.
equals
(
type
)
||
Download
Action
.
TYPE_HLS
.
equals
(
type
)
||
Download
Action
.
TYPE_SS
.
equals
(
type
));
&&
(
Download
Request
.
TYPE_DASH
.
equals
(
type
)
||
Download
Request
.
TYPE_HLS
.
equals
(
type
)
||
Download
Request
.
TYPE_SS
.
equals
(
type
));
String
customCacheKey
=
null
;
if
(!
isLegacySegmented
)
{
customCacheKey
=
input
.
readBoolean
()
?
input
.
readUTF
()
:
null
;
}
// Serialized version 0, 1 and 2 did not contain an id. We need to generate one.
String
id
=
version
<
3
?
generateDownload
Action
Id
(
uri
,
customCacheKey
)
:
input
.
readUTF
();
String
id
=
version
<
3
?
generateDownloadId
(
uri
,
customCacheKey
)
:
input
.
readUTF
();
if
(
isRemoveAction
)
{
// Remove actions are not supported anymore.
throw
new
Unsupported
Action
Exception
();
throw
new
Unsupported
Request
Exception
();
}
return
new
Download
Action
(
id
,
type
,
uri
,
keys
,
customCacheKey
,
data
);
return
new
Download
Request
(
id
,
type
,
uri
,
keys
,
customCacheKey
,
data
);
}
private
static
StreamKey
readKey
(
String
type
,
int
version
,
DataInputStream
input
)
...
...
@@ -145,7 +145,7 @@ import java.util.List;
int
trackIndex
;
// Serialized version 0 HLS/SS actions did not contain a period index.
if
((
Download
Action
.
TYPE_HLS
.
equals
(
type
)
||
DownloadAction
.
TYPE_SS
.
equals
(
type
))
if
((
Download
Request
.
TYPE_HLS
.
equals
(
type
)
||
DownloadRequest
.
TYPE_SS
.
equals
(
type
))
&&
version
==
0
)
{
periodIndex
=
0
;
groupIndex
=
input
.
readInt
();
...
...
@@ -158,7 +158,7 @@ import java.util.List;
return
new
StreamKey
(
periodIndex
,
groupIndex
,
trackIndex
);
}
private
static
String
generateDownload
Action
Id
(
Uri
uri
,
@Nullable
String
customCacheKey
)
{
private
static
String
generateDownloadId
(
Uri
uri
,
@Nullable
String
customCacheKey
)
{
return
customCacheKey
!=
null
?
customCacheKey
:
uri
.
toString
();
}
}
library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java
View file @
5856e757
...
...
@@ -28,18 +28,18 @@ public final class ActionFileUpgradeUtil {
public
interface
DownloadIdProvider
{
/**
* Returns a download id for given
action
.
* Returns a download id for given
request
.
*
* @param download
Action The action
for which an ID is required.
* @param download
Request The request
for which an ID is required.
* @return A corresponding download ID.
*/
String
getId
(
Download
Action
downloadAction
);
String
getId
(
Download
Request
downloadRequest
);
}
private
ActionFileUpgradeUtil
()
{}
/**
* Merges {@link Download
Action DownloadAction
s} contained in a legacy action file into a {@link
* Merges {@link Download
Request DownloadRequest
s} contained in a legacy action file into a {@link
* DefaultDownloadIndex}, deleting the action file if the merge is successful or if {@code
* deleteOnFailure} is {@code true}.
*
...
...
@@ -49,9 +49,9 @@ public final class ActionFileUpgradeUtil {
* @param actionFilePath The action file path.
* @param downloadIdProvider A download ID provider, or {@code null}. If {@code null} then ID of
* each download will be its custom cache key if one is specified, or else its URL.
* @param downloadIndex The index into which the
action
will be merged.
* @param downloadIndex The index into which the
requests
will be merged.
* @param deleteOnFailure Whether to delete the action file if the merge fails.
* @throws IOException If an error occurs loading or merging the
action
s.
* @throws IOException If an error occurs loading or merging the
request
s.
*/
@SuppressWarnings
(
"deprecation"
)
public
static
void
upgradeAndDelete
(
...
...
@@ -64,11 +64,11 @@ public final class ActionFileUpgradeUtil {
if
(
actionFile
.
exists
())
{
boolean
success
=
false
;
try
{
for
(
Download
Action
action
:
actionFile
.
load
())
{
for
(
Download
Request
request
:
actionFile
.
load
())
{
if
(
downloadIdProvider
!=
null
)
{
action
=
action
.
copyWithId
(
downloadIdProvider
.
getId
(
action
));
request
=
request
.
copyWithId
(
downloadIdProvider
.
getId
(
request
));
}
merge
Action
(
action
,
downloadIndex
);
merge
Request
(
request
,
downloadIndex
);
}
success
=
true
;
}
finally
{
...
...
@@ -80,22 +80,22 @@ public final class ActionFileUpgradeUtil {
}
/**
* Merges a {@link Download
Action
} into a {@link DefaultDownloadIndex}.
* Merges a {@link Download
Request
} into a {@link DefaultDownloadIndex}.
*
* @param
action The action
to be merged.
* @param downloadIndex The index into which the
action
will be merged.
* @throws IOException If an error occurs merging the
action
.
* @param
request The request
to be merged.
* @param downloadIndex The index into which the
request
will be merged.
* @throws IOException If an error occurs merging the
request
.
*/
/* package */
static
void
merge
Action
(
DownloadAction
action
,
DefaultDownloadIndex
downloadIndex
)
throws
IOException
{
Download
download
=
downloadIndex
.
getDownload
(
action
.
id
);
/* package */
static
void
merge
Request
(
DownloadRequest
request
,
DefaultDownloadIndex
downloadIndex
)
throws
IOException
{
Download
download
=
downloadIndex
.
getDownload
(
request
.
id
);
if
(
download
!=
null
)
{
download
=
DownloadManager
.
merge
Action
(
download
,
action
,
download
.
manualStopReason
);
download
=
DownloadManager
.
merge
Request
(
download
,
request
,
download
.
manualStopReason
);
}
else
{
long
nowMs
=
System
.
currentTimeMillis
();
download
=
new
Download
(
action
,
request
,
STATE_QUEUED
,
Download
.
FAILURE_REASON_NONE
,
Download
.
MANUAL_STOP_REASON_NONE
,
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java
View file @
5856e757
...
...
@@ -194,12 +194,12 @@ public final class DefaultDownloadIndex implements DownloadIndex {
public
void
putDownload
(
Download
download
)
throws
DatabaseIOException
{
ensureInitialized
();
ContentValues
values
=
new
ContentValues
();
values
.
put
(
COLUMN_ID
,
download
.
action
.
id
);
values
.
put
(
COLUMN_TYPE
,
download
.
action
.
type
);
values
.
put
(
COLUMN_URI
,
download
.
action
.
uri
.
toString
());
values
.
put
(
COLUMN_STREAM_KEYS
,
encodeStreamKeys
(
download
.
action
.
streamKeys
));
values
.
put
(
COLUMN_CUSTOM_CACHE_KEY
,
download
.
action
.
customCacheKey
);
values
.
put
(
COLUMN_DATA
,
download
.
action
.
data
);
values
.
put
(
COLUMN_ID
,
download
.
request
.
id
);
values
.
put
(
COLUMN_TYPE
,
download
.
request
.
type
);
values
.
put
(
COLUMN_URI
,
download
.
request
.
uri
.
toString
());
values
.
put
(
COLUMN_STREAM_KEYS
,
encodeStreamKeys
(
download
.
request
.
streamKeys
));
values
.
put
(
COLUMN_CUSTOM_CACHE_KEY
,
download
.
request
.
customCacheKey
);
values
.
put
(
COLUMN_DATA
,
download
.
request
.
data
);
values
.
put
(
COLUMN_STATE
,
download
.
state
);
values
.
put
(
COLUMN_DOWNLOAD_PERCENTAGE
,
download
.
getDownloadPercentage
());
values
.
put
(
COLUMN_DOWNLOADED_BYTES
,
download
.
getDownloadedBytes
());
...
...
@@ -342,8 +342,8 @@ public final class DefaultDownloadIndex implements DownloadIndex {
}
private
static
Download
getDownloadForCurrentRow
(
Cursor
cursor
)
{
Download
Action
action
=
new
Download
Action
(
Download
Request
request
=
new
Download
Request
(
cursor
.
getString
(
COLUMN_INDEX_ID
),
cursor
.
getString
(
COLUMN_INDEX_TYPE
),
Uri
.
parse
(
cursor
.
getString
(
COLUMN_INDEX_URI
)),
...
...
@@ -355,7 +355,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
cachingCounters
.
contentLength
=
cursor
.
getLong
(
COLUMN_INDEX_TOTAL_BYTES
);
cachingCounters
.
percentage
=
cursor
.
getFloat
(
COLUMN_INDEX_DOWNLOAD_PERCENTAGE
);
return
new
Download
(
action
,
request
,
cursor
.
getInt
(
COLUMN_INDEX_STATE
),
cursor
.
getInt
(
COLUMN_INDEX_FAILURE_REASON
),
cursor
.
getInt
(
COLUMN_INDEX_MANUAL_STOP_REASON
),
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactory.java
View file @
5856e757
...
...
@@ -76,32 +76,32 @@ public class DefaultDownloaderFactory implements DownloaderFactory {
}
@Override
public
Downloader
createDownloader
(
Download
Action
action
)
{
switch
(
action
.
type
)
{
case
Download
Action
.
TYPE_PROGRESSIVE
:
public
Downloader
createDownloader
(
Download
Request
request
)
{
switch
(
request
.
type
)
{
case
Download
Request
.
TYPE_PROGRESSIVE
:
return
new
ProgressiveDownloader
(
action
.
uri
,
action
.
customCacheKey
,
downloaderConstructorHelper
);
case
Download
Action
.
TYPE_DASH
:
return
createDownloader
(
action
,
DASH_DOWNLOADER_CONSTRUCTOR
);
case
Download
Action
.
TYPE_HLS
:
return
createDownloader
(
action
,
HLS_DOWNLOADER_CONSTRUCTOR
);
case
Download
Action
.
TYPE_SS
:
return
createDownloader
(
action
,
SS_DOWNLOADER_CONSTRUCTOR
);
request
.
uri
,
request
.
customCacheKey
,
downloaderConstructorHelper
);
case
Download
Request
.
TYPE_DASH
:
return
createDownloader
(
request
,
DASH_DOWNLOADER_CONSTRUCTOR
);
case
Download
Request
.
TYPE_HLS
:
return
createDownloader
(
request
,
HLS_DOWNLOADER_CONSTRUCTOR
);
case
Download
Request
.
TYPE_SS
:
return
createDownloader
(
request
,
SS_DOWNLOADER_CONSTRUCTOR
);
default
:
throw
new
IllegalArgumentException
(
"Unsupported type: "
+
action
.
type
);
throw
new
IllegalArgumentException
(
"Unsupported type: "
+
request
.
type
);
}
}
private
Downloader
createDownloader
(
Download
Action
action
,
@Nullable
Constructor
<?
extends
Downloader
>
constructor
)
{
Download
Request
request
,
@Nullable
Constructor
<?
extends
Downloader
>
constructor
)
{
if
(
constructor
==
null
)
{
throw
new
IllegalStateException
(
"Module missing for: "
+
action
.
type
);
throw
new
IllegalStateException
(
"Module missing for: "
+
request
.
type
);
}
try
{
// TODO: Support customCacheKey in DASH/HLS/SS, for completeness.
return
constructor
.
newInstance
(
action
.
uri
,
action
.
streamKeys
,
downloaderConstructorHelper
);
return
constructor
.
newInstance
(
request
.
uri
,
request
.
streamKeys
,
downloaderConstructorHelper
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Failed to instantiate downloader for: "
+
action
.
type
,
e
);
throw
new
RuntimeException
(
"Failed to instantiate downloader for: "
+
request
.
type
,
e
);
}
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/Download.java
View file @
5856e757
...
...
@@ -94,8 +94,8 @@ public final class Download {
}
}
/** The download
action
. */
public
final
Download
Action
action
;
/** The download
request
. */
public
final
Download
Request
request
;
/** The state of the download. */
@State
public
final
int
state
;
...
...
@@ -114,14 +114,14 @@ public final class Download {
/* package */
CachingCounters
counters
;
/* package */
Download
(
Download
Action
action
,
Download
Request
request
,
@State
int
state
,
@FailureReason
int
failureReason
,
int
manualStopReason
,
long
startTimeMs
,
long
updateTimeMs
)
{
this
(
action
,
request
,
state
,
failureReason
,
manualStopReason
,
...
...
@@ -131,7 +131,7 @@ public final class Download {
}
/* package */
Download
(
Download
Action
action
,
Download
Request
request
,
@State
int
state
,
@FailureReason
int
failureReason
,
int
manualStopReason
,
...
...
@@ -143,7 +143,7 @@ public final class Download {
if
(
manualStopReason
!=
0
)
{
Assertions
.
checkState
(
state
!=
STATE_DOWNLOADING
&&
state
!=
STATE_QUEUED
);
}
this
.
action
=
action
;
this
.
request
=
request
;
this
.
state
=
state
;
this
.
failureReason
=
failureReason
;
this
.
manualStopReason
=
manualStopReason
;
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java
View file @
5856e757
...
...
@@ -63,7 +63,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* A helper for initializing and removing downloads.
*
* <p>The helper extracts track information from the media, selects tracks for downloading, and
* creates {@link Download
Action download action
s} based on the selected tracks.
* creates {@link Download
Request download request
s} based on the selected tracks.
*
* <p>A typical usage of DownloadHelper follows these steps:
*
...
...
@@ -74,7 +74,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* #getTrackSelections(int, int)}, and make adjustments using {@link
* #clearTrackSelections(int)}, {@link #replaceTrackSelections(int, Parameters)} and {@link
* #addTrackSelection(int, Parameters)}.
* <li>Create a download
action for the selected track using {@link #getDownloadAction
(byte[])}.
* <li>Create a download
request for the selected track using {@link #getDownloadRequest
(byte[])}.
* <li>Release the helper using {@link #release()}.
* </ol>
*/
...
...
@@ -150,7 +150,7 @@ public final class DownloadHelper {
*/
public
static
DownloadHelper
forProgressive
(
Uri
uri
,
@Nullable
String
cacheKey
)
{
return
new
DownloadHelper
(
Download
Action
.
TYPE_PROGRESSIVE
,
Download
Request
.
TYPE_PROGRESSIVE
,
uri
,
cacheKey
,
/* mediaSource= */
null
,
...
...
@@ -199,7 +199,7 @@ public final class DownloadHelper {
@Nullable
DrmSessionManager
<
FrameworkMediaCrypto
>
drmSessionManager
,
DefaultTrackSelector
.
Parameters
trackSelectorParameters
)
{
return
new
DownloadHelper
(
Download
Action
.
TYPE_DASH
,
Download
Request
.
TYPE_DASH
,
uri
,
/* cacheKey= */
null
,
createMediaSource
(
...
...
@@ -249,7 +249,7 @@ public final class DownloadHelper {
@Nullable
DrmSessionManager
<
FrameworkMediaCrypto
>
drmSessionManager
,
DefaultTrackSelector
.
Parameters
trackSelectorParameters
)
{
return
new
DownloadHelper
(
Download
Action
.
TYPE_HLS
,
Download
Request
.
TYPE_HLS
,
uri
,
/* cacheKey= */
null
,
createMediaSource
(
...
...
@@ -299,7 +299,7 @@ public final class DownloadHelper {
@Nullable
DrmSessionManager
<
FrameworkMediaCrypto
>
drmSessionManager
,
DefaultTrackSelector
.
Parameters
trackSelectorParameters
)
{
return
new
DownloadHelper
(
Download
Action
.
TYPE_SS
,
Download
Request
.
TYPE_SS
,
uri
,
/* cacheKey= */
null
,
createMediaSource
(
uri
,
dataSourceFactory
,
SS_FACTORY_CONSTRUCTOR
,
SS_FACTORY_CREATE_METHOD
),
...
...
@@ -327,7 +327,7 @@ public final class DownloadHelper {
/**
* Creates download helper.
*
* @param downloadType A download type. This value will be used as {@link Download
Action
#type}.
* @param downloadType A download type. This value will be used as {@link Download
Request
#type}.
* @param uri A {@link Uri}.
* @param cacheKey An optional cache key.
* @param mediaSource A {@link MediaSource} for which tracks are selected, or null if no track
...
...
@@ -577,16 +577,16 @@ public final class DownloadHelper {
}
/**
* Builds a {@link Download
Action
} for downloading the selected tracks. Must not be called until
* Builds a {@link Download
Request
} for downloading the selected tracks. Must not be called until
* after preparation completes.
*
* @param data Application provided data to store in {@link Download
Action
#data}.
* @return The built {@link Download
Action
}.
* @param data Application provided data to store in {@link Download
Request
#data}.
* @return The built {@link Download
Request
}.
*/
public
Download
Action
getDownloadAction
(
@Nullable
byte
[]
data
)
{
public
Download
Request
getDownloadRequest
(
@Nullable
byte
[]
data
)
{
String
downloadId
=
uri
.
toString
();
if
(
mediaSource
==
null
)
{
return
new
Download
Action
(
return
new
Download
Request
(
downloadId
,
downloadType
,
uri
,
/* streamKeys= */
Collections
.
emptyList
(),
cacheKey
,
data
);
}
assertPreparedWithMedia
();
...
...
@@ -601,7 +601,7 @@ public final class DownloadHelper {
}
streamKeys
.
addAll
(
mediaPreparer
.
mediaPeriods
[
periodIndex
].
getStreamKeys
(
allSelections
));
}
return
new
Download
Action
(
downloadId
,
downloadType
,
uri
,
streamKeys
,
cacheKey
,
data
);
return
new
Download
Request
(
downloadId
,
downloadType
,
uri
,
streamKeys
,
cacheKey
,
data
);
}
// Initialization of array of Lists.
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java
View file @
5856e757
...
...
@@ -69,7 +69,7 @@ public final class DownloadManager {
public
interface
Listener
{
/**
* Called when all
action
s have been restored.
* Called when all
download
s have been restored.
*
* @param downloadManager The reporting instance.
*/
...
...
@@ -382,25 +382,25 @@ public final class DownloadManager {
}
/**
* Adds a download defined by the given
action
.
* Adds a download defined by the given
request
.
*
* @param
action The download action
.
* @param
request The download request
.
*/
public
void
addDownload
(
Download
Action
action
)
{
addDownload
(
action
,
Download
.
MANUAL_STOP_REASON_NONE
);
public
void
addDownload
(
Download
Request
request
)
{
addDownload
(
request
,
Download
.
MANUAL_STOP_REASON_NONE
);
}
/**
* Adds a download defined by the given
action
and with the specified manual stop reason.
* Adds a download defined by the given
request
and with the specified manual stop reason.
*
* @param
action The download action
.
* @param
request The download request
.
* @param manualStopReason An initial manual stop reason for the download, or {@link
* Download#MANUAL_STOP_REASON_NONE} if the download should be started.
*/
public
void
addDownload
(
Download
Action
action
,
int
manualStopReason
)
{
public
void
addDownload
(
Download
Request
request
,
int
manualStopReason
)
{
pendingMessages
++;
internalHandler
.
obtainMessage
(
MSG_ADD_DOWNLOAD
,
manualStopReason
,
/* unused */
0
,
action
)
.
obtainMessage
(
MSG_ADD_DOWNLOAD
,
manualStopReason
,
/* unused */
0
,
request
)
.
sendToTarget
();
}
...
...
@@ -415,9 +415,8 @@ public final class DownloadManager {
}
/**
* Stops all of the downloads and releases resources. If the action file isn't up to date, waits
* for the changes to be written. The manager must not be accessed after this method has been
* called.
* Stops the downloads and releases resources. Waits until the downloads are persisted to the
* download index. The manager must not be accessed after this method has been called.
*/
public
void
release
()
{
synchronized
(
releaseLock
)
{
...
...
@@ -498,7 +497,7 @@ public final class DownloadManager {
}
private
void
onDownloadChanged
(
Download
download
)
{
int
downloadIndex
=
getDownloadIndex
(
download
.
action
.
id
);
int
downloadIndex
=
getDownloadIndex
(
download
.
request
.
id
);
if
(
download
.
isTerminalState
())
{
if
(
downloadIndex
!=
C
.
INDEX_UNSET
)
{
downloads
.
remove
(
downloadIndex
);
...
...
@@ -514,7 +513,7 @@ public final class DownloadManager {
}
private
void
onDownloadRemoved
(
Download
download
)
{
downloads
.
remove
(
getDownloadIndex
(
download
.
action
.
id
));
downloads
.
remove
(
getDownloadIndex
(
download
.
request
.
id
));
for
(
Listener
listener
:
listeners
)
{
listener
.
onDownloadRemoved
(
this
,
download
);
}
...
...
@@ -532,7 +531,7 @@ public final class DownloadManager {
private
int
getDownloadIndex
(
String
id
)
{
for
(
int
i
=
0
;
i
<
downloads
.
size
();
i
++)
{
if
(
downloads
.
get
(
i
).
action
.
id
.
equals
(
id
))
{
if
(
downloads
.
get
(
i
).
request
.
id
.
equals
(
id
))
{
return
i
;
}
}
...
...
@@ -562,9 +561,9 @@ public final class DownloadManager {
setManualStopReasonInternal
(
id
,
manualStopReason
);
break
;
case
MSG_ADD_DOWNLOAD:
Download
Action
action
=
(
DownloadAction
)
message
.
obj
;
Download
Request
request
=
(
DownloadRequest
)
message
.
obj
;
manualStopReason
=
message
.
arg1
;
addDownloadInternal
(
action
,
manualStopReason
);
addDownloadInternal
(
request
,
manualStopReason
);
break
;
case
MSG_REMOVE_DOWNLOAD:
id
=
(
String
)
message
.
obj
;
...
...
@@ -657,27 +656,27 @@ public final class DownloadManager {
}
}
private
void
addDownloadInternal
(
Download
Action
action
,
int
manualStopReason
)
{
DownloadInternal
downloadInternal
=
getDownload
(
action
.
id
);
private
void
addDownloadInternal
(
Download
Request
request
,
int
manualStopReason
)
{
DownloadInternal
downloadInternal
=
getDownload
(
request
.
id
);
if
(
downloadInternal
!=
null
)
{
downloadInternal
.
add
Action
(
action
,
manualStopReason
);
logd
(
"
Action
is added to existing download"
,
downloadInternal
);
downloadInternal
.
add
Request
(
request
,
manualStopReason
);
logd
(
"
Request
is added to existing download"
,
downloadInternal
);
}
else
{
Download
download
=
loadDownload
(
action
.
id
);
Download
download
=
loadDownload
(
request
.
id
);
if
(
download
==
null
)
{
long
nowMs
=
System
.
currentTimeMillis
();
download
=
new
Download
(
action
,
request
,
manualStopReason
!=
Download
.
MANUAL_STOP_REASON_NONE
?
STATE_STOPPED
:
STATE_QUEUED
,
Download
.
FAILURE_REASON_NONE
,
manualStopReason
,
/* startTimeMs= */
nowMs
,
/* updateTimeMs= */
nowMs
);
logd
(
"Download state is created for "
+
action
.
id
);
logd
(
"Download state is created for "
+
request
.
id
);
}
else
{
download
=
merge
Action
(
download
,
action
,
manualStopReason
);
logd
(
"Download state is loaded for "
+
action
.
id
);
download
=
merge
Request
(
download
,
request
,
manualStopReason
);
logd
(
"Download state is loaded for "
+
request
.
id
);
}
addDownloadForState
(
download
);
}
...
...
@@ -698,8 +697,8 @@ public final class DownloadManager {
}
private
void
onDownloadThreadStoppedInternal
(
DownloadThread
downloadThread
)
{
logd
(
"Download is stopped"
,
downloadThread
.
action
);
String
downloadId
=
downloadThread
.
action
.
id
;
logd
(
"Download is stopped"
,
downloadThread
.
request
);
String
downloadId
=
downloadThread
.
request
.
id
;
downloadThreads
.
remove
(
downloadId
);
boolean
tryToStartDownloads
=
false
;
if
(!
downloadThread
.
isRemove
)
{
...
...
@@ -747,7 +746,7 @@ public final class DownloadManager {
private
void
onDownloadRemovedInternal
(
DownloadInternal
downloadInternal
,
Download
download
)
{
logd
(
"Download is removed"
,
downloadInternal
);
try
{
downloadIndex
.
removeDownload
(
download
.
action
.
id
);
downloadIndex
.
removeDownload
(
download
.
request
.
id
);
}
catch
(
DatabaseIOException
e
)
{
Log
.
e
(
TAG
,
"Failed to remove from index"
,
e
);
}
...
...
@@ -757,8 +756,8 @@ public final class DownloadManager {
@StartThreadResults
private
int
startDownloadThread
(
DownloadInternal
downloadInternal
)
{
Download
Action
action
=
downloadInternal
.
download
.
action
;
String
downloadId
=
action
.
id
;
Download
Request
request
=
downloadInternal
.
download
.
request
;
String
downloadId
=
request
.
id
;
if
(
downloadThreads
.
containsKey
(
downloadId
))
{
if
(
stopDownloadThreadInternal
(
downloadId
))
{
return
START_THREAD_WAIT_DOWNLOAD_CANCELLATION
;
...
...
@@ -772,9 +771,9 @@ public final class DownloadManager {
}
simultaneousDownloads
++;
}
Downloader
downloader
=
downloaderFactory
.
createDownloader
(
action
);
Downloader
downloader
=
downloaderFactory
.
createDownloader
(
request
);
DownloadThread
downloadThread
=
new
DownloadThread
(
action
,
downloader
,
isRemove
,
minRetryCount
,
internalHandler
);
new
DownloadThread
(
request
,
downloader
,
isRemove
,
minRetryCount
,
internalHandler
);
downloadThreads
.
put
(
downloadId
,
downloadThread
);
downloadInternal
.
setCounters
(
downloadThread
.
downloader
.
getCounters
());
downloadThread
.
start
();
...
...
@@ -786,7 +785,7 @@ public final class DownloadManager {
DownloadThread
downloadThread
=
downloadThreads
.
get
(
downloadId
);
if
(
downloadThread
!=
null
&&
!
downloadThread
.
isRemove
)
{
downloadThread
.
cancel
(
/* released= */
false
);
logd
(
"Download is cancelled"
,
downloadThread
.
action
);
logd
(
"Download is cancelled"
,
downloadThread
.
request
);
return
true
;
}
return
false
;
...
...
@@ -796,7 +795,7 @@ public final class DownloadManager {
private
DownloadInternal
getDownload
(
String
id
)
{
for
(
int
i
=
0
;
i
<
downloadInternals
.
size
();
i
++)
{
DownloadInternal
downloadInternal
=
downloadInternals
.
get
(
i
);
if
(
downloadInternal
.
download
.
action
.
id
.
equals
(
id
))
{
if
(
downloadInternal
.
download
.
request
.
id
.
equals
(
id
))
{
return
downloadInternal
;
}
}
...
...
@@ -823,8 +822,8 @@ public final class DownloadManager {
return
downloadsStarted
&&
notMetRequirements
==
0
;
}
/* package */
static
Download
merge
Action
(
Download
download
,
Download
Action
action
,
int
manualStopReason
)
{
/* package */
static
Download
merge
Request
(
Download
download
,
Download
Request
request
,
int
manualStopReason
)
{
@Download
.
State
int
state
=
download
.
state
;
if
(
state
==
STATE_REMOVING
||
state
==
STATE_RESTARTING
)
{
state
=
STATE_RESTARTING
;
...
...
@@ -836,7 +835,7 @@ public final class DownloadManager {
long
nowMs
=
System
.
currentTimeMillis
();
long
startTimeMs
=
download
.
isTerminalState
()
?
nowMs
:
download
.
startTimeMs
;
return
new
Download
(
download
.
action
.
copyWithMergedAction
(
action
),
download
.
request
.
copyWithMergedRequest
(
request
),
state
,
FAILURE_REASON_NONE
,
manualStopReason
,
...
...
@@ -847,7 +846,7 @@ public final class DownloadManager {
private
static
Download
copyWithState
(
Download
download
,
@Download
.
State
int
state
)
{
return
new
Download
(
download
.
action
,
download
.
request
,
state
,
FAILURE_REASON_NONE
,
download
.
manualStopReason
,
...
...
@@ -863,12 +862,12 @@ public final class DownloadManager {
}
private
static
void
logd
(
String
message
,
DownloadInternal
downloadInternal
)
{
logd
(
message
,
downloadInternal
.
download
.
action
);
logd
(
message
,
downloadInternal
.
download
.
request
);
}
private
static
void
logd
(
String
message
,
Download
Action
action
)
{
private
static
void
logd
(
String
message
,
Download
Request
request
)
{
if
(
DEBUG
)
{
logd
(
message
+
": "
+
action
);
logd
(
message
+
": "
+
request
);
}
}
...
...
@@ -899,8 +898,8 @@ public final class DownloadManager {
initialize
(
download
.
state
);
}
public
void
add
Action
(
DownloadAction
newAction
,
int
manualStopReason
)
{
download
=
merge
Action
(
download
,
newAction
,
manualStopReason
);
public
void
add
Request
(
DownloadRequest
newRequest
,
int
manualStopReason
)
{
download
=
merge
Request
(
download
,
newRequest
,
manualStopReason
);
initialize
();
}
...
...
@@ -911,7 +910,7 @@ public final class DownloadManager {
public
Download
getUpdatedDownload
()
{
download
=
new
Download
(
download
.
action
,
download
.
request
,
state
,
state
!=
STATE_FAILED
?
FAILURE_REASON_NONE
:
failureReason
,
manualStopReason
,
...
...
@@ -927,7 +926,7 @@ public final class DownloadManager {
@Override
public
String
toString
()
{
return
download
.
action
.
id
+
' '
+
Download
.
getStateString
(
state
);
return
download
.
request
.
id
+
' '
+
Download
.
getStateString
(
state
);
}
public
void
start
()
{
...
...
@@ -959,7 +958,7 @@ public final class DownloadManager {
}
}
else
{
if
(
state
==
STATE_DOWNLOADING
||
state
==
STATE_QUEUED
)
{
downloadManager
.
stopDownloadThreadInternal
(
download
.
action
.
id
);
downloadManager
.
stopDownloadThreadInternal
(
download
.
request
.
id
);
setState
(
STATE_STOPPED
);
}
}
...
...
@@ -1018,7 +1017,7 @@ public final class DownloadManager {
initialize
(
STATE_QUEUED
);
}
else
{
// STATE_DOWNLOADING
if
(
error
!=
null
)
{
Log
.
e
(
TAG
,
"Download failed: "
+
download
.
action
.
id
,
error
);
Log
.
e
(
TAG
,
"Download failed: "
+
download
.
request
.
id
,
error
);
failureReason
=
FAILURE_REASON_UNKNOWN
;
setState
(
STATE_FAILED
);
}
else
{
...
...
@@ -1030,7 +1029,7 @@ public final class DownloadManager {
private
static
class
DownloadThread
extends
Thread
{
private
final
Download
Action
action
;
private
final
Download
Request
request
;
private
final
Downloader
downloader
;
private
final
boolean
isRemove
;
private
final
int
minRetryCount
;
...
...
@@ -1040,12 +1039,12 @@ public final class DownloadManager {
private
Throwable
finalError
;
private
DownloadThread
(
Download
Action
action
,
Download
Request
request
,
Downloader
downloader
,
boolean
isRemove
,
int
minRetryCount
,
Handler
onStoppedHandler
)
{
this
.
action
=
action
;
this
.
request
=
request
;
this
.
isRemove
=
isRemove
;
this
.
downloader
=
downloader
;
this
.
minRetryCount
=
minRetryCount
;
...
...
@@ -1069,7 +1068,7 @@ public final class DownloadManager {
@Override
public
void
run
()
{
logd
(
"Download started"
,
action
);
logd
(
"Download started"
,
request
);
try
{
if
(
isRemove
)
{
downloader
.
remove
();
...
...
@@ -1084,14 +1083,14 @@ public final class DownloadManager {
if
(!
isCanceled
)
{
long
downloadedBytes
=
downloader
.
getDownloadedBytes
();
if
(
downloadedBytes
!=
errorPosition
)
{
logd
(
"Reset error count. downloadedBytes = "
+
downloadedBytes
,
action
);
logd
(
"Reset error count. downloadedBytes = "
+
downloadedBytes
,
request
);
errorPosition
=
downloadedBytes
;
errorCount
=
0
;
}
if
(++
errorCount
>
minRetryCount
)
{
throw
e
;
}
logd
(
"Download error. Retry "
+
errorCount
,
action
);
logd
(
"Download error. Retry "
+
errorCount
,
request
);
Thread
.
sleep
(
getRetryDelayMillis
(
errorCount
));
}
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/Download
Action
.java
→
library/core/src/main/java/com/google/android/exoplayer2/offline/Download
Request
.java
View file @
5856e757
...
...
@@ -30,10 +30,10 @@ import java.util.Collections;
import
java.util.List
;
/** Defines content to be downloaded. */
public
final
class
Download
Action
implements
Parcelable
{
public
final
class
Download
Request
implements
Parcelable
{
/** Thrown when the encoded
action data belongs to an unsupported DownloadAction
type. */
public
static
class
Unsupported
Action
Exception
extends
IOException
{}
/** Thrown when the encoded
request data belongs to an unsupported request
type. */
public
static
class
Unsupported
Request
Exception
extends
IOException
{}
/** Type for progressive downloads. */
public
static
final
String
TYPE_PROGRESSIVE
=
"progressive"
;
...
...
@@ -46,7 +46,7 @@ public final class DownloadAction implements Parcelable {
/** The unique content id. */
public
final
String
id
;
/** The type of the
action
. */
/** The type of the
request
. */
public
final
String
type
;
/** The uri being downloaded. */
public
final
Uri
uri
;
...
...
@@ -65,7 +65,7 @@ public final class DownloadAction implements Parcelable {
* @param customCacheKey See {@link #customCacheKey}.
* @param data See {@link #data}.
*/
public
Download
Action
(
public
Download
Request
(
String
id
,
String
type
,
Uri
uri
,
...
...
@@ -82,7 +82,7 @@ public final class DownloadAction implements Parcelable {
this
.
data
=
data
!=
null
?
Arrays
.
copyOf
(
data
,
data
.
length
)
:
Util
.
EMPTY_BYTE_ARRAY
;
}
/* package */
Download
Action
(
Parcel
in
)
{
/* package */
Download
Request
(
Parcel
in
)
{
id
=
castNonNull
(
in
.
readString
());
type
=
castNonNull
(
in
.
readString
());
uri
=
Uri
.
parse
(
castNonNull
(
in
.
readString
()));
...
...
@@ -103,40 +103,40 @@ public final class DownloadAction implements Parcelable {
* @param id The ID of the copy.
* @return The copy with the specified ID.
*/
public
Download
Action
copyWithId
(
String
id
)
{
return
new
Download
Action
(
id
,
type
,
uri
,
streamKeys
,
customCacheKey
,
data
);
public
Download
Request
copyWithId
(
String
id
)
{
return
new
Download
Request
(
id
,
type
,
uri
,
streamKeys
,
customCacheKey
,
data
);
}
/**
* Returns the result of merging {@code new
Action} into this action. The action
s must have the
* Returns the result of merging {@code new
Request} into this request. The request
s must have the
* same {@link #id} and {@link #type}.
*
* <p>If the
action
s have different {@link #uri}, {@link #customCacheKey} and {@link #data}
* values, then those from the
action
being merged are included in the result.
* <p>If the
request
s have different {@link #uri}, {@link #customCacheKey} and {@link #data}
* values, then those from the
request
being merged are included in the result.
*
* @param new
Action The action
being merged.
* @param new
Request The request
being merged.
* @return The merged result.
* @throws IllegalArgumentException If the
action
s do not have the same {@link #id} and {@link
* @throws IllegalArgumentException If the
request
s do not have the same {@link #id} and {@link
* #type}.
*/
public
Download
Action
copyWithMergedAction
(
DownloadAction
newAction
)
{
Assertions
.
checkArgument
(
id
.
equals
(
new
Action
.
id
));
Assertions
.
checkArgument
(
type
.
equals
(
new
Action
.
type
));
public
Download
Request
copyWithMergedRequest
(
DownloadRequest
newRequest
)
{
Assertions
.
checkArgument
(
id
.
equals
(
new
Request
.
id
));
Assertions
.
checkArgument
(
type
.
equals
(
new
Request
.
type
));
List
<
StreamKey
>
mergedKeys
;
if
(
streamKeys
.
isEmpty
()
||
new
Action
.
streamKeys
.
isEmpty
())
{
if
(
streamKeys
.
isEmpty
()
||
new
Request
.
streamKeys
.
isEmpty
())
{
// If either streamKeys is empty then all streams should be downloaded.
mergedKeys
=
Collections
.
emptyList
();
}
else
{
mergedKeys
=
new
ArrayList
<>(
streamKeys
);
for
(
int
i
=
0
;
i
<
new
Action
.
streamKeys
.
size
();
i
++)
{
StreamKey
newKey
=
new
Action
.
streamKeys
.
get
(
i
);
for
(
int
i
=
0
;
i
<
new
Request
.
streamKeys
.
size
();
i
++)
{
StreamKey
newKey
=
new
Request
.
streamKeys
.
get
(
i
);
if
(!
mergedKeys
.
contains
(
newKey
))
{
mergedKeys
.
add
(
newKey
);
}
}
}
return
new
Download
Action
(
id
,
type
,
new
Action
.
uri
,
mergedKeys
,
newAction
.
customCacheKey
,
newAction
.
data
);
return
new
Download
Request
(
id
,
type
,
new
Request
.
uri
,
mergedKeys
,
newRequest
.
customCacheKey
,
newRequest
.
data
);
}
@Override
...
...
@@ -146,10 +146,10 @@ public final class DownloadAction implements Parcelable {
@Override
public
boolean
equals
(
@Nullable
Object
o
)
{
if
(!(
o
instanceof
Download
Action
))
{
if
(!(
o
instanceof
Download
Request
))
{
return
false
;
}
Download
Action
that
=
(
DownloadAction
)
o
;
Download
Request
that
=
(
DownloadRequest
)
o
;
return
id
.
equals
(
that
.
id
)
&&
type
.
equals
(
that
.
type
)
&&
uri
.
equals
(
that
.
uri
)
...
...
@@ -191,17 +191,17 @@ public final class DownloadAction implements Parcelable {
dest
.
writeByteArray
(
data
);
}
public
static
final
Parcelable
.
Creator
<
Download
Action
>
CREATOR
=
new
Parcelable
.
Creator
<
Download
Action
>()
{
public
static
final
Parcelable
.
Creator
<
Download
Request
>
CREATOR
=
new
Parcelable
.
Creator
<
Download
Request
>()
{
@Override
public
Download
Action
createFromParcel
(
Parcel
in
)
{
return
new
Download
Action
(
in
);
public
Download
Request
createFromParcel
(
Parcel
in
)
{
return
new
Download
Request
(
in
);
}
@Override
public
Download
Action
[]
newArray
(
int
size
)
{
return
new
Download
Action
[
size
];
public
Download
Request
[]
newArray
(
int
size
)
{
return
new
Download
Request
[
size
];
}
};
}
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java
View file @
5856e757
...
...
@@ -39,7 +39,7 @@ import java.util.List;
public
abstract
class
DownloadService
extends
Service
{
/**
* Starts a download service
without adding a new {@link DownloadAction}
. Extras:
* Starts a download service
to resume any ongoing downloads
. Extras:
*
* <ul>
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
...
...
@@ -56,7 +56,7 @@ public abstract class DownloadService extends Service {
* Adds a new download. Extras:
*
* <ul>
* <li>{@link #KEY_DOWNLOAD_
ACTION} - A {@link DownloadAction
} defining the download to be
* <li>{@link #KEY_DOWNLOAD_
REQUEST} - A {@link DownloadRequest
} defining the download to be
* added.
* <li>{@link #KEY_MANUAL_STOP_REASON} - An initial manual stop reason for the download. If
* omitted {@link Download#MANUAL_STOP_REASON_NONE} is used.
...
...
@@ -103,7 +103,7 @@ public abstract class DownloadService extends Service {
"com.google.android.exoplayer.downloadService.action.SET_MANUAL_STOP_REASON"
;
/**
* Removes a
n existing
download. Extras:
* Removes a download. Extras:
*
* <ul>
* <li>{@link #KEY_CONTENT_ID} - The content id of a download to remove.
...
...
@@ -113,11 +113,8 @@ public abstract class DownloadService extends Service {
public
static
final
String
ACTION_REMOVE
=
"com.google.android.exoplayer.downloadService.action.REMOVE"
;
/**
* Key for the {@code byte[]} representation of the {@link DownloadAction} in {@link #ACTION_ADD}
* intents.
*/
public
static
final
String
KEY_DOWNLOAD_ACTION
=
"download_action"
;
/** Key for the {@link DownloadRequest} in {@link #ACTION_ADD} intents. */
public
static
final
String
KEY_DOWNLOAD_REQUEST
=
"download_request"
;
/**
* Key for the content id in {@link #ACTION_START}, {@link #ACTION_STOP} and {@link
...
...
@@ -234,42 +231,42 @@ public abstract class DownloadService extends Service {
}
/**
* Builds an {@link Intent} for adding a
n action to be executed by the service
.
* Builds an {@link Intent} for adding a
new download
.
*
* @param context A {@link Context}.
* @param clazz The concrete download service being targeted by the intent.
* @param download
Action The action
to be executed.
* @param download
Request The request
to be executed.
* @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent.
*/
public
static
Intent
buildAdd
Action
Intent
(
public
static
Intent
buildAdd
Request
Intent
(
Context
context
,
Class
<?
extends
DownloadService
>
clazz
,
Download
Action
downloadAction
,
Download
Request
downloadRequest
,
boolean
foreground
)
{
return
buildAdd
Action
Intent
(
context
,
clazz
,
download
Action
,
MANUAL_STOP_REASON_NONE
,
foreground
);
return
buildAdd
Request
Intent
(
context
,
clazz
,
download
Request
,
MANUAL_STOP_REASON_NONE
,
foreground
);
}
/**
* Builds an {@link Intent} for adding a
n action to be executed by the service
.
* Builds an {@link Intent} for adding a
new download
.
*
* @param context A {@link Context}.
* @param clazz The concrete download service being targeted by the intent.
* @param download
Action The action
to be executed.
* @param download
Request The request
to be executed.
* @param manualStopReason An initial manual stop reason for the download, or {@link
* Download#MANUAL_STOP_REASON_NONE} if the download should be started.
* @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent.
*/
public
static
Intent
buildAdd
Action
Intent
(
public
static
Intent
buildAdd
Request
Intent
(
Context
context
,
Class
<?
extends
DownloadService
>
clazz
,
Download
Action
downloadAction
,
Download
Request
downloadRequest
,
int
manualStopReason
,
boolean
foreground
)
{
return
getIntent
(
context
,
clazz
,
ACTION_ADD
)
.
putExtra
(
KEY_DOWNLOAD_
ACTION
,
downloadAction
)
.
putExtra
(
KEY_DOWNLOAD_
REQUEST
,
downloadRequest
)
.
putExtra
(
KEY_MANUAL_STOP_REASON
,
manualStopReason
)
.
putExtra
(
KEY_FOREGROUND
,
foreground
);
}
...
...
@@ -311,19 +308,19 @@ public abstract class DownloadService extends Service {
}
/**
* Starts the service, adding a
n action to be execute
d.
* Starts the service, adding a
new downloa
d.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
* @param download
Action The action
to be executed.
* @param download
Request The request
to be executed.
* @param foreground Whether the service is started in the foreground.
*/
public
static
void
startWith
Action
(
public
static
void
startWith
NewDownload
(
Context
context
,
Class
<?
extends
DownloadService
>
clazz
,
Download
Action
downloadAction
,
Download
Request
downloadRequest
,
boolean
foreground
)
{
Intent
intent
=
buildAdd
ActionIntent
(
context
,
clazz
,
downloadAction
,
foreground
);
Intent
intent
=
buildAdd
RequestIntent
(
context
,
clazz
,
downloadRequest
,
foreground
);
if
(
foreground
)
{
Util
.
startForegroundService
(
context
,
intent
);
}
else
{
...
...
@@ -350,8 +347,7 @@ public abstract class DownloadService extends Service {
}
/**
* Starts the service without adding a new action. If there are any not finished actions and the
* requirements are met, the service resumes executing actions. Otherwise it stops immediately.
* Starts a download service to resume any ongoing downloads.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
...
...
@@ -362,9 +358,9 @@ public abstract class DownloadService extends Service {
}
/**
* Starts the service in the foreground without adding a new
action. If there are any not finished
*
actions and the requirements are met, the service resumes executing actions. Otherwise it stops
* immediately.
* Starts the service in the foreground without adding a new
download request. If there are any
*
not finished downloads and the requirements are met, the service resumes downloading. Otherwise
* i
t stops i
mmediately.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
...
...
@@ -417,13 +413,13 @@ public abstract class DownloadService extends Service {
// Do nothing.
break
;
case
ACTION_ADD:
Download
Action
downloadAction
=
intent
.
getParcelableExtra
(
KEY_DOWNLOAD_ACTION
);
if
(
download
Action
==
null
)
{
Log
.
e
(
TAG
,
"Ignored ADD: Missing
download_action
extra"
);
Download
Request
downloadRequest
=
intent
.
getParcelableExtra
(
KEY_DOWNLOAD_REQUEST
);
if
(
download
Request
==
null
)
{
Log
.
e
(
TAG
,
"Ignored ADD: Missing
"
+
KEY_DOWNLOAD_REQUEST
+
"
extra"
);
}
else
{
int
manualStopReason
=
intent
.
getIntExtra
(
KEY_MANUAL_STOP_REASON
,
Download
.
MANUAL_STOP_REASON_NONE
);
downloadManager
.
addDownload
(
download
Action
,
manualStopReason
);
downloadManager
.
addDownload
(
download
Request
,
manualStopReason
);
}
break
;
case
ACTION_START:
...
...
@@ -434,7 +430,8 @@ public abstract class DownloadService extends Service {
break
;
case
ACTION_SET_MANUAL_STOP_REASON:
if
(!
intent
.
hasExtra
(
KEY_MANUAL_STOP_REASON
))
{
Log
.
e
(
TAG
,
"Ignored SET_MANUAL_STOP_REASON: Missing manual_stop_reason extra"
);
Log
.
e
(
TAG
,
"Ignored SET_MANUAL_STOP_REASON: Missing "
+
KEY_MANUAL_STOP_REASON
+
" extra"
);
}
else
{
String
contentId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
int
manualStopReason
=
...
...
@@ -445,7 +442,7 @@ public abstract class DownloadService extends Service {
case
ACTION_REMOVE:
String
contentId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
if
(
contentId
==
null
)
{
Log
.
e
(
TAG
,
"Ignored REMOVE: Missing
content_id
extra"
);
Log
.
e
(
TAG
,
"Ignored REMOVE: Missing
"
+
KEY_CONTENT_ID
+
"
extra"
);
}
else
{
downloadManager
.
removeDownload
(
contentId
);
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderFactory.java
View file @
5856e757
...
...
@@ -15,14 +15,14 @@
*/
package
com
.
google
.
android
.
exoplayer2
.
offline
;
/** Creates {@link Downloader Downloaders} for given {@link Download
Action DownloadAction
s}. */
/** Creates {@link Downloader Downloaders} for given {@link Download
Request DownloadRequest
s}. */
public
interface
DownloaderFactory
{
/**
* Creates a {@link Downloader} to perform the given {@link Download
Action
}.
* Creates a {@link Downloader} to perform the given {@link Download
Request
}.
*
* @param action The action.
* @return The downloader.
*/
Downloader
createDownloader
(
Download
Action
action
);
Downloader
createDownloader
(
Download
Request
action
);
}
library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java
View file @
5856e757
...
...
@@ -33,20 +33,21 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
/** Unit tests for {@link ActionFile}. */
@SuppressWarnings
(
"deprecation"
)
@RunWith
(
AndroidJUnit4
.
class
)
public
class
ActionFileTest
{
private
File
tempFile
;
private
Download
Action
expectedAction1
;
private
Download
Action
expectedAction2
;
private
Download
Request
expectedAction1
;
private
Download
Request
expectedAction2
;
@Before
public
void
setUp
()
throws
Exception
{
tempFile
=
Util
.
createTempFile
(
ApplicationProvider
.
getApplicationContext
(),
"ExoPlayerTest"
);
expectedAction1
=
buildExpected
Action
(
Uri
.
parse
(
"http://test1.uri"
),
TestUtil
.
buildTestData
(
16
));
buildExpected
Request
(
Uri
.
parse
(
"http://test1.uri"
),
TestUtil
.
buildTestData
(
16
));
expectedAction2
=
buildExpected
Action
(
Uri
.
parse
(
"http://test2.uri"
),
TestUtil
.
buildTestData
(
32
));
buildExpected
Request
(
Uri
.
parse
(
"http://test2.uri"
),
TestUtil
.
buildTestData
(
32
));
}
@After
...
...
@@ -79,7 +80,7 @@ public class ActionFileTest {
@Test
public
void
testLoadZeroActions
()
throws
Exception
{
ActionFile
actionFile
=
getActionFile
(
"offline/action_file_zero_actions.exi"
);
Download
Action
[]
actions
=
actionFile
.
load
();
Download
Request
[]
actions
=
actionFile
.
load
();
assertThat
(
actions
).
isNotNull
();
assertThat
(
actions
).
hasLength
(
0
);
}
...
...
@@ -87,7 +88,7 @@ public class ActionFileTest {
@Test
public
void
testLoadOneAction
()
throws
Exception
{
ActionFile
actionFile
=
getActionFile
(
"offline/action_file_one_action.exi"
);
Download
Action
[]
actions
=
actionFile
.
load
();
Download
Request
[]
actions
=
actionFile
.
load
();
assertThat
(
actions
).
hasLength
(
1
);
assertThat
(
actions
[
0
]).
isEqualTo
(
expectedAction1
);
}
...
...
@@ -95,7 +96,7 @@ public class ActionFileTest {
@Test
public
void
testLoadTwoActions
()
throws
Exception
{
ActionFile
actionFile
=
getActionFile
(
"offline/action_file_two_actions.exi"
);
Download
Action
[]
actions
=
actionFile
.
load
();
Download
Request
[]
actions
=
actionFile
.
load
();
assertThat
(
actions
).
hasLength
(
2
);
assertThat
(
actions
[
0
]).
isEqualTo
(
expectedAction1
);
assertThat
(
actions
[
1
]).
isEqualTo
(
expectedAction2
);
...
...
@@ -123,10 +124,10 @@ public class ActionFileTest {
return
new
ActionFile
(
tempFile
);
}
private
static
Download
Action
buildExpectedAction
(
Uri
uri
,
byte
[]
data
)
{
return
new
Download
Action
(
private
static
Download
Request
buildExpectedRequest
(
Uri
uri
,
byte
[]
data
)
{
return
new
Download
Request
(
/* id= */
uri
.
toString
(),
Download
Action
.
TYPE_PROGRESSIVE
,
Download
Request
.
TYPE_PROGRESSIVE
,
uri
,
/* streamKeys= */
Collections
.
emptyList
(),
/* customCacheKey= */
null
,
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java
View file @
5856e757
...
...
@@ -15,7 +15,7 @@
*/
package
com
.
google
.
android
.
exoplayer2
.
offline
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
Download
Action
.
TYPE_DASH
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
Download
Request
.
TYPE_DASH
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
android.net.Uri
;
...
...
@@ -70,16 +70,16 @@ public class ActionFileUpgradeUtilTest {
new
StreamKey
(
/* periodIndex= */
3
,
/* groupIndex= */
4
,
/* trackIndex= */
5
);
StreamKey
expectedStreamKey2
=
new
StreamKey
(
/* periodIndex= */
0
,
/* groupIndex= */
1
,
/* trackIndex= */
2
);
Download
Action
expectedAction
1
=
new
Download
Action
(
Download
Request
expectedRequest
1
=
new
Download
Request
(
"key123"
,
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download1"
),
asList
(
expectedStreamKey1
),
/* customCacheKey= */
"key123"
,
new
byte
[]
{
1
,
2
,
3
,
4
});
Download
Action
expectedAction
2
=
new
Download
Action
(
Download
Request
expectedRequest
2
=
new
Download
Request
(
"key234"
,
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download2"
),
...
...
@@ -90,15 +90,15 @@ public class ActionFileUpgradeUtilTest {
ActionFileUpgradeUtil
.
upgradeAndDelete
(
tempFile
,
/* downloadIdProvider= */
null
,
downloadIndex
,
/* deleteOnFailure= */
true
);
assertDownloadIndexContains
Action
(
expectedAction
1
,
Download
.
STATE_QUEUED
);
assertDownloadIndexContains
Action
(
expectedAction
2
,
Download
.
STATE_QUEUED
);
assertDownloadIndexContains
Request
(
expectedRequest
1
,
Download
.
STATE_QUEUED
);
assertDownloadIndexContains
Request
(
expectedRequest
2
,
Download
.
STATE_QUEUED
);
}
@Test
public
void
merge
Action
_nonExistingDownload_createsNewDownload
()
throws
IOException
{
public
void
merge
Request
_nonExistingDownload_createsNewDownload
()
throws
IOException
{
byte
[]
data
=
new
byte
[]
{
1
,
2
,
3
,
4
};
Download
Action
action
=
new
Download
Action
(
Download
Request
request
=
new
Download
Request
(
"id"
,
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download"
),
...
...
@@ -108,50 +108,50 @@ public class ActionFileUpgradeUtilTest {
/* customCacheKey= */
"key123"
,
data
);
ActionFileUpgradeUtil
.
merge
Action
(
action
,
downloadIndex
);
ActionFileUpgradeUtil
.
merge
Request
(
request
,
downloadIndex
);
assertDownloadIndexContains
Action
(
action
,
Download
.
STATE_QUEUED
);
assertDownloadIndexContains
Request
(
request
,
Download
.
STATE_QUEUED
);
}
@Test
public
void
merge
Action
_existingDownload_createsMergedDownload
()
throws
IOException
{
public
void
merge
Request
_existingDownload_createsMergedDownload
()
throws
IOException
{
StreamKey
streamKey1
=
new
StreamKey
(
/* periodIndex= */
3
,
/* groupIndex= */
4
,
/* trackIndex= */
5
);
StreamKey
streamKey2
=
new
StreamKey
(
/* periodIndex= */
0
,
/* groupIndex= */
1
,
/* trackIndex= */
2
);
Download
Action
action
1
=
new
Download
Action
(
Download
Request
request
1
=
new
Download
Request
(
"id"
,
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download1"
),
asList
(
streamKey1
),
/* customCacheKey= */
"key123"
,
new
byte
[]
{
1
,
2
,
3
,
4
});
Download
Action
action
2
=
new
Download
Action
(
Download
Request
request
2
=
new
Download
Request
(
"id"
,
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download2"
),
asList
(
streamKey2
),
/* customCacheKey= */
"key123"
,
new
byte
[]
{
5
,
4
,
3
,
2
,
1
});
ActionFileUpgradeUtil
.
merge
Action
(
action
1
,
downloadIndex
);
ActionFileUpgradeUtil
.
merge
Action
(
action
2
,
downloadIndex
);
ActionFileUpgradeUtil
.
merge
Request
(
request
1
,
downloadIndex
);
ActionFileUpgradeUtil
.
merge
Request
(
request
2
,
downloadIndex
);
Download
download
=
downloadIndex
.
getDownload
(
action
2
.
id
);
Download
download
=
downloadIndex
.
getDownload
(
request
2
.
id
);
assertThat
(
download
).
isNotNull
();
assertThat
(
download
.
action
.
type
).
isEqualTo
(
action
2
.
type
);
assertThat
(
download
.
action
.
customCacheKey
).
isEqualTo
(
action
2
.
customCacheKey
);
assertThat
(
download
.
action
.
data
).
isEqualTo
(
action
2
.
data
);
assertThat
(
download
.
action
.
uri
).
isEqualTo
(
action
2
.
uri
);
assertThat
(
download
.
action
.
streamKeys
).
containsExactly
(
streamKey1
,
streamKey2
);
assertThat
(
download
.
request
.
type
).
isEqualTo
(
request
2
.
type
);
assertThat
(
download
.
request
.
customCacheKey
).
isEqualTo
(
request
2
.
customCacheKey
);
assertThat
(
download
.
request
.
data
).
isEqualTo
(
request
2
.
data
);
assertThat
(
download
.
request
.
uri
).
isEqualTo
(
request
2
.
uri
);
assertThat
(
download
.
request
.
streamKeys
).
containsExactly
(
streamKey1
,
streamKey2
);
assertThat
(
download
.
state
).
isEqualTo
(
Download
.
STATE_QUEUED
);
}
private
void
assertDownloadIndexContains
Action
(
DownloadAction
action
,
int
state
)
private
void
assertDownloadIndexContains
Request
(
DownloadRequest
request
,
int
state
)
throws
IOException
{
Download
download
=
downloadIndex
.
getDownload
(
action
.
id
);
assertThat
(
download
.
action
).
isEqualTo
(
action
);
Download
download
=
downloadIndex
.
getDownload
(
request
.
id
);
assertThat
(
download
.
request
).
isEqualTo
(
request
);
assertThat
(
download
.
state
).
isEqualTo
(
state
);
}
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java
View file @
5856e757
...
...
@@ -301,7 +301,7 @@ public class DefaultDownloadIndexTest {
}
private
static
void
assertEqual
(
Download
download
,
Download
that
)
{
assertThat
(
download
.
action
).
isEqualTo
(
that
.
action
);
assertThat
(
download
.
request
).
isEqualTo
(
that
.
request
);
assertThat
(
download
.
state
).
isEqualTo
(
that
.
state
);
assertThat
(
download
.
startTimeMs
).
isEqualTo
(
that
.
startTimeMs
);
assertThat
(
download
.
updateTimeMs
).
isEqualTo
(
that
.
updateTimeMs
);
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactoryTest.java
View file @
5856e757
...
...
@@ -38,9 +38,9 @@ public final class DefaultDownloaderFactoryTest {
Downloader
downloader
=
factory
.
createDownloader
(
new
Download
Action
(
new
Download
Request
(
"id"
,
Download
Action
.
TYPE_PROGRESSIVE
,
Download
Request
.
TYPE_PROGRESSIVE
,
Uri
.
parse
(
"https://www.test.com/download"
),
/* streamKeys= */
Collections
.
emptyList
(),
/* customCacheKey= */
null
,
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadBuilder.java
View file @
5856e757
...
...
@@ -47,8 +47,14 @@ class DownloadBuilder {
this
(
id
,
"type"
,
Uri
.
parse
(
"uri"
),
/* cacheKey= */
null
,
new
byte
[
0
],
Collections
.
emptyList
());
}
DownloadBuilder
(
DownloadAction
action
)
{
this
(
action
.
id
,
action
.
type
,
action
.
uri
,
action
.
customCacheKey
,
action
.
data
,
action
.
streamKeys
);
DownloadBuilder
(
DownloadRequest
request
)
{
this
(
request
.
id
,
request
.
type
,
request
.
uri
,
request
.
customCacheKey
,
request
.
data
,
request
.
streamKeys
);
}
DownloadBuilder
(
...
...
@@ -147,8 +153,9 @@ class DownloadBuilder {
}
public
Download
build
()
{
DownloadAction
action
=
new
DownloadAction
(
id
,
type
,
uri
,
streamKeys
,
cacheKey
,
customMetadata
);
DownloadRequest
request
=
new
DownloadRequest
(
id
,
type
,
uri
,
streamKeys
,
cacheKey
,
customMetadata
);
return
new
Download
(
action
,
state
,
failureReason
,
manualStopReason
,
startTimeMs
,
updateTimeMs
,
counters
);
request
,
state
,
failureReason
,
manualStopReason
,
startTimeMs
,
updateTimeMs
,
counters
);
}
}
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadHelperTest.java
View file @
5856e757
...
...
@@ -379,7 +379,7 @@ public class DownloadHelperTest {
}
@Test
public
void
getDownload
Action_createsDownloadAction
_withAllSelectedTracks
()
throws
Exception
{
public
void
getDownload
Request_createsDownloadRequest
_withAllSelectedTracks
()
throws
Exception
{
prepareDownloadHelper
(
downloadHelper
);
// Ensure we have track groups with multiple indices, renderers with multiple track groups and
// also renderers without any track groups.
...
...
@@ -392,13 +392,13 @@ public class DownloadHelperTest {
byte
[]
data
=
new
byte
[
10
];
Arrays
.
fill
(
data
,
(
byte
)
123
);
Download
Action
downloadAction
=
downloadHelper
.
getDownloadAction
(
data
);
Download
Request
downloadRequest
=
downloadHelper
.
getDownloadRequest
(
data
);
assertThat
(
download
Action
.
type
).
isEqualTo
(
TEST_DOWNLOAD_TYPE
);
assertThat
(
download
Action
.
uri
).
isEqualTo
(
testUri
);
assertThat
(
download
Action
.
customCacheKey
).
isEqualTo
(
TEST_CACHE_KEY
);
assertThat
(
download
Action
.
data
).
isEqualTo
(
data
);
assertThat
(
download
Action
.
streamKeys
)
assertThat
(
download
Request
.
type
).
isEqualTo
(
TEST_DOWNLOAD_TYPE
);
assertThat
(
download
Request
.
uri
).
isEqualTo
(
testUri
);
assertThat
(
download
Request
.
customCacheKey
).
isEqualTo
(
TEST_CACHE_KEY
);
assertThat
(
download
Request
.
data
).
isEqualTo
(
data
);
assertThat
(
download
Request
.
streamKeys
)
.
containsExactly
(
new
StreamKey
(
/* periodIndex= */
0
,
/* groupIndex= */
0
,
/* trackIndex= */
0
),
new
StreamKey
(
/* periodIndex= */
0
,
/* groupIndex= */
0
,
/* trackIndex= */
1
),
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
View file @
5856e757
...
...
@@ -104,29 +104,29 @@ public class DownloadManagerTest {
}
@Test
public
void
multiple
Action
sForTheSameContent_executedOnTheSameTask
()
{
// Two download
action
s on first task
new
DownloadRunner
(
uri1
).
postDownload
Action
().
postDownloadAction
();
// One download, one remove
action
s on second task
new
DownloadRunner
(
uri2
).
postDownload
Action
().
postRemoveAction
();
// Two remove
action
s on third task
new
DownloadRunner
(
uri3
).
postRemove
Action
().
postRemoveAction
();
public
void
multiple
Request
sForTheSameContent_executedOnTheSameTask
()
{
// Two download
request
s on first task
new
DownloadRunner
(
uri1
).
postDownload
Request
().
postDownloadRequest
();
// One download, one remove
request
s on second task
new
DownloadRunner
(
uri2
).
postDownload
Request
().
postRemoveRequest
();
// Two remove
request
s on third task
new
DownloadRunner
(
uri3
).
postRemove
Request
().
postRemoveRequest
();
}
@Test
public
void
action
sForDifferentContent_executedOnDifferentTasks
()
{
TaskWrapper
task1
=
new
DownloadRunner
(
uri1
).
postDownload
Action
().
getTask
();
TaskWrapper
task2
=
new
DownloadRunner
(
uri2
).
postDownload
Action
().
getTask
();
TaskWrapper
task3
=
new
DownloadRunner
(
uri3
).
postRemove
Action
().
getTask
();
public
void
request
sForDifferentContent_executedOnDifferentTasks
()
{
TaskWrapper
task1
=
new
DownloadRunner
(
uri1
).
postDownload
Request
().
getTask
();
TaskWrapper
task2
=
new
DownloadRunner
(
uri2
).
postDownload
Request
().
getTask
();
TaskWrapper
task3
=
new
DownloadRunner
(
uri3
).
postRemove
Request
().
getTask
();
assertThat
(
task1
).
isNoneOf
(
task2
,
task3
);
assertThat
(
task2
).
isNotEqualTo
(
task3
);
}
@Test
public
void
postDownload
Action
_downloads
()
throws
Throwable
{
public
void
postDownload
Request
_downloads
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
TaskWrapper
task
=
runner
.
postDownload
Action
().
getTask
();
TaskWrapper
task
=
runner
.
postDownload
Request
().
getTask
();
task
.
assertDownloading
();
runner
.
getDownloader
(
0
).
unblock
().
assertReleased
().
assertStartCount
(
1
);
task
.
assertCompleted
();
...
...
@@ -135,9 +135,9 @@ public class DownloadManagerTest {
}
@Test
public
void
postRemove
Action
_removes
()
throws
Throwable
{
public
void
postRemove
Request
_removes
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
TaskWrapper
task
=
runner
.
postDownload
Action
().
postRemoveAction
().
getTask
();
TaskWrapper
task
=
runner
.
postDownload
Request
().
postRemoveRequest
().
getTask
();
task
.
assertRemoving
();
runner
.
getDownloader
(
1
).
unblock
().
assertReleased
().
assertStartCount
(
1
);
task
.
assertRemoved
();
...
...
@@ -148,7 +148,7 @@ public class DownloadManagerTest {
@Test
public
void
downloadFails_retriesThenTaskFails
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
runner
.
postDownload
Action
();
runner
.
postDownload
Request
();
FakeDownloader
downloader
=
runner
.
getDownloader
(
0
);
for
(
int
i
=
0
;
i
<=
MIN_RETRY_COUNT
;
i
++)
{
...
...
@@ -163,7 +163,7 @@ public class DownloadManagerTest {
@Test
public
void
downloadFails_retries
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
runner
.
postDownload
Action
();
runner
.
postDownload
Request
();
FakeDownloader
downloader
=
runner
.
getDownloader
(
0
);
for
(
int
i
=
0
;
i
<
MIN_RETRY_COUNT
;
i
++)
{
...
...
@@ -179,7 +179,7 @@ public class DownloadManagerTest {
@Test
public
void
downloadProgressOnRetry_retryCountResets
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
runner
.
postDownload
Action
();
runner
.
postDownload
Request
();
FakeDownloader
downloader
=
runner
.
getDownloader
(
0
);
int
tooManyRetries
=
MIN_RETRY_COUNT
+
10
;
...
...
@@ -199,9 +199,9 @@ public class DownloadManagerTest {
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
FakeDownloader
downloader1
=
runner
.
getDownloader
(
0
);
runner
.
postDownload
Action
();
runner
.
postDownload
Request
();
downloader1
.
assertStarted
();
runner
.
postRemove
Action
();
runner
.
postRemove
Request
();
downloader1
.
assertCanceled
().
assertStartCount
(
1
);
runner
.
getDownloader
(
1
).
unblock
().
assertNotCanceled
();
...
...
@@ -213,9 +213,9 @@ public class DownloadManagerTest {
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
FakeDownloader
downloader1
=
runner
.
getDownloader
(
1
);
runner
.
postDownload
Action
().
postRemoveAction
();
runner
.
postDownload
Request
().
postRemoveRequest
();
downloader1
.
assertStarted
();
runner
.
postDownload
Action
();
runner
.
postDownload
Request
();
downloader1
.
unblock
().
assertNotCanceled
();
runner
.
getDownloader
(
2
).
unblock
().
assertNotCanceled
();
...
...
@@ -223,13 +223,13 @@ public class DownloadManagerTest {
}
@Test
public
void
secondSameRemove
Action
Ignored
()
throws
Throwable
{
public
void
secondSameRemove
Request
Ignored
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
FakeDownloader
downloader1
=
runner
.
getDownloader
(
1
);
runner
.
postDownload
Action
().
postRemoveAction
();
runner
.
postDownload
Request
().
postRemoveRequest
();
downloader1
.
assertStarted
();
runner
.
postRemove
Action
();
runner
.
postRemove
Request
();
downloader1
.
unblock
().
assertNotCanceled
();
runner
.
getTask
().
assertRemoved
();
...
...
@@ -238,22 +238,22 @@ public class DownloadManagerTest {
}
@Test
public
void
differentDownload
Action
sMerged
()
throws
Throwable
{
public
void
differentDownload
Request
sMerged
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
);
FakeDownloader
downloader1
=
runner
.
getDownloader
(
0
);
StreamKey
streamKey1
=
new
StreamKey
(
/* groupIndex= */
0
,
/* trackIndex= */
0
);
StreamKey
streamKey2
=
new
StreamKey
(
/* groupIndex= */
1
,
/* trackIndex= */
1
);
runner
.
postDownload
Action
(
streamKey1
);
runner
.
postDownload
Request
(
streamKey1
);
downloader1
.
assertStarted
();
runner
.
postDownload
Action
(
streamKey2
);
runner
.
postDownload
Request
(
streamKey2
);
downloader1
.
assertCanceled
();
FakeDownloader
downloader2
=
runner
.
getDownloader
(
1
);
downloader2
.
assertStarted
();
assertThat
(
downloader2
.
action
.
streamKeys
).
containsExactly
(
streamKey1
,
streamKey2
);
assertThat
(
downloader2
.
request
.
streamKeys
).
containsExactly
(
streamKey1
,
streamKey2
);
downloader2
.
unblock
();
runner
.
getTask
().
assertCompleted
();
...
...
@@ -262,9 +262,9 @@ public class DownloadManagerTest {
}
@Test
public
void
action
sForDifferentContent_executedInParallel
()
throws
Throwable
{
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Action
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Action
();
public
void
request
sForDifferentContent_executedInParallel
()
throws
Throwable
{
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Request
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Request
();
FakeDownloader
downloader1
=
runner1
.
getDownloader
(
0
);
FakeDownloader
downloader2
=
runner2
.
getDownloader
(
0
);
...
...
@@ -279,10 +279,10 @@ public class DownloadManagerTest {
}
@Test
public
void
action
sForDifferentContent_ifMaxDownloadIs1_executedSequentially
()
throws
Throwable
{
public
void
request
sForDifferentContent_ifMaxDownloadIs1_executedSequentially
()
throws
Throwable
{
setUpDownloadManager
(
1
);
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Action
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Action
();
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Request
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Request
();
FakeDownloader
downloader1
=
runner1
.
getDownloader
(
0
);
FakeDownloader
downloader2
=
runner2
.
getDownloader
(
0
);
...
...
@@ -299,11 +299,11 @@ public class DownloadManagerTest {
}
@Test
public
void
remove
Action
ForDifferentContent_ifMaxDownloadIs1_executedInParallel
()
public
void
remove
Request
ForDifferentContent_ifMaxDownloadIs1_executedInParallel
()
throws
Throwable
{
setUpDownloadManager
(
1
);
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Action
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Action
().
postRemoveAction
();
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Request
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Request
().
postRemoveRequest
();
FakeDownloader
downloader1
=
runner1
.
getDownloader
(
0
);
FakeDownloader
downloader2
=
runner2
.
getDownloader
(
0
);
...
...
@@ -318,11 +318,11 @@ public class DownloadManagerTest {
}
@Test
public
void
download
Action
FollowingRemove_ifMaxDownloadIs1_isNotStarted
()
throws
Throwable
{
public
void
download
Request
FollowingRemove_ifMaxDownloadIs1_isNotStarted
()
throws
Throwable
{
setUpDownloadManager
(
1
);
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Action
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Action
().
postRemoveAction
();
runner2
.
postDownload
Action
();
DownloadRunner
runner1
=
new
DownloadRunner
(
uri1
).
postDownload
Request
();
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
).
postDownload
Request
().
postRemoveRequest
();
runner2
.
postDownload
Request
();
FakeDownloader
downloader1
=
runner1
.
getDownloader
(
0
);
FakeDownloader
downloader2
=
runner2
.
getDownloader
(
0
);
FakeDownloader
downloader3
=
runner2
.
getDownloader
(
1
);
...
...
@@ -342,9 +342,10 @@ public class DownloadManagerTest {
@Test
public
void
getTasks_returnTasks
()
{
TaskWrapper
task1
=
new
DownloadRunner
(
uri1
).
postDownloadAction
().
getTask
();
TaskWrapper
task2
=
new
DownloadRunner
(
uri2
).
postDownloadAction
().
getTask
();
TaskWrapper
task3
=
new
DownloadRunner
(
uri3
).
postDownloadAction
().
postRemoveAction
().
getTask
();
TaskWrapper
task1
=
new
DownloadRunner
(
uri1
).
postDownloadRequest
().
getTask
();
TaskWrapper
task2
=
new
DownloadRunner
(
uri2
).
postDownloadRequest
().
getTask
();
TaskWrapper
task3
=
new
DownloadRunner
(
uri3
).
postDownloadRequest
().
postRemoveRequest
().
getTask
();
task3
.
assertRemoving
();
List
<
Download
>
downloads
=
downloadManager
.
getCurrentDownloads
();
...
...
@@ -352,7 +353,7 @@ public class DownloadManagerTest {
assertThat
(
downloads
).
hasSize
(
3
);
String
[]
taskIds
=
{
task1
.
taskId
,
task2
.
taskId
,
task3
.
taskId
};
String
[]
downloadIds
=
{
downloads
.
get
(
0
).
action
.
id
,
downloads
.
get
(
1
).
action
.
id
,
downloads
.
get
(
2
).
action
.
id
downloads
.
get
(
0
).
request
.
id
,
downloads
.
get
(
1
).
request
.
id
,
downloads
.
get
(
2
).
request
.
id
};
assertThat
(
downloadIds
).
isEqualTo
(
taskIds
);
}
...
...
@@ -363,27 +364,27 @@ public class DownloadManagerTest {
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
);
DownloadRunner
runner3
=
new
DownloadRunner
(
uri3
);
runner1
.
postDownload
Action
().
getTask
().
assertDownloading
();
runner2
.
postDownload
Action
().
postRemoveAction
().
getTask
().
assertRemoving
();
runner2
.
postDownload
Action
();
runner1
.
postDownload
Request
().
getTask
().
assertDownloading
();
runner2
.
postDownload
Request
().
postRemoveRequest
().
getTask
().
assertRemoving
();
runner2
.
postDownload
Request
();
runOnMainThread
(()
->
downloadManager
.
stopDownloads
());
runner1
.
getTask
().
assertStopped
();
// remove
action
s aren't stopped.
// remove
request
s aren't stopped.
runner2
.
getDownloader
(
1
).
unblock
().
assertReleased
();
runner2
.
getTask
().
assertStopped
();
// Although remove2 is finished, download2 doesn't start.
runner2
.
getDownloader
(
2
).
assertDoesNotStart
();
// When a new remove
action is added, it cancels stopped download action
s with the same media.
runner1
.
postRemove
Action
();
// When a new remove
request is added, it cancels stopped download request
s with the same media.
runner1
.
postRemove
Request
();
runner1
.
getDownloader
(
1
).
assertStarted
().
unblock
();
runner1
.
getTask
().
assertRemoved
();
// New download
action
s can be added but they don't start.
runner3
.
postDownload
Action
().
getDownloader
(
0
).
assertDoesNotStart
();
// New download
request
s can be added but they don't start.
runner3
.
postDownload
Request
().
getDownloader
(
0
).
assertDoesNotStart
();
runOnMainThread
(()
->
downloadManager
.
startDownloads
());
...
...
@@ -395,7 +396,7 @@ public class DownloadManagerTest {
@Test
public
void
manuallyStopAndResumeSingleDownload
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
).
postDownload
Action
();
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
).
postDownload
Request
();
TaskWrapper
task
=
runner
.
getTask
();
task
.
assertDownloading
();
...
...
@@ -414,7 +415,7 @@ public class DownloadManagerTest {
@Test
public
void
manuallyStoppedDownloadCanBeCancelled
()
throws
Throwable
{
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
).
postDownload
Action
();
DownloadRunner
runner
=
new
DownloadRunner
(
uri1
).
postDownload
Request
();
TaskWrapper
task
=
runner
.
getTask
();
task
.
assertDownloading
();
...
...
@@ -423,7 +424,7 @@ public class DownloadManagerTest {
task
.
assertStopped
();
runner
.
postRemove
Action
();
runner
.
postRemove
Request
();
runner
.
getDownloader
(
1
).
assertStarted
().
unblock
();
task
.
assertRemoved
();
...
...
@@ -436,8 +437,8 @@ public class DownloadManagerTest {
DownloadRunner
runner2
=
new
DownloadRunner
(
uri2
);
DownloadRunner
runner3
=
new
DownloadRunner
(
uri3
);
runner1
.
postDownload
Action
().
getTask
().
assertDownloading
();
runner2
.
postDownload
Action
().
postRemoveAction
().
getTask
().
assertRemoving
();
runner1
.
postDownload
Request
().
getTask
().
assertDownloading
();
runner2
.
postDownload
Request
().
postRemoveRequest
().
getTask
().
assertRemoving
();
runOnMainThread
(
()
->
downloadManager
.
setManualStopReason
(
runner1
.
getTask
().
taskId
,
APP_STOP_REASON
));
...
...
@@ -447,37 +448,37 @@ public class DownloadManagerTest {
// Other downloads aren't affected.
runner2
.
getDownloader
(
1
).
unblock
().
assertReleased
();
// New download
action
s can be added and they start.
runner3
.
postDownload
Action
().
getDownloader
(
0
).
assertStarted
().
unblock
();
// New download
request
s can be added and they start.
runner3
.
postDownload
Request
().
getDownloader
(
0
).
assertStarted
().
unblock
();
downloadManagerListener
.
blockUntilTasksCompleteAndThrowAnyDownloadError
();
}
@Test
public
void
merge
Action
_removingDownload_becomesRestarting
()
{
Download
Action
downloadAction
=
createDownloadAction
();
public
void
merge
Request
_removingDownload_becomesRestarting
()
{
Download
Request
downloadRequest
=
createDownloadRequest
();
DownloadBuilder
downloadBuilder
=
new
DownloadBuilder
(
download
Action
).
setState
(
Download
.
STATE_REMOVING
);
new
DownloadBuilder
(
download
Request
).
setState
(
Download
.
STATE_REMOVING
);
Download
download
=
downloadBuilder
.
build
();
Download
mergedDownload
=
DownloadManager
.
merge
Action
(
download
,
downloadAction
,
download
.
manualStopReason
);
DownloadManager
.
merge
Request
(
download
,
downloadRequest
,
download
.
manualStopReason
);
Download
expectedDownload
=
downloadBuilder
.
setState
(
Download
.
STATE_RESTARTING
).
build
();
assertEqualIgnoringTimeFields
(
mergedDownload
,
expectedDownload
);
}
@Test
public
void
merge
Action
_failedDownload_becomesQueued
()
{
Download
Action
downloadAction
=
createDownloadAction
();
public
void
merge
Request
_failedDownload_becomesQueued
()
{
Download
Request
downloadRequest
=
createDownloadRequest
();
DownloadBuilder
downloadBuilder
=
new
DownloadBuilder
(
download
Action
)
new
DownloadBuilder
(
download
Request
)
.
setState
(
Download
.
STATE_FAILED
)
.
setFailureReason
(
Download
.
FAILURE_REASON_UNKNOWN
);
Download
download
=
downloadBuilder
.
build
();
Download
mergedDownload
=
DownloadManager
.
merge
Action
(
download
,
downloadAction
,
download
.
manualStopReason
);
DownloadManager
.
merge
Request
(
download
,
downloadRequest
,
download
.
manualStopReason
);
Download
expectedDownload
=
downloadBuilder
...
...
@@ -488,31 +489,31 @@ public class DownloadManagerTest {
}
@Test
public
void
merge
Action
_stoppedDownload_staysStopped
()
{
Download
Action
downloadAction
=
createDownloadAction
();
public
void
merge
Request
_stoppedDownload_staysStopped
()
{
Download
Request
downloadRequest
=
createDownloadRequest
();
DownloadBuilder
downloadBuilder
=
new
DownloadBuilder
(
download
Action
)
new
DownloadBuilder
(
download
Request
)
.
setState
(
Download
.
STATE_STOPPED
)
.
setManualStopReason
(
/* manualStopReason= */
1
);
Download
download
=
downloadBuilder
.
build
();
Download
mergedDownload
=
DownloadManager
.
merge
Action
(
download
,
downloadAction
,
download
.
manualStopReason
);
DownloadManager
.
merge
Request
(
download
,
downloadRequest
,
download
.
manualStopReason
);
assertEqualIgnoringTimeFields
(
mergedDownload
,
download
);
}
@Test
public
void
merge
Action
_manualStopReasonSetButNotStopped_becomesStopped
()
{
Download
Action
downloadAction
=
createDownloadAction
();
public
void
merge
Request
_manualStopReasonSetButNotStopped_becomesStopped
()
{
Download
Request
downloadRequest
=
createDownloadRequest
();
DownloadBuilder
downloadBuilder
=
new
DownloadBuilder
(
download
Action
)
new
DownloadBuilder
(
download
Request
)
.
setState
(
Download
.
STATE_COMPLETED
)
.
setManualStopReason
(
/* manualStopReason= */
1
);
Download
download
=
downloadBuilder
.
build
();
Download
mergedDownload
=
DownloadManager
.
merge
Action
(
download
,
downloadAction
,
download
.
manualStopReason
);
DownloadManager
.
merge
Request
(
download
,
downloadRequest
,
download
.
manualStopReason
);
Download
expectedDownload
=
downloadBuilder
.
setState
(
Download
.
STATE_STOPPED
).
build
();
assertEqualIgnoringTimeFields
(
mergedDownload
,
expectedDownload
);
...
...
@@ -556,7 +557,7 @@ public class DownloadManagerTest {
}
private
static
void
assertEqualIgnoringTimeFields
(
Download
download
,
Download
that
)
{
assertThat
(
download
.
action
).
isEqualTo
(
that
.
action
);
assertThat
(
download
.
request
).
isEqualTo
(
that
.
request
);
assertThat
(
download
.
state
).
isEqualTo
(
that
.
state
);
assertThat
(
download
.
failureReason
).
isEqualTo
(
that
.
failureReason
);
assertThat
(
download
.
manualStopReason
).
isEqualTo
(
that
.
manualStopReason
);
...
...
@@ -565,10 +566,10 @@ public class DownloadManagerTest {
assertThat
(
download
.
getTotalBytes
()).
isEqualTo
(
that
.
getTotalBytes
());
}
private
static
Download
Action
createDownloadAction
()
{
return
new
Download
Action
(
private
static
Download
Request
createDownloadRequest
()
{
return
new
Download
Request
(
"id"
,
Download
Action
.
TYPE_DASH
,
Download
Request
.
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download"
),
Collections
.
emptyList
(),
/* customCacheKey= */
null
,
...
...
@@ -593,21 +594,21 @@ public class DownloadManagerTest {
taskWrapper
=
new
TaskWrapper
(
id
);
}
private
DownloadRunner
postRemove
Action
()
{
private
DownloadRunner
postRemove
Request
()
{
runOnMainThread
(()
->
downloadManager
.
removeDownload
(
id
));
return
this
;
}
private
DownloadRunner
postDownload
Action
(
StreamKey
...
keys
)
{
Download
Action
downloadAction
=
new
Download
Action
(
private
DownloadRunner
postDownload
Request
(
StreamKey
...
keys
)
{
Download
Request
downloadRequest
=
new
Download
Request
(
id
,
Download
Action
.
TYPE_PROGRESSIVE
,
Download
Request
.
TYPE_PROGRESSIVE
,
uri
,
Arrays
.
asList
(
keys
),
/* customCacheKey= */
null
,
/* data= */
null
);
runOnMainThread
(()
->
downloadManager
.
addDownload
(
download
Action
));
runOnMainThread
(()
->
downloadManager
.
addDownload
(
download
Request
));
return
this
;
}
...
...
@@ -624,9 +625,9 @@ public class DownloadManagerTest {
return
downloaders
.
get
(
index
);
}
private
synchronized
Downloader
createDownloader
(
Download
Action
action
)
{
private
synchronized
Downloader
createDownloader
(
Download
Request
request
)
{
downloader
=
getDownloader
(
createdDownloaderCount
++);
downloader
.
action
=
action
;
downloader
.
request
=
request
;
return
downloader
;
}
...
...
@@ -710,8 +711,8 @@ public class DownloadManagerTest {
}
@Override
public
Downloader
createDownloader
(
Download
Action
action
)
{
return
downloaders
.
get
(
action
.
uri
).
createDownloader
(
action
);
public
Downloader
createDownloader
(
Download
Request
request
)
{
return
downloaders
.
get
(
request
.
uri
).
createDownloader
(
request
);
}
}
...
...
@@ -719,7 +720,7 @@ public class DownloadManagerTest {
private
final
com
.
google
.
android
.
exoplayer2
.
util
.
ConditionVariable
blocker
;
private
Download
Action
action
;
private
Download
Request
request
;
private
CountDownLatch
started
;
private
volatile
boolean
interrupted
;
private
volatile
boolean
cancelled
;
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/Download
Action
Test.java
→
library/core/src/test/java/com/google/android/exoplayer2/offline/Download
Request
Test.java
View file @
5856e757
...
...
@@ -15,8 +15,8 @@
*/
package
com
.
google
.
android
.
exoplayer2
.
offline
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
Download
Action
.
TYPE_DASH
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
Download
Action
.
TYPE_HLS
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
Download
Request
.
TYPE_DASH
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
Download
Request
.
TYPE_HLS
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
...
...
@@ -30,9 +30,9 @@ import org.junit.Before;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
/** Unit tests for {@link Download
Action
}. */
/** Unit tests for {@link Download
Request
}. */
@RunWith
(
AndroidJUnit4
.
class
)
public
class
Download
Action
Test
{
public
class
Download
Request
Test
{
private
Uri
uri1
;
private
Uri
uri2
;
...
...
@@ -44,17 +44,17 @@ public class DownloadActionTest {
}
@Test
public
void
testMerge
Action
s_withDifferentIds_fails
()
{
Download
Action
action
1
=
new
Download
Action
(
public
void
testMerge
Request
s_withDifferentIds_fails
()
{
Download
Request
request
1
=
new
Download
Request
(
"id1"
,
TYPE_DASH
,
uri1
,
/* streamKeys= */
Collections
.
emptyList
(),
/* customCacheKey= */
null
,
/* data= */
null
);
Download
Action
action
2
=
new
Download
Action
(
Download
Request
request
2
=
new
Download
Request
(
"id2"
,
TYPE_DASH
,
uri2
,
...
...
@@ -62,7 +62,7 @@ public class DownloadActionTest {
/* customCacheKey= */
null
,
/* data= */
null
);
try
{
action1
.
copyWithMergedAction
(
action
2
);
request1
.
copyWithMergedRequest
(
request
2
);
fail
();
}
catch
(
IllegalArgumentException
e
)
{
// Expected.
...
...
@@ -70,17 +70,17 @@ public class DownloadActionTest {
}
@Test
public
void
testMerge
Action
s_withDifferentTypes_fails
()
{
Download
Action
action
1
=
new
Download
Action
(
public
void
testMerge
Request
s_withDifferentTypes_fails
()
{
Download
Request
request
1
=
new
Download
Request
(
"id1"
,
TYPE_DASH
,
uri1
,
/* streamKeys= */
Collections
.
emptyList
(),
/* customCacheKey= */
null
,
/* data= */
null
);
Download
Action
action
2
=
new
Download
Action
(
Download
Request
request
2
=
new
Download
Request
(
"id1"
,
TYPE_HLS
,
uri1
,
...
...
@@ -88,7 +88,7 @@ public class DownloadActionTest {
/* customCacheKey= */
null
,
/* data= */
null
);
try
{
action1
.
copyWithMergedAction
(
action
2
);
request1
.
copyWithMergedRequest
(
request
2
);
fail
();
}
catch
(
IllegalArgumentException
e
)
{
// Expected.
...
...
@@ -96,56 +96,56 @@ public class DownloadActionTest {
}
@Test
public
void
testMerge
Action_withSameAction
()
{
Download
Action
action1
=
createAction
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
public
void
testMerge
Request_withSameRequest
()
{
Download
Request
request1
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
Download
Action
mergedAction
=
action1
.
copyWithMergedAction
(
action
1
);
assertEqual
(
action1
,
mergedAction
);
Download
Request
mergedRequest
=
request1
.
copyWithMergedRequest
(
request
1
);
assertEqual
(
request1
,
mergedRequest
);
}
@Test
public
void
testMerge
Action
s_withEmptyStreamKeys
()
{
Download
Action
action1
=
createAction
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
Download
Action
action2
=
createAction
(
uri1
);
public
void
testMerge
Request
s_withEmptyStreamKeys
()
{
Download
Request
request1
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
Download
Request
request2
=
createRequest
(
uri1
);
// If either of the
action
s have empty streamKeys, the merge should have empty streamKeys.
Download
Action
mergedAction
=
action1
.
copyWithMergedAction
(
action
2
);
assertThat
(
merged
Action
.
streamKeys
).
isEmpty
();
// If either of the
request
s have empty streamKeys, the merge should have empty streamKeys.
Download
Request
mergedRequest
=
request1
.
copyWithMergedRequest
(
request
2
);
assertThat
(
merged
Request
.
streamKeys
).
isEmpty
();
merged
Action
=
action2
.
copyWithMergedAction
(
action
1
);
assertThat
(
merged
Action
.
streamKeys
).
isEmpty
();
merged
Request
=
request2
.
copyWithMergedRequest
(
request
1
);
assertThat
(
merged
Request
.
streamKeys
).
isEmpty
();
}
@Test
public
void
testMerge
Action
s_withOverlappingStreamKeys
()
{
public
void
testMerge
Request
s_withOverlappingStreamKeys
()
{
StreamKey
streamKey1
=
new
StreamKey
(
0
,
1
,
2
);
StreamKey
streamKey2
=
new
StreamKey
(
3
,
4
,
5
);
StreamKey
streamKey3
=
new
StreamKey
(
6
,
7
,
8
);
Download
Action
action1
=
createAction
(
uri1
,
streamKey1
,
streamKey2
);
Download
Action
action2
=
createAction
(
uri1
,
streamKey2
,
streamKey3
);
Download
Request
request1
=
createRequest
(
uri1
,
streamKey1
,
streamKey2
);
Download
Request
request2
=
createRequest
(
uri1
,
streamKey2
,
streamKey3
);
// Merged streamKeys should be in their original order without duplicates.
Download
Action
mergedAction
=
action1
.
copyWithMergedAction
(
action
2
);
assertThat
(
merged
Action
.
streamKeys
).
containsExactly
(
streamKey1
,
streamKey2
,
streamKey3
);
Download
Request
mergedRequest
=
request1
.
copyWithMergedRequest
(
request
2
);
assertThat
(
merged
Request
.
streamKeys
).
containsExactly
(
streamKey1
,
streamKey2
,
streamKey3
);
merged
Action
=
action2
.
copyWithMergedAction
(
action
1
);
assertThat
(
merged
Action
.
streamKeys
).
containsExactly
(
streamKey2
,
streamKey3
,
streamKey1
);
merged
Request
=
request2
.
copyWithMergedRequest
(
request
1
);
assertThat
(
merged
Request
.
streamKeys
).
containsExactly
(
streamKey2
,
streamKey3
,
streamKey1
);
}
@Test
public
void
testMerge
Action
s_withDifferentFields
()
{
public
void
testMerge
Request
s_withDifferentFields
()
{
byte
[]
data1
=
new
byte
[]
{
0
,
1
,
2
};
byte
[]
data2
=
new
byte
[]
{
3
,
4
,
5
};
Download
Action
action
1
=
new
Download
Action
(
Download
Request
request
1
=
new
Download
Request
(
"id1"
,
TYPE_DASH
,
uri1
,
/* streamKeys= */
Collections
.
emptyList
(),
"key1"
,
/* data= */
data1
);
Download
Action
action
2
=
new
Download
Action
(
Download
Request
request
2
=
new
Download
Request
(
"id1"
,
TYPE_DASH
,
uri2
,
...
...
@@ -153,16 +153,16 @@ public class DownloadActionTest {
"key2"
,
/* data= */
data2
);
// uri, customCacheKey and data should be from the
action
being merged.
Download
Action
mergedAction
=
action1
.
copyWithMergedAction
(
action
2
);
assertThat
(
merged
Action
.
uri
).
isEqualTo
(
uri2
);
assertThat
(
merged
Action
.
customCacheKey
).
isEqualTo
(
"key2"
);
assertThat
(
merged
Action
.
data
).
isEqualTo
(
data2
);
// uri, customCacheKey and data should be from the
request
being merged.
Download
Request
mergedRequest
=
request1
.
copyWithMergedRequest
(
request
2
);
assertThat
(
merged
Request
.
uri
).
isEqualTo
(
uri2
);
assertThat
(
merged
Request
.
customCacheKey
).
isEqualTo
(
"key2"
);
assertThat
(
merged
Request
.
data
).
isEqualTo
(
data2
);
merged
Action
=
action2
.
copyWithMergedAction
(
action
1
);
assertThat
(
merged
Action
.
uri
).
isEqualTo
(
uri1
);
assertThat
(
merged
Action
.
customCacheKey
).
isEqualTo
(
"key1"
);
assertThat
(
merged
Action
.
data
).
isEqualTo
(
data1
);
merged
Request
=
request2
.
copyWithMergedRequest
(
request
1
);
assertThat
(
merged
Request
.
uri
).
isEqualTo
(
uri1
);
assertThat
(
merged
Request
.
customCacheKey
).
isEqualTo
(
"key1"
);
assertThat
(
merged
Request
.
data
).
isEqualTo
(
data1
);
}
@Test
...
...
@@ -170,8 +170,8 @@ public class DownloadActionTest {
ArrayList
<
StreamKey
>
streamKeys
=
new
ArrayList
<>();
streamKeys
.
add
(
new
StreamKey
(
1
,
2
,
3
));
streamKeys
.
add
(
new
StreamKey
(
4
,
5
,
6
));
Download
Action
action
ToParcel
=
new
Download
Action
(
Download
Request
request
ToParcel
=
new
Download
Request
(
"id"
,
"type"
,
Uri
.
parse
(
"https://abc.def/ghi"
),
...
...
@@ -179,11 +179,11 @@ public class DownloadActionTest {
"key"
,
new
byte
[]
{
1
,
2
,
3
,
4
,
5
});
Parcel
parcel
=
Parcel
.
obtain
();
action
ToParcel
.
writeToParcel
(
parcel
,
0
);
request
ToParcel
.
writeToParcel
(
parcel
,
0
);
parcel
.
setDataPosition
(
0
);
Download
Action
actionFromParcel
=
DownloadAction
.
CREATOR
.
createFromParcel
(
parcel
);
assertThat
(
actionFromParcel
).
isEqualTo
(
action
ToParcel
);
Download
Request
requestFromParcel
=
DownloadRequest
.
CREATOR
.
createFromParcel
(
parcel
);
assertThat
(
requestFromParcel
).
isEqualTo
(
request
ToParcel
);
parcel
.
recycle
();
}
...
...
@@ -191,50 +191,50 @@ public class DownloadActionTest {
@SuppressWarnings
(
"EqualsWithItself"
)
@Test
public
void
testEquals
()
{
Download
Action
action1
=
createAction
(
uri1
);
assertThat
(
action1
.
equals
(
action
1
)).
isTrue
();
Download
Request
request1
=
createRequest
(
uri1
);
assertThat
(
request1
.
equals
(
request
1
)).
isTrue
();
Download
Action
action2
=
createAction
(
uri1
);
Download
Action
action3
=
createAction
(
uri1
);
assertEqual
(
action2
,
action
3
);
Download
Request
request2
=
createRequest
(
uri1
);
Download
Request
request3
=
createRequest
(
uri1
);
assertEqual
(
request2
,
request
3
);
Download
Action
action4
=
createAction
(
uri1
);
Download
Action
action5
=
createAction
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
assertNotEqual
(
action4
,
action
5
);
Download
Request
request4
=
createRequest
(
uri1
);
Download
Request
request5
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
assertNotEqual
(
request4
,
request
5
);
Download
Action
action6
=
createAction
(
uri1
,
new
StreamKey
(
0
,
1
,
1
));
Download
Action
action7
=
createAction
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
assertNotEqual
(
action6
,
action
7
);
Download
Request
request6
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
1
,
1
));
Download
Request
request7
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
assertNotEqual
(
request6
,
request
7
);
Download
Action
action8
=
createAction
(
uri1
);
Download
Action
action9
=
createAction
(
uri2
);
assertNotEqual
(
action8
,
action
9
);
Download
Request
request8
=
createRequest
(
uri1
);
Download
Request
request9
=
createRequest
(
uri2
);
assertNotEqual
(
request8
,
request
9
);
Download
Action
action10
=
createAction
(
uri1
,
new
StreamKey
(
0
,
0
,
0
),
new
StreamKey
(
0
,
1
,
1
));
Download
Action
action11
=
createAction
(
uri1
,
new
StreamKey
(
0
,
1
,
1
),
new
StreamKey
(
0
,
0
,
0
));
assertEqual
(
action10
,
action
11
);
Download
Request
request10
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
0
,
0
),
new
StreamKey
(
0
,
1
,
1
));
Download
Request
request11
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
1
,
1
),
new
StreamKey
(
0
,
0
,
0
));
assertEqual
(
request10
,
request
11
);
Download
Action
action12
=
createAction
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
Download
Action
action13
=
createAction
(
uri1
,
new
StreamKey
(
0
,
1
,
1
),
new
StreamKey
(
0
,
0
,
0
));
assertNotEqual
(
action12
,
action
13
);
Download
Request
request12
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
0
,
0
));
Download
Request
request13
=
createRequest
(
uri1
,
new
StreamKey
(
0
,
1
,
1
),
new
StreamKey
(
0
,
0
,
0
));
assertNotEqual
(
request12
,
request
13
);
Download
Action
action14
=
createAction
(
uri1
);
Download
Action
action15
=
createAction
(
uri1
);
assertEqual
(
action14
,
action
15
);
Download
Request
request14
=
createRequest
(
uri1
);
Download
Request
request15
=
createRequest
(
uri1
);
assertEqual
(
request14
,
request
15
);
}
private
static
void
assertNotEqual
(
Download
Action
action1
,
DownloadAction
action
2
)
{
assertThat
(
action1
).
isNotEqualTo
(
action
2
);
assertThat
(
action2
).
isNotEqualTo
(
action
1
);
private
static
void
assertNotEqual
(
Download
Request
request1
,
DownloadRequest
request
2
)
{
assertThat
(
request1
).
isNotEqualTo
(
request
2
);
assertThat
(
request2
).
isNotEqualTo
(
request
1
);
}
private
static
void
assertEqual
(
Download
Action
action1
,
DownloadAction
action
2
)
{
assertThat
(
action1
).
isEqualTo
(
action
2
);
assertThat
(
action2
).
isEqualTo
(
action
1
);
private
static
void
assertEqual
(
Download
Request
request1
,
DownloadRequest
request
2
)
{
assertThat
(
request1
).
isEqualTo
(
request
2
);
assertThat
(
request2
).
isEqualTo
(
request
1
);
}
private
static
Download
Action
createAction
(
Uri
uri
,
StreamKey
...
keys
)
{
return
new
Download
Action
(
private
static
Download
Request
createRequest
(
Uri
uri
,
StreamKey
...
keys
)
{
return
new
Download
Request
(
uri
.
toString
(),
TYPE_DASH
,
uri
,
toList
(
keys
),
/* customCacheKey= */
null
,
/* data= */
null
);
}
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java
View file @
5856e757
...
...
@@ -29,8 +29,8 @@ import android.net.Uri;
import
androidx.test.core.app.ApplicationProvider
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.offline.DefaultDownloaderFactory
;
import
com.google.android.exoplayer2.offline.DownloadAction
;
import
com.google.android.exoplayer2.offline.DownloadException
;
import
com.google.android.exoplayer2.offline.DownloadRequest
;
import
com.google.android.exoplayer2.offline.Downloader
;
import
com.google.android.exoplayer2.offline.DownloaderConstructorHelper
;
import
com.google.android.exoplayer2.offline.DownloaderFactory
;
...
...
@@ -84,9 +84,9 @@ public class DashDownloaderTest {
Downloader
downloader
=
factory
.
createDownloader
(
new
Download
Action
(
new
Download
Request
(
"id"
,
Download
Action
.
TYPE_DASH
,
Download
Request
.
TYPE_DASH
,
Uri
.
parse
(
"https://www.test.com/download"
),
Collections
.
singletonList
(
new
StreamKey
(
/* groupIndex= */
0
,
/* trackIndex= */
0
)),
/* customCacheKey= */
null
,
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java
View file @
5856e757
...
...
@@ -28,8 +28,8 @@ import androidx.test.core.app.ApplicationProvider;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.offline.DefaultDownloadIndex
;
import
com.google.android.exoplayer2.offline.DefaultDownloaderFactory
;
import
com.google.android.exoplayer2.offline.DownloadAction
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.offline.DownloadRequest
;
import
com.google.android.exoplayer2.offline.DownloaderConstructorHelper
;
import
com.google.android.exoplayer2.offline.StreamKey
;
import
com.google.android.exoplayer2.scheduler.Requirements
;
...
...
@@ -132,7 +132,7 @@ public class DownloadManagerDashTest {
dummyMainThread
.
runOnMainThread
(
()
->
{
// Setup an Action and immediately release the DM.
handleDownload
Action
(
fakeStreamKey1
,
fakeStreamKey2
);
handleDownload
Request
(
fakeStreamKey1
,
fakeStreamKey2
);
downloadManager
.
release
();
});
...
...
@@ -149,29 +149,29 @@ public class DownloadManagerDashTest {
}
@Test
public
void
testHandleDownload
Action
()
throws
Throwable
{
handleDownload
Action
(
fakeStreamKey1
,
fakeStreamKey2
);
public
void
testHandleDownload
Request
()
throws
Throwable
{
handleDownload
Request
(
fakeStreamKey1
,
fakeStreamKey2
);
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCachedData
(
cache
,
fakeDataSet
);
}
@Test
public
void
testHandleMultipleDownload
Action
()
throws
Throwable
{
handleDownload
Action
(
fakeStreamKey1
);
handleDownload
Action
(
fakeStreamKey2
);
public
void
testHandleMultipleDownload
Request
()
throws
Throwable
{
handleDownload
Request
(
fakeStreamKey1
);
handleDownload
Request
(
fakeStreamKey2
);
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCachedData
(
cache
,
fakeDataSet
);
}
@Test
public
void
testHandleInterferingDownload
Action
()
throws
Throwable
{
public
void
testHandleInterferingDownload
Request
()
throws
Throwable
{
fakeDataSet
.
newData
(
"audio_segment_2"
)
.
appendReadAction
(()
->
handleDownload
Action
(
fakeStreamKey2
))
.
appendReadAction
(()
->
handleDownload
Request
(
fakeStreamKey2
))
.
appendReadData
(
TestUtil
.
buildTestData
(
5
))
.
endData
();
handleDownload
Action
(
fakeStreamKey1
);
handleDownload
Request
(
fakeStreamKey1
);
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCachedData
(
cache
,
fakeDataSet
);
...
...
@@ -179,7 +179,7 @@ public class DownloadManagerDashTest {
@Test
public
void
testHandleRemoveAction
()
throws
Throwable
{
handleDownload
Action
(
fakeStreamKey1
);
handleDownload
Request
(
fakeStreamKey1
);
blockUntilTasksCompleteAndThrowAnyDownloadError
();
...
...
@@ -194,7 +194,7 @@ public class DownloadManagerDashTest {
@Ignore
@Test
public
void
testHandleRemoveActionBeforeDownloadFinish
()
throws
Throwable
{
handleDownload
Action
(
fakeStreamKey1
);
handleDownload
Request
(
fakeStreamKey1
);
handleRemoveAction
();
blockUntilTasksCompleteAndThrowAnyDownloadError
();
...
...
@@ -213,7 +213,7 @@ public class DownloadManagerDashTest {
.
appendReadData
(
TestUtil
.
buildTestData
(
5
))
.
endData
();
handleDownload
Action
(
fakeStreamKey1
);
handleDownload
Request
(
fakeStreamKey1
);
assertThat
(
downloadInProgressCondition
.
block
(
ASSERT_TRUE_TIMEOUT
)).
isTrue
();
...
...
@@ -228,13 +228,13 @@ public class DownloadManagerDashTest {
downloadManagerListener
.
blockUntilTasksCompleteAndThrowAnyDownloadError
();
}
private
void
handleDownload
Action
(
StreamKey
...
keys
)
{
private
void
handleDownload
Request
(
StreamKey
...
keys
)
{
ArrayList
<
StreamKey
>
keysList
=
new
ArrayList
<>();
Collections
.
addAll
(
keysList
,
keys
);
Download
Action
action
=
new
Download
Action
(
Download
Request
action
=
new
Download
Request
(
TEST_ID
,
Download
Action
.
TYPE_DASH
,
Download
Request
.
TYPE_DASH
,
TEST_MPD_URI
,
keysList
,
/* customCacheKey= */
null
,
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java
View file @
5856e757
...
...
@@ -30,8 +30,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import
com.google.android.exoplayer2.offline.DefaultDownloadIndex
;
import
com.google.android.exoplayer2.offline.DefaultDownloaderFactory
;
import
com.google.android.exoplayer2.offline.Download
;
import
com.google.android.exoplayer2.offline.DownloadAction
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.offline.DownloadRequest
;
import
com.google.android.exoplayer2.offline.DownloadService
;
import
com.google.android.exoplayer2.offline.DownloaderConstructorHelper
;
import
com.google.android.exoplayer2.offline.StreamKey
;
...
...
@@ -159,7 +159,7 @@ public class DownloadServiceDashTest {
@Ignore
// b/78877092
@Test
public
void
testMultipleDownload
Action
()
throws
Throwable
{
public
void
testMultipleDownload
Request
()
throws
Throwable
{
downloadKeys
(
fakeStreamKey1
);
downloadKeys
(
fakeStreamKey2
);
...
...
@@ -208,10 +208,10 @@ public class DownloadServiceDashTest {
private
void
downloadKeys
(
StreamKey
...
keys
)
{
ArrayList
<
StreamKey
>
keysList
=
new
ArrayList
<>();
Collections
.
addAll
(
keysList
,
keys
);
Download
Action
action
=
new
Download
Action
(
Download
Request
action
=
new
Download
Request
(
TEST_ID
,
Download
Action
.
TYPE_DASH
,
Download
Request
.
TYPE_DASH
,
TEST_MPD_URI
,
keysList
,
/* customCacheKey= */
null
,
...
...
@@ -219,7 +219,7 @@ public class DownloadServiceDashTest {
dummyMainThread
.
runOnMainThread
(
()
->
{
Intent
startIntent
=
DownloadService
.
buildAdd
Action
Intent
(
DownloadService
.
buildAdd
Request
Intent
(
context
,
DownloadService
.
class
,
action
,
/* foreground= */
false
);
dashDownloadService
.
onStartCommand
(
startIntent
,
0
,
0
);
});
...
...
library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java
View file @
5856e757
...
...
@@ -38,7 +38,7 @@ import android.net.Uri;
import
androidx.test.core.app.ApplicationProvider
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.offline.DefaultDownloaderFactory
;
import
com.google.android.exoplayer2.offline.Download
Action
;
import
com.google.android.exoplayer2.offline.Download
Request
;
import
com.google.android.exoplayer2.offline.Downloader
;
import
com.google.android.exoplayer2.offline.DownloaderConstructorHelper
;
import
com.google.android.exoplayer2.offline.DownloaderFactory
;
...
...
@@ -101,9 +101,9 @@ public class HlsDownloaderTest {
Downloader
downloader
=
factory
.
createDownloader
(
new
Download
Action
(
new
Download
Request
(
"id"
,
Download
Action
.
TYPE_HLS
,
Download
Request
.
TYPE_HLS
,
Uri
.
parse
(
"https://www.test.com/download"
),
Collections
.
singletonList
(
new
StreamKey
(
/* groupIndex= */
0
,
/* trackIndex= */
0
)),
/* customCacheKey= */
null
,
...
...
library/smoothstreaming/src/test/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloaderTest.java
View file @
5856e757
...
...
@@ -20,7 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import
android.net.Uri
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.offline.DefaultDownloaderFactory
;
import
com.google.android.exoplayer2.offline.Download
Action
;
import
com.google.android.exoplayer2.offline.Download
Request
;
import
com.google.android.exoplayer2.offline.Downloader
;
import
com.google.android.exoplayer2.offline.DownloaderConstructorHelper
;
import
com.google.android.exoplayer2.offline.DownloaderFactory
;
...
...
@@ -44,9 +44,9 @@ public final class SsDownloaderTest {
Downloader
downloader
=
factory
.
createDownloader
(
new
Download
Action
(
new
Download
Request
(
"id"
,
Download
Action
.
TYPE_SS
,
Download
Request
.
TYPE_SS
,
Uri
.
parse
(
"https://www.test.com/download"
),
Collections
.
singletonList
(
new
StreamKey
(
/* groupIndex= */
0
,
/* trackIndex= */
0
)),
/* customCacheKey= */
null
,
...
...
testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java
View file @
5856e757
...
...
@@ -38,7 +38,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
private
final
DownloadManager
downloadManager
;
private
final
DummyMainThread
dummyMainThread
;
private
final
HashMap
<
String
,
ArrayBlockingQueue
<
Integer
>>
action
States
;
private
final
HashMap
<
String
,
ArrayBlockingQueue
<
Integer
>>
download
States
;
private
final
ConditionVariable
initializedCondition
;
private
CountDownLatch
downloadFinishedCondition
;
...
...
@@ -48,7 +48,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
DownloadManager
downloadManager
,
DummyMainThread
dummyMainThread
)
{
this
.
downloadManager
=
downloadManager
;
this
.
dummyMainThread
=
dummyMainThread
;
action
States
=
new
HashMap
<>();
download
States
=
new
HashMap
<>();
initializedCondition
=
new
ConditionVariable
();
downloadManager
.
addListener
(
this
);
}
...
...
@@ -73,12 +73,12 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
if
(
download
.
state
==
Download
.
STATE_FAILED
)
{
failureReason
=
download
.
failureReason
;
}
getStateQueue
(
download
.
action
.
id
).
add
(
download
.
state
);
getStateQueue
(
download
.
request
.
id
).
add
(
download
.
state
);
}
@Override
public
void
onDownloadRemoved
(
DownloadManager
downloadManager
,
Download
download
)
{
getStateQueue
(
download
.
action
.
id
).
add
(
STATE_REMOVED
);
getStateQueue
(
download
.
request
.
id
).
add
(
STATE_REMOVED
);
}
@Override
...
...
@@ -114,11 +114,11 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
}
private
ArrayBlockingQueue
<
Integer
>
getStateQueue
(
String
taskId
)
{
synchronized
(
action
States
)
{
if
(!
action
States
.
containsKey
(
taskId
))
{
action
States
.
put
(
taskId
,
new
ArrayBlockingQueue
<>(
10
));
synchronized
(
download
States
)
{
if
(!
download
States
.
containsKey
(
taskId
))
{
download
States
.
put
(
taskId
,
new
ArrayBlockingQueue
<>(
10
));
}
return
action
States
.
get
(
taskId
);
return
download
States
.
get
(
taskId
);
}
}
...
...
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