diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-09-22 15:59:21 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-09-22 15:59:21 +0200 |
commit | 7a0297f937218f95922443134fc823c56cf4f424 (patch) | |
tree | 175dd662b0f71ba71a030a74da931677acfc9d2b /src/de/danoeh/antennapod | |
parent | f12e763fb62408dc1d87fab20b305044207ba9e8 (diff) | |
download | AntennaPod-7a0297f937218f95922443134fc823c56cf4f424.zip |
Implemented auto-delete feature
Diffstat (limited to 'src/de/danoeh/antennapod')
-rw-r--r-- | src/de/danoeh/antennapod/PodcastApp.java | 13 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/feed/FeedManager.java | 45 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/service/PlaybackService.java | 39 |
3 files changed, 88 insertions, 9 deletions
diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index e79be2c07..9bca9a5e2 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -16,7 +16,9 @@ import android.util.Log; import de.danoeh.antennapod.activity.OpmlImportActivity; import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.receiver.FeedUpdateReceiver; +import de.danoeh.antennapod.service.PlaybackService; /** Main application class. */ public class PodcastApp extends Application implements @@ -32,6 +34,7 @@ public class PodcastApp extends Application implements public static final String PREF_MOBILE_UPDATE = "prefMobileUpdate"; public static final String PREF_AUTO_QUEUE = "prefAutoQueue"; public static final String PREF_DISPLAY_ONLY_EPISODES = "prefDisplayOnlyEpisodes"; + public static final String PREF_AUTO_DELETE = "prefAutoDelete"; private static float LOGICAL_DENSITY; @@ -129,6 +132,16 @@ public class PodcastApp extends Application implements } 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); + } 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); + if (mediaId != -1) { + FeedManager manager = FeedManager.getInstance(); + FeedMedia media = manager.getFeedMedia(mediaId); + if (media != null) { + manager.autoDeleteIfPossible(this, media); + } + } } } diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index 134feb6b5..370c515f6 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -12,6 +12,7 @@ import java.util.concurrent.ThreadFactory; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.database.Cursor; import android.os.AsyncTask; import android.os.Handler; @@ -461,6 +462,7 @@ public class FeedManager { public void removeQueueItem(final Context context, FeedItem item) { boolean removed = queue.remove(item); if (removed) { + autoDeleteIfPossible(context, item.getMedia()); dbExec.execute(new Runnable() { @Override @@ -476,6 +478,49 @@ public class FeedManager { sendQueueUpdateBroadcast(context, item); } + /** + * Delete the episode of this FeedMedia object if auto-delete is enabled and + * it is not the last played media or it is the last played media and + * playback has been completed. + */ + public void autoDeleteIfPossible(Context context, FeedMedia media) { + if (media != null) { + SharedPreferences prefs = PreferenceManager + .getDefaultSharedPreferences(context + .getApplicationContext()); + boolean autoDelete = prefs.getBoolean(PodcastApp.PREF_AUTO_DELETE, + false); + if (autoDelete) { + long lastPlayedId = prefs.getLong( + PlaybackService.PREF_LAST_PLAYED_ID, -1); + long autoDeleteId = prefs.getLong( + PlaybackService.PREF_AUTODELETE_MEDIA_ID, -1); + boolean playbackCompleted = prefs + .getBoolean( + PlaybackService.PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED, + false); + if ((media.getId() != lastPlayedId) + && ((media.getId() != autoDeleteId) || (media.getId() == autoDeleteId && playbackCompleted))) { + if (AppConfig.DEBUG) + Log.d(TAG, "Performing auto-cleanup"); + deleteFeedMedia(context, media); + + SharedPreferences.Editor editor = prefs.edit(); + editor.putLong(PlaybackService.PREF_AUTODELETE_MEDIA_ID, -1); + editor.commit(); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Didn't do auto-cleanup"); + } + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Auto-delete preference is disabled"); + } + } else { + Log.e(TAG, "Could not do auto-cleanup: media was null"); + } + } + public void moveQueueItem(final Context context, FeedItem item, int delta) { if (AppConfig.DEBUG) Log.d(TAG, "Moving queue item"); diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index 83104edc8..dcd157719 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -61,7 +61,12 @@ public class PlaybackService extends Service { /** True if last played media was a video. */ public static final String PREF_LAST_IS_VIDEO = "de.danoeh.antennapod.preferences.lastIsVideo"; /** True if playback of last played media has been completed. */ - public static final String PREF_LAST_PLAYBACK_COMPLETED = "de.danoeh.antennapod.preferences.lastPlaybackCompleted"; + public static final String PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED = "de.danoeh.antennapod.preferences.lastPlaybackCompleted"; + /** + * ID of the last played media which should be auto-deleted as soon as + * PREF_LAST_PLAYED_ID changes. + */ + public static final String PREF_AUTODELETE_MEDIA_ID = "de.danoeh.antennapod.preferences.autoDeleteMediaId"; /** Contains the id of the FeedMedia object. */ public static final String EXTRA_MEDIA_ID = "extra.de.danoeh.antennapod.service.mediaId"; @@ -185,7 +190,7 @@ public class PlaybackService extends Service { return new Intent(context, AudioplayerActivity.class); } } - + /** Get last played FeedMedia object or null if it doesn't exist. */ public static FeedMedia getLastPlayedMediaFromPreferences(Context context) { SharedPreferences prefs = PreferenceManager @@ -203,6 +208,18 @@ public class PlaybackService extends Service { return null; } + private void setLastPlayedMediaId(long mediaId) { + SharedPreferences prefs = PreferenceManager + .getDefaultSharedPreferences(getApplicationContext()); + long autoDeleteId = prefs.getLong(PREF_AUTODELETE_MEDIA_ID, -1); + SharedPreferences.Editor editor = prefs.edit(); + if (mediaId == autoDeleteId) { + editor.putBoolean(PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED, false); + } + editor.putLong(PREF_LAST_PLAYED_ID, mediaId); + editor.commit(); + } + @SuppressLint("NewApi") @Override public void onCreate() { @@ -466,7 +483,7 @@ public class PlaybackService extends Service { player.setDataSource(media.getDownload_url()); setStatus(PlayerStatus.PREPARING); player.prepareAsync(); - } else { + } else if (media.getFile_url() != null){ player.setDataSource(media.getFile_url()); setStatus(PlayerStatus.PREPARING); player.prepare(); @@ -610,11 +627,17 @@ public class PlaybackService extends Service { manager.removeQueueItem(PlaybackService.this, media.getItem()); } manager.setFeedMedia(PlaybackService.this, media); - + + long autoDeleteMediaId = media.getId(); + + if (shouldStream) { + autoDeleteMediaId = -1; + } SharedPreferences.Editor editor = prefs.edit(); - editor.putBoolean(PREF_LAST_PLAYBACK_COMPLETED, true); + editor.putLong(PREF_AUTODELETE_MEDIA_ID, autoDeleteMediaId); + editor.putBoolean(PREF_AUTO_DELETE_MEDIA_PLAYBACK_COMPLETED, true); editor.commit(); - + // Prepare for playing next item boolean followQueue = prefs.getBoolean( PodcastApp.PREF_FOLLOW_QUEUE, false); @@ -723,13 +746,11 @@ public class PlaybackService extends Service { SharedPreferences.Editor editor = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()) .edit(); - editor.putLong(PREF_LAST_PLAYED_ID, 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); - editor.putBoolean(PREF_LAST_PLAYBACK_COMPLETED, false); editor.commit(); - + setLastPlayedMediaId(media.getId()); player.start(); if (status != PlayerStatus.PAUSED) { player.seekTo((int) media.getPosition()); |