Showing progress in notification

This commit is contained in:
ByteHamster 2019-10-05 21:58:25 +02:00
parent 570b5d47ae
commit 53ea702772
2 changed files with 35 additions and 22 deletions

View File

@ -216,6 +216,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
private PlaybackServiceFlavorHelper flavorHelper;
private PlaybackServiceStateManager stateManager;
private Disposable positionEventTimer;
private PlaybackServiceNotificationBuilder notificationBuilder;
/**
* Used for Lollipop notifications, Android Wear, and Android Auto.
@ -271,7 +272,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
isRunning = true;
stateManager = new PlaybackServiceStateManager(this);
PlaybackServiceNotificationBuilder notificationBuilder = new PlaybackServiceNotificationBuilder(this);
notificationBuilder = new PlaybackServiceNotificationBuilder(this);
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
registerReceiver(autoStateUpdated, new IntentFilter("com.google.android.gms.car.media.STATUS"));
@ -444,20 +445,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
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());
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancel(NOTIFICATION_ID_STREAMING);
@ -1217,9 +1207,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
return;
}
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
PlaybackServiceNotificationBuilder notificationBuilder =
new PlaybackServiceNotificationBuilder(PlaybackService.this);
notificationBuilder.addMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting);
notificationBuilder = new PlaybackServiceNotificationBuilder(PlaybackService.this);
notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting);
if (Build.VERSION.SDK_INT < 29) {
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
}
if (!notificationBuilder.isIconCached(playable)) {
// 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");
positionEventTimer = Observable.interval(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aLong ->
EventBus.getDefault().post(new PlaybackPositionEvent(getCurrentPosition(), getDuration())));
.subscribe(number -> {
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() {

View File

@ -10,12 +10,12 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
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.util.Log;
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.request.RequestOptions;
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.preferences.UserPreferences;
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.TimeSpeedConverter;
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
import de.danoeh.antennapod.core.util.playback.Playable;
@ -32,6 +34,7 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
private static Bitmap defaultIcon = null;
private Context context;
private boolean actionsInitialized = false;
public PlaybackServiceNotificationBuilder(@NonNull Context context) {
super(context, NotificationUtils.CHANNEL_ID_PLAYING);
@ -50,9 +53,10 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
setWhen(0); // we don't need the time
setSmallIcon(smallIcon);
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);
setContentTitle(playable.getFeedTitle());
setContentText(playable.getEpisodeTitle());
@ -62,6 +66,11 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
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) {
int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
try {
@ -131,6 +140,10 @@ public class PlaybackServiceNotificationBuilder extends NotificationCompat.Build
}
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();
int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction