mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Add convenience constructors for notification channel descriptions
This commit is contained in:
parent
da1f3f01fa
commit
04524a688d
3 changed files with 141 additions and 1 deletions
|
|
@ -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 {
|
||||||
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 {
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -414,6 +414,38 @@ public class PlayerNotificationManager {
|
||||||
context, channelId, notificationId, mediaDescriptionAdapter);
|
context, channelId, notificationId, mediaDescriptionAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* 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
|
||||||
|
|
@ -440,6 +472,35 @@ public class PlayerNotificationManager {
|
||||||
context, channelId, notificationId, mediaDescriptionAdapter, notificationListener);
|
context, channelId, notificationId, mediaDescriptionAdapter, notificationListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue