Fix PlayerNotificationManager lint error

builder.mActions is marked as @RestrictTo(LIBRARY_GROUP). We'll
just have to put up with notification flicker on KitKat if the
actions change.

PiperOrigin-RevId: 233420076
This commit is contained in:
olly 2019-02-11 17:19:36 +00:00 committed by Andrew Lewis
parent de720fef77
commit 6d461f5b25

View File

@ -338,10 +338,9 @@ public class PlayerNotificationManager {
private final Context context; private final Context context;
private final String channelId; private final String channelId;
private final NotificationCompat.Builder builder;
private final int notificationId; private final int notificationId;
private final MediaDescriptionAdapter mediaDescriptionAdapter; private final MediaDescriptionAdapter mediaDescriptionAdapter;
private final @Nullable CustomActionReceiver customActionReceiver; @Nullable private final CustomActionReceiver customActionReceiver;
private final Handler mainHandler; private final Handler mainHandler;
private final NotificationManagerCompat notificationManager; private final NotificationManagerCompat notificationManager;
private final IntentFilter intentFilter; private final IntentFilter intentFilter;
@ -353,13 +352,15 @@ public class PlayerNotificationManager {
private final int instanceId; private final int instanceId;
private final Timeline.Window window; private final Timeline.Window window;
@Nullable private NotificationCompat.Builder builder;
@Nullable private ArrayList<NotificationCompat.Action> builderActions;
@Nullable private Player player; @Nullable private Player player;
@Nullable private PlaybackPreparer playbackPreparer; @Nullable private PlaybackPreparer playbackPreparer;
private ControlDispatcher controlDispatcher; private ControlDispatcher controlDispatcher;
private boolean isNotificationStarted; private boolean isNotificationStarted;
private int currentNotificationTag; private int currentNotificationTag;
private @Nullable NotificationListener notificationListener; @Nullable private NotificationListener notificationListener;
private @Nullable MediaSessionCompat.Token mediaSessionToken; @Nullable private MediaSessionCompat.Token mediaSessionToken;
private boolean useNavigationActions; private boolean useNavigationActions;
private boolean usePlayPauseActions; private boolean usePlayPauseActions;
private boolean useStopAction; private boolean useStopAction;
@ -369,9 +370,9 @@ public class PlayerNotificationManager {
private boolean colorized; private boolean colorized;
private int defaults; private int defaults;
private int color; private int color;
private @DrawableRes int smallIconResourceId; @DrawableRes private int smallIconResourceId;
private int visibility; private int visibility;
private @Priority int priority; @Priority private int priority;
private boolean useChronometer; private boolean useChronometer;
private boolean wasPlayWhenReady; private boolean wasPlayWhenReady;
private int lastPlaybackState; private int lastPlaybackState;
@ -538,7 +539,6 @@ public class PlayerNotificationManager {
this.mediaDescriptionAdapter = mediaDescriptionAdapter; this.mediaDescriptionAdapter = mediaDescriptionAdapter;
this.notificationListener = notificationListener; this.notificationListener = notificationListener;
this.customActionReceiver = customActionReceiver; this.customActionReceiver = customActionReceiver;
builder = new NotificationCompat.Builder(context, channelId);
controlDispatcher = new DefaultControlDispatcher(); controlDispatcher = new DefaultControlDispatcher();
window = new Timeline.Window(); window = new Timeline.Window();
instanceId = instanceIdCounter++; instanceId = instanceIdCounter++;
@ -890,11 +890,12 @@ public class PlayerNotificationManager {
private Notification startOrUpdateNotification(@Nullable Bitmap bitmap) { private Notification startOrUpdateNotification(@Nullable Bitmap bitmap) {
Player player = this.player; Player player = this.player;
boolean ongoing = getOngoing(player); boolean ongoing = getOngoing(player);
Notification notification = createNotification(player, builder, ongoing, bitmap); builder = createNotification(player, builder, ongoing, bitmap);
if (notification == null) { if (builder == null) {
stopNotification(/* dismissedByUser= */ false); stopNotification(/* dismissedByUser= */ false);
return null; return null;
} }
Notification notification = builder.build();
notificationManager.notify(notificationId, notification); notificationManager.notify(notificationId, notification);
if (!isNotificationStarted) { if (!isNotificationStarted) {
isNotificationStarted = true; isNotificationStarted = true;
@ -926,28 +927,27 @@ public class PlayerNotificationManager {
* Creates the notification given the current player state. * Creates the notification given the current player state.
* *
* @param player The player for which state to build a notification. * @param player The player for which state to build a notification.
* @param builder A builder that can optionally be used for creating the notification. The same * @param builder The builder used to build the last notification, or {@code null}. Re-using the
* builder is passed each time this method is called, since reusing the same builder can * builder when possible can prevent notification flicker when {@code Util#SDK_INT} &lt; 21.
* prevent notification flicker when {@code Util#SDK_INT} &lt; 21. This means implementations
* must take care to ensure anything set on the builder during a previous call is cleared, if
* no longer required.
* @param ongoing Whether the notification should be ongoing. * @param ongoing Whether the notification should be ongoing.
* @param largeIcon The large icon to be used. * @param largeIcon The large icon to be used.
* @return The {@link Notification} which has been built, or {@code null} if no notification * @return The {@link NotificationCompat.Builder} on which to call {@link
* should be displayed. * NotificationCompat.Builder#build()} to obtain the notification, or {@code null} if no
* notification should be displayed.
*/ */
@Nullable @Nullable
protected Notification createNotification( protected NotificationCompat.Builder createNotification(
Player player, Player player,
NotificationCompat.Builder builder, @Nullable NotificationCompat.Builder builder,
boolean ongoing, boolean ongoing,
@Nullable Bitmap largeIcon) { @Nullable Bitmap largeIcon) {
if (player.getPlaybackState() == Player.STATE_IDLE) { if (player.getPlaybackState() == Player.STATE_IDLE) {
builderActions = null;
return null; return null;
} }
builder.mActions.clear();
List<String> actionNames = getActions(player); List<String> actionNames = getActions(player);
ArrayList<NotificationCompat.Action> actions = new ArrayList<>(actionNames.size());
for (int i = 0; i < actionNames.size(); i++) { for (int i = 0; i < actionNames.size(); i++) {
String actionName = actionNames.get(i); String actionName = actionNames.get(i);
NotificationCompat.Action action = NotificationCompat.Action action =
@ -955,7 +955,15 @@ public class PlayerNotificationManager {
? playbackActions.get(actionName) ? playbackActions.get(actionName)
: customActions.get(actionName); : customActions.get(actionName);
if (action != null) { if (action != null) {
builder.addAction(action); actions.add(action);
}
}
if (builder == null || !actions.equals(builderActions)) {
builder = new NotificationCompat.Builder(context, channelId);
builderActions = actions;
for (int i = 0; i < actions.size(); i++) {
builder.addAction(actions.get(i));
} }
} }
@ -1010,7 +1018,7 @@ public class PlayerNotificationManager {
setLargeIcon(builder, largeIcon); setLargeIcon(builder, largeIcon);
builder.setContentIntent(mediaDescriptionAdapter.createCurrentContentIntent(player)); builder.setContentIntent(mediaDescriptionAdapter.createCurrentContentIntent(player));
return builder.build(); return builder;
} }
/** /**