Remove dependency of FeedMedia to DbWriter

This commit is contained in:
ByteHamster 2021-04-22 22:27:39 +02:00
parent 09d402a945
commit 2a47f49fde
5 changed files with 31 additions and 37 deletions

View File

@ -1,7 +1,6 @@
package de.danoeh.antennapod.core.feed;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
@ -14,7 +13,6 @@ import android.support.v4.media.MediaDescriptionCompat;
import java.util.Date;
import java.util.List;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.playback.Playable;
public class FeedMedia extends FeedFile implements Playable {
@ -171,6 +169,10 @@ public class FeedMedia extends FeedFile implements Playable {
return played_duration;
}
public int getPlayedDurationWhenStarted() {
return playedDurationWhenStarted;
}
public void setPlayedDuration(int played_duration) {
this.played_duration = played_duration;
}
@ -373,19 +375,6 @@ public class FeedMedia extends FeedFile implements Playable {
return itemID;
}
@Override
public void saveCurrentPosition(SharedPreferences pref, int newPosition, long timeStamp) {
if(item != null && item.isNew()) {
DBWriter.markItemPlayed(FeedItem.UNPLAYED, item.getId());
}
setPosition(newPosition);
setLastPlayedTime(timeStamp);
if(startPosition>=0 && position > startPosition) {
setPlayedDuration(playedDurationWhenStarted + position - startPosition);
}
DBWriter.setFeedMediaPlaybackInformation(this);
}
@Override
public void onPlaybackStart() {
startPosition = Math.max(position, 0);

View File

@ -1450,10 +1450,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
if (position != INVALID_TIME && duration != INVALID_TIME && playable != null) {
Log.d(TAG, "Saving current position to " + position);
playable.saveCurrentPosition(
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()),
position,
System.currentTimeMillis());
PlayableUtils.saveCurrentPosition(playable, position, System.currentTimeMillis());
}
}

View File

@ -101,17 +101,6 @@ public interface Playable extends Parcelable {
*/
boolean localFileAvailable();
/**
* Saves the current position of this object. Implementations can use the
* provided SharedPreference to save this information and retrieve it later
* via PlayableUtils.createInstanceFromPreferences.
*
* @param pref shared prefs that might be used to store this object
* @param newPosition new playback position in ms
* @param timestamp current time in ms
*/
void saveCurrentPosition(SharedPreferences pref, int newPosition, long timestamp);
void setPosition(int newPosition);
void setDuration(int newDuration);

View File

@ -8,9 +8,11 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
/**
* Provides utility methods for Playable objects.
@ -70,4 +72,28 @@ public abstract class PlayableUtils {
}
return result;
}
/**
* Saves the current position of this object.
*
* @param newPosition new playback position in ms
* @param timestamp current time in ms
*/
public static void saveCurrentPosition(Playable playable, int newPosition, long timestamp) {
playable.setPosition(newPosition);
playable.setLastPlayedTime(timestamp);
if (playable instanceof FeedMedia) {
FeedMedia media = (FeedMedia) playable;
FeedItem item = media.getItem();
if (item != null && item.isNew()) {
DBWriter.markItemPlayed(FeedItem.UNPLAYED, item.getId());
}
if (media.getStartPosition() >= 0 && playable.getPosition() > media.getStartPosition()) {
media.setPlayedDuration(media.getPlayedDurationWhenStarted()
+ playable.getPosition() - media.getStartPosition());
}
DBWriter.setFeedMediaPlaybackInformation(media);
}
}
}

View File

@ -188,13 +188,6 @@ public class RemoteMedia implements Playable {
return false;
}
@Override
public void saveCurrentPosition(SharedPreferences pref, int newPosition, long timestamp) {
//we're not saving playback information for this kind of items on preferences
setPosition(newPosition);
setLastPlayedTime(timestamp);
}
@Override
public void setPosition(int newPosition) {
position = newPosition;