Fix notification content intent not being updated when needed

This commit is contained in:
Stypox 2020-09-08 21:42:25 +02:00
parent 9cf0bc6c82
commit bc8954fbba
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 48 additions and 52 deletions

View File

@ -120,8 +120,6 @@ 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))
.setContentIntent(PendingIntent.getActivity(player.context, NOTIFICATION_ID,
getIntentForNotification(player), FLAG_UPDATE_CURRENT))
.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));
@ -141,6 +139,9 @@ public final class NotificationUtil {
return; return;
} }
// also update content intent, in case the user switched players
notificationBuilder.setContentIntent(PendingIntent.getActivity(player.context,
NOTIFICATION_ID, getIntentForNotification(player), FLAG_UPDATE_CURRENT));
notificationBuilder.setContentTitle(player.getVideoTitle()); notificationBuilder.setContentTitle(player.getVideoTitle());
notificationBuilder.setContentText(player.getUploaderName()); notificationBuilder.setContentText(player.getUploaderName());
updateActions(notificationBuilder, player); updateActions(notificationBuilder, player);
@ -326,19 +327,18 @@ public final class NotificationUtil {
} }
private Intent getIntentForNotification(final VideoPlayerImpl player) { private Intent getIntentForNotification(final VideoPlayerImpl player) {
final Intent intent;
if (player.audioPlayerSelected() || player.popupPlayerSelected()) { if (player.audioPlayerSelected() || player.popupPlayerSelected()) {
// Means we play in popup or audio only. Let's show BackgroundPlayerActivity // Means we play in popup or audio only. Let's show the play queue
intent = NavigationHelper.getBackgroundPlayerActivityIntent(player.context); return NavigationHelper.getPlayQueueActivityIntent(player.context);
} else { } else {
// We are playing in fragment. Don't open another activity just show fragment. That's it // We are playing in fragment. Don't open another activity just show fragment. That's it
intent = NavigationHelper.getPlayerIntent( final Intent intent = NavigationHelper.getPlayerIntent(
player.context, MainActivity.class, null, true); player.context, MainActivity.class, null, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN); intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addCategory(Intent.CATEGORY_LAUNCHER);
return intent;
} }
return intent;
} }

View File

@ -57,7 +57,7 @@ import org.schabi.newpipe.settings.SettingsActivity;
import java.util.ArrayList; import java.util.ArrayList;
@SuppressWarnings({"unused", "WeakerAccess"}) @SuppressWarnings({"unused"})
public final class NavigationHelper { public final class NavigationHelper {
public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag"; public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag";
public static final String SEARCH_FRAGMENT_TAG = "search_fragment_tag"; public static final String SEARCH_FRAGMENT_TAG = "search_fragment_tag";
@ -69,16 +69,18 @@ public final class NavigationHelper {
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@NonNull @NonNull
public static Intent getPlayerIntent(@NonNull final Context context, public static <T> Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz, @NonNull final Class<T> targetClazz,
@NonNull final PlayQueue playQueue, @Nullable final PlayQueue playQueue,
@Nullable final String quality, @Nullable final String quality,
final boolean resumePlayback) { final boolean resumePlayback) {
final Intent intent = new Intent(context, targetClazz); final Intent intent = new Intent(context, targetClazz);
final String cacheKey = SerializedCache.getInstance().put(playQueue, PlayQueue.class); if (playQueue != null) {
if (cacheKey != null) { final String cacheKey = SerializedCache.getInstance().put(playQueue, PlayQueue.class);
intent.putExtra(VideoPlayer.PLAY_QUEUE_KEY, cacheKey); if (cacheKey != null) {
intent.putExtra(VideoPlayer.PLAY_QUEUE_KEY, cacheKey);
}
} }
if (quality != null) { if (quality != null) {
intent.putExtra(VideoPlayer.PLAYBACK_QUALITY, quality); intent.putExtra(VideoPlayer.PLAYBACK_QUALITY, quality);
@ -90,53 +92,51 @@ public final class NavigationHelper {
} }
@NonNull @NonNull
public static Intent getPlayerIntent(@NonNull final Context context, public static <T> Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz, @NonNull final Class<T> targetClazz,
@NonNull final PlayQueue playQueue, @Nullable final PlayQueue playQueue,
final boolean resumePlayback) { final boolean resumePlayback) {
return getPlayerIntent(context, targetClazz, playQueue, null, resumePlayback); return getPlayerIntent(context, targetClazz, playQueue, null, resumePlayback);
} }
@NonNull @NonNull
public static Intent getPlayerEnqueueIntent(@NonNull final Context context, public static <T> Intent getPlayerEnqueueIntent(@NonNull final Context context,
@NonNull final Class targetClazz, @NonNull final Class<T> targetClazz,
@NonNull final PlayQueue playQueue, @Nullable final PlayQueue playQueue,
final boolean selectOnAppend, final boolean selectOnAppend,
final boolean resumePlayback) { final boolean resumePlayback) {
return getPlayerIntent(context, targetClazz, playQueue, resumePlayback) return getPlayerIntent(context, targetClazz, playQueue, resumePlayback)
.putExtra(BasePlayer.APPEND_ONLY, true) .putExtra(BasePlayer.APPEND_ONLY, true)
.putExtra(BasePlayer.SELECT_ON_APPEND, selectOnAppend); .putExtra(BasePlayer.SELECT_ON_APPEND, selectOnAppend);
} }
@NonNull @NonNull
public static Intent getPlayerIntent(@NonNull final Context context, public static <T> Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz, @NonNull final Class<T> targetClazz,
@NonNull final PlayQueue playQueue, @Nullable final PlayQueue playQueue,
final int repeatMode, final int repeatMode,
final float playbackSpeed, final float playbackSpeed,
final float playbackPitch, final float playbackPitch,
final boolean playbackSkipSilence, final boolean playbackSkipSilence,
@Nullable final String playbackQuality, @Nullable final String playbackQuality,
final boolean resumePlayback, final boolean resumePlayback,
final boolean startPaused, final boolean startPaused,
final boolean isMuted) { final boolean isMuted) {
return getPlayerIntent(context, targetClazz, playQueue, playbackQuality, resumePlayback) return getPlayerIntent(context, targetClazz, playQueue, playbackQuality, resumePlayback)
.putExtra(BasePlayer.REPEAT_MODE, repeatMode) .putExtra(BasePlayer.REPEAT_MODE, repeatMode)
.putExtra(BasePlayer.START_PAUSED, startPaused) .putExtra(BasePlayer.START_PAUSED, startPaused)
.putExtra(BasePlayer.IS_MUTED, isMuted); .putExtra(BasePlayer.IS_MUTED, isMuted);
} }
public static void playOnMainPlayer( public static void playOnMainPlayer(final AppCompatActivity activity,
final AppCompatActivity activity, final PlayQueue queue,
final PlayQueue queue, final boolean autoPlay) {
final boolean autoPlay) {
playOnMainPlayer(activity.getSupportFragmentManager(), queue, autoPlay); playOnMainPlayer(activity.getSupportFragmentManager(), queue, autoPlay);
} }
public static void playOnMainPlayer( public static void playOnMainPlayer(final FragmentManager fragmentManager,
final FragmentManager fragmentManager, final PlayQueue queue,
final PlayQueue queue, final boolean autoPlay) {
final boolean autoPlay) {
final PlayQueueItem currentStream = queue.getItem(); final PlayQueueItem currentStream = queue.getItem();
openVideoDetailFragment( openVideoDetailFragment(
fragmentManager, fragmentManager,
@ -148,7 +148,7 @@ public final class NavigationHelper {
} }
public static void playOnMainPlayer(@NonNull final Context context, public static void playOnMainPlayer(@NonNull final Context context,
@NonNull final PlayQueue queue, @Nullable final PlayQueue queue,
@NonNull final StreamingService.LinkType linkType, @NonNull final StreamingService.LinkType linkType,
@NonNull final String url, @NonNull final String url,
@NonNull final String title, @NonNull final String title,
@ -553,18 +553,14 @@ public final class NavigationHelper {
return true; return true;
} }
public static Intent getBackgroundPlayerActivityIntent(final Context context) { public static Intent getPlayQueueActivityIntent(final Context context) {
return getServicePlayerActivityIntent(context, BackgroundPlayerActivity.class); final Intent intent = new Intent(context, BackgroundPlayerActivity.class);
}
private static Intent getServicePlayerActivityIntent(final Context context,
final Class activityClass) {
final Intent intent = new Intent(context, activityClass);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} }
return intent; return intent;
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Link handling // Link handling
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/