From 8c541fb82d2470b9c97eb12b5d121e3c283e0ab1 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Tue, 30 Oct 2012 21:03:59 +0100 Subject: [PATCH] Use separate preference for currently playing media, reset after playback has completed --- src/de/danoeh/antennapod/PodcastApp.java | 52 ++++++++++++------- src/de/danoeh/antennapod/feed/FeedItem.java | 2 +- .../antennapod/service/PlaybackService.java | 20 ++++++- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index b47688b87..f4d238c60 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -39,10 +39,10 @@ public class PodcastApp extends Application implements private static float LOGICAL_DENSITY; private static PodcastApp singleton; - + private boolean displayOnlyEpisodes; - - private static long lastPlayedMediaId; + + private static long currentlyPlayingMediaId; public static PodcastApp getInstance() { return singleton; @@ -55,8 +55,11 @@ public class PodcastApp extends Application implements LOGICAL_DENSITY = getResources().getDisplayMetrics().density; SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(this); - displayOnlyEpisodes = prefs.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false); - lastPlayedMediaId = prefs.getLong(PlaybackService.PREF_LAST_PLAYED_ID, -1); + displayOnlyEpisodes = prefs.getBoolean(PREF_DISPLAY_ONLY_EPISODES, + false); + currentlyPlayingMediaId = prefs.getLong( + PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA, + PlaybackService.NO_MEDIA_PLAYING); createImportDirectory(); createNoMediaFile(); prefs.registerOnSharedPreferenceChangeListener(this); @@ -74,7 +77,8 @@ public class PodcastApp extends Application implements Log.e(TAG, "Could not create .nomedia file"); e.printStackTrace(); } - if (AppConfig.DEBUG) Log.d(TAG, ".nomedia file created"); + if (AppConfig.DEBUG) + Log.d(TAG, ".nomedia file created"); } } @@ -133,15 +137,15 @@ public class PodcastApp extends Application implements Log.d(TAG, "Automatic update was deactivated"); } } else if (key.equals(PREF_DISPLAY_ONLY_EPISODES)) { - if (AppConfig.DEBUG) Log.d(TAG, "PREF_DISPLAY_ONLY_EPISODES changed"); - displayOnlyEpisodes = sharedPreferences.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false); + if (AppConfig.DEBUG) + Log.d(TAG, "PREF_DISPLAY_ONLY_EPISODES changed"); + displayOnlyEpisodes = sharedPreferences.getBoolean( + PREF_DISPLAY_ONLY_EPISODES, false); } else if (key.equals(PlaybackService.PREF_LAST_PLAYED_ID)) { - if (AppConfig.DEBUG) Log.d(TAG, "PREF_LAST_PLAYED_ID changed"); - long mediaId = sharedPreferences.getLong(PlaybackService.PREF_AUTODELETE_MEDIA_ID, -1); - long lastPlayedId = sharedPreferences.getLong(PlaybackService.PREF_LAST_PLAYED_ID, -1); - if (lastPlayedId != lastPlayedMediaId) { - lastPlayedMediaId = lastPlayedId; - } + if (AppConfig.DEBUG) + Log.d(TAG, "PREF_LAST_PLAYED_ID changed"); + long mediaId = sharedPreferences.getLong( + PlaybackService.PREF_AUTODELETE_MEDIA_ID, -1); if (mediaId != -1) { FeedManager manager = FeedManager.getInstance(); FeedMedia media = manager.getFeedMedia(mediaId); @@ -149,23 +153,33 @@ public class PodcastApp extends Application implements manager.autoDeleteIfPossible(this, media); } } + } else if (key.equals(PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA)) { + long id = sharedPreferences.getLong( + PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA, + PlaybackService.NO_MEDIA_PLAYING); + if (AppConfig.DEBUG) + Log.d(TAG, "Currently playing media set to " + id); + if (id != currentlyPlayingMediaId) { + currentlyPlayingMediaId = id; + } } } public static float getLogicalDensity() { return LOGICAL_DENSITY; } - + public boolean displayOnlyEpisodes() { return displayOnlyEpisodes; } - - public static long getLastPlayedMediaId() { - return lastPlayedMediaId; + + public static long getCurrentlyPlayingMediaId() { + return currentlyPlayingMediaId; } public boolean isLargeScreen() { - return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE || (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE; + return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE + || (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE; } } diff --git a/src/de/danoeh/antennapod/feed/FeedItem.java b/src/de/danoeh/antennapod/feed/FeedItem.java index c69adf98d..93655f8cb 100644 --- a/src/de/danoeh/antennapod/feed/FeedItem.java +++ b/src/de/danoeh/antennapod/feed/FeedItem.java @@ -164,7 +164,7 @@ public class FeedItem extends FeedComponent { public boolean isPlaying() { if (media != null) { - if (PodcastApp.getLastPlayedMediaId() == media.getId()) { + if (PodcastApp.getCurrentlyPlayingMediaId() == media.getId()) { return true; } } diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index 98721877e..11719127d 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -58,6 +58,12 @@ public class PlaybackService extends Service { public static final String PREF_LAST_PLAYED_ID = "de.danoeh.antennapod.preferences.lastPlayedId"; /** Contains the feed id of the last played item. */ public static final String PREF_LAST_PLAYED_FEED_ID = "de.danoeh.antennapod.preferences.lastPlayedFeedId"; + /** + * ID of the media object that is currently being played. This preference is + * set to NO_MEDIA_PLAYING after playback has been completed and is set as + * soon as the 'play' button is pressed. + */ + 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"; /** True if last played media was a video. */ @@ -159,6 +165,9 @@ public class PlaybackService extends Service { /** True if mediaplayer was paused because it lost audio focus temporarily */ private boolean pausedBecauseOfTransientAudiofocusLoss; + /** Value of PREF_CURRENTLY_PLAYING_MEDIA if no media is playing. */ + public static final long NO_MEDIA_PLAYING = -1; + private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { @@ -657,6 +666,7 @@ public class PlaybackService extends Service { pause(true, true); } sendNotificationBroadcast(NOTIFICATION_TYPE_ERROR, what); + setCurrentlyPlayingMedia(NO_MEDIA_PLAYING); stopSelf(); return true; } @@ -692,6 +702,7 @@ public class PlaybackService extends Service { autoDeleteMediaId = -1; } SharedPreferences.Editor editor = prefs.edit(); + editor.putLong(PREF_CURRENTLY_PLAYING_MEDIA, NO_MEDIA_PLAYING); editor.putLong(PREF_AUTODELETE_MEDIA_ID, autoDeleteMediaId); editor.putBoolean(PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED, true); editor.commit(); @@ -794,6 +805,7 @@ public class PlaybackService extends Service { if (AppConfig.DEBUG) Log.d(TAG, "Stopping playback"); player.stop(); + setCurrentlyPlayingMedia(NO_MEDIA_PLAYING); stopSelf(); } @@ -834,6 +846,7 @@ public class PlaybackService extends Service { SharedPreferences.Editor editor = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()) .edit(); + editor.putLong(PREF_CURRENTLY_PLAYING_MEDIA, media.getId()); editor.putLong(PREF_LAST_PLAYED_FEED_ID, feed.getId()); editor.putBoolean(PREF_LAST_IS_STREAM, shouldStream); editor.putBoolean(PREF_LAST_IS_VIDEO, playingVideo); @@ -1261,5 +1274,10 @@ public class PlaybackService extends Service { return INVALID_TIME; } } - + + private void setCurrentlyPlayingMedia(long id) { + SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit(); + editor.putLong(PREF_CURRENTLY_PLAYING_MEDIA, id); + editor.commit(); + } }