Random adjustements to notification

This commit is contained in:
Stypox 2020-09-16 14:00:22 +02:00
parent 2017e6a3e3
commit 59e7ebabfa
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -1,7 +1,6 @@
package org.schabi.newpipe.player; package org.schabi.newpipe.player;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
@ -14,6 +13,7 @@ import android.util.Log;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.MainActivity;
@ -23,7 +23,6 @@ import org.schabi.newpipe.util.NavigationHelper;
import java.util.List; import java.util.List;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL; import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE; import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
import static org.schabi.newpipe.player.MainPlayer.ACTION_BUFFERING; import static org.schabi.newpipe.player.MainPlayer.ACTION_BUFFERING;
@ -51,7 +50,7 @@ public final class NotificationUtil {
@NotificationConstants.Action @NotificationConstants.Action
private int[] notificationSlots = NotificationConstants.SLOT_DEFAULTS.clone(); private int[] notificationSlots = NotificationConstants.SLOT_DEFAULTS.clone();
private NotificationManager notificationManager; private NotificationManagerCompat notificationManager;
private NotificationCompat.Builder notificationBuilder; private NotificationCompat.Builder notificationBuilder;
private NotificationUtil() { private NotificationUtil() {
@ -82,6 +81,7 @@ public final class NotificationUtil {
notificationBuilder = createNotification(player); notificationBuilder = createNotification(player);
} }
updateNotification(player); updateNotification(player);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
} }
private synchronized NotificationCompat.Builder createNotification( private synchronized NotificationCompat.Builder createNotification(
@ -89,8 +89,7 @@ public final class NotificationUtil {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "createNotification()"); Log.d(TAG, "createNotification()");
} }
notificationManager = notificationManager = NotificationManagerCompat.from(player.context);
(NotificationManager) player.context.getSystemService(NOTIFICATION_SERVICE);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(player.context, final NotificationCompat.Builder builder = new NotificationCompat.Builder(player.context,
player.context.getString(R.string.notification_channel_id)); player.context.getString(R.string.notification_channel_id));
@ -120,6 +119,7 @@ public final class NotificationUtil {
.setSmallIcon(R.drawable.ic_newpipe_triangle_white) .setSmallIcon(R.drawable.ic_newpipe_triangle_white)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setColor(ContextCompat.getColor(player.context, R.color.gray)) .setColor(ContextCompat.getColor(player.context, R.color.gray))
.setCategory(NotificationCompat.CATEGORY_TRANSPORT)
.setDeleteIntent(PendingIntent.getBroadcast(player.context, NOTIFICATION_ID, .setDeleteIntent(PendingIntent.getBroadcast(player.context, NOTIFICATION_ID,
new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT)); new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT));
@ -127,7 +127,7 @@ public final class NotificationUtil {
} }
/** /**
* Updates the notification and the button icons depending on the playback state. * Updates the notification builder and the button icons depending on the playback state.
* @param player the player currently open, to take data from * @param player the player currently open, to take data from
*/ */
private synchronized void updateNotification(final VideoPlayerImpl player) { private synchronized void updateNotification(final VideoPlayerImpl player) {
@ -135,19 +135,14 @@ public final class NotificationUtil {
Log.d(TAG, "updateNotification()"); Log.d(TAG, "updateNotification()");
} }
if (notificationBuilder == null) {
return;
}
// also update content intent, in case the user switched players // also update content intent, in case the user switched players
notificationBuilder.setContentIntent(PendingIntent.getActivity(player.context, notificationBuilder.setContentIntent(PendingIntent.getActivity(player.context,
NOTIFICATION_ID, getIntentForNotification(player), FLAG_UPDATE_CURRENT)); NOTIFICATION_ID, getIntentForNotification(player), FLAG_UPDATE_CURRENT));
notificationBuilder.setContentTitle(player.getVideoTitle()); notificationBuilder.setContentTitle(player.getVideoTitle());
notificationBuilder.setContentText(player.getUploaderName()); notificationBuilder.setContentText(player.getUploaderName());
notificationBuilder.setTicker(player.getVideoTitle());
updateActions(notificationBuilder, player); updateActions(notificationBuilder, player);
setLargeIcon(notificationBuilder, player); setLargeIcon(notificationBuilder, player);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
} }
@ -158,7 +153,10 @@ public final class NotificationUtil {
void createNotificationAndStartForeground(final VideoPlayerImpl player, final Service service) { void createNotificationAndStartForeground(final VideoPlayerImpl player, final Service service) {
createNotificationIfNeededAndUpdate(player, false); if (notificationBuilder == null) {
notificationBuilder = createNotification(player);
}
updateNotification(player);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
service.startForeground(NOTIFICATION_ID, notificationBuilder.build(), service.startForeground(NOTIFICATION_ID, notificationBuilder.build(),
@ -169,17 +167,13 @@ public final class NotificationUtil {
} }
void cancelNotificationAndStopForeground(final Service service) { void cancelNotificationAndStopForeground(final Service service) {
try { service.stopForeground(true);
if (notificationManager != null) {
notificationManager.cancel(NOTIFICATION_ID); if (notificationManager != null) {
} notificationManager.cancel(NOTIFICATION_ID);
} catch (final Exception e) {
Log.e(TAG, "Could not cancel notification", e);
} }
notificationManager = null; notificationManager = null;
notificationBuilder = null; notificationBuilder = null;
service.stopForeground(true);
} }