Feeds weren't always deleted properly

This commit is contained in:
daniel oeh 2012-09-25 13:37:47 +02:00
parent d39bd6831b
commit 88b3a015cd
5 changed files with 134 additions and 43 deletions

View File

@ -122,6 +122,11 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity
public void onServiceQueried() {
MediaplayerActivity.this.onServiceQueried();
}
@Override
public void onShutdownNotification() {
finish();
}
};
}

View File

@ -141,14 +141,20 @@ public class FeedManager {
media.setDownloaded(false);
media.setFile_url(null);
setFeedMedia(context, media);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final long lastPlayedId = prefs.getLong(PlaybackService.PREF_LAST_PLAYED_ID, -1);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
final long lastPlayedId = prefs.getLong(
PlaybackService.PREF_LAST_PLAYED_ID, -1);
if (media.getId() == lastPlayedId) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PlaybackService.PREF_LAST_IS_STREAM, true);
editor.commit();
}
if (lastPlayedId == media.getId()) {
context.sendBroadcast(new Intent(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
}
}
if (AppConfig.DEBUG)
Log.d(TAG, "Deleting File. Result: " + result);
@ -157,51 +163,71 @@ public class FeedManager {
/** Remove a feed with all its items and media files and its image. */
public void deleteFeed(final Context context, final Feed feed) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
long lastPlayedFeed = prefs.getLong(
PlaybackService.PREF_LAST_PLAYED_FEED_ID, -1);
if (lastPlayedFeed == feed.getId()) {
context.sendBroadcast(new Intent(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
SharedPreferences.Editor editor = prefs.edit();
editor.putLong(PlaybackService.PREF_LAST_PLAYED_ID, -1);
editor.putLong(PlaybackService.PREF_LAST_PLAYED_FEED_ID, -1);
editor.commit();
}
contentChanger.post(new Runnable() {
@Override
public void run() {
feeds.remove(feed);
sendFeedUpdateBroadcast(context);
}
});
dbExec.execute(new Runnable() {
dbExec.execute(new Runnable() {
@Override
public void run() {
PodDBAdapter adapter = new PodDBAdapter(context);
DownloadRequester requester = DownloadRequester.getInstance();
adapter.open();
// delete image file
if (feed.getImage() != null) {
if (feed.getImage().isDownloaded()
&& feed.getImage().getFile_url() != null) {
File imageFile = new File(feed.getImage().getFile_url());
imageFile.delete();
} else if (requester.isDownloadingFile(feed.getImage())) {
requester.cancelDownload(context, feed.getImage());
}
}
// delete stored media files and mark them as read
for (FeedItem item : feed.getItems()) {
if (!item.isRead()) {
unreadItems.remove(item);
}
if (queue.contains(item)) {
removeQueueItem(item, adapter);
}
if (item.getMedia() != null
&& item.getMedia().isDownloaded()) {
File mediaFile = new File(item.getMedia().getFile_url());
mediaFile.delete();
} else if (item.getMedia() != null
&& requester.isDownloadingFile(item.getMedia())) {
requester.cancelDownload(context, item.getMedia());
}
}
@Override
public void run() {
PodDBAdapter adapter = new PodDBAdapter(context);
DownloadRequester requester = DownloadRequester
.getInstance();
adapter.open();
// delete image file
if (feed.getImage() != null) {
if (feed.getImage().isDownloaded()
&& feed.getImage().getFile_url() != null) {
File imageFile = new File(feed.getImage()
.getFile_url());
imageFile.delete();
} else if (requester.isDownloadingFile(feed
.getImage())) {
requester.cancelDownload(context,
feed.getImage());
}
}
// delete stored media files and mark them as read
for (FeedItem item : feed.getItems()) {
if (!item.isRead()) {
unreadItems.remove(item);
}
if (queue.contains(item)) {
removeQueueItem(item, adapter);
}
if (item.getMedia() != null
&& item.getMedia().isDownloaded()) {
File mediaFile = new File(item.getMedia()
.getFile_url());
mediaFile.delete();
} else if (item.getMedia() != null
&& requester.isDownloadingFile(item
.getMedia())) {
requester.cancelDownload(context,
item.getMedia());
}
}
adapter.removeFeed(feed);
adapter.close();
adapter.removeFeed(feed);
adapter.close();
}
});
}
});

View File

@ -144,6 +144,13 @@ public class ExternalPlayerFragment extends SherlockFragment {
@Override
public void onServiceQueried() {
}
@Override
public void onShutdownNotification() {
if (fragmentLayout != null) {
fragmentLayout.setVisibility(View.GONE);
}
}
};
butPlay.setOnClickListener(controller.newOnPlayButtonClickListener());
}

View File

@ -87,6 +87,12 @@ public class PlaybackService extends Service {
public static final String EXTRA_NOTIFICATION_CODE = "extra.de.danoeh.antennapod.service.notificationCode";
public static final String EXTRA_NOTIFICATION_TYPE = "extra.de.danoeh.antennapod.service.notificationType";
/**
* If the PlaybackService receives this action, it will stop playback and
* try to shutdown.
*/
public static final String ACTION_SHUTDOWN_PLAYBACK_SERVICE = "action.de.danoeh.antennapod.service.actionShutdownPlaybackService";
/** Used in NOTIFICATION_TYPE_RELOAD. */
public static final int EXTRA_CODE_AUDIO = 1;
public static final int EXTRA_CODE_VIDEO = 2;
@ -266,6 +272,8 @@ public class PlaybackService extends Service {
}
registerReceiver(headsetDisconnected, new IntentFilter(
Intent.ACTION_HEADSET_PLUG));
registerReceiver(shutdownReceiver, new IntentFilter(
ACTION_SHUTDOWN_PLAYBACK_SERVICE));
}
@ -278,6 +286,7 @@ public class PlaybackService extends Service {
isRunning = false;
disableSleepTimer();
unregisterReceiver(headsetDisconnected);
unregisterReceiver(shutdownReceiver);
if (android.os.Build.VERSION.SDK_INT >= 14) {
audioManager.unregisterRemoteControlClient(remoteControlClient);
}
@ -486,7 +495,7 @@ public class PlaybackService extends Service {
player.setDataSource(media.getDownload_url());
setStatus(PlayerStatus.PREPARING);
player.prepareAsync();
} else if (media.getFile_url() != null){
} else if (media.getFile_url() != null) {
player.setDataSource(media.getFile_url());
setStatus(PlayerStatus.PREPARING);
player.prepare();
@ -556,9 +565,12 @@ public class PlaybackService extends Service {
ChapterUtils
.readID3ChaptersFromFeedMediaDownloadUrl(media
.getItem());
if (media.getItem().getChapters() != null) {
if (media.getItem().getChapters() != null
&& !interrupted()) {
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD,
0);
manager.setFeedItem(PlaybackService.this,
media.getItem());
}
if (AppConfig.DEBUG)
Log.d(TAG, "ChapterLoaderThread has finished");
@ -731,7 +743,8 @@ public class PlaybackService extends Service {
/** Pauses playback and destroys service. Recommended for video playback. */
public void stop() {
pause(true);
if (AppConfig.DEBUG) Log.d(TAG, "Stopping playback");
player.stop();
stopSelf();
}
@ -985,6 +998,20 @@ public class PlaybackService extends Service {
}
};
private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
schedExecutor.shutdownNow();
stop();
media = null;
feed = null;
}
}
};
/** Periodically saves the position of the media file */
class PositionSaver implements Runnable {
public static final int WAITING_INTERVALL = 5000;

View File

@ -116,7 +116,16 @@ public abstract class PlaybackController {
} catch (IllegalArgumentException e) {
// ignore
}
try {
activity.unregisterReceiver(shutdownReceiver);
} catch (IllegalArgumentException e) {
// ignore
}
cancelPositionObserver();
schedExecutor.shutdownNow();
media = null;
}
/** Should be called in the activity's onPause() method. */
@ -226,6 +235,9 @@ public abstract class PlaybackController {
activity.registerReceiver(notificationReceiver, new IntentFilter(
PlaybackService.ACTION_PLAYER_NOTIFICATION));
activity.registerReceiver(shutdownReceiver, new IntentFilter(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
queryService();
if (AppConfig.DEBUG)
Log.d(TAG, "Connection to Service established");
@ -300,6 +312,20 @@ public abstract class PlaybackController {
};
private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
release();
onShutdownNotification();
}
}
};
public abstract void onShutdownNotification();
/** Called when the currently displayed information should be refreshed. */
public abstract void onReloadNotification(int code);