From 92e7dbfa9e17d25196444d7b16ba4cebaeb4c389 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 14 Nov 2021 13:12:24 +0100 Subject: Mention ad blockers on streaming error dialog as well --- .../core/service/download/HttpDownloader.java | 25 +++++++--------------- .../core/service/playback/ExoPlayerWrapper.java | 15 ++++++++++--- .../danoeh/antennapod/core/util/NetworkUtils.java | 20 +++++++++++++++++ core/src/main/res/values/strings.xml | 4 ++-- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java index 781110f82..cbfb2cede 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import android.text.TextUtils; import android.util.Log; +import de.danoeh.antennapod.core.util.NetworkUtils; import okhttp3.CacheControl; import org.apache.commons.io.IOUtils; @@ -19,8 +20,6 @@ import java.net.URI; import java.net.UnknownHostException; import java.util.Collections; import java.util.Date; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.model.feed.FeedMedia; @@ -39,7 +38,6 @@ public class HttpDownloader extends Downloader { private static final String TAG = "HttpDownloader"; private static final int BUFFER_SIZE = 8 * 1024; - private static final String REGEX_PATTERN_IP_ADDRESS = "([0-9]{1,3}[\\.]){3}[0-9]{1,3}"; public HttpDownloader(@NonNull DownloadRequest request) { super(request); @@ -259,21 +257,14 @@ public class HttpDownloader extends Downloader { onFail(DownloadError.ERROR_UNKNOWN_HOST, e.getMessage()); } catch (IOException e) { e.printStackTrace(); + if (NetworkUtils.wasDownloadBlocked(e)) { + onFail(DownloadError.ERROR_IO_BLOCKED, e.getMessage()); + return; + } String message = e.getMessage(); - if (message != null) { - // Try to parse message for a more detailed error message - Pattern pattern = Pattern.compile(REGEX_PATTERN_IP_ADDRESS); - Matcher matcher = pattern.matcher(message); - if (matcher.find()) { - String ip = matcher.group(); - if (ip.startsWith("127.") || ip.startsWith("0.")) { - onFail(DownloadError.ERROR_IO_BLOCKED, e.getMessage()); - return; - } - } else if (message.contains("Trust anchor for certification path not found")) { - onFail(DownloadError.ERROR_CERTIFICATE, e.getMessage()); - return; - } + if (message != null && message.contains("Trust anchor for certification path not found")) { + onFail(DownloadError.ERROR_CERTIFICATE, e.getMessage()); + return; } onFail(DownloadError.ERROR_IO_ERROR, e.getMessage()); } catch (NullPointerException e) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java index dd85cec5a..79363e872 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java @@ -32,10 +32,13 @@ import com.google.android.exoplayer2.ui.TrackNameProvider; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.upstream.HttpDataSource; import de.danoeh.antennapod.core.ClientConfig; +import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; import de.danoeh.antennapod.core.service.download.HttpDownloader; +import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.core.util.playback.IPlayer; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -102,9 +105,15 @@ public class ExoPlayerWrapper implements IPlayer { @Override public void onPlayerError(@NonNull ExoPlaybackException error) { if (audioErrorListener != null) { - Throwable cause = error.getCause(); - audioErrorListener.accept(cause != null - ? cause.getLocalizedMessage() : error.getLocalizedMessage()); + if (NetworkUtils.wasDownloadBlocked(error)) { + audioErrorListener.accept(context.getString(R.string.download_error_blocked)); + } else { + Throwable cause = error.getCause(); + if (cause instanceof HttpDataSource.HttpDataSourceException) { + cause = cause.getCause(); + } + audioErrorListener.accept(cause != null ? cause.getMessage() : error.getMessage()); + } } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java index 06d5d67a3..4aeed734e 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java @@ -16,6 +16,8 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -30,6 +32,8 @@ import okhttp3.Request; import okhttp3.Response; public class NetworkUtils { + private static final String REGEX_PATTERN_IP_ADDRESS = "([0-9]{1,3}[\\.]){3}[0-9]{1,3}"; + private NetworkUtils(){} private static final String TAG = NetworkUtils.class.getSimpleName(); @@ -142,6 +146,22 @@ public class NetworkUtils { return null; } + public static boolean wasDownloadBlocked(Throwable throwable) { + String message = throwable.getMessage(); + if (message != null) { + Pattern pattern = Pattern.compile(REGEX_PATTERN_IP_ADDRESS); + Matcher matcher = pattern.matcher(message); + if (matcher.find()) { + String ip = matcher.group(); + return ip.startsWith("127.") || ip.startsWith("0."); + } + } + if (throwable.getCause() != null) { + return wasDownloadBlocked(throwable.getCause()); + } + return false; + } + public static Single getFeedMediaSizeObservable(FeedMedia media) { return Single.create((SingleOnSubscribe) emitter -> { if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) { diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index a38e53270..4333929c4 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -263,8 +263,8 @@ The podcast host\'s server refuses to respond. Download canceled The server connection was lost before completing the download - The download was blocked by another app on your device. - Unable to establish a secure connection. This can mean that another app on your device blocked the download, or that something is wrong with the server certificates. + The download was blocked by another app on your device (like a VPN or ad blocker). + Unable to establish a secure connection. This can mean that another app on your device (like a VPN or an ad blocker) blocked the download, or that something is wrong with the server certificates. Downloads completed with error(s) Auto-downloads completed IO Error -- cgit v1.2.3