Refactoring
This commit is contained in:
parent
b44e0dde58
commit
07ce9579fb
|
@ -26,7 +26,6 @@ import java.util.Map;
|
|||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import de.danoeh.antennapod.BuildConfig;
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.core.feed.Feed;
|
||||
import de.danoeh.antennapod.core.feed.FeedPreferences;
|
||||
|
@ -91,12 +90,10 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
|
||||
getSupportActionBar().setTitle(R.string.add_new_feed_label);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Activity must be started with feedurl argument!");
|
||||
throw new IllegalArgumentException("Activity must be started with feedurl argument!");
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Activity was started with url " + feedUrl);
|
||||
Log.d(TAG, "Activity was started with url " + feedUrl);
|
||||
setLoadingLayout();
|
||||
if (savedInstanceState == null) {
|
||||
startFeedDownload(feedUrl, null, null);
|
||||
|
@ -147,7 +144,7 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Download was completed");
|
||||
Log.d(TAG, "Download was completed");
|
||||
DownloadStatus status = downloader.getResult();
|
||||
if (status != null) {
|
||||
if (!status.isCancelled()) {
|
||||
|
@ -164,15 +161,13 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
OnlineFeedViewActivity.this);
|
||||
if (errorMsg != null
|
||||
&& status.getReasonDetailed() != null) {
|
||||
errorMsg += " ("
|
||||
+ status.getReasonDetailed() + ")";
|
||||
errorMsg += " (" + status.getReasonDetailed() + ")";
|
||||
}
|
||||
showErrorDialog(errorMsg);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.wtf(TAG,
|
||||
"DownloadStatus returned by Downloader was null");
|
||||
Log.wtf(TAG, "DownloadStatus returned by Downloader was null");
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
@ -181,21 +176,18 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
}
|
||||
|
||||
private void startFeedDownload(String url, String username, String password) {
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Starting feed download");
|
||||
Log.d(TAG, "Starting feed download");
|
||||
url = URLChecker.prepareURL(url);
|
||||
feed = new Feed(url, new Date(0));
|
||||
if (username != null && password != null) {
|
||||
feed.setPreferences(new FeedPreferences(0, false, username, password));
|
||||
}
|
||||
String fileUrl = new File(getExternalCacheDir(),
|
||||
FileNameGenerator.generateFileName(feed.getDownload_url()))
|
||||
.toString();
|
||||
FileNameGenerator.generateFileName(feed.getDownload_url())).toString();
|
||||
feed.setFile_url(fileUrl);
|
||||
final DownloadRequest request = new DownloadRequest(feed.getFile_url(),
|
||||
feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED, username, password, true, null);
|
||||
downloader = new HttpDownloader(
|
||||
request);
|
||||
downloader = new HttpDownloader(request);
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -233,8 +225,7 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
"feed must be non-null and downloaded when parseFeed is called");
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Parsing feed");
|
||||
Log.d(TAG, "Parsing feed");
|
||||
|
||||
Thread thread = new Thread() {
|
||||
|
||||
|
@ -258,7 +249,7 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
e.printStackTrace();
|
||||
reasonDetailed = e.getMessage();
|
||||
} catch (UnsupportedFeedtypeException e) {
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Unsupported feed type detected");
|
||||
Log.d(TAG, "Unsupported feed type detected");
|
||||
if (StringUtils.equalsIgnoreCase("html", e.getRootElement())) {
|
||||
if (showFeedDiscoveryDialog(new File(feed.getFile_url()), feed.getDownload_url())) {
|
||||
return;
|
||||
|
@ -269,8 +260,7 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity {
|
|||
}
|
||||
} finally {
|
||||
boolean rc = new File(feed.getFile_url()).delete();
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Deleted feed source file. Result: " + rc);
|
||||
Log.d(TAG, "Deleted feed source file. Result: " + rc);
|
||||
}
|
||||
|
||||
if (successful) {
|
||||
|
|
|
@ -2,7 +2,6 @@ package de.danoeh.antennapod.core.service.download;
|
|||
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.okhttp.Credentials;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
|
@ -18,19 +17,20 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
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;
|
||||
import de.danoeh.antennapod.core.R;
|
||||
import de.danoeh.antennapod.core.feed.FeedImage;
|
||||
import de.danoeh.antennapod.core.util.DownloadError;
|
||||
import de.danoeh.antennapod.core.util.StorageUtils;
|
||||
import de.danoeh.antennapod.core.util.URIUtil;
|
||||
import okio.ByteString;
|
||||
|
||||
public class HttpDownloader extends Downloader {
|
||||
private static final String TAG = "HttpDownloader";
|
||||
|
@ -99,13 +99,11 @@ public class HttpDownloader extends Downloader {
|
|||
|
||||
Response response = httpClient.newCall(httpReq.build()).execute();
|
||||
responseBody = response.body();
|
||||
|
||||
String contentEncodingHeader = response.header("Content-Encoding");
|
||||
boolean isGzip = StringUtils.equalsIgnoreCase(contentEncodingHeader, "gzip");
|
||||
|
||||
final boolean isGzip = StringUtils.equalsIgnoreCase(contentEncodingHeader, "gzip");
|
||||
Log.d(TAG, "Response code is " + response.code());
|
||||
|
||||
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");
|
||||
|
@ -151,22 +149,18 @@ public class HttpDownloader extends Downloader {
|
|||
out = new RandomAccessFile(destination, "rw");
|
||||
}
|
||||
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int count = 0;
|
||||
request.setStatusMsg(R.string.download_running);
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Getting size of download");
|
||||
Log.d(TAG, "Getting size of download");
|
||||
request.setSize(responseBody.contentLength() + request.getSoFar());
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Size is " + request.getSize());
|
||||
Log.d(TAG, "Size is " + request.getSize());
|
||||
if (request.getSize() < 0) {
|
||||
request.setSize(DownloadStatus.SIZE_UNKNOWN);
|
||||
}
|
||||
|
||||
long freeSpace = StorageUtils.getFreeSpaceAvailable();
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Free space is " + freeSpace);
|
||||
Log.d(TAG, "Free space is " + freeSpace);
|
||||
|
||||
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN
|
||||
&& request.getSize() > freeSpace) {
|
||||
|
@ -174,8 +168,7 @@ public class HttpDownloader extends Downloader {
|
|||
return;
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Starting download");
|
||||
Log.d(TAG, "Starting download");
|
||||
while (!cancelled
|
||||
&& (count = connection.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, count);
|
||||
|
@ -226,15 +219,12 @@ public class HttpDownloader extends Downloader {
|
|||
}
|
||||
|
||||
private void onSuccess() {
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Download was successful");
|
||||
Log.d(TAG, "Download was successful");
|
||||
result.setSuccessful();
|
||||
}
|
||||
|
||||
private void onFail(DownloadError reason, String reasonDetailed) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "Download failed");
|
||||
}
|
||||
Log.d(TAG, "Download failed");
|
||||
result.setFailed(reason, reasonDetailed);
|
||||
if (request.isDeleteOnFailure()) {
|
||||
cleanup();
|
||||
|
@ -242,8 +232,7 @@ public class HttpDownloader extends Downloader {
|
|||
}
|
||||
|
||||
private void onCancelled() {
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Download was cancelled");
|
||||
Log.d(TAG, "Download was cancelled");
|
||||
result.setCancelled();
|
||||
cleanup();
|
||||
}
|
||||
|
@ -256,12 +245,10 @@ public class HttpDownloader extends Downloader {
|
|||
File dest = new File(request.getDestination());
|
||||
if (dest.exists()) {
|
||||
boolean rc = dest.delete();
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "Deleted file " + dest.getName() + "; Result: "
|
||||
Log.d(TAG, "Deleted file " + dest.getName() + "; Result: "
|
||||
+ rc);
|
||||
} else {
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "cleanup() didn't delete file: does not exist.");
|
||||
Log.d(TAG, "cleanup() didn't delete file: does not exist.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue