summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/feed/FeedManager.java
blob: c564f007044dd8e8376389ebdf8325011bd03944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
package de.podfetcher.feed;

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

import de.podfetcher.activity.MediaplayerActivity;
import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.*;
import de.podfetcher.util.FeedItemPubdateComparator;
import de.podfetcher.util.FeedtitleComparator;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Debug;
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;

	/** Contains the queue of items to be played. */
	private ArrayList<FeedItem> queue;

	private DownloadRequester requester;

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

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

	/**
	 * Play FeedMedia and start the playback service + launch Mediaplayer
	 * Activity.
	 * 
	 * @param context
	 *            for starting the playbackservice
	 * @param media
	 *            that shall be played
	 * @param showPlayer
	 *            if Mediaplayer activity shall be started
	 * @param startWhenPrepared
	 *            if Mediaplayer shall be started after it has been prepared
	 */
	public void playMedia(Context context, FeedMedia media, boolean showPlayer,
			boolean startWhenPrepared, boolean shouldStream) {
		// 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());
		launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED,
				startWhenPrepared);
		launchIntent
				.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, shouldStream);
		context.startService(launchIntent);
		if (showPlayer) {
			// 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);
		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();
			}
		}
		// 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();
			}
		}

		adapter.removeFeed(feed);
		adapter.close();
		return feeds.remove(feed);

	}

	/**
	 * Sets the 'read'-attribute of a FeedItem. Should be used by all Classes
	 * instead of the setters of FeedItem.
	 */
	public void markItemRead(Context context, FeedItem item, boolean read) {
		Log.d(TAG, "Setting item with title " + item.getTitle() + " as read/unread");
		item.read = read;
		setFeedItem(context, item);
		if (read == true) {
			unreadItems.remove(item);
		} else {
			unreadItems.add(item);
			Collections.sort(unreadItems, new FeedItemPubdateComparator());
		}
	}

	/**
	 * Sets the 'read' attribute of all FeedItems of a specific feed to true
	 * 
	 * @param context
	 */
	public void markFeedRead(Context context, Feed feed) {
		for (FeedItem item : feed.getItems()) {
			if (unreadItems.contains(item)) {
				markItemRead(context, item, true);
			}
		}
	}

	public void refreshAllFeeds(Context context) {
		Log.d(TAG, "Refreshing all feeds.");
		for (Feed feed : feeds) {
			requester.downloadFeed(context, new Feed(feed.getDownload_url(),
					new Date()));
		}
	}

	public long addDownloadStatus(Context context, DownloadStatus status) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		downloadLog.add(status);
		adapter.open();
		if (downloadLog.size() > DOWNLOAD_LOG_SIZE) {
			adapter.removeDownloadStatus(downloadLog.remove(0));
		}
		long result = adapter.setDownloadStatus(status);
		adapter.close();
		return result;
	}

	public void addQueueItem(Context context, FeedItem item) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		queue.add(item);
		adapter.open();
		adapter.setQueue(queue);
		adapter.close();
	}

	/** Uses external adapter. */
	public void removeQueueItem(FeedItem item, PodDBAdapter adapter) {
		boolean removed = queue.remove(item);
		if (removed) {
			adapter.setQueue(queue);
		}
	}

	/** Uses its own adapter. */
	public void removeQueueItem(Context context, FeedItem item) {
		boolean removed = queue.remove(item);
		if (removed) {
			PodDBAdapter adapter = new PodDBAdapter(context);
			adapter.open();
			adapter.setQueue(queue);
			adapter.close();
		}
	}
	
	public void moveQueueItem(Context context, FeedItem item, int delta) {
		Log.d(TAG, "Moving queue item");
		int itemIndex = queue.indexOf(item);
		int newIndex = itemIndex + delta;
		if (newIndex >= 0 && newIndex < queue.size()) {
			FeedItem oldItem = queue.set(newIndex, item);
			queue.set(itemIndex, oldItem);
			PodDBAdapter adapter = new PodDBAdapter(context);
			adapter.open();
			adapter.setQueue(queue);
			adapter.close();
		}
	}

	public boolean isInQueue(FeedItem item) {
		return queue.contains(item);
	}

	public FeedItem getFirstQueueItem() {
		if (queue.isEmpty()) {
			return null;
		} else {
			return queue.get(0);
		}
	}

	private void addNewFeed(Context context, Feed feed) {
		feeds.add(feed);
		Collections.sort(feeds, new FeedtitleComparator());
		PodDBAdapter adapter = new PodDBAdapter(context);
		adapter.open();
		adapter.setCompleteFeed(feed);
		adapter.close();
	}

	/**
	 * Updates an existing feed or adds it as a new one if it doesn't exist.
	 * 
	 * @return The saved Feed with a database ID
	 */
	public Feed updateFeed(Context context, final Feed newFeed) {
		// Look up feed in the feedslist
		final Feed savedFeed = searchFeedByLink(newFeed.getLink());
		if (savedFeed == null) {
			Log.d(TAG,
					"Found no existing Feed with title " + newFeed.getTitle()
							+ ". Adding as new one.");
			// Add a new Feed
			newFeed.getItems().get(0).read = false;
			addNewFeed(context, newFeed);
			return newFeed;
		} else {
			Log.d(TAG, "Feed with title " + newFeed.getTitle()
					+ " already exists. Syncing new with existing one.");
			// Look for new or updated Items
			for (int idx = 0; idx < newFeed.getItems().size(); idx++) {
				FeedItem item = newFeed.getItems().get(idx);
				FeedItem oldItem = searchFeedItemByTitle(savedFeed,
						item.getTitle());
				if (oldItem == null) {
					// item is new
					item.setFeed(savedFeed);
					savedFeed.getItems().add(idx, item);
					markItemRead(context, item, false);
				}
			}
			savedFeed.setLastUpdate(newFeed.getLastUpdate());
			setFeed(context, savedFeed);
			return savedFeed;
		}

	}

	/** Get a Feed by its link */
	private Feed searchFeedByLink(String link) {
		for (Feed feed : feeds) {
			if (feed.getLink().equals(link)) {
				return feed;
			}
		}
		return null;
	}

	/** Get a FeedItem by its link */
	private FeedItem searchFeedItemByTitle(Feed feed, String title) {
		for (FeedItem item : feed.getItems()) {
			if (item.getTitle().equals(title)) {
				return item;
			}
		}
		return null;
	}

	/** Updates Information of an existing Feed. Uses external adapter. */
	public long setFeed(Feed feed, PodDBAdapter adapter) {
		if (adapter != null) {
			return adapter.setFeed(feed);
		} else {
			Log.w(TAG, "Adapter in setFeed was null");
			return 0;
		}
	}

	/** Updates Information of an existing Feeditem. Uses external adapter. */
	public long setFeedItem(FeedItem item, PodDBAdapter adapter) {
		if (adapter != null) {
			return adapter.setSingleFeedItem(item);
		} else {
			Log.w(TAG, "Adapter in setFeedItem was null");
			return 0;
		}
	}

	/** Updates Information of an existing Feedimage. Uses external adapter. */
	public long setFeedImage(FeedImage image, PodDBAdapter adapter) {
		if (adapter != null) {
			return adapter.setImage(image);
		} else {
			Log.w(TAG, "Adapter in setFeedImage was null");
			return 0;
		}
	}

	/**
	 * Updates Information of an existing Feedmedia object. Uses external
	 * adapter.
	 */
	public long setFeedImage(FeedMedia media, PodDBAdapter adapter) {
		if (adapter != null) {
			return adapter.setMedia(media);
		} else {
			Log.w(TAG, "Adapter in setFeedMedia was null");
			return 0;
		}
	}

	/**
	 * Updates Information of an existing Feed. Creates and opens its own
	 * adapter.
	 */
	public long setFeed(Context context, Feed feed) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		adapter.open();
		long result = adapter.setFeed(feed);
		adapter.close();
		return result;
	}

	/**
	 * Updates information of an existing FeedItem. Creates and opens its own
	 * adapter.
	 */
	public long setFeedItem(Context context, FeedItem item) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		adapter.open();
		long result = adapter.setSingleFeedItem(item);
		adapter.close();
		return result;
	}

	/**
	 * Updates information of an existing FeedImage. Creates and opens its own
	 * adapter.
	 */
	public long setFeedImage(Context context, FeedImage image) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		adapter.open();
		long result = adapter.setImage(image);
		adapter.close();
		return result;
	}

	/**
	 * Updates information of an existing FeedMedia object. Creates and opens
	 * its own adapter.
	 */
	public long setFeedMedia(Context context, FeedMedia media) {
		PodDBAdapter adapter = new PodDBAdapter(context);
		adapter.open();
		long result = adapter.setMedia(media);
		adapter.close();
		return result;
	}

	/** Get a Feed by its id */
	public Feed getFeed(long id) {
		for (Feed f : feeds) {
			if (f.id == id) {
				return f;
			}
		}
		Log.e(TAG, "Couldn't find Feed with id " + id);
		return null;
	}

	/** Get a Feed Image by its id */
	public FeedImage getFeedImage(long id) {
		for (Feed f : feeds) {
			FeedImage image = f.getImage();
			if (image != null && image.getId() == id) {
				return image;
			}
		}
		return null;
	}

	/** Get a Feed Item by its id and its feed */
	public FeedItem getFeedItem(long id, Feed feed) {
		for (FeedItem item : feed.getItems()) {
			if (item.getId() == id) {
				return item;
			}
		}
		Log.e(TAG, "Couldn't find FeedItem with id " + id);
		return null;
	}

	/** Get a FeedMedia object by the id of the Media object and the feed object */
	public FeedMedia getFeedMedia(long id, Feed feed) {
		if (feed != null) {
			for (FeedItem item : feed.getItems()) {
				if (item.getMedia().getId() == id) {
					return item.getMedia();
				}
			}
		}
		Log.e(TAG, "Couldn't find FeedMedia with id " + id);
		if (feed == null)
			Log.e(TAG, "Feed was null");
		return null;
	}

	/** Get a FeedMedia object by the id of the Media object. */
	public FeedMedia getFeedMedia(long id) {
		for (Feed feed : feeds) {
			for (FeedItem item : feed.getItems()) {
				if (item.getMedia() != null && item.getMedia().getId() == id) {
					return item.getMedia();
				}
			}
		}
		Log.w(TAG, "Couldn't find FeedMedia with id " + id);
		return null;
	}

	public DownloadStatus getDownloadStatus(long statusId) {
		for (DownloadStatus status : downloadLog) {
			if (status.getId() == statusId) {
				return status;
			}
		}
		return null;
	}

	/** Reads the database */
	public void loadDBData(Context context) {
		updateArrays(context);
	}

	public void updateArrays(Context context) {
		feeds.clear();
		categories.clear();
		PodDBAdapter adapter = new PodDBAdapter(context);
		adapter.open();
		extractFeedlistFromCursor(context, adapter);
		extractDownloadLogFromCursor(context, adapter);
		extractQueueFromCursor(context, adapter);
		adapter.close();
		Collections.sort(feeds, new FeedtitleComparator());
		Collections.sort(unreadItems, new FeedItemPubdateComparator());
	}

	private void extractFeedlistFromCursor(Context context, PodDBAdapter adapter) {
		Log.d(TAG, "Extracting Feedlist");
		Cursor feedlistCursor = adapter.getAllFeedsCursor();
		if (feedlistCursor.moveToFirst()) {
			do {
				Date lastUpdate = new Date(
						feedlistCursor.getLong(feedlistCursor
								.getColumnIndex(PodDBAdapter.KEY_LASTUPDATE)));
				Feed feed = new Feed(lastUpdate);

				feed.id = feedlistCursor.getLong(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_ID));
				feed.setTitle(feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_TITLE)));
				feed.setLink(feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_LINK)));
				feed.setDescription(feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION)));
				feed.setPaymentLink(feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_PAYMENT_LINK)));
				feed.setAuthor(feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_AUTHOR)));
				feed.setLanguage(feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_LANGUAGE)));
				feed.setImage(adapter.getFeedImage(feedlistCursor
						.getLong(feedlistCursor
								.getColumnIndex(PodDBAdapter.KEY_IMAGE))));
				feed.file_url = feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_FILE_URL));
				feed.download_url = feedlistCursor.getString(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL));
				feed.setDownloaded(feedlistCursor.getInt(feedlistCursor
						.getColumnIndex(PodDBAdapter.KEY_DOWNLOADED)) > 0);
				// Get FeedItem-Object
				Cursor itemlistCursor = adapter.getAllItemsOfFeedCursor(feed);
				feed.setItems(extractFeedItemsFromCursor(context, feed,
						itemlistCursor, adapter));
				itemlistCursor.close();

				feeds.add(feed);
			} while (feedlistCursor.moveToNext());
		}
		feedlistCursor.close();

	}

	private ArrayList<FeedItem> extractFeedItemsFromCursor(Context context,
			Feed feed, Cursor itemlistCursor, PodDBAdapter adapter) {
		Log.d(TAG, "Extracting Feeditems of feed " + feed.getTitle());
		ArrayList<FeedItem> items = new ArrayList<FeedItem>();
		if (itemlistCursor.moveToFirst()) {
			do {
				FeedItem item = new FeedItem();

				item.id = itemlistCursor.getLong(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_ID));
				item.setFeed(feed);
				item.setTitle(itemlistCursor.getString(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_TITLE)));
				item.setLink(itemlistCursor.getString(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_LINK)));
				item.setDescription(itemlistCursor.getString(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION)));
				item.setContentEncoded(itemlistCursor.getString(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_CONTENT_ENCODED)));
				item.setPubDate(new Date(itemlistCursor.getLong(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_PUBDATE))));
				item.setPaymentLink(itemlistCursor.getString(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_PAYMENT_LINK)));
				long mediaId = itemlistCursor.getLong(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_MEDIA));
				if (mediaId != 0) {
					item.setMedia(adapter.getFeedMedia(mediaId, item));
				}
				item.read = (itemlistCursor.getInt(itemlistCursor
						.getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true
						: false;
				if (!item.read) {
					unreadItems.add(item);
				}

				// extract chapters
				Cursor chapterCursor = adapter
						.getSimpleChaptersOfFeedItemCursor(item);
				if (chapterCursor.moveToFirst()) {
					item.setSimpleChapters(new ArrayList<SimpleChapter>());
					do {
						SimpleChapter chapter = new SimpleChapter(
								chapterCursor
										.getLong(chapterCursor
												.getColumnIndex(PodDBAdapter.KEY_START)),
								chapterCursor.getString(chapterCursor
										.getColumnIndex(PodDBAdapter.KEY_TITLE)));
						item.getSimpleChapters().add(chapter);
					} while (chapterCursor.moveToNext());
				}
				chapterCursor.close();

				items.add(item);
			} while (itemlistCursor.moveToNext());
		}
		Collections.sort(items, new FeedItemPubdateComparator());
		return items;
	}

	private void extractDownloadLogFromCursor(Context context,
			PodDBAdapter adapter) {
		Log.d(TAG, "Extracting DownloadLog");
		Cursor logCursor = adapter.getDownloadLogCursor();
		if (logCursor.moveToFirst()) {
			do {
				long id = logCursor.getLong(logCursor
						.getColumnIndex(PodDBAdapter.KEY_ID));
				long feedfileId = logCursor.getLong(logCursor
						.getColumnIndex(PodDBAdapter.KEY_FEEDFILE));
				int feedfileType = logCursor.getInt(logCursor
						.getColumnIndex(PodDBAdapter.KEY_FEEDFILETYPE));
				FeedFile feedfile = null;
				switch (feedfileType) {
				case PodDBAdapter.FEEDFILETYPE_FEED:
					feedfile = getFeed(feedfileId);
					break;
				case PodDBAdapter.FEEDFILETYPE_FEEDIMAGE:
					feedfile = getFeedImage(feedfileId);
					break;
				case PodDBAdapter.FEEDFILETYPE_FEEDMEDIA:
					feedfile = getFeedMedia(feedfileId);
				}
				if (feedfile != null) { // otherwise ignore status
					boolean successful = logCursor.getInt(logCursor
							.getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL)) > 0;
					int reason = logCursor.getInt(logCursor
							.getColumnIndex(PodDBAdapter.KEY_REASON));
					Date completionDate = new Date(logCursor.getLong(logCursor
							.getColumnIndex(PodDBAdapter.KEY_COMPLETION_DATE)));
					downloadLog.add(new DownloadStatus(id, feedfile,
							successful, reason, completionDate));
				}

			} while (logCursor.moveToNext());
		}
		logCursor.close();
	}

	private void extractQueueFromCursor(Context context, PodDBAdapter adapter) {
		Log.d(TAG, "Extracting Queue");
		Cursor cursor = adapter.getQueueCursor();
		if (cursor.moveToFirst()) {
			do {
				int index = cursor.getInt(cursor
						.getColumnIndex(PodDBAdapter.KEY_ID));
				Feed feed = getFeed(cursor.getLong(cursor
						.getColumnIndex(PodDBAdapter.KEY_FEED)));
				if (feed != null) {
					FeedItem item = getFeedItem(cursor.getLong(cursor
							.getColumnIndex(PodDBAdapter.KEY_FEEDITEM)), feed);
					if (item != null) {
						queue.add(index, item);
					}
				}

			} while (cursor.moveToNext());
		}
		cursor.close();
	}

	public ArrayList<Feed> getFeeds() {
		return feeds;
	}

	public ArrayList<FeedItem> getUnreadItems() {
		return unreadItems;
	}

	public ArrayList<DownloadStatus> getDownloadLog() {
		return downloadLog;
	}

	public ArrayList<FeedItem> getQueue() {
		return queue;
	}

}