Search in sources :

Example 36 with FeedMedia

use of de.danoeh.antennapod.model.feed.FeedMedia in project AntennaPod by AntennaPod.

the class NetworkUtils method getFeedMediaSizeObservable.

public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
    return Single.create((SingleOnSubscribe<Long>) emitter -> {
        if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) {
            emitter.onSuccess(0L);
            return;
        }
        long size = Integer.MIN_VALUE;
        if (media.isDownloaded()) {
            File mediaFile = new File(media.getLocalMediaUrl());
            if (mediaFile.exists()) {
                size = mediaFile.length();
            }
        } else if (!media.checkedOnSizeButUnknown()) {
            String url = media.getDownload_url();
            if (TextUtils.isEmpty(url)) {
                emitter.onSuccess(0L);
                return;
            }
            OkHttpClient client = AntennapodHttpClient.getHttpClient();
            Request.Builder httpReq = new Request.Builder().url(url).header("Accept-Encoding", "identity").head();
            try {
                Response response = client.newCall(httpReq.build()).execute();
                if (response.isSuccessful()) {
                    String contentLength = response.header("Content-Length");
                    try {
                        size = Integer.parseInt(contentLength);
                    } catch (NumberFormatException e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                    }
                }
            } catch (IOException e) {
                emitter.onSuccess(0L);
                Log.e(TAG, Log.getStackTraceString(e));
                return;
            }
        }
        Log.d(TAG, "new size: " + size);
        if (size <= 0) {
            media.setCheckedOnSizeButUnknown();
        } else {
            media.setSize(size);
        }
        emitter.onSuccess(size);
        DBWriter.setFeedMedia(media);
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) WifiInfo(android.net.wifi.WifiInfo) NetworkCapabilities(android.net.NetworkCapabilities) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) Matcher(java.util.regex.Matcher) Response(okhttp3.Response) Schedulers(io.reactivex.schedulers.Schedulers) Build(android.os.Build) AntennapodHttpClient(de.danoeh.antennapod.core.service.download.AntennapodHttpClient) Log(android.util.Log) SingleOnSubscribe(io.reactivex.SingleOnSubscribe) ConnectivityManager(android.net.ConnectivityManager) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) Request(okhttp3.Request) NetworkInfo(android.net.NetworkInfo) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Network(android.net.Network) File(java.io.File) WifiManager(android.net.wifi.WifiManager) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) DBWriter(de.danoeh.antennapod.core.storage.DBWriter) Pattern(java.util.regex.Pattern) DownloadService(de.danoeh.antennapod.core.service.download.DownloadService) DBTasks(de.danoeh.antennapod.core.storage.DBTasks) Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) File(java.io.File)

Example 37 with FeedMedia

use of de.danoeh.antennapod.model.feed.FeedMedia in project AntennaPod by AntennaPod.

the class ItemEnqueuePositionCalculatorTest method createFeedItem.

static FeedItem createFeedItem(long id) {
    FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", new Date(), FeedItem.PLAYED, FeedMother.anyFeed());
    FeedMedia media = new FeedMedia(item, "http://download.url.net/" + id, 1234567, "audio/mpeg");
    media.setId(item.getId());
    item.setMedia(media);
    return item;
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) Date(java.util.Date)

Example 38 with FeedMedia

use of de.danoeh.antennapod.model.feed.FeedMedia in project AntennaPod by AntennaPod.

the class FeedItemTest method testAutoDownloadBackoff.

@Test
public void testAutoDownloadBackoff() {
    FeedItem item = new FeedItem();
    item.setMedia(new FeedMedia(item, "https://example.com/file.mp3", 0, "audio/mpeg"));
    // In reality, this is System.currentTimeMillis()
    long now = ONE_HOUR;
    assertTrue(item.isAutoDownloadable(now));
    item.increaseFailedAutoDownloadAttempts(now);
    assertFalse(item.isAutoDownloadable(now));
    now += ONE_HOUR;
    assertTrue(item.isAutoDownloadable(now));
    item.increaseFailedAutoDownloadAttempts(now);
    assertFalse(item.isAutoDownloadable(now));
    now += ONE_HOUR;
    // Should backoff, so more than 1 hour needed
    assertFalse(item.isAutoDownloadable(now));
    now += ONE_HOUR;
    // Now it's enough
    assertTrue(item.isAutoDownloadable(now));
    item.increaseFailedAutoDownloadAttempts(now);
    item.increaseFailedAutoDownloadAttempts(now);
    item.increaseFailedAutoDownloadAttempts(now);
    now += 1000L * ONE_HOUR;
    // Should have given up
    assertFalse(item.isAutoDownloadable(now));
    item.increaseFailedAutoDownloadAttempts(now);
    now += 1000L * ONE_HOUR;
    // Still given up
    assertFalse(item.isAutoDownloadable(now));
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) Test(org.junit.Test)

Example 39 with FeedMedia

use of de.danoeh.antennapod.model.feed.FeedMedia in project AntennaPod by AntennaPod.

the class DownloadAuthenticationActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(UserPreferences.getTranslucentTheme());
    super.onCreate(savedInstanceState);
    Validate.isTrue(getIntent().hasExtra(ARG_DOWNLOAD_REQUEST), "Download request missing");
    DownloadRequest request = getIntent().getParcelableExtra(ARG_DOWNLOAD_REQUEST);
    new AuthenticationDialog(this, R.string.authentication_label, true, "", "") {

        @Override
        protected void onConfirmed(String username, String password) {
            Completable.fromAction(() -> {
                request.setUsername(username);
                request.setPassword(password);
                if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
                    long mediaId = request.getFeedfileId();
                    FeedMedia media = DBReader.getFeedMedia(mediaId);
                    if (media != null) {
                        FeedPreferences preferences = media.getItem().getFeed().getPreferences();
                        if (TextUtils.isEmpty(preferences.getPassword()) || TextUtils.isEmpty(preferences.getUsername())) {
                            preferences.setUsername(username);
                            preferences.setPassword(password);
                            DBWriter.setFeedPreferences(preferences);
                        }
                    }
                }
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                DownloadService.download(DownloadAuthenticationActivity.this, false, request);
                finish();
            });
        }

        @Override
        protected void onCancelled() {
            finish();
        }
    }.show();
}
Also used : FeedPreferences(de.danoeh.antennapod.model.feed.FeedPreferences) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) AuthenticationDialog(de.danoeh.antennapod.dialog.AuthenticationDialog) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest)

Example 40 with FeedMedia

use of de.danoeh.antennapod.model.feed.FeedMedia in project AntennaPod by AntennaPod.

the class VideoplayerActivity method onPrepareOptionsMenu.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (controller == null) {
        return false;
    }
    Playable media = controller.getMedia();
    boolean isFeedMedia = (media instanceof FeedMedia);
    // FeedMedia implies it belongs to a Feed
    menu.findItem(R.id.open_feed_item).setVisible(isFeedMedia);
    boolean hasWebsiteLink = getWebsiteLinkWithFallback(media) != null;
    menu.findItem(R.id.visit_website_item).setVisible(hasWebsiteLink);
    boolean isItemAndHasLink = isFeedMedia && ShareUtils.hasLinkToShare(((FeedMedia) media).getItem());
    boolean isItemHasDownloadLink = isFeedMedia && ((FeedMedia) media).getDownload_url() != null;
    menu.findItem(R.id.share_item).setVisible(hasWebsiteLink || isItemAndHasLink || isItemHasDownloadLink);
    menu.findItem(R.id.add_to_favorites_item).setVisible(false);
    menu.findItem(R.id.remove_from_favorites_item).setVisible(false);
    if (isFeedMedia) {
        menu.findItem(R.id.add_to_favorites_item).setVisible(!isFavorite);
        menu.findItem(R.id.remove_from_favorites_item).setVisible(isFavorite);
    }
    menu.findItem(R.id.set_sleeptimer_item).setVisible(!controller.sleepTimerActive());
    menu.findItem(R.id.disable_sleeptimer_item).setVisible(controller.sleepTimerActive());
    if (PictureInPictureUtil.supportsPictureInPicture(this)) {
        menu.findItem(R.id.player_go_to_picture_in_picture).setVisible(true);
    }
    menu.findItem(R.id.audio_controls).setIcon(R.drawable.ic_sliders);
    return true;
}
Also used : Playable(de.danoeh.antennapod.model.playback.Playable) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia)

Aggregations

FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)91 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)46 Test (org.junit.Test)28 Feed (de.danoeh.antennapod.model.feed.Feed)26 Date (java.util.Date)25 ArrayList (java.util.ArrayList)18 FeedPreferences (de.danoeh.antennapod.model.feed.FeedPreferences)15 File (java.io.File)15 Playable (de.danoeh.antennapod.model.playback.Playable)8 View (android.view.View)6 NonNull (androidx.annotation.NonNull)6 Context (android.content.Context)5 Log (android.util.Log)5 PlaybackServiceStarter (de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter)5 Cursor (android.database.Cursor)4 Nullable (androidx.annotation.Nullable)4 R (de.danoeh.antennapod.R)4 DownloadRequest (de.danoeh.antennapod.core.service.download.DownloadRequest)4 DownloadService (de.danoeh.antennapod.core.service.download.DownloadService)4 Intent (android.content.Intent)3