Merge pull request #6042 from Timbals:dev-v2

PiperOrigin-RevId: 258812820
This commit is contained in:
Oliver Woodman 2019-07-18 19:40:34 +01:00
commit 0e7f015020
5 changed files with 117 additions and 15 deletions

View File

@ -32,6 +32,8 @@
* Fix issue where invalid language tags were normalized to "und" instead of
keeping the original
([#6153](https://github.com/google/ExoPlayer/issues/6153)).
* Add ability to specify a description when creating notification channels via
ExoPlayer library classes.
### 2.10.3 ###

View File

@ -41,7 +41,8 @@ public class DemoDownloadService extends DownloadService {
FOREGROUND_NOTIFICATION_ID,
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL,
CHANNEL_ID,
R.string.exo_download_notification_channel_name);
R.string.exo_download_notification_channel_name,
/* channelDescriptionResourceId= */ 0);
nextNotificationId = FOREGROUND_NOTIFICATION_ID + 1;
}

View File

@ -174,6 +174,7 @@ public abstract class DownloadService extends Service {
@Nullable private final ForegroundNotificationUpdater foregroundNotificationUpdater;
@Nullable private final String channelId;
@StringRes private final int channelNameResourceId;
@StringRes private final int channelDescriptionResourceId;
@Nullable private DownloadManager downloadManager;
private int lastStartId;
@ -214,7 +215,23 @@ public abstract class DownloadService extends Service {
foregroundNotificationId,
foregroundNotificationUpdateInterval,
/* channelId= */ null,
/* channelNameResourceId= */ 0);
/* channelNameResourceId= */ 0,
/* channelDescriptionResourceId= */ 0);
}
/** @deprecated Use {@link #DownloadService(int, long, String, int, int)}. */
@Deprecated
protected DownloadService(
int foregroundNotificationId,
long foregroundNotificationUpdateInterval,
@Nullable String channelId,
@StringRes int channelNameResourceId) {
this(
foregroundNotificationId,
foregroundNotificationUpdateInterval,
channelId,
channelNameResourceId,
/* channelDescriptionResourceId= */ 0);
}
/**
@ -230,25 +247,33 @@ public abstract class DownloadService extends Service {
* 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
* notification channel. The recommended maximum length is 40 characters. The value may be
* truncated if it's too long. Ignored if {@code channelId} is null or if {@code
* foregroundNotificationId} is {@link #FOREGROUND_NOTIFICATION_ID_NONE}.
* @param channelDescriptionResourceId A string resource identifier for the user visible
* description of the notification channel, or 0 if no description is provided. The
* recommended maximum length is 300 characters. The value may be truncated if it is too long.
* Ignored if {@code channelId} is null or if {@code foregroundNotificationId} is {@link
* #FOREGROUND_NOTIFICATION_ID_NONE}.
*/
protected DownloadService(
int foregroundNotificationId,
long foregroundNotificationUpdateInterval,
@Nullable String channelId,
@StringRes int channelNameResourceId) {
@StringRes int channelNameResourceId,
@StringRes int channelDescriptionResourceId) {
if (foregroundNotificationId == FOREGROUND_NOTIFICATION_ID_NONE) {
this.foregroundNotificationUpdater = null;
this.channelId = null;
this.channelNameResourceId = 0;
this.channelDescriptionResourceId = 0;
} else {
this.foregroundNotificationUpdater =
new ForegroundNotificationUpdater(
foregroundNotificationId, foregroundNotificationUpdateInterval);
this.channelId = channelId;
this.channelNameResourceId = channelNameResourceId;
this.channelDescriptionResourceId = channelDescriptionResourceId;
}
}
@ -543,7 +568,11 @@ public abstract class DownloadService extends Service {
public void onCreate() {
if (channelId != null) {
NotificationUtil.createNotificationChannel(
this, channelId, channelNameResourceId, NotificationUtil.IMPORTANCE_LOW);
this,
channelId,
channelNameResourceId,
channelDescriptionResourceId,
NotificationUtil.IMPORTANCE_LOW);
}
Class<? extends DownloadService> clazz = getClass();
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(clazz);

View File

@ -61,6 +61,14 @@ public final class NotificationUtil {
/** @see NotificationManager#IMPORTANCE_HIGH */
public static final int IMPORTANCE_HIGH = NotificationManager.IMPORTANCE_HIGH;
/** @deprecated Use {@link #createNotificationChannel(Context, String, int, int, int)}. */
@Deprecated
public static void createNotificationChannel(
Context context, String id, @StringRes int nameResourceId, @Importance int importance) {
createNotificationChannel(
context, id, nameResourceId, /* descriptionResourceId= */ 0, importance);
}
/**
* Creates a notification channel that notifications can be posted to. See {@link
* NotificationChannel} and {@link
@ -70,21 +78,33 @@ public final class NotificationUtil {
* @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.
* The recommended maximum length is 40 characters. The string may be truncated if it's too
* long. You can rename the channel when the system locale changes by listening for the {@link
* Intent#ACTION_LOCALE_CHANGED} broadcast.
* @param descriptionResourceId A string resource identifier for the user visible description of
* the channel, or 0 if no description is provided. The recommended maximum length is 300
* characters. The value may be truncated if it is too long. You can change the description of
* the channel when the system locale changes by listening for the {@link
* Intent#ACTION_LOCALE_CHANGED} broadcast.
* @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}.
*/
public static void createNotificationChannel(
Context context, String id, @StringRes int nameResourceId, @Importance int importance) {
Context context,
String id,
@StringRes int nameResourceId,
@StringRes int descriptionResourceId,
@Importance int importance) {
if (Util.SDK_INT >= 26) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel =
new NotificationChannel(id, context.getString(nameResourceId), importance);
if (descriptionResourceId != 0) {
channel.setDescription(context.getString(descriptionResourceId));
}
notificationManager.createNotificationChannel(channel);
}
}

View File

@ -385,6 +385,26 @@ public class PlayerNotificationManager {
private boolean wasPlayWhenReady;
private int lastPlaybackState;
/**
* @deprecated Use {@link #createWithNotificationChannel(Context, String, int, int, int,
* MediaDescriptionAdapter)}.
*/
@Deprecated
public static PlayerNotificationManager createWithNotificationChannel(
Context context,
String channelId,
@StringRes int channelName,
int notificationId,
MediaDescriptionAdapter mediaDescriptionAdapter) {
return createWithNotificationChannel(
context,
channelId,
channelName,
/* channelDescription= */ 0,
notificationId,
mediaDescriptionAdapter);
}
/**
* Creates a notification manager and a low-priority notification channel with the specified
* {@code channelId} and {@code channelName}.
@ -397,8 +417,12 @@ public class PlayerNotificationManager {
*
* @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 channelName A string resource identifier for the user visible name of the notification
* channel. The recommended maximum length is 40 characters. The string may be truncated if
* it's too long.
* @param channelDescription A string resource identifier for the user visible description of the
* notification channel, or 0 if no description is provided. The recommended maximum length is
* 300 characters. The value may be truncated if it is too long.
* @param notificationId The id of the notification.
* @param mediaDescriptionAdapter The {@link MediaDescriptionAdapter}.
*/
@ -406,14 +430,37 @@ public class PlayerNotificationManager {
Context context,
String channelId,
@StringRes int channelName,
@StringRes int channelDescription,
int notificationId,
MediaDescriptionAdapter mediaDescriptionAdapter) {
NotificationUtil.createNotificationChannel(
context, channelId, channelName, NotificationUtil.IMPORTANCE_LOW);
context, channelId, channelName, channelDescription, NotificationUtil.IMPORTANCE_LOW);
return new PlayerNotificationManager(
context, channelId, notificationId, mediaDescriptionAdapter);
}
/**
* @deprecated Use {@link #createWithNotificationChannel(Context, String, int, int, int,
* MediaDescriptionAdapter, NotificationListener)}.
*/
@Deprecated
public static PlayerNotificationManager createWithNotificationChannel(
Context context,
String channelId,
@StringRes int channelName,
int notificationId,
MediaDescriptionAdapter mediaDescriptionAdapter,
@Nullable NotificationListener notificationListener) {
return createWithNotificationChannel(
context,
channelId,
channelName,
/* channelDescription= */ 0,
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
@ -422,7 +469,9 @@ public class PlayerNotificationManager {
* @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.
* recommended maximum length is 40 characters. The string may be truncated if it's too long.
* @param channelDescription A string resource identifier for the user visible description of the
* channel, or 0 if no description is provided.
* @param notificationId The id of the notification.
* @param mediaDescriptionAdapter The {@link MediaDescriptionAdapter}.
* @param notificationListener The {@link NotificationListener}.
@ -431,11 +480,12 @@ public class PlayerNotificationManager {
Context context,
String channelId,
@StringRes int channelName,
@StringRes int channelDescription,
int notificationId,
MediaDescriptionAdapter mediaDescriptionAdapter,
@Nullable NotificationListener notificationListener) {
NotificationUtil.createNotificationChannel(
context, channelId, channelName, NotificationUtil.IMPORTANCE_LOW);
context, channelId, channelName, channelDescription, NotificationUtil.IMPORTANCE_LOW);
return new PlayerNotificationManager(
context, channelId, notificationId, mediaDescriptionAdapter, notificationListener);
}