2022-04-14 18:40:55 +02:00
|
|
|
package org.schabi.newpipe.player.notification;
|
2020-03-03 00:35:44 +01:00
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Intent;
|
2020-09-03 21:49:21 +02:00
|
|
|
import android.content.pm.ServiceInfo;
|
2020-03-03 00:35:44 +01:00
|
|
|
import android.graphics.Bitmap;
|
2020-09-03 21:49:21 +02:00
|
|
|
import android.os.Build;
|
2020-03-03 00:35:44 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
import androidx.annotation.DrawableRes;
|
|
|
|
import androidx.annotation.Nullable;
|
2020-09-22 16:46:09 +02:00
|
|
|
import androidx.annotation.StringRes;
|
2020-03-03 00:35:44 +01:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2020-09-16 14:00:22 +02:00
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
2020-12-19 12:22:17 +01:00
|
|
|
import androidx.core.app.ServiceCompat;
|
2020-03-03 00:35:44 +01:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
import org.schabi.newpipe.MainActivity;
|
2020-03-03 00:35:44 +01:00
|
|
|
import org.schabi.newpipe.R;
|
2022-04-14 18:40:55 +02:00
|
|
|
import org.schabi.newpipe.player.Player;
|
2022-07-21 18:00:43 +02:00
|
|
|
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
|
2020-03-03 00:35:44 +01:00
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
|
|
|
|
2020-09-08 19:02:05 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
2022-07-21 18:00:43 +02:00
|
|
|
import static androidx.media.app.NotificationCompat.MediaStyle;
|
2020-08-02 22:59:43 +02:00
|
|
|
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
|
|
|
|
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE;
|
2022-04-14 18:40:55 +02:00
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_FORWARD;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_REWIND;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_NEXT;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PAUSE;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PREVIOUS;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT;
|
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE;
|
2020-03-03 00:35:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a utility class for player notifications.
|
|
|
|
*
|
|
|
|
* @author cool-student
|
|
|
|
*/
|
|
|
|
public final class NotificationUtil {
|
2020-09-08 21:19:43 +02:00
|
|
|
private static final String TAG = NotificationUtil.class.getSimpleName();
|
2021-01-08 18:35:33 +01:00
|
|
|
private static final boolean DEBUG = Player.DEBUG;
|
2020-08-02 22:59:43 +02:00
|
|
|
private static final int NOTIFICATION_ID = 123789;
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-08 19:02:05 +02:00
|
|
|
@NotificationConstants.Action
|
2020-11-18 23:50:00 +01:00
|
|
|
private final int[] notificationSlots = NotificationConstants.SLOT_DEFAULTS.clone();
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-16 14:00:22 +02:00
|
|
|
private NotificationManagerCompat notificationManager;
|
2020-08-02 22:59:43 +02:00
|
|
|
private NotificationCompat.Builder notificationBuilder;
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2022-04-14 18:40:55 +02:00
|
|
|
private final Player player;
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
public NotificationUtil(final Player player) {
|
|
|
|
this.player = player;
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
// NOTIFICATION
|
|
|
|
/////////////////////////////////////////////////////
|
2020-03-03 00:35:44 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-08 21:19:43 +02:00
|
|
|
* Creates the notification if it does not exist already and recreates it if forceRecreate is
|
|
|
|
* true. Updates the notification with the data in the player.
|
2020-08-15 19:15:08 +02:00
|
|
|
* @param forceRecreate whether to force the recreation of the notification even if it already
|
|
|
|
* exists
|
|
|
|
*/
|
2022-04-14 18:14:28 +02:00
|
|
|
public synchronized void createNotificationIfNeededAndUpdate(final boolean forceRecreate) {
|
2020-09-10 18:47:22 +02:00
|
|
|
if (forceRecreate || notificationBuilder == null) {
|
2022-04-14 18:14:28 +02:00
|
|
|
notificationBuilder = createNotification();
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
2022-04-14 18:14:28 +02:00
|
|
|
updateNotification();
|
2020-09-16 14:00:22 +02:00
|
|
|
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
private synchronized NotificationCompat.Builder createNotification() {
|
2020-09-10 18:47:22 +02:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "createNotification()");
|
|
|
|
}
|
2021-01-08 18:35:33 +01:00
|
|
|
notificationManager = NotificationManagerCompat.from(player.getContext());
|
|
|
|
final NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(player.getContext(),
|
|
|
|
player.getContext().getString(R.string.notification_channel_id));
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
initializeNotificationSlots();
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
// count the number of real slots, to make sure compact slots indices are not out of bound
|
|
|
|
int nonNothingSlotCount = 5;
|
|
|
|
if (notificationSlots[3] == NotificationConstants.NOTHING) {
|
|
|
|
--nonNothingSlotCount;
|
|
|
|
}
|
|
|
|
if (notificationSlots[4] == NotificationConstants.NOTHING) {
|
|
|
|
--nonNothingSlotCount;
|
2020-08-15 19:15:08 +02:00
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-08 19:02:05 +02:00
|
|
|
// build the compact slot indices array (need code to convert from Integer... because Java)
|
|
|
|
final List<Integer> compactSlotList = NotificationConstants.getCompactSlotsFromPreferences(
|
2021-01-08 18:35:33 +01:00
|
|
|
player.getContext(), player.getPrefs(), nonNothingSlotCount);
|
2022-08-05 03:24:03 +02:00
|
|
|
final int[] compactSlots = compactSlotList.stream().mapToInt(Integer::intValue).toArray();
|
2020-09-03 21:49:21 +02:00
|
|
|
|
2022-07-21 18:00:43 +02:00
|
|
|
final MediaStyle mediaStyle = new MediaStyle().setShowActionsInCompactView(compactSlots);
|
|
|
|
player.UIs().get(MediaSessionPlayerUi.class).flatMap(MediaSessionPlayerUi::getSessionToken)
|
|
|
|
.ifPresent(mediaStyle::setMediaSession);
|
|
|
|
|
|
|
|
builder.setStyle(mediaStyle)
|
2020-09-03 21:49:21 +02:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
2020-08-15 19:15:08 +02:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
2020-09-16 14:00:22 +02:00
|
|
|
.setCategory(NotificationCompat.CATEGORY_TRANSPORT)
|
2020-10-05 15:57:14 +02:00
|
|
|
.setShowWhen(false)
|
|
|
|
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
|
2021-01-08 18:35:33 +01:00
|
|
|
.setColor(ContextCompat.getColor(player.getContext(),
|
|
|
|
R.color.dark_background_color))
|
|
|
|
.setColorized(player.getPrefs().getBoolean(
|
|
|
|
player.getContext().getString(R.string.notification_colorize_key), true))
|
|
|
|
.setDeleteIntent(PendingIntent.getBroadcast(player.getContext(), NOTIFICATION_ID,
|
2020-09-03 21:49:21 +02:00
|
|
|
new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT));
|
2020-08-02 22:59:43 +02:00
|
|
|
|
2020-08-15 19:15:08 +02:00
|
|
|
return builder;
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-09-03 21:49:21 +02:00
|
|
|
/**
|
2020-09-16 14:00:22 +02:00
|
|
|
* Updates the notification builder and the button icons depending on the playback state.
|
2020-09-03 21:49:21 +02:00
|
|
|
*/
|
2022-04-14 18:14:28 +02:00
|
|
|
private synchronized void updateNotification() {
|
2020-09-03 21:49:21 +02:00
|
|
|
if (DEBUG) {
|
2020-09-10 18:47:22 +02:00
|
|
|
Log.d(TAG, "updateNotification()");
|
2020-09-03 21:49:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 21:42:25 +02:00
|
|
|
// also update content intent, in case the user switched players
|
2021-01-08 18:35:33 +01:00
|
|
|
notificationBuilder.setContentIntent(PendingIntent.getActivity(player.getContext(),
|
2022-04-16 12:03:59 +02:00
|
|
|
NOTIFICATION_ID, getIntentForNotification(), FLAG_UPDATE_CURRENT));
|
2020-09-03 21:49:21 +02:00
|
|
|
notificationBuilder.setContentTitle(player.getVideoTitle());
|
|
|
|
notificationBuilder.setContentText(player.getUploaderName());
|
2020-09-16 14:00:22 +02:00
|
|
|
notificationBuilder.setTicker(player.getVideoTitle());
|
2022-04-14 18:14:28 +02:00
|
|
|
updateActions(notificationBuilder);
|
2021-01-08 18:35:33 +01:00
|
|
|
final boolean showThumbnail = player.getPrefs().getBoolean(
|
|
|
|
player.getContext().getString(R.string.show_thumbnail_key), true);
|
2020-11-14 10:01:07 +01:00
|
|
|
if (showThumbnail) {
|
2022-04-14 18:14:28 +02:00
|
|
|
setLargeIcon(notificationBuilder);
|
2020-11-14 10:01:07 +01:00
|
|
|
}
|
2020-09-03 21:49:21 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-22 17:36:39 +02:00
|
|
|
@SuppressLint("RestrictedApi")
|
2022-04-14 18:14:28 +02:00
|
|
|
public boolean shouldUpdateBufferingSlot() {
|
2020-10-05 15:53:50 +02:00
|
|
|
if (notificationBuilder == null) {
|
|
|
|
// if there is no notification active, there is no point in updating it
|
|
|
|
return false;
|
|
|
|
} else if (notificationBuilder.mActions.size() < 3) {
|
2020-09-22 17:36:39 +02:00
|
|
|
// this should never happen, but let's make sure notification actions are populated
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only second and third slot could contain PLAY_PAUSE_BUFFERING, update them only if they
|
|
|
|
// are not already in the buffering state (the only one with a null action intent)
|
|
|
|
return (notificationSlots[1] == NotificationConstants.PLAY_PAUSE_BUFFERING
|
|
|
|
&& notificationBuilder.mActions.get(1).actionIntent != null)
|
|
|
|
|| (notificationSlots[2] == NotificationConstants.PLAY_PAUSE_BUFFERING
|
|
|
|
&& notificationBuilder.mActions.get(2).actionIntent != null);
|
2020-09-08 21:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
public void createNotificationAndStartForeground() {
|
2020-09-16 14:00:22 +02:00
|
|
|
if (notificationBuilder == null) {
|
2022-04-14 18:14:28 +02:00
|
|
|
notificationBuilder = createNotification();
|
2020-09-16 14:00:22 +02:00
|
|
|
}
|
2022-04-14 18:14:28 +02:00
|
|
|
updateNotification();
|
2020-09-08 21:19:43 +02:00
|
|
|
|
2020-09-03 21:49:21 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
2022-04-14 18:14:28 +02:00
|
|
|
player.getService().startForeground(NOTIFICATION_ID, notificationBuilder.build(),
|
2020-09-03 21:49:21 +02:00
|
|
|
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
|
|
|
|
} else {
|
2022-04-14 18:14:28 +02:00
|
|
|
player.getService().startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
2020-09-03 21:49:21 +02:00
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
public void cancelNotificationAndStopForeground() {
|
|
|
|
ServiceCompat.stopForeground(player.getService(), ServiceCompat.STOP_FOREGROUND_REMOVE);
|
2020-09-16 14:00:22 +02:00
|
|
|
|
|
|
|
if (notificationManager != null) {
|
|
|
|
notificationManager.cancel(NOTIFICATION_ID);
|
2020-08-02 22:59:43 +02:00
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
notificationManager = null;
|
|
|
|
notificationBuilder = null;
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
// ACTIONS
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
private void initializeNotificationSlots() {
|
2020-09-08 19:02:05 +02:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
2021-01-08 18:35:33 +01:00
|
|
|
notificationSlots[i] = player.getPrefs().getInt(
|
|
|
|
player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
|
2020-09-08 19:02:05 +02:00
|
|
|
NotificationConstants.SLOT_DEFAULTS[i]);
|
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
@SuppressLint("RestrictedApi")
|
2022-04-14 18:14:28 +02:00
|
|
|
private void updateActions(final NotificationCompat.Builder builder) {
|
2020-08-15 23:45:23 +02:00
|
|
|
builder.mActions.clear();
|
2020-09-08 19:02:05 +02:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
2022-04-14 18:40:55 +02:00
|
|
|
addAction(builder, notificationSlots[i]);
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
2020-08-15 23:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addAction(final NotificationCompat.Builder builder,
|
2020-09-08 19:02:05 +02:00
|
|
|
@NotificationConstants.Action final int slot) {
|
2022-04-14 18:14:28 +02:00
|
|
|
final NotificationCompat.Action action = getAction(slot);
|
2020-08-15 23:45:23 +02:00
|
|
|
if (action != null) {
|
|
|
|
builder.addAction(action);
|
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 23:45:23 +02:00
|
|
|
@Nullable
|
2020-09-08 19:02:05 +02:00
|
|
|
private NotificationCompat.Action getAction(
|
|
|
|
@NotificationConstants.Action final int selectedAction) {
|
|
|
|
final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction];
|
|
|
|
switch (selectedAction) {
|
|
|
|
case NotificationConstants.PREVIOUS:
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(baseActionIcon,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.NEXT:
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(baseActionIcon,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_next_description, ACTION_PLAY_NEXT);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.REWIND:
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(baseActionIcon,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_rewind_description, ACTION_FAST_REWIND);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.FORWARD:
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(baseActionIcon,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.SMART_REWIND_PREVIOUS:
|
2021-01-08 18:35:33 +01:00
|
|
|
if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_notification_previous,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS);
|
2020-08-02 22:59:43 +02:00
|
|
|
} else {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_controls_rewind,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_rewind_description, ACTION_FAST_REWIND);
|
2020-08-02 22:59:43 +02:00
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.SMART_FORWARD_NEXT:
|
2021-01-08 18:35:33 +01:00
|
|
|
if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_notification_next,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_next_description, ACTION_PLAY_NEXT);
|
2020-08-02 22:59:43 +02:00
|
|
|
} else {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_controls_fastforward,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD);
|
2020-08-02 22:59:43 +02:00
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.PLAY_PAUSE_BUFFERING:
|
2021-01-08 18:35:33 +01:00
|
|
|
if (player.getCurrentState() == Player.STATE_PREFLIGHT
|
|
|
|
|| player.getCurrentState() == Player.STATE_BLOCKED
|
|
|
|
|| player.getCurrentState() == Player.STATE_BUFFERING) {
|
2020-09-22 17:36:39 +02:00
|
|
|
// null intent -> show hourglass icon that does nothing when clicked
|
2021-03-27 15:45:49 +01:00
|
|
|
return new NotificationCompat.Action(R.drawable.ic_hourglass_top,
|
2021-01-08 18:35:33 +01:00
|
|
|
player.getContext().getString(R.string.notification_action_buffering),
|
2020-09-22 17:36:39 +02:00
|
|
|
null);
|
2020-09-22 18:12:51 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 16:01:23 +02:00
|
|
|
// fallthrough
|
2020-09-22 18:12:51 +02:00
|
|
|
case NotificationConstants.PLAY_PAUSE:
|
2021-01-08 18:35:33 +01:00
|
|
|
if (player.getCurrentState() == Player.STATE_COMPLETED) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.ic_replay,
|
2020-09-22 18:12:51 +02:00
|
|
|
R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE);
|
|
|
|
} else if (player.isPlaying()
|
2021-01-08 18:35:33 +01:00
|
|
|
|| player.getCurrentState() == Player.STATE_PREFLIGHT
|
|
|
|
|| player.getCurrentState() == Player.STATE_BLOCKED
|
|
|
|
|| player.getCurrentState() == Player.STATE_BUFFERING) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_notification_pause,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE);
|
2020-09-08 19:02:05 +02:00
|
|
|
} else {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_notification_play,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_play_description, ACTION_PLAY_PAUSE);
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case NotificationConstants.REPEAT:
|
2020-09-22 16:46:09 +02:00
|
|
|
if (player.getRepeatMode() == REPEAT_MODE_ALL) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_media_action_repeat_all,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_repeat_all_description, ACTION_REPEAT);
|
|
|
|
} else if (player.getRepeatMode() == REPEAT_MODE_ONE) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_media_action_repeat_one,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_repeat_one_description, ACTION_REPEAT);
|
|
|
|
} else /* player.getRepeatMode() == REPEAT_MODE_OFF */ {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_media_action_repeat_off,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_repeat_off_description, ACTION_REPEAT);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.SHUFFLE:
|
2021-01-08 18:35:33 +01:00
|
|
|
if (player.getPlayQueue() != null && player.getPlayQueue().isShuffled()) {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_controls_shuffle_on,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_shuffle_on_description, ACTION_SHUFFLE);
|
|
|
|
} else {
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.exo_controls_shuffle_off,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.exo_controls_shuffle_off_description, ACTION_SHUFFLE);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.CLOSE:
|
2022-04-14 18:14:28 +02:00
|
|
|
return getAction(R.drawable.ic_close,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.close, ACTION_CLOSE);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.NOTHING:
|
2020-08-02 22:59:43 +02:00
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
return null;
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
private NotificationCompat.Action getAction(@DrawableRes final int drawable,
|
2020-09-22 16:46:09 +02:00
|
|
|
@StringRes final int title,
|
2020-08-02 22:59:43 +02:00
|
|
|
final String intentAction) {
|
2021-01-08 18:35:33 +01:00
|
|
|
return new NotificationCompat.Action(drawable, player.getContext().getString(title),
|
|
|
|
PendingIntent.getBroadcast(player.getContext(), NOTIFICATION_ID,
|
2020-09-22 16:46:09 +02:00
|
|
|
new Intent(intentAction), FLAG_UPDATE_CURRENT));
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2022-04-16 12:03:59 +02:00
|
|
|
private Intent getIntentForNotification() {
|
2020-08-02 22:59:43 +02:00
|
|
|
if (player.audioPlayerSelected() || player.popupPlayerSelected()) {
|
2020-09-08 21:42:25 +02:00
|
|
|
// Means we play in popup or audio only. Let's show the play queue
|
2021-01-08 18:35:33 +01:00
|
|
|
return NavigationHelper.getPlayQueueActivityIntent(player.getContext());
|
2020-08-02 22:59:43 +02:00
|
|
|
} else {
|
|
|
|
// We are playing in fragment. Don't open another activity just show fragment. That's it
|
2020-09-08 21:42:25 +02:00
|
|
|
final Intent intent = NavigationHelper.getPlayerIntent(
|
2021-01-08 18:35:33 +01:00
|
|
|
player.getContext(), MainActivity.class, null, true);
|
2020-08-02 22:59:43 +02:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
intent.setAction(Intent.ACTION_MAIN);
|
|
|
|
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
2020-09-08 21:42:25 +02:00
|
|
|
return intent;
|
2020-08-02 22:59:43 +02:00
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
// BITMAP
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
|
2022-04-14 18:14:28 +02:00
|
|
|
private void setLargeIcon(final NotificationCompat.Builder builder) {
|
2021-01-08 18:35:33 +01:00
|
|
|
final boolean scaleImageToSquareAspectRatio = player.getPrefs().getBoolean(
|
|
|
|
player.getContext().getString(R.string.scale_to_square_image_in_notifications_key),
|
2020-09-03 21:49:21 +02:00
|
|
|
false);
|
|
|
|
if (scaleImageToSquareAspectRatio) {
|
|
|
|
builder.setLargeIcon(getBitmapWithSquareAspectRatio(player.getThumbnail()));
|
|
|
|
} else {
|
|
|
|
builder.setLargeIcon(player.getThumbnail());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
private Bitmap getBitmapWithSquareAspectRatio(final Bitmap bitmap) {
|
2022-07-13 17:19:44 +02:00
|
|
|
// Find the smaller dimension and then take a center portion of the image that
|
|
|
|
// has that size.
|
|
|
|
final int w = bitmap.getWidth();
|
|
|
|
final int h = bitmap.getHeight();
|
|
|
|
final int dstSize = Math.min(w, h);
|
|
|
|
final int x = (w - dstSize) / 2;
|
|
|
|
final int y = (h - dstSize) / 2;
|
|
|
|
return Bitmap.createBitmap(bitmap, x, y, dstSize, dstSize);
|
2020-08-02 22:59:43 +02:00
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|