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());
}
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;
}
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));
}
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();
}
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;
}
Aggregations