Implemented auto-delete feature

This commit is contained in:
daniel oeh 2012-09-22 15:59:21 +02:00
parent f12e763fb6
commit 7a0297f937
5 changed files with 91 additions and 9 deletions

View File

@ -157,6 +157,8 @@
<string name="pref_display_only_episodes_title">Display only episodes</string>
<string name="pref_display_only_episodes_sum">Display only items which also have an episode.</string>
<string name="user_interface_label">User Interface</string>
<string name="pref_auto_delete_title">Auto-delete</string>
<string name="pref_auto_delete_sum">Delete an episode when playback completes or when it is removed from the queue.</string>
<!-- Search -->
<string name="search_hint">Search for Feeds or Episodes</string>

View File

@ -16,6 +16,7 @@
android:key="prefFollowQueue"
android:summary="@string/pref_followQueue_sum"
android:title="@string/pref_followQueue_title" />
<CheckBoxPreference android:key="prefAutoDelete" android:summary="@string/pref_auto_delete_sum" android:title="@string/pref_auto_delete_title"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/network_pref" >

View File

@ -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);
}
}
}
}

View File

@ -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");

View File

@ -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());