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() { public void onServiceQueried() {
MediaplayerActivity.this.onServiceQueried(); MediaplayerActivity.this.onServiceQueried();
} }
@Override
public void onShutdownNotification() {
finish();
}
}; };
} }

View File

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

View File

@ -144,6 +144,13 @@ public class ExternalPlayerFragment extends SherlockFragment {
@Override @Override
public void onServiceQueried() { public void onServiceQueried() {
} }
@Override
public void onShutdownNotification() {
if (fragmentLayout != null) {
fragmentLayout.setVisibility(View.GONE);
}
}
}; };
butPlay.setOnClickListener(controller.newOnPlayButtonClickListener()); 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_CODE = "extra.de.danoeh.antennapod.service.notificationCode";
public static final String EXTRA_NOTIFICATION_TYPE = "extra.de.danoeh.antennapod.service.notificationType"; 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. */ /** Used in NOTIFICATION_TYPE_RELOAD. */
public static final int EXTRA_CODE_AUDIO = 1; public static final int EXTRA_CODE_AUDIO = 1;
public static final int EXTRA_CODE_VIDEO = 2; public static final int EXTRA_CODE_VIDEO = 2;
@ -266,6 +272,8 @@ public class PlaybackService extends Service {
} }
registerReceiver(headsetDisconnected, new IntentFilter( registerReceiver(headsetDisconnected, new IntentFilter(
Intent.ACTION_HEADSET_PLUG)); Intent.ACTION_HEADSET_PLUG));
registerReceiver(shutdownReceiver, new IntentFilter(
ACTION_SHUTDOWN_PLAYBACK_SERVICE));
} }
@ -278,6 +286,7 @@ public class PlaybackService extends Service {
isRunning = false; isRunning = false;
disableSleepTimer(); disableSleepTimer();
unregisterReceiver(headsetDisconnected); unregisterReceiver(headsetDisconnected);
unregisterReceiver(shutdownReceiver);
if (android.os.Build.VERSION.SDK_INT >= 14) { if (android.os.Build.VERSION.SDK_INT >= 14) {
audioManager.unregisterRemoteControlClient(remoteControlClient); audioManager.unregisterRemoteControlClient(remoteControlClient);
} }
@ -486,7 +495,7 @@ public class PlaybackService extends Service {
player.setDataSource(media.getDownload_url()); player.setDataSource(media.getDownload_url());
setStatus(PlayerStatus.PREPARING); setStatus(PlayerStatus.PREPARING);
player.prepareAsync(); player.prepareAsync();
} else if (media.getFile_url() != null){ } else if (media.getFile_url() != null) {
player.setDataSource(media.getFile_url()); player.setDataSource(media.getFile_url());
setStatus(PlayerStatus.PREPARING); setStatus(PlayerStatus.PREPARING);
player.prepare(); player.prepare();
@ -556,9 +565,12 @@ public class PlaybackService extends Service {
ChapterUtils ChapterUtils
.readID3ChaptersFromFeedMediaDownloadUrl(media .readID3ChaptersFromFeedMediaDownloadUrl(media
.getItem()); .getItem());
if (media.getItem().getChapters() != null) { if (media.getItem().getChapters() != null
&& !interrupted()) {
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD,
0); 0);
manager.setFeedItem(PlaybackService.this,
media.getItem());
} }
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "ChapterLoaderThread has finished"); Log.d(TAG, "ChapterLoaderThread has finished");
@ -731,7 +743,8 @@ public class PlaybackService extends Service {
/** Pauses playback and destroys service. Recommended for video playback. */ /** Pauses playback and destroys service. Recommended for video playback. */
public void stop() { public void stop() {
pause(true); if (AppConfig.DEBUG) Log.d(TAG, "Stopping playback");
player.stop();
stopSelf(); 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 */ /** Periodically saves the position of the media file */
class PositionSaver implements Runnable { class PositionSaver implements Runnable {
public static final int WAITING_INTERVALL = 5000; public static final int WAITING_INTERVALL = 5000;

View File

@ -116,7 +116,16 @@ public abstract class PlaybackController {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// ignore // ignore
} }
try {
activity.unregisterReceiver(shutdownReceiver);
} catch (IllegalArgumentException e) {
// ignore
}
cancelPositionObserver(); cancelPositionObserver();
schedExecutor.shutdownNow();
media = null;
} }
/** Should be called in the activity's onPause() method. */ /** Should be called in the activity's onPause() method. */
@ -226,6 +235,9 @@ public abstract class PlaybackController {
activity.registerReceiver(notificationReceiver, new IntentFilter( activity.registerReceiver(notificationReceiver, new IntentFilter(
PlaybackService.ACTION_PLAYER_NOTIFICATION)); PlaybackService.ACTION_PLAYER_NOTIFICATION));
activity.registerReceiver(shutdownReceiver, new IntentFilter(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
queryService(); queryService();
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Connection to Service established"); 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. */ /** Called when the currently displayed information should be refreshed. */
public abstract void onReloadNotification(int code); public abstract void onReloadNotification(int code);