Refresh at least every 3 days

This commit is contained in:
Martin Fietz 2015-03-27 19:39:19 +01:00
parent d280a32c23
commit d4b20a4154
3 changed files with 28 additions and 3 deletions

View File

@ -21,6 +21,7 @@ import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Date;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.ClientConfig;
@ -64,6 +65,15 @@ public class HttpDownloader extends Downloader {
final URI uri = URIUtil.getURIFromRequestUrl(request.getSource());
Request.Builder httpReq = new Request.Builder().url(uri.toURL())
.header("User-Agent", ClientConfig.USER_AGENT);
if(request.getIfModifiedSince() > 0) {
long threeDaysAgo = System.currentTimeMillis() - 1000*60*60*24*3;
if(request.getIfModifiedSince() > threeDaysAgo) {
Date date = new Date(request.getIfModifiedSince());
String httpDate = HttpDate.format(date);
Log.d(TAG, "addHeader(\"If-Modified-Since\", \"" + httpDate + "\")");
httpReq.addHeader("If-Modified-Since", httpDate);
}
}
// add authentication information
String userInfo = uri.getUserInfo();
@ -96,6 +106,12 @@ public class HttpDownloader extends Downloader {
if (BuildConfig.DEBUG)
Log.d(TAG, "Response code is " + response.code());
if(!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_NOT_MODIFIED) {
Log.d(TAG, "Feed '" + request.getSource() + "' not modified since last update, Download canceled");
onCancelled();
return;
}
if (!response.isSuccessful() || response.body() == null) {
final DownloadError error;
final String details;

View File

@ -298,7 +298,8 @@ public final class DBTasks {
}
/**
* Updates a specific Feed.
* Refresh a specific Feed. The refresh may get canceled if the feed does not seem to be modified
* and the last update was only few days ago.
*
* @param context Used for requesting the download.
* @param feed The Feed object.
@ -311,9 +312,9 @@ public final class DBTasks {
private static void refreshFeed(Context context, Feed feed, boolean loadAllPages) throws DownloadRequestException {
Feed f;
if (feed.getPreferences() == null) {
f = new Feed(feed.getDownload_url(), new Date(), feed.getTitle());
f = new Feed(feed.getDownload_url(), feed.getLastUpdate(), feed.getTitle());
} else {
f = new Feed(feed.getDownload_url(), new Date(), feed.getTitle(),
f = new Feed(feed.getDownload_url(), feed.getLastUpdate(), feed.getTitle(),
feed.getPreferences().getUsername(), feed.getPreferences().getPassword());
}
f.setId(feed.getId());

View File

@ -163,11 +163,19 @@ public class DownloadRequester {
return true;
}
/**
* Downloads a feed
*
* @param context The application's environment.
* @param feed Feed to download
* @param loadAllPages Set to true to download all pages
*/
public synchronized void downloadFeed(Context context, Feed feed, boolean loadAllPages)
throws DownloadRequestException {
if (feedFileValid(feed)) {
String username = (feed.getPreferences() != null) ? feed.getPreferences().getUsername() : null;
String password = (feed.getPreferences() != null) ? feed.getPreferences().getPassword() : null;
long ifModifiedSince = feed.getLastUpdate().getTime();
Bundle args = new Bundle();
args.putInt(REQUEST_ARG_PAGE_NR, feed.getPageNr());