Search in sources :

Example 1 with DownloadRequest

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);
}
Also used : DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Example 2 with DownloadRequest

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)));
}
Also used : FeedPreferences(de.danoeh.antennapod.core.feed.FeedPreferences) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) DownloadRequestException(de.danoeh.antennapod.core.storage.DownloadRequestException) ImageView(android.widget.ImageView) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) StringUtils(org.apache.commons.lang3.StringUtils) DownloadRequestErrorDialogCreator(de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator) NavUtils(android.support.v4.app.NavUtils) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) FeedItemlistDescriptionAdapter(de.danoeh.antennapod.adapter.FeedItemlistDescriptionAdapter) Looper(android.os.Looper) Map(java.util.Map) HtmlToPlainText(de.danoeh.antennapod.core.util.syndication.HtmlToPlainText) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Button(android.widget.Button) AdapterView(android.widget.AdapterView) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) Log(android.util.Log) DownloadError(de.danoeh.antennapod.core.util.DownloadError) FileNameGenerator(de.danoeh.antennapod.core.util.FileNameGenerator) FeedDiscoverer(de.danoeh.antennapod.core.util.syndication.FeedDiscoverer) AppCompatActivity(android.support.v7.app.AppCompatActivity) FeedPreferences(de.danoeh.antennapod.core.feed.FeedPreferences) List(java.util.List) TextView(android.widget.TextView) DownloadEvent(de.danoeh.antennapod.core.event.DownloadEvent) Document(org.jsoup.nodes.Document) UnsupportedFeedtypeException(de.danoeh.antennapod.core.syndication.handler.UnsupportedFeedtypeException) RelativeLayout(android.widget.RelativeLayout) ListView(android.widget.ListView) Jsoup(org.jsoup.Jsoup) Subscription(rx.Subscription) FeedHandlerResult(de.danoeh.antennapod.core.syndication.handler.FeedHandlerResult) Context(android.content.Context) EventBus(de.greenrobot.event.EventBus) AuthenticationDialog(de.danoeh.antennapod.dialog.AuthenticationDialog) Dialog(android.app.Dialog) Intent(android.content.Intent) ApGlideSettings(de.danoeh.antennapod.core.glide.ApGlideSettings) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Observable(rx.Observable) HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) FeedHandler(de.danoeh.antennapod.core.syndication.handler.FeedHandler) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) Feed(de.danoeh.antennapod.core.feed.Feed) StorageUtils(de.danoeh.antennapod.core.util.StorageUtils) DialogInterface(android.content.DialogInterface) Downloader(de.danoeh.antennapod.core.service.download.Downloader) Subscriber(rx.Subscriber) LayoutInflater(android.view.LayoutInflater) R(de.danoeh.antennapod.R) TextUtils(android.text.TextUtils) IOException(java.io.IOException) EventDistributor(de.danoeh.antennapod.core.feed.EventDistributor) File(java.io.File) Spinner(android.widget.Spinner) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) ArrayAdapter(android.widget.ArrayAdapter) AlertDialog(android.support.v7.app.AlertDialog) Glide(com.bumptech.glide.Glide) URLChecker(de.danoeh.antennapod.core.util.URLChecker) DBReader(de.danoeh.antennapod.core.storage.DBReader) DownloadRequester(de.danoeh.antennapod.core.storage.DownloadRequester) HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) File(java.io.File) Observable(rx.Observable) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 3 with DownloadRequest

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;
}
Also used : HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) Downloader(de.danoeh.antennapod.core.service.download.Downloader) HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Example 4 with DownloadRequest

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());
}
Also used : HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) Downloader(de.danoeh.antennapod.core.service.download.Downloader) HttpDownloader(de.danoeh.antennapod.core.service.download.HttpDownloader) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) FeedFile(de.danoeh.antennapod.core.feed.FeedFile) File(java.io.File)

Example 5 with DownloadRequest

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;
}
Also used : LayoutInflater(android.view.LayoutInflater) Downloader(de.danoeh.antennapod.core.service.download.Downloader) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest)

Aggregations

DownloadRequest (de.danoeh.antennapod.core.service.download.DownloadRequest)5 Downloader (de.danoeh.antennapod.core.service.download.Downloader)4 File (java.io.File)4 FeedFile (de.danoeh.antennapod.core.feed.FeedFile)3 DownloadStatus (de.danoeh.antennapod.core.service.download.DownloadStatus)3 HttpDownloader (de.danoeh.antennapod.core.service.download.HttpDownloader)3 LayoutInflater (android.view.LayoutInflater)2 Dialog (android.app.Dialog)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Looper (android.os.Looper)1 NavUtils (android.support.v4.app.NavUtils)1 AlertDialog (android.support.v7.app.AlertDialog)1 AppCompatActivity (android.support.v7.app.AppCompatActivity)1 TextUtils (android.text.TextUtils)1 Log (android.util.Log)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1