2020-03-03 00:35:44 +01:00
|
|
|
package org.schabi.newpipe.player;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.app.PendingIntent;
|
2020-08-02 22:59:43 +02:00
|
|
|
import android.app.Service;
|
2020-03-03 00:35:44 +01:00
|
|
|
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;
|
|
|
|
import android.graphics.Matrix;
|
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-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;
|
|
|
|
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;
|
|
|
|
import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL;
|
|
|
|
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_CLOSE;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.ACTION_FAST_FORWARD;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.ACTION_FAST_REWIND;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_NEXT;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_PAUSE;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.ACTION_PLAY_PREVIOUS;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.ACTION_REPEAT;
|
|
|
|
import static org.schabi.newpipe.player.MainPlayer.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();
|
2020-03-03 00:35:44 +01:00
|
|
|
private static final boolean DEBUG = BasePlayer.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-08-02 22:59:43 +02:00
|
|
|
@Nullable private static NotificationUtil instance = null;
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-08 19:02:05 +02:00
|
|
|
@NotificationConstants.Action
|
|
|
|
private 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
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
private NotificationUtil() {
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
public static NotificationUtil getInstance() {
|
|
|
|
if (instance == null) {
|
|
|
|
instance = new NotificationUtil();
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
2020-08-02 22:59:43 +02:00
|
|
|
return instance;
|
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 player the player currently open, to take data from
|
|
|
|
* @param forceRecreate whether to force the recreation of the notification even if it already
|
|
|
|
* exists
|
|
|
|
*/
|
2020-09-08 21:19:43 +02:00
|
|
|
synchronized void createNotificationIfNeededAndUpdate(final VideoPlayerImpl player,
|
|
|
|
final boolean forceRecreate) {
|
2020-09-10 18:47:22 +02:00
|
|
|
if (forceRecreate || notificationBuilder == null) {
|
2020-08-15 15:16:17 +02:00
|
|
|
notificationBuilder = createNotification(player);
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
2020-09-08 21:19:43 +02:00
|
|
|
updateNotification(player);
|
2020-09-16 14:00:22 +02:00
|
|
|
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 21:19:43 +02:00
|
|
|
private synchronized NotificationCompat.Builder createNotification(
|
|
|
|
final VideoPlayerImpl player) {
|
2020-09-10 18:47:22 +02:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "createNotification()");
|
|
|
|
}
|
2020-09-16 14:00:22 +02:00
|
|
|
notificationManager = NotificationManagerCompat.from(player.context);
|
2020-08-15 23:45:23 +02:00
|
|
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(player.context,
|
2020-08-15 19:15:08 +02:00
|
|
|
player.context.getString(R.string.notification_channel_id));
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-08 19:02:05 +02:00
|
|
|
initializeNotificationSlots(player);
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
player.context, player.sharedPreferences, nonNothingSlotCount);
|
|
|
|
final int[] compactSlots = new int[compactSlotList.size()];
|
|
|
|
for (int i = 0; i < compactSlotList.size(); i++) {
|
|
|
|
compactSlots[i] = compactSlotList.get(i);
|
|
|
|
}
|
2020-09-03 21:49:21 +02:00
|
|
|
|
2020-08-15 19:15:08 +02:00
|
|
|
builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
|
2020-09-03 21:49:21 +02:00
|
|
|
.setMediaSession(player.mediaSessionManager.getSessionToken())
|
2020-09-08 19:02:05 +02:00
|
|
|
.setShowActionsInCompactView(compactSlots))
|
2020-09-03 21:49:21 +02:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
2020-08-15 19:15:08 +02:00
|
|
|
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
|
.setColor(ContextCompat.getColor(player.context, R.color.gray))
|
2020-09-16 14:00:22 +02:00
|
|
|
.setCategory(NotificationCompat.CATEGORY_TRANSPORT)
|
2020-09-03 21:49:21 +02:00
|
|
|
.setDeleteIntent(PendingIntent.getBroadcast(player.context, NOTIFICATION_ID,
|
|
|
|
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
|
|
|
* @param player the player currently open, to take data from
|
|
|
|
*/
|
2020-09-08 21:19:43 +02:00
|
|
|
private synchronized void updateNotification(final VideoPlayerImpl player) {
|
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
|
|
|
|
notificationBuilder.setContentIntent(PendingIntent.getActivity(player.context,
|
|
|
|
NOTIFICATION_ID, getIntentForNotification(player), 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());
|
2020-09-03 21:49:21 +02:00
|
|
|
updateActions(notificationBuilder, player);
|
|
|
|
setLargeIcon(notificationBuilder, player);
|
|
|
|
}
|
|
|
|
|
2020-03-03 00:35:44 +01:00
|
|
|
|
2020-09-08 21:19:43 +02:00
|
|
|
boolean hasSlotWithBuffering() {
|
|
|
|
return notificationSlots[1] == NotificationConstants.PLAY_PAUSE_BUFFERING
|
|
|
|
|| notificationSlots[2] == NotificationConstants.PLAY_PAUSE_BUFFERING;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void createNotificationAndStartForeground(final VideoPlayerImpl player, final Service service) {
|
2020-09-16 14:00:22 +02:00
|
|
|
if (notificationBuilder == null) {
|
|
|
|
notificationBuilder = createNotification(player);
|
|
|
|
}
|
|
|
|
updateNotification(player);
|
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) {
|
|
|
|
service.startForeground(NOTIFICATION_ID, notificationBuilder.build(),
|
|
|
|
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
|
|
|
|
} else {
|
|
|
|
service.startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 21:19:43 +02:00
|
|
|
void cancelNotificationAndStopForeground(final Service service) {
|
2020-09-16 14:00:22 +02:00
|
|
|
service.stopForeground(true);
|
|
|
|
|
|
|
|
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
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
|
2020-08-15 23:45:23 +02:00
|
|
|
private void initializeNotificationSlots(final VideoPlayerImpl player) {
|
2020-09-08 19:02:05 +02:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
notificationSlots[i] = player.sharedPreferences.getInt(
|
|
|
|
player.context.getString(NotificationConstants.SLOT_PREF_KEYS[i]),
|
|
|
|
NotificationConstants.SLOT_DEFAULTS[i]);
|
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
@SuppressLint("RestrictedApi")
|
2020-08-15 23:45:23 +02:00
|
|
|
private void updateActions(final NotificationCompat.Builder builder,
|
|
|
|
final VideoPlayerImpl player) {
|
|
|
|
builder.mActions.clear();
|
2020-09-08 19:02:05 +02:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
addAction(builder, player, notificationSlots[i]);
|
|
|
|
}
|
2020-08-15 23:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addAction(final NotificationCompat.Builder builder,
|
|
|
|
final VideoPlayerImpl player,
|
2020-09-08 19:02:05 +02:00
|
|
|
@NotificationConstants.Action final int slot) {
|
2020-08-15 23:45:23 +02:00
|
|
|
final NotificationCompat.Action action = getAction(player, slot);
|
|
|
|
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(
|
|
|
|
final VideoPlayerImpl player,
|
|
|
|
@NotificationConstants.Action final int selectedAction) {
|
|
|
|
final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction];
|
|
|
|
switch (selectedAction) {
|
|
|
|
case NotificationConstants.PREVIOUS:
|
2020-09-22 16:46:09 +02:00
|
|
|
return getAction(player, baseActionIcon,
|
|
|
|
R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.NEXT:
|
2020-09-22 16:46:09 +02:00
|
|
|
return getAction(player, baseActionIcon,
|
|
|
|
R.string.exo_controls_next_description, ACTION_PLAY_NEXT);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.REWIND:
|
2020-09-22 16:46:09 +02:00
|
|
|
return getAction(player, baseActionIcon,
|
|
|
|
R.string.exo_controls_rewind_description, ACTION_FAST_REWIND);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.FORWARD:
|
2020-09-22 16:46:09 +02:00
|
|
|
return getAction(player, baseActionIcon,
|
|
|
|
R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.SMART_REWIND_PREVIOUS:
|
2020-08-02 22:59:43 +02:00
|
|
|
if (player.playQueue != null && player.playQueue.size() > 1) {
|
2020-08-15 19:15:08 +02:00
|
|
|
return getAction(player, 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 {
|
2020-08-15 19:15:08 +02:00
|
|
|
return getAction(player, 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:
|
2020-08-02 22:59:43 +02:00
|
|
|
if (player.playQueue != null && player.playQueue.size() > 1) {
|
2020-08-15 19:15:08 +02:00
|
|
|
return getAction(player, 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 {
|
2020-08-15 19:15:08 +02:00
|
|
|
return getAction(player, 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:
|
2020-09-22 16:46:09 +02:00
|
|
|
if (player.isPlaying()
|
2020-09-08 19:02:05 +02:00
|
|
|
|| player.getCurrentState() == BasePlayer.STATE_PREFLIGHT
|
|
|
|
|| player.getCurrentState() == BasePlayer.STATE_BLOCKED
|
2020-09-22 16:46:09 +02:00
|
|
|
|| player.getCurrentState() == BasePlayer.STATE_BUFFERING) {
|
|
|
|
return getAction(player, R.drawable.exo_notification_pause,
|
|
|
|
R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE);
|
|
|
|
} else {
|
|
|
|
return getAction(player, R.drawable.exo_notification_play,
|
|
|
|
R.string.exo_controls_play_description, ACTION_PLAY_PAUSE);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.PLAY_PAUSE_BUFFERING:
|
|
|
|
if (player.getCurrentState() == BasePlayer.STATE_PREFLIGHT
|
|
|
|
|| player.getCurrentState() == BasePlayer.STATE_BLOCKED
|
|
|
|
|| player.getCurrentState() == BasePlayer.STATE_BUFFERING) {
|
|
|
|
return getAction(player, R.drawable.ic_hourglass_top_white_24dp_png,
|
2020-09-22 16:46:09 +02:00
|
|
|
R.string.notification_action_buffering, ACTION_BUFFERING);
|
|
|
|
} else if (player.isPlaying()) {
|
|
|
|
return getAction(player, R.drawable.exo_notification_pause,
|
|
|
|
R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE);
|
2020-09-08 19:02:05 +02:00
|
|
|
} else {
|
2020-09-22 16:46:09 +02:00
|
|
|
return getAction(player, R.drawable.exo_notification_play,
|
|
|
|
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) {
|
|
|
|
return getAction(player, R.drawable.exo_media_action_repeat_all,
|
|
|
|
R.string.exo_controls_repeat_all_description, ACTION_REPEAT);
|
|
|
|
} else if (player.getRepeatMode() == REPEAT_MODE_ONE) {
|
|
|
|
return getAction(player, R.drawable.exo_media_action_repeat_one,
|
|
|
|
R.string.exo_controls_repeat_one_description, ACTION_REPEAT);
|
|
|
|
} else /* player.getRepeatMode() == REPEAT_MODE_OFF */ {
|
|
|
|
return getAction(player, R.drawable.exo_media_action_repeat_off,
|
|
|
|
R.string.exo_controls_repeat_off_description, ACTION_REPEAT);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.SHUFFLE:
|
2020-09-22 16:46:09 +02:00
|
|
|
if (player.playQueue != null && player.playQueue.isShuffled()) {
|
|
|
|
return getAction(player, R.drawable.exo_controls_shuffle_on,
|
|
|
|
R.string.exo_controls_shuffle_on_description, ACTION_SHUFFLE);
|
|
|
|
} else {
|
|
|
|
return getAction(player, R.drawable.exo_controls_shuffle_off,
|
|
|
|
R.string.exo_controls_shuffle_off_description, ACTION_SHUFFLE);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
case NotificationConstants.CLOSE:
|
|
|
|
return getAction(player, R.drawable.ic_close_white_24dp_png,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 19:15:08 +02:00
|
|
|
private NotificationCompat.Action getAction(final VideoPlayerImpl player,
|
2020-08-02 22:59:43 +02:00
|
|
|
@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) {
|
2020-09-22 16:46:09 +02:00
|
|
|
return new NotificationCompat.Action(drawable, player.context.getString(title),
|
|
|
|
PendingIntent.getBroadcast(player.context, NOTIFICATION_ID,
|
|
|
|
new Intent(intentAction), FLAG_UPDATE_CURRENT));
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
private Intent getIntentForNotification(final VideoPlayerImpl player) {
|
|
|
|
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
|
|
|
|
return NavigationHelper.getPlayQueueActivityIntent(player.context);
|
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(
|
2020-08-02 22:59:43 +02:00
|
|
|
player.context, MainActivity.class, null, true);
|
|
|
|
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
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
|
2020-09-03 21:49:21 +02:00
|
|
|
private void setLargeIcon(final NotificationCompat.Builder builder,
|
|
|
|
final VideoPlayerImpl player) {
|
|
|
|
final boolean scaleImageToSquareAspectRatio = player.sharedPreferences.getBoolean(
|
|
|
|
player.context.getString(R.string.scale_to_square_image_in_notifications_key),
|
|
|
|
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) {
|
|
|
|
return getResizedBitmap(bitmap, bitmap.getWidth(), bitmap.getWidth());
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:59:43 +02:00
|
|
|
private Bitmap getResizedBitmap(final Bitmap bitmap, final int newWidth, final int newHeight) {
|
|
|
|
final int width = bitmap.getWidth();
|
|
|
|
final int height = bitmap.getHeight();
|
|
|
|
final float scaleWidth = ((float) newWidth) / width;
|
|
|
|
final float scaleHeight = ((float) newHeight) / height;
|
|
|
|
final Matrix matrix = new Matrix();
|
|
|
|
matrix.postScale(scaleWidth, scaleHeight);
|
|
|
|
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
|
|
|
|
}
|
2020-03-03 00:35:44 +01:00
|
|
|
}
|