DownloadService will now create a report when finished
This commit is contained in:
parent
fcbb28981f
commit
d4188088ce
|
@ -91,5 +91,6 @@
|
||||||
<string name="pref_pauseOnHeadsetDisconnect_title">Headset disconnect</string>
|
<string name="pref_pauseOnHeadsetDisconnect_title">Headset disconnect</string>
|
||||||
<string name="pref_mobileUpdate_title">Mobile Updates</string>
|
<string name="pref_mobileUpdate_title">Mobile Updates</string>
|
||||||
<string name="pref_mobileUpdate_sum">Allow updates over the mobile data connection</string>
|
<string name="pref_mobileUpdate_sum">Allow updates over the mobile data connection</string>
|
||||||
|
<string name="download_report_title">All downloads completed</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
|
@ -7,6 +7,7 @@ package de.podfetcher.service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -17,6 +18,7 @@ import org.xml.sax.SAXException;
|
||||||
|
|
||||||
import de.podfetcher.activity.DownloadActivity;
|
import de.podfetcher.activity.DownloadActivity;
|
||||||
import de.podfetcher.activity.MediaplayerActivity;
|
import de.podfetcher.activity.MediaplayerActivity;
|
||||||
|
import de.podfetcher.activity.PodfetcherActivity;
|
||||||
import de.podfetcher.asynctask.DownloadObserver;
|
import de.podfetcher.asynctask.DownloadObserver;
|
||||||
import de.podfetcher.asynctask.DownloadStatus;
|
import de.podfetcher.asynctask.DownloadStatus;
|
||||||
import de.podfetcher.feed.*;
|
import de.podfetcher.feed.*;
|
||||||
|
@ -26,6 +28,7 @@ import de.podfetcher.syndication.handler.FeedHandler;
|
||||||
import de.podfetcher.syndication.handler.UnsupportedFeedtypeException;
|
import de.podfetcher.syndication.handler.UnsupportedFeedtypeException;
|
||||||
import de.podfetcher.util.DownloadError;
|
import de.podfetcher.util.DownloadError;
|
||||||
import android.R;
|
import android.R;
|
||||||
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
@ -61,11 +64,14 @@ public class DownloadService extends Service {
|
||||||
public static final String EXTRA_DOWNLOAD_ID = "extra.de.podfetcher.service.download_id";
|
public static final String EXTRA_DOWNLOAD_ID = "extra.de.podfetcher.service.download_id";
|
||||||
public static final String EXTRA_IMAGE_DOWNLOAD_ID = "extra.de.podfetcher.service.image_download_id";
|
public static final String EXTRA_IMAGE_DOWNLOAD_ID = "extra.de.podfetcher.service.image_download_id";
|
||||||
|
|
||||||
|
private ArrayList<DownloadStatus> completedDownloads;
|
||||||
|
|
||||||
private ExecutorService syncExecutor;
|
private ExecutorService syncExecutor;
|
||||||
private DownloadRequester requester;
|
private DownloadRequester requester;
|
||||||
private FeedManager manager;
|
private FeedManager manager;
|
||||||
private NotificationCompat.Builder notificationBuilder;
|
private NotificationCompat.Builder notificationBuilder;
|
||||||
private int NOTIFICATION_ID = 2;
|
private int NOTIFICATION_ID = 2;
|
||||||
|
private int REPORT_ID = 3;
|
||||||
/** Needed to determine the duration of a media file */
|
/** Needed to determine the duration of a media file */
|
||||||
private MediaPlayer mediaplayer;
|
private MediaPlayer mediaplayer;
|
||||||
private DownloadManager downloadManager;
|
private DownloadManager downloadManager;
|
||||||
|
@ -94,6 +100,7 @@ public class DownloadService extends Service {
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
Log.d(TAG, "Service started");
|
Log.d(TAG, "Service started");
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
|
completedDownloads = new ArrayList<DownloadStatus>();
|
||||||
registerReceiver(downloadReceiver, createIntentFilter());
|
registerReceiver(downloadReceiver, createIntentFilter());
|
||||||
syncExecutor = Executors.newSingleThreadExecutor();
|
syncExecutor = Executors.newSingleThreadExecutor();
|
||||||
manager = FeedManager.getInstance();
|
manager = FeedManager.getInstance();
|
||||||
|
@ -118,6 +125,7 @@ public class DownloadService extends Service {
|
||||||
mediaplayer.release();
|
mediaplayer.release();
|
||||||
unregisterReceiver(downloadReceiver);
|
unregisterReceiver(downloadReceiver);
|
||||||
downloadObserver.cancel(true);
|
downloadObserver.cancel(true);
|
||||||
|
createReport();
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntentFilter createIntentFilter() {
|
private IntentFilter createIntentFilter() {
|
||||||
|
@ -206,8 +214,8 @@ public class DownloadService extends Service {
|
||||||
Log.e(TAG, "Download failed");
|
Log.e(TAG, "Download failed");
|
||||||
Log.e(TAG, "reason code is " + reason);
|
Log.e(TAG, "reason code is " + reason);
|
||||||
successful = false;
|
successful = false;
|
||||||
long statusId = manager.addDownloadStatus(context,
|
long statusId = saveDownloadStatus(new DownloadStatus(
|
||||||
new DownloadStatus(download, reason, successful));
|
download, reason, successful));
|
||||||
requester.removeDownload(download);
|
requester.removeDownload(download);
|
||||||
sendDownloadHandledIntent(download.getDownloadId(),
|
sendDownloadHandledIntent(download.getDownloadId(),
|
||||||
statusId, false, 0);
|
statusId, false, 0);
|
||||||
|
@ -221,6 +229,18 @@ public class DownloadService extends Service {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 long saveDownloadStatus(DownloadStatus status) {
|
||||||
|
completedDownloads.add(status);
|
||||||
|
return manager.addDownloadStatus(this, status);
|
||||||
|
}
|
||||||
|
|
||||||
private void sendDownloadHandledIntent(long downloadId, long statusId,
|
private void sendDownloadHandledIntent(long downloadId, long statusId,
|
||||||
boolean feedHasImage, long imageDownloadId) {
|
boolean feedHasImage, long imageDownloadId) {
|
||||||
Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED);
|
Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED);
|
||||||
|
@ -233,6 +253,64 @@ public class DownloadService extends Service {
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 createReport() {
|
||||||
|
// check if report should be created
|
||||||
|
boolean createReport = false;
|
||||||
|
int feedCount = 0;
|
||||||
|
for (DownloadStatus status : completedDownloads) {
|
||||||
|
if (status.getFeedFile().getClass() == Feed.class) {
|
||||||
|
feedCount++;
|
||||||
|
if (feedCount > 1) {
|
||||||
|
createReport = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (status.getFeedFile().getClass() == FeedMedia.class) {
|
||||||
|
createReport = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (createReport) {
|
||||||
|
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.podfetcher.R.string.download_report_title))
|
||||||
|
.setContentTitle(
|
||||||
|
getString(de.podfetcher.R.string.download_report_title))
|
||||||
|
.setContentText(
|
||||||
|
successfulDownloads + " Downloads succeeded, "
|
||||||
|
+ failedDownloads + " failed")
|
||||||
|
.setSmallIcon(R.drawable.stat_notify_sync)
|
||||||
|
.setLargeIcon(
|
||||||
|
BitmapFactory.decodeResource(null,
|
||||||
|
R.drawable.stat_notify_sync))
|
||||||
|
.setContentIntent(
|
||||||
|
PendingIntent.getActivity(this, 0, new Intent(this,
|
||||||
|
PodfetcherActivity.class), 0))
|
||||||
|
.setAutoCancel(true).getNotification();
|
||||||
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(REPORT_ID, notification);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "No report is created");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Check if there's something else to download, otherwise stop */
|
/** Check if there's something else to download, otherwise stop */
|
||||||
public synchronized void queryDownloads() {
|
public synchronized void queryDownloads() {
|
||||||
int numOfDownloads = requester.getNumberOfDownloads();
|
int numOfDownloads = requester.getNumberOfDownloads();
|
||||||
|
@ -328,8 +406,8 @@ public class DownloadService extends Service {
|
||||||
|
|
||||||
requester.removeDownload(feed);
|
requester.removeDownload(feed);
|
||||||
cleanup();
|
cleanup();
|
||||||
long statusId = manager.addDownloadStatus(service,
|
long statusId = saveDownloadStatus(new DownloadStatus(savedFeed,
|
||||||
new DownloadStatus(savedFeed, reason, successful));
|
reason, successful));
|
||||||
sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId);
|
sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId);
|
||||||
queryDownloads();
|
queryDownloads();
|
||||||
}
|
}
|
||||||
|
@ -360,8 +438,8 @@ public class DownloadService extends Service {
|
||||||
image.setDownloaded(true);
|
image.setDownloaded(true);
|
||||||
requester.removeDownload(image);
|
requester.removeDownload(image);
|
||||||
|
|
||||||
long statusId = manager.addDownloadStatus(service,
|
long statusId = saveDownloadStatus(new DownloadStatus(image, 0,
|
||||||
new DownloadStatus(image, 0, true));
|
true));
|
||||||
sendDownloadHandledIntent(image.getDownloadId(), statusId, false, 0);
|
sendDownloadHandledIntent(image.getDownloadId(), statusId, false, 0);
|
||||||
image.setDownloadId(0);
|
image.setDownloadId(0);
|
||||||
|
|
||||||
|
@ -395,8 +473,8 @@ public class DownloadService extends Service {
|
||||||
media.setDuration(mediaplayer.getDuration());
|
media.setDuration(mediaplayer.getDuration());
|
||||||
Log.d(TAG, "Duration of file is " + media.getDuration());
|
Log.d(TAG, "Duration of file is " + media.getDuration());
|
||||||
mediaplayer.reset();
|
mediaplayer.reset();
|
||||||
long statusId = manager.addDownloadStatus(service,
|
long statusId = saveDownloadStatus(new DownloadStatus(media, 0,
|
||||||
new DownloadStatus(media, 0, true));
|
true));
|
||||||
sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0);
|
sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0);
|
||||||
media.setDownloadId(0);
|
media.setDownloadId(0);
|
||||||
manager.setFeedMedia(service, media);
|
manager.setFeedMedia(service, media);
|
||||||
|
|
Loading…
Reference in New Issue