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
9d99f921
authored
Mar 26, 2019
by
olly
Committed by
Toni
Mar 29, 2019
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Improve DownloadService action documentation
PiperOrigin-RevId: 240440477
parent
e3a8429e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
97 additions
and
46 deletions
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java
View file @
9d99f921
...
@@ -36,51 +36,95 @@ import java.util.HashMap;
...
@@ -36,51 +36,95 @@ import java.util.HashMap;
/** A {@link Service} for downloading media. */
/** A {@link Service} for downloading media. */
public
abstract
class
DownloadService
extends
Service
{
public
abstract
class
DownloadService
extends
Service
{
/** Starts a download service without adding a new {@link DownloadAction}. */
/**
* Starts a download service without adding a new {@link DownloadAction}. Extras:
*
* <ul>
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public
static
final
String
ACTION_INIT
=
public
static
final
String
ACTION_INIT
=
"com.google.android.exoplayer.downloadService.action.INIT"
;
"com.google.android.exoplayer.downloadService.action.INIT"
;
/** Starts a download service, adding a new {@link DownloadAction} to be executed. */
/** Like {@link #ACTION_INIT}, but with {@link #KEY_FOREGROUND} implicitly set to true. */
public
static
final
String
ACTION_ADD
=
"com.google.android.exoplayer.downloadService.action.ADD"
;
private
static
final
String
ACTION_RESTART
=
"com.google.android.exoplayer.downloadService.action.RESTART"
;
/** Stops one or all downloads. */
/**
public
static
final
String
ACTION_STOP
=
* Adds a new download. Extras:
"com.google.android.exoplayer.downloadService.action.STOP"
;
*
* <ul>
* <li>{@link #KEY_DOWNLOAD_ACTION} - The {@code byte[]} representation of the {@link
* DownloadAction} that defines the download to be added. The required representation can be
* obtained by calling {@link DownloadAction#toByteArray()}.
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public
static
final
String
ACTION_ADD
=
"com.google.android.exoplayer.downloadService.action.ADD"
;
/** Starts one or all downloads. */
/**
* Starts one or all of the current downloads. Extras:
*
* <ul>
* <li>{@link #KEY_CONTENT_ID} - The content id of a single download to start. If omitted, all
* of the current downloads will be started.
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public
static
final
String
ACTION_START
=
public
static
final
String
ACTION_START
=
"com.google.android.exoplayer.downloadService.action.START"
;
"com.google.android.exoplayer.downloadService.action.START"
;
/** Removes one download. */
/**
* Stops one or all of the current downloads. Extras:
*
* <ul>
* <li>{@link #KEY_CONTENT_ID} - The content id of a single download to stop. If omitted, all of
* the current downloads will be stopped.
* <li>{@link #KEY_STOP_REASON} - An application provided reason for stopping the download or
* downloads. Must not be {@link DownloadState#MANUAL_STOP_REASON_NONE}. If omitted, the
* stop reason will be {@link DownloadState#MANUAL_STOP_REASON_UNDEFINED}.
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public
static
final
String
ACTION_STOP
=
"com.google.android.exoplayer.downloadService.action.STOP"
;
/**
* Removes an existing download. Extras:
*
* <ul>
* <li>{@link #KEY_CONTENT_ID} - The content id of a download to remove.
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public
static
final
String
ACTION_REMOVE
=
public
static
final
String
ACTION_REMOVE
=
"com.google.android.exoplayer.downloadService.action.REMOVE"
;
"com.google.android.exoplayer.downloadService.action.REMOVE"
;
/** Like {@link #ACTION_INIT}, but with {@link #KEY_FOREGROUND} implicitly set to true. */
/**
private
static
final
String
ACTION_RESTART
=
* Key for the {@code byte[]} representation of the {@link DownloadAction} in {@link #ACTION_ADD}
"com.google.android.exoplayer.downloadService.action.RESTART"
;
* intents.
*/
/** Key for the {@link DownloadAction} in an {@link #ACTION_ADD} intent. */
public
static
final
String
KEY_DOWNLOAD_ACTION
=
"download_action"
;
public
static
final
String
KEY_DOWNLOAD_ACTION
=
"download_action"
;
/**
/**
* Key for
content id in an {@link #ACTION_STOP}, {@link #ACTION_START} or {@link #ACTION_REMOVE}
* Key for
the content id in {@link #ACTION_START}, {@link #ACTION_STOP} and {@link
*
intent
.
*
#ACTION_REMOVE} intents
.
*/
*/
public
static
final
String
KEY_CONTENT_ID
=
"content_id"
;
public
static
final
String
KEY_CONTENT_ID
=
"content_id"
;
/** Key for
manual stop reason in an {@link #ACTION_STOP} intent
. */
/** Key for
the stop reason in {@link #ACTION_STOP} intents
. */
public
static
final
String
KEY_STOP_REASON
=
"stop_reason"
;
public
static
final
String
KEY_STOP_REASON
=
"stop_reason"
;
/** Invalid foreground notification id which can be used to run the service in the background. */
public
static
final
int
FOREGROUND_NOTIFICATION_ID_NONE
=
0
;
/**
/**
* Key for a boolean
flag in any intent to indicate whether the service was started in the
* Key for a boolean
extra that can be set on any intent to indicate whether the service was
*
foreground. If set, the service is guaranteed to call {@link #startForeground(int,
*
started in the foreground. If set, the service is guaranteed to call {@link
* Notification)}.
*
#startForeground(int,
Notification)}.
*/
*/
public
static
final
String
KEY_FOREGROUND
=
"foreground"
;
public
static
final
String
KEY_FOREGROUND
=
"foreground"
;
/** Invalid foreground notification id that can be used to run the service in the background. */
public
static
final
int
FOREGROUND_NOTIFICATION_ID_NONE
=
0
;
/** Default foreground notification update interval in milliseconds. */
/** Default foreground notification update interval in milliseconds. */
public
static
final
long
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
=
1000
;
public
static
final
long
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
=
1000
;
...
@@ -121,7 +165,7 @@ public abstract class DownloadService extends Service {
...
@@ -121,7 +165,7 @@ public abstract class DownloadService extends Service {
}
}
/**
/**
* Creates a DownloadService
which
will run in the foreground. {@link
* Creates a DownloadService
that
will run in the foreground. {@link
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
*
*
* @param foregroundNotificationId The notification id for the foreground notification, must not
* @param foregroundNotificationId The notification id for the foreground notification, must not
...
@@ -139,7 +183,7 @@ public abstract class DownloadService extends Service {
...
@@ -139,7 +183,7 @@ public abstract class DownloadService extends Service {
}
}
/**
/**
* Creates a DownloadService
which
will run in the foreground. {@link
* Creates a DownloadService
that
will run in the foreground. {@link
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
*
*
* @param foregroundNotificationId The notification id for the foreground notification. Must not
* @param foregroundNotificationId The notification id for the foreground notification. Must not
...
@@ -340,42 +384,47 @@ public abstract class DownloadService extends Service {
...
@@ -340,42 +384,47 @@ public abstract class DownloadService extends Service {
case
ACTION_ADD:
case
ACTION_ADD:
byte
[]
actionData
=
intent
.
getByteArrayExtra
(
KEY_DOWNLOAD_ACTION
);
byte
[]
actionData
=
intent
.
getByteArrayExtra
(
KEY_DOWNLOAD_ACTION
);
if
(
actionData
==
null
)
{
if
(
actionData
==
null
)
{
Log
.
e
(
TAG
,
"Ignor
ing ADD action with no action dat
a"
);
Log
.
e
(
TAG
,
"Ignor
ed ADD action: Missing download_action extr
a"
);
}
else
{
}
else
{
DownloadAction
downloadAction
=
null
;
try
{
try
{
download
Manager
.
handleAction
(
DownloadAction
.
fromByteArray
(
actionData
)
);
download
Action
=
DownloadAction
.
fromByteArray
(
actionData
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
Log
.
e
(
TAG
,
"Failed to handle ADD action"
,
e
);
Log
.
e
(
TAG
,
"Ignored ADD action: Failed to deserialize download_action extra"
,
e
);
}
if
(
downloadAction
!=
null
)
{
downloadManager
.
handleAction
(
downloadAction
);
}
}
}
}
break
;
break
;
case
ACTION_STOP:
case
ACTION_START:
String
stopDownloadId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
String
contentId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
int
stopReason
=
intent
.
getIntExtra
(
KEY_STOP_REASON
,
0
);
if
(
contentId
==
null
)
{
if
(
stopDownloadId
==
null
)
{
downloadManager
.
startDownloads
();
downloadManager
.
stopDownloads
(
stopReason
);
}
else
{
}
else
{
downloadManager
.
st
opDownload
(
stopDownloadId
,
stopReason
);
downloadManager
.
st
artDownload
(
contentId
);
}
}
break
;
break
;
case
ACTION_START:
case
ACTION_STOP:
String
startDownloadId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
contentId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
if
(
startDownloadId
==
null
)
{
int
stopReason
=
downloadManager
.
startDownloads
();
intent
.
getIntExtra
(
KEY_STOP_REASON
,
DownloadState
.
MANUAL_STOP_REASON_UNDEFINED
);
if
(
contentId
==
null
)
{
downloadManager
.
stopDownloads
(
stopReason
);
}
else
{
}
else
{
downloadManager
.
st
artDownload
(
startDownloadId
);
downloadManager
.
st
opDownload
(
contentId
,
stopReason
);
}
}
break
;
break
;
case
ACTION_REMOVE:
case
ACTION_REMOVE:
String
id3
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
contentId
=
intent
.
getStringExtra
(
KEY_CONTENT_ID
);
if
(
id3
==
null
)
{
if
(
contentId
==
null
)
{
Log
.
e
(
TAG
,
"Ignor
ing REMOVE action with no id
"
);
Log
.
e
(
TAG
,
"Ignor
ed REMOVE action: Missing content_id extra
"
);
}
else
{
}
else
{
downloadManager
.
removeDownload
(
id3
);
downloadManager
.
removeDownload
(
contentId
);
}
}
break
;
break
;
default
:
default
:
Log
.
e
(
TAG
,
"Ignor
ing
unrecognized action: "
+
intentAction
);
Log
.
e
(
TAG
,
"Ignor
ed
unrecognized action: "
+
intentAction
);
break
;
break
;
}
}
...
@@ -402,7 +451,9 @@ public abstract class DownloadService extends Service {
...
@@ -402,7 +451,9 @@ public abstract class DownloadService extends Service {
}
}
}
}
/** DownloadService isn't designed to be bound. */
/**
* Throws {@link UnsupportedOperationException} because this service is not designed to be bound.
*/
@Nullable
@Nullable
@Override
@Override
public
final
IBinder
onBind
(
Intent
intent
)
{
public
final
IBinder
onBind
(
Intent
intent
)
{
...
@@ -445,9 +496,9 @@ public abstract class DownloadService extends Service {
...
@@ -445,9 +496,9 @@ public abstract class DownloadService extends Service {
}
}
/**
/**
* Called when the state of a download changes.
* Called when the state of a download changes.
The default implementation is a no-op.
*
*
* @param downloadState The state of the download.
* @param downloadState The
new
state of the download.
*/
*/
protected
void
onDownloadStateChanged
(
DownloadState
downloadState
)
{
protected
void
onDownloadStateChanged
(
DownloadState
downloadState
)
{
// Do nothing.
// Do nothing.
...
...
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