Removed last played media preference

This commit is contained in:
daniel oeh 2013-03-08 11:43:33 +01:00
parent 1b89523bf7
commit 7f81f8a438
6 changed files with 98 additions and 134 deletions

View File

@ -213,9 +213,7 @@ public class FeedItem extends FeedComponent {
private boolean isPlaying() {
if (media != null) {
if (PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == media.getId()) {
return true;
}
return media.isPlaying();
}
return false;
}

View File

@ -157,7 +157,7 @@ public class FeedManager {
}
} catch (MediaFileNotFoundException e) {
e.printStackTrace();
if (PlaybackPreferences.getLastPlayedId() == media.getId()) {
if (media.isPlaying()) {
context.sendBroadcast(new Intent(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
}
@ -179,14 +179,20 @@ public class FeedManager {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
if (media.getId() == PlaybackPreferences.getLastPlayedId()) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PlaybackPreferences.PREF_LAST_IS_STREAM, true);
editor.commit();
}
if (PlaybackPreferences.getLastPlayedId() == media.getId()) {
context.sendBroadcast(new Intent(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA) {
if (media.getId() == PlaybackPreferences
.getCurrentlyPlayingFeedMediaId()) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(
PlaybackPreferences.PREF_CURRENT_EPISODE_IS_STREAM,
true);
editor.commit();
}
if (PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == media
.getId()) {
context.sendBroadcast(new Intent(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
}
}
}
if (AppConfig.DEBUG)
@ -198,11 +204,11 @@ public class FeedManager {
public void deleteFeed(final Context context, final Feed feed) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
if (PlaybackPreferences.getLastPlayedFeedId() == feed.getId()) {
if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA
&& PlaybackPreferences.getLastPlayedFeedId() == feed.getId()) {
context.sendBroadcast(new Intent(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
SharedPreferences.Editor editor = prefs.edit();
editor.putLong(PlaybackPreferences.PREF_LAST_PLAYED_ID, -1);
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
-1);
editor.commit();
@ -748,7 +754,7 @@ public class FeedManager {
.getApplicationContext());
if (UserPreferences.isAutoDelete()) {
if ((media.getId() != PlaybackPreferences.getLastPlayedId())
if (!media.isPlaying()
&& ((media.getId() != PlaybackPreferences
.getAutoDeleteMediaId()) || (media.getId() == PlaybackPreferences
.getAutoDeleteMediaId() && PlaybackPreferences

View File

@ -8,6 +8,7 @@ import android.content.SharedPreferences.Editor;
import android.os.Parcel;
import android.os.Parcelable;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.preferences.PlaybackPreferences;
import de.danoeh.antennapod.util.ChapterUtils;
import de.danoeh.antennapod.util.playback.Playable;
@ -103,6 +104,15 @@ public class FeedMedia extends FeedFile implements Playable {
return false;
}
/**
* Reads playback preferences to determine whether this FeedMedia object is
* currently being played.
*/
public boolean isPlaying() {
return PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA
&& PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == id;
}
@Override
public int getTypeAsInt() {
return FEEDFILETYPE_FEEDMEDIA;

View File

@ -17,9 +17,6 @@ public class PlaybackPreferences implements
SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "PlaybackPreferences";
/** Contains the type of the media that was played last. */
public static final String PREF_LAST_PLAYED_ID = "de.danoeh.antennapod.preferences.lastPlayedId";
/**
* Contains the feed id of the currently playing item if it is a FeedMedia
* object.
@ -40,10 +37,10 @@ public class PlaybackPreferences implements
public static final String PREF_CURRENTLY_PLAYING_MEDIA = "de.danoeh.antennapod.preferences.currentlyPlayingMedia";
/** True if last played media was streamed. */
public static final String PREF_LAST_IS_STREAM = "de.danoeh.antennapod.preferences.lastIsStream";
public static final String PREF_CURRENT_EPISODE_IS_STREAM = "de.danoeh.antennapod.preferences.lastIsStream";
/** True if last played media was a video. */
public static final String PREF_LAST_IS_VIDEO = "de.danoeh.antennapod.preferences.lastIsVideo";
public static final String PREF_CURRENT_EPISODE_IS_VIDEO = "de.danoeh.antennapod.preferences.lastIsVideo";
/** True if playback of last played media has been completed. */
public static final String PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED = "de.danoeh.antennapod.preferences.lastPlaybackCompleted";
@ -57,12 +54,11 @@ public class PlaybackPreferences implements
/** Value of PREF_CURRENTLY_PLAYING_MEDIA if no media is playing. */
public static final long NO_MEDIA_PLAYING = -1;
private long lastPlayedId;
private long currentlyPlayingFeedId;
private long currentlyPlayingFeedMediaId;
private long currentlyPlayingMedia;
private boolean lastIsStream;
private boolean lastIsVideo;
private boolean currentEpisodeIsStream;
private boolean currentEpisodeIsVideo;
private boolean autoDeleteMediaPlaybackCompleted;
private long autoDeleteMediaId;
@ -94,14 +90,13 @@ public class PlaybackPreferences implements
private void loadPreferences() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(context);
lastPlayedId = sp.getLong(PREF_LAST_PLAYED_ID, -1);
currentlyPlayingFeedId = sp.getLong(PREF_CURRENTLY_PLAYING_FEED_ID, -1);
currentlyPlayingFeedMediaId = sp.getLong(
PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID, NO_MEDIA_PLAYING);
currentlyPlayingMedia = sp.getLong(PREF_CURRENTLY_PLAYING_MEDIA,
NO_MEDIA_PLAYING);
lastIsStream = sp.getBoolean(PREF_LAST_IS_STREAM, true);
lastIsVideo = sp.getBoolean(PREF_LAST_IS_VIDEO, false);
currentEpisodeIsStream = sp.getBoolean(PREF_CURRENT_EPISODE_IS_STREAM, true);
currentEpisodeIsVideo = sp.getBoolean(PREF_CURRENT_EPISODE_IS_VIDEO, false);
autoDeleteMediaPlaybackCompleted = sp.getBoolean(
PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED, false);
autoDeleteMediaId = sp.getLong(PREF_AUTODELETE_MEDIA_ID, -1);
@ -109,18 +104,7 @@ public class PlaybackPreferences implements
@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
if (key.equals(PREF_LAST_PLAYED_ID)) {
lastPlayedId = sp.getLong(PREF_LAST_PLAYED_ID, -1);
long mediaId = sp.getLong(
PlaybackPreferences.PREF_AUTODELETE_MEDIA_ID, -1);
if (mediaId != -1) {
FeedManager manager = FeedManager.getInstance();
FeedMedia media = manager.getFeedMedia(mediaId);
if (media != null) {
manager.autoDeleteIfPossible(context, media);
}
}
} else if (key.equals(PREF_CURRENTLY_PLAYING_FEED_ID)) {
if (key.equals(PREF_CURRENTLY_PLAYING_FEED_ID)) {
currentlyPlayingFeedId = sp.getLong(PREF_CURRENTLY_PLAYING_FEED_ID,
-1);
@ -128,11 +112,11 @@ public class PlaybackPreferences implements
currentlyPlayingMedia = sp
.getLong(PREF_CURRENTLY_PLAYING_MEDIA, -1);
} else if (key.equals(PREF_LAST_IS_STREAM)) {
lastIsStream = sp.getBoolean(PREF_LAST_IS_STREAM, true);
} else if (key.equals(PREF_CURRENT_EPISODE_IS_STREAM)) {
currentEpisodeIsStream = sp.getBoolean(PREF_CURRENT_EPISODE_IS_STREAM, true);
} else if (key.equals(PREF_LAST_IS_VIDEO)) {
lastIsVideo = sp.getBoolean(PREF_LAST_IS_VIDEO, false);
} else if (key.equals(PREF_CURRENT_EPISODE_IS_VIDEO)) {
currentEpisodeIsVideo = sp.getBoolean(PREF_CURRENT_EPISODE_IS_VIDEO, false);
} else if (key.equals(PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED)) {
autoDeleteMediaPlaybackCompleted = sp.getBoolean(
@ -152,11 +136,6 @@ public class PlaybackPreferences implements
}
}
public static long getLastPlayedId() {
instanceAvailable();
return instance.lastPlayedId;
}
public static long getAutoDeleteMediaId() {
return instance.autoDeleteMediaId;
}
@ -175,14 +154,14 @@ public class PlaybackPreferences implements
return instance.currentlyPlayingFeedMediaId;
}
public static boolean isLastIsStream() {
public static boolean getCurrentEpisodeIsStream() {
instanceAvailable();
return instance.lastIsStream;
return instance.currentEpisodeIsStream;
}
public static boolean isLastIsVideo() {
public static boolean getCurrentEpisodeIsVideo() {
instanceAvailable();
return instance.lastIsVideo;
return instance.currentEpisodeIsVideo;
}
public static boolean isAutoDeleteMediaPlaybackCompleted() {

View File

@ -179,7 +179,7 @@ public class PlaybackService extends Service {
return new Intent(context, AudioplayerActivity.class);
}
} else {
if (PlaybackPreferences.isLastIsVideo()) {
if (PlaybackPreferences.getCurrentEpisodeIsVideo()) {
return new Intent(context, VideoplayerActivity.class);
} else {
return new Intent(context, AudioplayerActivity.class);
@ -200,35 +200,6 @@ public class PlaybackService extends Service {
}
}
/** Get last played FeedMedia object or null if it doesn't exist. */
public static FeedMedia getLastPlayedMediaFromPreferences(Context context) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
long mediaId = PlaybackPreferences.getLastPlayedId();
long feedId = PlaybackPreferences.getLastPlayedFeedId();
FeedManager manager = FeedManager.getInstance();
if (mediaId != -1 && feedId != -1) {
Feed feed = manager.getFeed(feedId);
if (feed != null) {
return manager.getFeedMedia(mediaId, feed);
}
}
return null;
}
private void setLastPlayedMediaId(long mediaId) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
long autoDeleteId = PlaybackPreferences.getAutoDeleteMediaId();
SharedPreferences.Editor editor = prefs.edit();
if (mediaId == autoDeleteId) {
editor.putBoolean(
PlaybackPreferences.PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED,
false);
}
editor.commit();
}
@SuppressLint("NewApi")
@Override
public void onCreate() {
@ -272,7 +243,8 @@ public class PlaybackService extends Service {
ACTION_SHUTDOWN_PLAYBACK_SERVICE));
registerReceiver(audioBecomingNoisy, new IntentFilter(
AudioManager.ACTION_AUDIO_BECOMING_NOISY));
registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter(ACTION_SKIP_CURRENT_EPISODE));
registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter(
ACTION_SKIP_CURRENT_EPISODE));
}
@ -711,9 +683,6 @@ public class PlaybackService extends Service {
if (AppConfig.DEBUG)
Log.d(TAG, "Playback ended");
audioManager.abandonAudioFocus(audioFocusChangeListener);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
// Save state
cancelPositionSaver();
@ -737,19 +706,7 @@ public class PlaybackService extends Service {
if (shouldStream) {
autoDeleteMediaId = -1;
}
editor.putLong(PlaybackPreferences.PREF_AUTODELETE_MEDIA_ID,
autoDeleteMediaId);
}
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_MEDIA,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putBoolean(
PlaybackPreferences.PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED,
true);
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.commit();
// Load next episode if previous episode was in the queue and if there
// is an episode in the queue left.
@ -784,6 +741,7 @@ public class PlaybackService extends Service {
} else if (media.getMediaType() == MediaType.VIDEO) {
notificationCode = EXTRA_CODE_VIDEO;
}
writePlaybackPreferences();
resetVideoSurface();
refreshRemoteControlClientState();
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, notificationCode);
@ -888,40 +846,8 @@ public class PlaybackService extends Service {
Log.d(TAG, "Audiofocus successfully requested");
if (AppConfig.DEBUG)
Log.d(TAG, "Resuming/Starting playback");
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext())
.edit();
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_MEDIA,
media.getPlayableType());
editor.putBoolean(PlaybackPreferences.PREF_LAST_IS_STREAM,
shouldStream);
editor.putBoolean(PlaybackPreferences.PREF_LAST_IS_VIDEO,
playingVideo);
editor.putLong(PlaybackPreferences.PREF_LAST_PLAYED_ID,
media.getPlayableType());
if (media instanceof FeedMedia) {
FeedMedia fMedia = (FeedMedia) media;
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
fMedia.getItem().getFeed().getId());
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
fMedia.getId());
} else {
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
}
media.writeToPreferences(editor);
writePlaybackPreferences();
editor.commit();
if (media instanceof FeedMedia) {
setLastPlayedMediaId(((FeedMedia) media).getId());
}
player.start();
if (status != PlayerStatus.PAUSED) {
player.seekTo((int) media.getPosition());
@ -945,6 +871,51 @@ public class PlaybackService extends Service {
}
}
private void writePlaybackPreferences() {
if (AppConfig.DEBUG)
Log.d(TAG, "Writing playback preferences");
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()).edit();
if (media != null) {
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_MEDIA,
media.getPlayableType());
editor.putBoolean(
PlaybackPreferences.PREF_CURRENT_EPISODE_IS_STREAM,
shouldStream);
editor.putBoolean(
PlaybackPreferences.PREF_CURRENT_EPISODE_IS_VIDEO,
playingVideo);
if (media instanceof FeedMedia) {
FeedMedia fMedia = (FeedMedia) media;
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
fMedia.getItem().getFeed().getId());
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
fMedia.getId());
} else {
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
}
media.writeToPreferences(editor);
} else {
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_MEDIA,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
editor.putLong(
PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID,
PlaybackPreferences.NO_MEDIA_PLAYING);
}
editor.commit();
}
private void setStatus(PlayerStatus newStatus) {
if (AppConfig.DEBUG)
Log.d(TAG, "Setting status to " + newStatus);

View File

@ -188,9 +188,9 @@ public abstract class PlaybackController {
Log.d(TAG, "Trying to restore last played media");
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
long lastPlayedId = PlaybackPreferences.getLastPlayedId();
if (lastPlayedId != PlaybackPreferences.NO_MEDIA_PLAYING) {
Playable media = PlayableUtils.createInstanceFromPreferences((int) lastPlayedId, prefs);
long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia();
if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) {
Playable media = PlayableUtils.createInstanceFromPreferences((int) currentlyPlayingMedia, prefs);
if (media != null) {
Intent serviceIntent = new Intent(activity,
PlaybackService.class);
@ -200,7 +200,7 @@ public abstract class PlaybackController {
serviceIntent.putExtra(
PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false);
boolean fileExists = media.localFileAvailable();
boolean lastIsStream = PlaybackPreferences.isLastIsStream();
boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream();
if (!fileExists && !lastIsStream && media instanceof FeedMedia) {
FeedManager.getInstance().notifyMissingFeedMediaFile(
activity, (FeedMedia) media);