Showing progress in notification
This commit is contained in:
parent
570b5d47ae
commit
53ea702772
@ -216,6 +216,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
|||||||
private PlaybackServiceFlavorHelper flavorHelper;
|
private PlaybackServiceFlavorHelper flavorHelper;
|
||||||
private PlaybackServiceStateManager stateManager;
|
private PlaybackServiceStateManager stateManager;
|
||||||
private Disposable positionEventTimer;
|
private Disposable positionEventTimer;
|
||||||
|
private PlaybackServiceNotificationBuilder notificationBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used for Lollipop notifications, Android Wear, and Android Auto.
|
* Used for Lollipop notifications, Android Wear, and Android Auto.
|
||||||
@ -271,7 +272,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
|||||||
isRunning = true;
|
isRunning = true;
|
||||||
|
|
||||||
stateManager = new PlaybackServiceStateManager(this);
|
stateManager = new PlaybackServiceStateManager(this);
|
||||||
PlaybackServiceNotificationBuilder notificationBuilder = new PlaybackServiceNotificationBuilder(this);
|
notificationBuilder = new PlaybackServiceNotificationBuilder(this);
|
||||||
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
||||||
|
|
||||||
registerReceiver(autoStateUpdated, new IntentFilter("com.google.android.gms.car.media.STATUS"));
|
registerReceiver(autoStateUpdated, new IntentFilter("com.google.android.gms.car.media.STATUS"));
|
||||||
@ -444,20 +445,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
|||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
super.onStartCommand(intent, flags, startId);
|
super.onStartCommand(intent, flags, startId);
|
||||||
|
|
||||||
Log.d(TAG, "OnStartCommand called");
|
Log.d(TAG, "OnStartCommand called");
|
||||||
|
|
||||||
if (!stateManager.isInForeground()) {
|
|
||||||
PlaybackServiceNotificationBuilder notificationBuilder = new PlaybackServiceNotificationBuilder(this);
|
|
||||||
if (mediaPlayer != null && getPlayable() != null) {
|
|
||||||
notificationBuilder.addMetadata(getPlayable(), mediaSession.getSessionToken(), getStatus(), isCasting);
|
|
||||||
if (notificationBuilder.isIconCached(getPlayable())) {
|
|
||||||
notificationBuilder.loadIcon(getPlayable());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
||||||
}
|
|
||||||
|
|
||||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
||||||
notificationManager.cancel(NOTIFICATION_ID_STREAMING);
|
notificationManager.cancel(NOTIFICATION_ID_STREAMING);
|
||||||
|
|
||||||
@ -1217,9 +1207,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
|
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
|
||||||
PlaybackServiceNotificationBuilder notificationBuilder =
|
notificationBuilder = new PlaybackServiceNotificationBuilder(PlaybackService.this);
|
||||||
new PlaybackServiceNotificationBuilder(PlaybackService.this);
|
notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting);
|
||||||
notificationBuilder.addMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting);
|
|
||||||
|
if (Build.VERSION.SDK_INT < 29) {
|
||||||
|
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
|
||||||
|
}
|
||||||
|
|
||||||
if (!notificationBuilder.isIconCached(playable)) {
|
if (!notificationBuilder.isIconCached(playable)) {
|
||||||
// To make sure that the notification is shown instantly
|
// To make sure that the notification is shown instantly
|
||||||
@ -1592,8 +1585,15 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
|||||||
Log.d(TAG, "Setting up position observer");
|
Log.d(TAG, "Setting up position observer");
|
||||||
positionEventTimer = Observable.interval(1, TimeUnit.SECONDS)
|
positionEventTimer = Observable.interval(1, TimeUnit.SECONDS)
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(aLong ->
|
.subscribe(number -> {
|
||||||
EventBus.getDefault().post(new PlaybackPositionEvent(getCurrentPosition(), getDuration())));
|
EventBus.getDefault().post(new PlaybackPositionEvent(getCurrentPosition(), getDuration()));
|
||||||
|
if (Build.VERSION.SDK_INT < 29) {
|
||||||
|
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
|
||||||
|
NotificationManager notificationManager = (NotificationManager)
|
||||||
|
getSystemService(NOTIFICATION_SERVICE);
|
||||||
|
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cancelPositionObserver() {
|
private void cancelPositionObserver() {
|
||||||
|
@ -10,12 +10,12 @@ import android.graphics.drawable.BitmapDrawable;
|
|||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.graphics.drawable.VectorDrawable;
|
import android.graphics.drawable.VectorDrawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.core.app.NotificationCompat;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import android.support.v4.media.session.MediaSessionCompat;
|
import android.support.v4.media.session.MediaSessionCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.request.RequestOptions;
|
import com.bumptech.glide.request.RequestOptions;
|
||||||
import de.danoeh.antennapod.core.ClientConfig;
|
import de.danoeh.antennapod.core.ClientConfig;
|
||||||
@ -23,7 +23,9 @@ import de.danoeh.antennapod.core.R;
|
|||||||
import de.danoeh.antennapod.core.glide.ApGlideSettings;
|
import de.danoeh.antennapod.core.glide.ApGlideSettings;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
|
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
|
||||||
|
import de.danoeh.antennapod.core.util.Converter;
|
||||||
import de.danoeh.antennapod.core.util.IntList;
|
import de.danoeh.antennapod.core.util.IntList;
|
||||||
|
import de.danoeh.antennapod.core.util.TimeSpeedConverter;
|
||||||
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
|
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
|
||||||
import de.danoeh.antennapod.core.util.playback.Playable;
|
import de.danoeh.antennapod.core.util.playback.Playable;
|
||||||
|
|
||||||
@ -32,6 +34,7 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
|
|||||||
private static Bitmap defaultIcon = null;
|
private static Bitmap defaultIcon = null;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private boolean actionsInitialized = false;
|
||||||
|
|
||||||
public PlaybackServiceNotificationBuilder(@NonNull Context context) {
|
public PlaybackServiceNotificationBuilder(@NonNull Context context) {
|
||||||
super(context, NotificationUtils.CHANNEL_ID_PLAYING);
|
super(context, NotificationUtils.CHANNEL_ID_PLAYING);
|
||||||
@ -50,9 +53,10 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
|
|||||||
setWhen(0); // we don't need the time
|
setWhen(0); // we don't need the time
|
||||||
setSmallIcon(smallIcon);
|
setSmallIcon(smallIcon);
|
||||||
setPriority(NotificationCompat.PRIORITY_MIN);
|
setPriority(NotificationCompat.PRIORITY_MIN);
|
||||||
|
setOnlyAlertOnce(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMetadata(Playable playable, MediaSessionCompat.Token mediaSessionToken, PlayerStatus playerStatus, boolean isCasting) {
|
public void setMetadata(Playable playable, MediaSessionCompat.Token mediaSessionToken, PlayerStatus playerStatus, boolean isCasting) {
|
||||||
Log.v(TAG, "notificationSetupTask: playerStatus=" + playerStatus);
|
Log.v(TAG, "notificationSetupTask: playerStatus=" + playerStatus);
|
||||||
setContentTitle(playable.getFeedTitle());
|
setContentTitle(playable.getFeedTitle());
|
||||||
setContentText(playable.getEpisodeTitle());
|
setContentText(playable.getEpisodeTitle());
|
||||||
@ -62,6 +66,11 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
|
|||||||
setColor(NotificationCompat.COLOR_DEFAULT);
|
setColor(NotificationCompat.COLOR_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updatePosition(int position,float speed) {
|
||||||
|
TimeSpeedConverter converter = new TimeSpeedConverter(speed);
|
||||||
|
setSubText(Converter.getDurationStringLong(converter.convert(position)));
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isIconCached(Playable playable) {
|
public boolean isIconCached(Playable playable) {
|
||||||
int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
|
int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
|
||||||
try {
|
try {
|
||||||
@ -131,6 +140,10 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addActions(MediaSessionCompat.Token mediaSessionToken, PlayerStatus playerStatus, boolean isCasting) {
|
private void addActions(MediaSessionCompat.Token mediaSessionToken, PlayerStatus playerStatus, boolean isCasting) {
|
||||||
|
if (actionsInitialized) {
|
||||||
|
throw new IllegalStateException("Notification actions must not be added multiple times");
|
||||||
|
}
|
||||||
|
actionsInitialized = true;
|
||||||
IntList compactActionList = new IntList();
|
IntList compactActionList = new IntList();
|
||||||
|
|
||||||
int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction
|
int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction
|
||||||
|
Loading…
x
Reference in New Issue
Block a user