summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/service/download/DownloadService.java
blob: aac40a664f8d728870dd6f2644bd42abbd9e1fab (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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/** 
 * Registers a DownloadReceiver and waits for all Downloads 
 * to complete, then stops
 * */

package de.danoeh.antennapod.service.download;

import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.webkit.URLUtil;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.activity.DownloadActivity;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.asynctask.DownloadStatus;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedFile;
import de.danoeh.antennapod.feed.FeedImage;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.storage.DownloadRequester;
import de.danoeh.antennapod.syndication.handler.FeedHandler;
import de.danoeh.antennapod.syndication.handler.UnsupportedFeedtypeException;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.InvalidFeedException;

public class DownloadService extends Service {
	private static final String TAG = "DownloadService";

	public static String ACTION_ALL_FEED_DOWNLOADS_COMPLETED = "action.de.danoeh.antennapod.storage.all_feed_downloads_completed";

	public static final String ACTION_ENQUEUE_DOWNLOAD = "action.de.danoeh.antennapod.service.enqueueDownload";
	public static final String ACTION_CANCEL_DOWNLOAD = "action.de.danoeh.antennapod.service.cancelDownload";
	public static final String ACTION_CANCEL_ALL_DOWNLOADS = "action.de.danoeh.antennapod.service.cancelAllDownloads";

	/** Is used for sending the delete intent for the report notification */
	private static final String ACTION_REPORT_DELETED = "action.de.danoeh.antennapod.service.reportDeleted";

	/** Extra for ACTION_CANCEL_DOWNLOAD */
	public static final String EXTRA_DOWNLOAD_URL = "downloadUrl";

	public static final String ACTION_DOWNLOAD_HANDLED = "action.de.danoeh.antennapod.service.download_handled";
	/**
	 * Sent by the DownloadService when the content of the downloads list
	 * changes.
	 */
	public static final String ACTION_DOWNLOADS_CONTENT_CHANGED = "action.de.danoeh.antennapod.service.downloadsContentChanged";

	public static final String EXTRA_DOWNLOAD_ID = "extra.de.danoeh.antennapod.service.download_id";

	/** Extra for ACTION_ENQUEUE_DOWNLOAD intent. */
	public static final String EXTRA_REQUEST = "request";

	// Download types for ACTION_DOWNLOAD_HANDLED
	public static final String EXTRA_DOWNLOAD_TYPE = "extra.de.danoeh.antennapod.service.downloadType";
	public static final int DOWNLOAD_TYPE_FEED = 1;
	public static final int DOWNLOAD_TYPE_MEDIA = 2;
	public static final int DOWNLOAD_TYPE_IMAGE = 3;

	private CopyOnWriteArrayList<DownloadStatus> completedDownloads;

	private ExecutorService syncExecutor;
	private ExecutorService downloadExecutor;
	/** Number of threads of downloadExecutor. */
	private static final int NUM_PARALLEL_DOWNLOADS = 4;

	private DownloadRequester requester;
	private FeedManager manager;
	private NotificationCompat.Builder notificationBuilder;
	private int NOTIFICATION_ID = 2;
	private int REPORT_ID = 3;
	/** Needed to determine the duration of a media file */
	private MediaPlayer mediaplayer;

	private List<Downloader> downloads;

	/** Number of completed downloads which are currently being handled. */
	private volatile int downloadsBeingHandled;

	private volatile boolean shutdownInitiated = false;
	/** True if service is running. */
	public static boolean isRunning = false;

	private Handler handler;

	private final IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public DownloadService getService() {
			return DownloadService.this;
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent.getParcelableExtra(EXTRA_REQUEST) != null) {
			onDownloadQueued(intent);
		}
		return Service.START_NOT_STICKY;
	}

	@SuppressLint("NewApi")
	@Override
	public void onCreate() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Service started");
		isRunning = true;
		handler = new Handler();
		completedDownloads = new CopyOnWriteArrayList<DownloadStatus>(
				new ArrayList<DownloadStatus>());
		downloads = new ArrayList<Downloader>();
		registerReceiver(downloadQueued, new IntentFilter(
				ACTION_ENQUEUE_DOWNLOAD));

		IntentFilter cancelDownloadReceiverFilter = new IntentFilter();
		cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_ALL_DOWNLOADS);
		cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD);
		registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter);
		registerReceiver(reportDeleted, new IntentFilter(ACTION_REPORT_DELETED));
		syncExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {

			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				t.setPriority(Thread.MIN_PRIORITY);
				t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

					@Override
					public void uncaughtException(Thread thread, Throwable ex) {
						Log.e(TAG, "Thread exited with uncaught exception");
						ex.printStackTrace();
						downloadsBeingHandled -= 1;
						queryDownloads();
					}
				});
				return t;
			}
		});
		downloadExecutor = Executors.newFixedThreadPool(NUM_PARALLEL_DOWNLOADS,
				new ThreadFactory() {

					@Override
					public Thread newThread(Runnable r) {
						Thread t = new Thread(r);
						t.setPriority(Thread.MIN_PRIORITY);
						return t;
					}
				});
		manager = FeedManager.getInstance();
		requester = DownloadRequester.getInstance();
		mediaplayer = new MediaPlayer();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onDestroy() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Service shutting down");
		isRunning = false;
		mediaplayer.release();
		unregisterReceiver(cancelDownloadReceiver);
		unregisterReceiver(downloadQueued);
	}

	private void setupNotification() {
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(
				this, DownloadActivity.class),
				PendingIntent.FLAG_UPDATE_CURRENT);

		Bitmap icon = BitmapFactory.decodeResource(null,
				android.R.drawable.stat_notify_sync_noanim);
		notificationBuilder = new NotificationCompat.Builder(this)
				.setContentTitle("Downloading Podcast data")
				.setContentText(
						requester.getNumberOfDownloads() + " Downloads left")
				.setOngoing(true).setContentIntent(pIntent).setLargeIcon(icon)
				.setSmallIcon(android.R.drawable.stat_notify_sync_noanim);

		startForeground(NOTIFICATION_ID, notificationBuilder.getNotification());
		if (AppConfig.DEBUG)
			Log.d(TAG, "Notification set up");
	}

	private Downloader getDownloader(String downloadUrl) {
		for (Downloader downloader : downloads) {
			if (downloader.getStatus().getFeedFile().getDownload_url()
					.equals(downloadUrl)) {
				return downloader;
			}
		}
		return null;
	}

	private BroadcastReceiver cancelDownloadReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(ACTION_CANCEL_DOWNLOAD)) {
				String url = intent.getStringExtra(EXTRA_DOWNLOAD_URL);
				if (url == null) {
					throw new IllegalArgumentException(
							"ACTION_CANCEL_DOWNLOAD intent needs download url extra");
				}
				if (AppConfig.DEBUG)
					Log.d(TAG, "Cancelling download with url " + url);
				Downloader d = getDownloader(url);
				if (d != null) {
					d.interrupt();
					removeDownload(d);
				} else {
					Log.e(TAG, "Could not cancel download with url " + url);
				}

			} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
				for (Downloader d : downloads) {
					d.interrupt();
					DownloadRequester.getInstance().removeDownload(
							d.getStatus().getFeedFile());
					d.getStatus().getFeedFile().setFile_url(null);
					if (AppConfig.DEBUG)
						Log.d(TAG, "Cancelled all downloads");
				}
				downloads.clear();
				sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));

			}
			queryDownloads();
		}

	};

	private void onDownloadQueued(Intent intent) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Received enqueue request");
		Request request = intent.getParcelableExtra(EXTRA_REQUEST);
		if (request == null) {
			throw new IllegalArgumentException(
					"ACTION_ENQUEUE_DOWNLOAD intent needs request extra");
		}
		if (shutdownInitiated) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Cancelling shutdown; new download was queued");
			shutdownInitiated = false;
			setupNotification();
		}

		DownloadRequester requester = DownloadRequester.getInstance();
		FeedFile feedfile = requester.getDownload(request.source);
		if (feedfile != null) {

			DownloadStatus status = new DownloadStatus(feedfile);
			Downloader downloader = getDownloader(status);
			if (downloader != null) {
				downloads.add(downloader);
				downloadExecutor.submit(downloader);
				sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
			}
		} else {
			Log.e(TAG,
					"Could not find feedfile in download requester when trying to enqueue new download");
		}
		queryDownloads();
	}

	private BroadcastReceiver downloadQueued = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			onDownloadQueued(intent);
		}

	};

	private Downloader getDownloader(DownloadStatus status) {
		if (URLUtil.isHttpUrl(status.getFeedFile().getDownload_url())) {
			return new HttpDownloader(this, status);
		}
		Log.e(TAG, "Could not find appropriate downloader for "
				+ status.getFeedFile().getDownload_url());
		return null;
	}

	@SuppressLint("NewApi")
	public void onDownloadCompleted(final Downloader downloader) {
		final AsyncTask<Void, Void, Void> handlerTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Received 'Download Complete' - message.");
				downloadsBeingHandled += 1;
				DownloadStatus status = downloader.getStatus();
				status.setCompletionDate(new Date());
				boolean successful = status.isSuccessful();
				int reason = status.getReason();

				FeedFile download = status.getFeedFile();
				if (download != null) {
					if (successful) {
						if (download.getClass() == Feed.class) {
							handleCompletedFeedDownload(status);
						} else if (download.getClass() == FeedImage.class) {
							handleCompletedImageDownload(status);
						} else if (download.getClass() == FeedMedia.class) {
							handleCompletedFeedMediaDownload(status);
						}
					} else {
						if (!successful
								&& reason != DownloadError.ERROR_DOWNLOAD_CANCELLED) {
							Log.e(TAG, "Download failed");
						}
						download.setFile_url(null);
						download.setDownloaded(false);
						saveDownloadStatus(status);
						sendDownloadHandledIntent(getDownloadType(download));
						downloadsBeingHandled -= 1;
					}
				}
				removeDownload(downloader);
				if (!successful) {
					queryDownloads();
				}
				return null;
			}
		};
		if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
			handlerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else {
			handlerTask.execute();
		}
	}

	/**
	 * Remove download from the DownloadRequester list and from the
	 * DownloadService list.
	 */
	private void removeDownload(final Downloader d) {
		downloads.remove(d);
		DownloadRequester.getInstance().removeDownload(
				d.getStatus().getFeedFile());
		sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
	}

	/**
	 * Adds a new DownloadStatus object to the list of completed downloads and
	 * saves it in the database
	 * 
	 * @param status
	 *            the download that is going to be saved
	 */
	private void saveDownloadStatus(DownloadStatus status) {
		completedDownloads.add(status);
		manager.addDownloadStatus(this, status);
	}

	/** Returns correct value for EXTRA_DOWNLOAD_TYPE. */
	private int getDownloadType(FeedFile f) {
		if (f.getClass() == Feed.class) {
			return DOWNLOAD_TYPE_FEED;
		} else if (f.getClass() == FeedImage.class) {
			return DOWNLOAD_TYPE_IMAGE;
		} else if (f.getClass() == FeedMedia.class) {
			return DOWNLOAD_TYPE_MEDIA;
		} else {
			return 0;
		}
	}

	private void sendDownloadHandledIntent(int type) {
		Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED);
		intent.putExtra(EXTRA_DOWNLOAD_TYPE, type);

		sendBroadcast(intent);
	}

	private BroadcastReceiver reportDeleted = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(ACTION_REPORT_DELETED)) {
				completedDownloads.clear();
			}
		}
	};

	/**
	 * Creates a notification at the end of the service lifecycle to notify the
	 * user about the number of completed downloads. A report will only be
	 * created if the number of feeds is > 1 or if at least one media file was
	 * downloaded.
	 */
	private void updateReport() {
		// check if report should be created
		if (!completedDownloads.isEmpty()) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Creating report");
			int successfulDownloads = 0;
			int failedDownloads = 0;
			for (DownloadStatus status : completedDownloads) {
				if (status.isSuccessful()) {
					successfulDownloads++;
				} else {
					failedDownloads++;
				}
			}
			// create notification object
			Notification notification = new NotificationCompat.Builder(this)
					.setTicker(
							getString(de.danoeh.antennapod.R.string.download_report_title))
					.setContentTitle(
							getString(de.danoeh.antennapod.R.string.download_report_title))
					.setContentText(
							successfulDownloads + " Downloads succeeded, "
									+ failedDownloads + " failed")
					.setSmallIcon(android.R.drawable.stat_notify_sync)
					.setLargeIcon(
							BitmapFactory.decodeResource(null,
									android.R.drawable.stat_notify_sync))
					.setContentIntent(
							PendingIntent.getActivity(this, 0, new Intent(this,
									MainActivity.class), 0))
					.setAutoCancel(true)
					.setDeleteIntent(
							PendingIntent.getBroadcast(this, 0, new Intent(
									ACTION_REPORT_DELETED),
									PendingIntent.FLAG_UPDATE_CURRENT))
					.getNotification();
			NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			nm.notify(REPORT_ID, notification);

		} else {
			if (AppConfig.DEBUG)
				Log.d(TAG, "No report is created");
		}
	}

	/** Check if there's something else to download, otherwise stop */
	void queryDownloads() {
		int numOfDownloads = downloads.size();
		if (AppConfig.DEBUG) {
			Log.d(TAG, numOfDownloads + " downloads left");
			Log.d(TAG, "Downloads being handled: " + downloadsBeingHandled);
			Log.d(TAG, "ShutdownInitiated: " + shutdownInitiated);
		}

		if (numOfDownloads == 0 && downloadsBeingHandled <= 0) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Starting shutdown");
			shutdownInitiated = true;
			updateReport();
			stopForeground(true);
		} else {
			if (notificationBuilder != null) {
				// update notification
				notificationBuilder.setContentText(numOfDownloads
						+ " Downloads left");
				NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				nm.notify(NOTIFICATION_ID,
						notificationBuilder.getNotification());
			} else {
				setupNotification();
			}
		}
	}

	/** Is called whenever a Feed is downloaded */
	private void handleCompletedFeedDownload(DownloadStatus status) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Handling completed Feed Download");
		syncExecutor.execute(new FeedSyncThread(status));

	}

	/** Is called whenever a Feed-Image is downloaded */
	private void handleCompletedImageDownload(DownloadStatus status) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Handling completed Image Download");
		syncExecutor.execute(new ImageHandlerThread(status));
	}

	/** Is called whenever a FeedMedia is downloaded. */
	private void handleCompletedFeedMediaDownload(DownloadStatus status) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Handling completed FeedMedia Download");
		syncExecutor.execute(new MediaHandlerThread(status));
	}

	/**
	 * Takes a single Feed, parses the corresponding file and refreshes
	 * information in the manager
	 */
	class FeedSyncThread implements Runnable {
		private static final String TAG = "FeedSyncThread";

		private Feed feed;
		private DownloadStatus status;

		private int reason;
		private boolean successful;

		public FeedSyncThread(DownloadStatus status) {
			this.feed = (Feed) status.getFeedFile();
			this.status = status;
		}

		public void run() {
			Feed savedFeed = null;
			reason = 0;
			successful = true;
			FeedManager manager = FeedManager.getInstance();
			FeedHandler handler = new FeedHandler();
			feed.setDownloaded(true);

			try {
				feed = handler.parseFeed(feed);
				if (AppConfig.DEBUG)
					Log.d(TAG, feed.getTitle() + " parsed");
				if (checkFeedData(feed) == false) {
					throw new InvalidFeedException();
				}
				// Save information of feed in DB
				savedFeed = manager.updateFeed(DownloadService.this, feed);
				// Download Feed Image if provided and not downloaded
				if (savedFeed.getImage() != null
						&& savedFeed.getImage().isDownloaded() == false) {
					if (AppConfig.DEBUG)
						Log.d(TAG, "Feed has image; Downloading....");
					savedFeed.getImage().setFeed(savedFeed);
					requester.downloadImage(DownloadService.this,
							savedFeed.getImage());
				}

			} catch (SAXException e) {
				successful = false;
				e.printStackTrace();
				reason = DownloadError.ERROR_PARSER_EXCEPTION;
			} catch (IOException e) {
				successful = false;
				e.printStackTrace();
				reason = DownloadError.ERROR_PARSER_EXCEPTION;
			} catch (ParserConfigurationException e) {
				successful = false;
				e.printStackTrace();
				reason = DownloadError.ERROR_PARSER_EXCEPTION;
			} catch (UnsupportedFeedtypeException e) {
				e.printStackTrace();
				successful = false;
				reason = DownloadError.ERROR_UNSUPPORTED_TYPE;
			} catch (InvalidFeedException e) {
				e.printStackTrace();
				successful = false;
				reason = DownloadError.ERROR_PARSER_EXCEPTION;
			}

			// cleanup();
			if (savedFeed == null) {
				savedFeed = feed;
			}

			saveDownloadStatus(new DownloadStatus(savedFeed, reason, successful));
			sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED);
			downloadsBeingHandled -= 1;
			queryDownloads();
		}

		/** Checks if the feed was parsed correctly. */
		private boolean checkFeedData(Feed feed) {
			if (feed.getTitle() == null) {
				Log.e(TAG, "Feed has no title.");
				return false;
			} else {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Feed appears to be valid.");
				return true;
			}
		}

		/** Delete files that aren't needed anymore */
		private void cleanup() {
			if (feed.getFile_url() != null) {
				if (new File(feed.getFile_url()).delete())
					if (AppConfig.DEBUG)
						Log.d(TAG, "Successfully deleted cache file.");
					else
						Log.e(TAG, "Failed to delete cache file.");
				feed.setFile_url(null);
			} else if (AppConfig.DEBUG) {
				Log.d(TAG, "Didn't delete cache file: File url is not set.");
			}
		}

	}

	/** Handles a completed image download. */
	class ImageHandlerThread implements Runnable {
		private FeedImage image;
		private DownloadStatus status;

		public ImageHandlerThread(DownloadStatus status) {
			this.image = (FeedImage) status.getFeedFile();
			this.status = status;
		}

		@Override
		public void run() {
			image.setDownloaded(true);

			saveDownloadStatus(status);
			sendDownloadHandledIntent(DOWNLOAD_TYPE_IMAGE);
			manager.setFeedImage(DownloadService.this, image);
			if (image.getFeed() != null) {
				manager.setFeed(DownloadService.this, image.getFeed());
			} else {
				Log.e(TAG,
						"Image has no feed, image might not be saved correctly!");
			}
			downloadsBeingHandled -= 1;
			queryDownloads();
		}
	}

	/** Handles a completed media download. */
	class MediaHandlerThread implements Runnable {
		private FeedMedia media;
		private DownloadStatus status;

		public MediaHandlerThread(DownloadStatus status) {
			super();
			this.media = (FeedMedia) status.getFeedFile();
			this.status = status;
		}

		@Override
		public void run() {
			media.setDownloaded(true);
			// Get duration
			try {
				mediaplayer.setDataSource(media.getFile_url());
				mediaplayer.prepare();
			} catch (IOException e) {
				e.printStackTrace();
			}
			media.setDuration(mediaplayer.getDuration());
			if (AppConfig.DEBUG)
				Log.d(TAG, "Duration of file is " + media.getDuration());
			mediaplayer.reset();

			saveDownloadStatus(status);
			sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA);
			manager.setFeedMedia(DownloadService.this, media);
			boolean autoQueue = PreferenceManager.getDefaultSharedPreferences(
					getApplicationContext()).getBoolean(
					PodcastApp.PREF_AUTO_QUEUE, true);

			if (!manager.isInQueue(media.getItem())) {
				// Auto-queue
				if (autoQueue) {
					if (AppConfig.DEBUG)
						Log.d(TAG, "Autoqueue is enabled. Adding item to queue");
					manager.addQueueItem(DownloadService.this, media.getItem());
				} else {
					if (AppConfig.DEBUG)
						Log.d(TAG, "Autoqueue is disabled");
				}
			} else {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Item is already in queue");
			}
			downloadsBeingHandled -= 1;
			queryDownloads();
		}
	}

	/** Is used to request a new download. */
	public static class Request implements Parcelable {
		private String destination;
		private String source;

		public Request(String destination, String source) {
			super();
			this.destination = destination;
			this.source = source;
		}

		private Request(Parcel in) {
			destination = in.readString();
			source = in.readString();
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(destination);
			dest.writeString(source);
		}

		public static final Parcelable.Creator<Request> CREATOR = new Parcelable.Creator<Request>() {
			public Request createFromParcel(Parcel in) {
				return new Request(in);
			}

			public Request[] newArray(int size) {
				return new Request[size];
			}
		};

		public String getDestination() {
			return destination;
		}

		public String getSource() {
			return source;
		}

	}

	public List<Downloader> getDownloads() {
		return downloads;
	}

}