use of de.danoeh.antennapod.core.service.download.DownloadRequest in project AntennaPod by AntennaPod.
the class DownloadRequester method download.
private void download(Context context, FeedFile item, FeedFile container, File dest, boolean overwriteIfExists, String username, String password, String lastModified, boolean deleteOnFailure, Bundle arguments) {
final boolean partiallyDownloadedFileExists = item.getFile_url() != null;
if (isDownloadingFile(item)) {
Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded");
return;
}
if (!isFilenameAvailable(dest.toString()) || (!partiallyDownloadedFileExists && dest.exists())) {
Log.d(TAG, "Filename already used.");
if (isFilenameAvailable(dest.toString()) && overwriteIfExists) {
boolean result = dest.delete();
Log.d(TAG, "Deleting file. Result: " + result);
} else {
// find different name
File newDest = null;
for (int i = 1; i < Integer.MAX_VALUE; i++) {
String newName = FilenameUtils.getBaseName(dest.getName()) + "-" + i + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(dest.getName());
Log.d(TAG, "Testing filename " + newName);
newDest = new File(dest.getParent(), newName);
if (!newDest.exists() && isFilenameAvailable(newDest.toString())) {
Log.d(TAG, "File doesn't exist yet. Using " + newName);
break;
}
}
if (newDest != null) {
dest = newDest;
}
}
}
Log.d(TAG, "Requesting download of url " + item.getDownload_url());
String baseUrl = (container != null) ? container.getDownload_url() : null;
item.setDownload_url(URLChecker.prepareURL(item.getDownload_url(), baseUrl));
DownloadRequest.Builder builder = new DownloadRequest.Builder(dest.toString(), item).withAuthentication(username, password).lastModified(lastModified).deleteOnFailure(deleteOnFailure).withArguments(arguments);
DownloadRequest request = builder.build();
download(context, request);
}
use of de.danoeh.antennapod.core.service.download.DownloadRequest in project AntennaPod by AntennaPod.
the class OnlineFeedViewActivity method startFeedDownload.
private void startFeedDownload(String url, String username, String password) {
Log.d(TAG, "Starting feed download");
url = URLChecker.prepareURL(url);
feed = new Feed(url, null);
if (username != null && password != null) {
feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, username, password));
}
String fileUrl = new File(getExternalCacheDir(), 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);
download = Observable.create(new Observable.OnSubscribe<DownloadStatus>() {
@Override
public void call(Subscriber<? super DownloadStatus> subscriber) {
feeds = DBReader.getFeedList();
downloader = new HttpDownloader(request);
downloader.call();
Log.d(TAG, "Download was completed");
subscriber.onNext(downloader.getResult());
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(this::checkDownloadResult, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
use of de.danoeh.antennapod.core.service.download.DownloadRequest in project AntennaPod by AntennaPod.
the class HttpDownloaderTest method download.
private Downloader download(String url, String title, boolean expectedResult, boolean deleteExisting, String username, String password, boolean deleteOnFail) {
FeedFile feedFile = setupFeedFile(url, title, deleteExisting);
DownloadRequest request = new DownloadRequest(feedFile.getFile_url(), url, title, 0, feedFile.getTypeAsInt(), username, password, deleteOnFail, null);
Downloader downloader = new HttpDownloader(request);
downloader.call();
DownloadStatus status = downloader.getResult();
assertNotNull(status);
assertTrue(status.isSuccessful() == expectedResult);
assertTrue(status.isDone());
// the file should not exist if the download has failed and deleteExisting was true
assertTrue(!deleteExisting || new File(feedFile.getFile_url()).exists() == expectedResult);
return downloader;
}
use of de.danoeh.antennapod.core.service.download.DownloadRequest in project AntennaPod by AntennaPod.
the class HttpDownloaderTest method testCancel.
public void testCancel() {
final String url = HTTPBin.BASE_URL + "/delay/3";
FeedFileImpl feedFile = setupFeedFile(url, "delay", true);
final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt()));
Thread t = new Thread() {
@Override
public void run() {
downloader.call();
}
};
t.start();
downloader.cancel();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
DownloadStatus result = downloader.getResult();
assertTrue(result.isDone());
assertFalse(result.isSuccessful());
assertTrue(result.isCancelled());
assertFalse(new File(feedFile.getFile_url()).exists());
}
use of de.danoeh.antennapod.core.service.download.DownloadRequest in project AntennaPod by AntennaPod.
the class DownloadlistAdapter method getView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
Downloader downloader = getItem(position);
DownloadRequest request = downloader.getDownloadRequest();
// Inflate layout
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.downloadlist_item, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
holder.downloaded = (TextView) convertView.findViewById(R.id.txtvDownloaded);
holder.percent = (TextView) convertView.findViewById(R.id.txtvPercent);
holder.progbar = (ProgressBar) convertView.findViewById(R.id.progProgress);
holder.butSecondary = (ImageButton) convertView.findViewById(R.id.butSecondaryAction);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (position == selectedItemIndex) {
convertView.setBackgroundColor(ContextCompat.getColor(convertView.getContext(), ThemeUtils.getSelectionBackgroundColor()));
} else {
convertView.setBackgroundResource(0);
}
holder.title.setText(request.getTitle());
holder.progbar.setIndeterminate(request.getSoFar() <= 0);
String strDownloaded = Converter.byteToString(request.getSoFar());
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN) {
strDownloaded += " / " + Converter.byteToString(request.getSize());
holder.percent.setText(request.getProgressPercent() + "%");
holder.progbar.setProgress(request.getProgressPercent());
holder.percent.setVisibility(View.VISIBLE);
} else {
holder.progbar.setProgress(0);
holder.percent.setVisibility(View.INVISIBLE);
}
holder.downloaded.setText(strDownloaded);
holder.butSecondary.setFocusable(false);
holder.butSecondary.setTag(downloader);
holder.butSecondary.setOnClickListener(butSecondaryListener);
return convertView;
}
Aggregations