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
04524a68
authored
Jun 15, 2019
by
Tim Balsfulland
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add convenience constructors for notification channel descriptions
parent
da1f3f01
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
1 deletions
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java
library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java
View file @
04524a68
...
@@ -174,6 +174,7 @@ public abstract class DownloadService extends Service {
...
@@ -174,6 +174,7 @@ public abstract class DownloadService extends Service {
@Nullable
private
final
ForegroundNotificationUpdater
foregroundNotificationUpdater
;
@Nullable
private
final
ForegroundNotificationUpdater
foregroundNotificationUpdater
;
@Nullable
private
final
String
channelId
;
@Nullable
private
final
String
channelId
;
@StringRes
private
final
int
channelNameResourceId
;
@StringRes
private
final
int
channelNameResourceId
;
@StringRes
private
final
int
channelDescriptionResourceId
;
private
DownloadManager
downloadManager
;
private
DownloadManager
downloadManager
;
private
int
lastStartId
;
private
int
lastStartId
;
...
@@ -239,16 +240,53 @@ public abstract class DownloadService extends Service {
...
@@ -239,16 +240,53 @@ public abstract class DownloadService extends Service {
long
foregroundNotificationUpdateInterval
,
long
foregroundNotificationUpdateInterval
,
@Nullable
String
channelId
,
@Nullable
String
channelId
,
@StringRes
int
channelNameResourceId
)
{
@StringRes
int
channelNameResourceId
)
{
this
(
foregroundNotificationId
,
foregroundNotificationUpdateInterval
,
channelId
,
channelNameResourceId
,
/* channelDescriptionResourceId= */
0
);
}
/**
* Creates a DownloadService.
*
* @param foregroundNotificationId The notification id for the foreground notification, or {@link
* #FOREGROUND_NOTIFICATION_ID_NONE} if the service should only ever run in the background.
* @param foregroundNotificationUpdateInterval The maximum interval between updates to the
* foreground notification, in milliseconds. Ignored if {@code foregroundNotificationId} is
* {@link #FOREGROUND_NOTIFICATION_ID_NONE}.
* @param channelId An id for a low priority notification channel to create, or {@code null} if
* the app will take care of creating a notification channel if needed. If specified, must be
* unique per package. The value may be truncated if it's too long. Ignored if {@code
* foregroundNotificationId} is {@link #FOREGROUND_NOTIFICATION_ID_NONE}.
* @param channelNameResourceId A string resource identifier for the user visible name of the
* channel, if {@code channelId} is specified. The recommended maximum length is 40
* characters. The value may be truncated if it is too long. Ignored if {@code
* foregroundNotificationId} is {@link #FOREGROUND_NOTIFICATION_ID_NONE}.
* @param channelDescriptionResourceId A string resource identifier for the user visible
* description. Ignored if {@code foregroundNotificationId} is
* {@link #FOREGROUND_NOTIFICATION_ID_NONE}.
*/
protected
DownloadService
(
int
foregroundNotificationId
,
long
foregroundNotificationUpdateInterval
,
@Nullable
String
channelId
,
@StringRes
int
channelNameResourceId
,
@StringRes
int
channelDescriptionResourceId
)
{
if
(
foregroundNotificationId
==
FOREGROUND_NOTIFICATION_ID_NONE
)
{
if
(
foregroundNotificationId
==
FOREGROUND_NOTIFICATION_ID_NONE
)
{
this
.
foregroundNotificationUpdater
=
null
;
this
.
foregroundNotificationUpdater
=
null
;
this
.
channelId
=
null
;
this
.
channelId
=
null
;
this
.
channelNameResourceId
=
0
;
this
.
channelNameResourceId
=
0
;
this
.
channelDescriptionResourceId
=
0
;
}
else
{
}
else
{
this
.
foregroundNotificationUpdater
=
this
.
foregroundNotificationUpdater
=
new
ForegroundNotificationUpdater
(
new
ForegroundNotificationUpdater
(
foregroundNotificationId
,
foregroundNotificationUpdateInterval
);
foregroundNotificationId
,
foregroundNotificationUpdateInterval
);
this
.
channelId
=
channelId
;
this
.
channelId
=
channelId
;
this
.
channelNameResourceId
=
channelNameResourceId
;
this
.
channelNameResourceId
=
channelNameResourceId
;
this
.
channelDescriptionResourceId
=
channelDescriptionResourceId
;
}
}
}
}
...
@@ -543,7 +581,11 @@ public abstract class DownloadService extends Service {
...
@@ -543,7 +581,11 @@ public abstract class DownloadService extends Service {
public
void
onCreate
()
{
public
void
onCreate
()
{
if
(
channelId
!=
null
)
{
if
(
channelId
!=
null
)
{
NotificationUtil
.
createNotificationChannel
(
NotificationUtil
.
createNotificationChannel
(
this
,
channelId
,
channelNameResourceId
,
NotificationUtil
.
IMPORTANCE_LOW
);
this
,
channelId
,
channelNameResourceId
,
channelDescriptionResourceId
,
NotificationUtil
.
IMPORTANCE_LOW
);
}
}
Class
<?
extends
DownloadService
>
clazz
=
getClass
();
Class
<?
extends
DownloadService
>
clazz
=
getClass
();
DownloadManagerHelper
downloadManagerHelper
=
downloadManagerListeners
.
get
(
clazz
);
DownloadManagerHelper
downloadManagerHelper
=
downloadManagerListeners
.
get
(
clazz
);
...
...
library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java
View file @
04524a68
...
@@ -80,11 +80,48 @@ public final class NotificationUtil {
...
@@ -80,11 +80,48 @@ public final class NotificationUtil {
*/
*/
public
static
void
createNotificationChannel
(
public
static
void
createNotificationChannel
(
Context
context
,
String
id
,
@StringRes
int
nameResourceId
,
@Importance
int
importance
)
{
Context
context
,
String
id
,
@StringRes
int
nameResourceId
,
@Importance
int
importance
)
{
createNotificationChannel
(
context
,
id
,
nameResourceId
,
importance
,
/* descriptionResourceId= */
0
);
}
/**
* Creates a notification channel that notifications can be posted to. See {@link
* NotificationChannel} and {@link
* NotificationManager#createNotificationChannel(NotificationChannel)} for details.
*
* @param context A {@link Context}.
* @param id The id of the channel. Must be unique per package. The value may be truncated if it's
* too long.
* @param nameResourceId A string resource identifier for the user visible name of the channel.
* You can rename this channel when the system locale changes by listening for the {@link
* Intent#ACTION_LOCALE_CHANGED} broadcast. The recommended maximum length is 40 characters.
* The value may be truncated if it is too long.
* @param importance The importance of the channel. This controls how interruptive notifications
* posted to this channel are. One of {@link #IMPORTANCE_UNSPECIFIED}, {@link
* #IMPORTANCE_NONE}, {@link #IMPORTANCE_MIN}, {@link #IMPORTANCE_LOW}, {@link
* #IMPORTANCE_DEFAULT} and {@link #IMPORTANCE_HIGH}.
* @param descriptionResourceId A String resource identifier for the user visible description of
* the channel. You can change the description of this channel when the system locale changes
* by listening for the {@link Intent#ACTION_LOCALE_CHANGED} broadcast. Ignored if set to 0.
*/
public
static
void
createNotificationChannel
(
Context
context
,
String
id
,
@StringRes
int
nameResourceId
,
@Importance
int
importance
,
@StringRes
int
descriptionResourceId
)
{
if
(
Util
.
SDK_INT
>=
26
)
{
if
(
Util
.
SDK_INT
>=
26
)
{
NotificationManager
notificationManager
=
NotificationManager
notificationManager
=
(
NotificationManager
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
(
NotificationManager
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
NotificationChannel
channel
=
NotificationChannel
channel
=
new
NotificationChannel
(
id
,
context
.
getString
(
nameResourceId
),
importance
);
new
NotificationChannel
(
id
,
context
.
getString
(
nameResourceId
),
importance
);
if
(
descriptionResourceId
!=
0
)
{
channel
.
setDescription
(
context
.
getString
(
descriptionResourceId
));
}
notificationManager
.
createNotificationChannel
(
channel
);
notificationManager
.
createNotificationChannel
(
channel
);
}
}
}
}
...
...
library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java
View file @
04524a68
...
@@ -416,6 +416,38 @@ public class PlayerNotificationManager {
...
@@ -416,6 +416,38 @@ public class PlayerNotificationManager {
/**
/**
* Creates a notification manager and a low-priority notification channel with the specified
* Creates a notification manager and a low-priority notification channel with the specified
* {@code channelId} and {@code channelName}.
*
* <p>If the player notification manager is intended to be used within a foreground service,
* {@link #createWithNotificationChannel(Context, String, int, int, MediaDescriptionAdapter,
* NotificationListener)} should be used to which a {@link NotificationListener} can be passed.
* This way you'll receive the notification to put the service into the foreground by calling
* {@link android.app.Service#startForeground(int, Notification)}.
*
* @param context The {@link Context}.
* @param channelId The id of the notification channel.
* @param channelName A string resource identifier for the user visible name of the channel. The
* recommended maximum length is 40 characters; the value may be truncated if it is too long.
* @param channelDescription A String resource identifier for the user visible description of the
* channel.
* @param notificationId The id of the notification.
* @param mediaDescriptionAdapter The {@link MediaDescriptionAdapter}.
*/
public
static
PlayerNotificationManager
createWithNotificationChannel
(
Context
context
,
String
channelId
,
@StringRes
int
channelName
,
@StringRes
int
channelDescription
,
int
notificationId
,
MediaDescriptionAdapter
mediaDescriptionAdapter
)
{
NotificationUtil
.
createNotificationChannel
(
context
,
channelId
,
channelName
,
channelDescription
,
NotificationUtil
.
IMPORTANCE_LOW
);
return
new
PlayerNotificationManager
(
context
,
channelId
,
notificationId
,
mediaDescriptionAdapter
);
}
/**
* Creates a notification manager and a low-priority notification channel with the specified
* {@code channelId} and {@code channelName}. The {@link NotificationListener} passed as the last
* {@code channelId} and {@code channelName}. The {@link NotificationListener} passed as the last
* parameter will be notified when the notification is created and cancelled.
* parameter will be notified when the notification is created and cancelled.
*
*
...
@@ -441,6 +473,35 @@ public class PlayerNotificationManager {
...
@@ -441,6 +473,35 @@ public class PlayerNotificationManager {
}
}
/**
/**
* Creates a notification manager and a low-priority notification channel with the specified
* {@code channelId} and {@code channelName}. The {@link NotificationListener} passed as the last
* parameter will be notified when the notification is created and cancelled.
*
* @param context The {@link Context}.
* @param channelId The id of the notification channel.
* @param channelName A string resource identifier for the user visible name of the channel. The
* recommended maximum length is 40 characters; the value may be truncated if it is too long.
* @param channelDescription A String resource identifier for the user visible description of the
* channel.
* @param notificationId The id of the notification.
* @param mediaDescriptionAdapter The {@link MediaDescriptionAdapter}.
* @param notificationListener The {@link NotificationListener}.
*/
public
static
PlayerNotificationManager
createWithNotificationChannel
(
Context
context
,
String
channelId
,
@StringRes
int
channelName
,
@StringRes
int
channelDescription
,
int
notificationId
,
MediaDescriptionAdapter
mediaDescriptionAdapter
,
@Nullable
NotificationListener
notificationListener
)
{
NotificationUtil
.
createNotificationChannel
(
context
,
channelId
,
channelName
,
channelDescription
,
NotificationUtil
.
IMPORTANCE_LOW
);
return
new
PlayerNotificationManager
(
context
,
channelId
,
notificationId
,
mediaDescriptionAdapter
,
notificationListener
);
}
/**
* Creates a notification manager using the specified notification {@code channelId}. The caller
* Creates a notification manager using the specified notification {@code channelId}. The caller
* is responsible for creating the notification channel.
* is responsible for creating the notification channel.
*
*
...
...
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