<commit_before>package de.podfetcher.feed;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import de.podfetcher.activity.MediaplayerActivity;
import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.*;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;

/**
 * Singleton class Manages all feeds, categories and feeditems
 * 
 * 
 * */
public class FeedManager {
	private static final String TAG = "FeedManager";

	/** Number of completed Download status entries to store. */
	private static final int DOWNLOAD_LOG_SIZE = 25;

	private static FeedManager singleton;

	private ArrayList<Feed> feeds;
	private ArrayList<FeedCategory> categories;

	/** Contains all items where 'read' is false */
	private ArrayList<FeedItem> unreadItems;

	/** Contains completed Download status entries */
	private ArrayList<DownloadStatus> downloadLog;

	private DownloadRequester requester;

	private FeedManager() {
		feeds = new ArrayList<Feed>();
		categories = new ArrayList<FeedCategory>();
		unreadItems = new ArrayList<FeedItem>();
		requester = DownloadRequester.getInstance();
		downloadLog = new ArrayList<DownloadStatus>();
	}

	public static FeedManager getInstance() {
		if (singleton == null) {
			singleton = new FeedManager();
		}
		return singleton;
	}

	/**
	 * Play FeedMedia and start the playback service + launch Mediaplayer
	 * Activity.
	 */
	public void playMedia(Context context, FeedMedia media) {
		// Start playback Service
		Intent launchIntent = new Intent(context, PlaybackService.class);
		launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, media.getId());
		launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, media.getItem()
				.getFeed().getId());
		context.startService(launchIntent);

		// Launch Mediaplayer
		Intent playerIntent = new Intent(context, MediaplayerActivity.class);
		context.startActivity(playerIntent);
	}

	/** Remove media item that has been downloaded. */
	public boolean deleteFeedMedia(Context context, FeedMedia media) {
		boolean result = false;
		if (media.isDownloaded()) {
			File mediaFile = new File(media.file_url);
			if (mediaFile.exists()) {
				result = mediaFile.delete();
			}
			media.setDownloaded(false);
			media.setFile_url("");
			setFeedMedia(context, media);
		}
		Log.d(TAG, "Deleting File. Result: " + result);
		return result;
	}
	
	/** Remove a feed with all its items and media files and its image. */
	public boolean deleteFeed(Context context, Feed feed) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		
		// 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();
			}
		}
		// delete stored media files and mark them as read
		for (FeedItem item : feed.getItems()) {
			if (!item.isRead()) {
				unreadItems.remove(item);
			}
			if (item.getMedia() != null && item.getMedia().isDownloaded()) {
				File mediaFile = new File(item.getMedia().getFile_url());
				mediaFile.delete();<commit_msg>Fixed bug in media removing process<commit_after>package de.podfetcher.feed;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import de.podfetcher.activity.MediaplayerActivity;
import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.*;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;

/**
 * Singleton class Manages all feeds, categories and feeditems
 * 
 * 
 * */
public class FeedManager {
	private static final String TAG = "FeedManager";

	/** Number of completed Download status entries to store. */
	private static final int DOWNLOAD_LOG_SIZE = 25;

	private static FeedManager singleton;

	private ArrayList<Feed> feeds;
	private ArrayList<FeedCategory> categories;

	/** Contains all items where 'read' is false */
	private ArrayList<FeedItem> unreadItems;

	/** Contains completed Download status entries */
	private ArrayList<DownloadStatus> downloadLog;

	private DownloadRequester requester;

	private FeedManager() {
		feeds = new ArrayList<Feed>();
		categories = new ArrayList<FeedCategory>();
		unreadItems = new ArrayList<FeedItem>();
		requester = DownloadRequester.getInstance();
		downloadLog = new ArrayList<DownloadStatus>();
	}

	public static FeedManager getInstance() {
		if (singleton == null) {
			singleton = new FeedManager();
		}
		return singleton;
	}

	/**
	 * Play FeedMedia and start the playback service + launch Mediaplayer
	 * Activity.
	 */
	public void playMedia(Context context, FeedMedia media) {
		// Start playback Service
		Intent launchIntent = new Intent(context, PlaybackService.class);
		launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, media.getId());
		launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, media.getItem()
				.getFeed().getId());
		context.startService(launchIntent);

		// Launch Mediaplayer
		Intent playerIntent = new Intent(context, MediaplayerActivity.class);
		context.startActivity(playerIntent);
	}

	/** Remove media item that has been downloaded. */
	public boolean deleteFeedMedia(Context context, FeedMedia media) {
		boolean result = false;
		if (media.isDownloaded()) {
			File mediaFile = new File(media.file_url);
			if (mediaFile.exists()) {
				result = mediaFile.delete();
			}
			media.setDownloaded(false);
			media.setFile_url(null);
			setFeedMedia(context, media);
		}
		Log.d(TAG, "Deleting File. Result: " + result);
		return result;
	}
	
	/** Remove a feed with all its items and media files and its image. */
	public boolean deleteFeed(Context context, Feed feed) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		
		// 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();
			}
		}
		// delete stored media files and mark them as read
		for (FeedItem item : feed.getItems()) {
			if (!item.isRead()) {
				unreadItems.remove(item);
			}
			if (item.getMedia() != null && item.getMedia().isDownloaded()) {
				File mediaFile = new File(item.getMedia().getFile_url());
				mediaFile.delete();
{'fraction_non_alphanumeric': 0.11636589919103921, 'fraction_numerical': 0.0006222775357809583, 'mean_word_length': 4.429898648648648, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5987570', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1846, 'n_words': 508}