Show dedicated error message when an app messes with the network

This commit is contained in:
ByteHamster 2021-03-04 10:45:19 +01:00
parent 878da84666
commit 352df348cb
3 changed files with 19 additions and 1 deletions

View File

@ -19,6 +19,8 @@ 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.core.feed.FeedMedia;
@ -37,6 +39,7 @@ 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);
@ -250,6 +253,19 @@ public class HttpDownloader extends Downloader {
onFail(DownloadError.ERROR_UNKNOWN_HOST, e.getMessage());
} catch (IOException e) {
e.printStackTrace();
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;
}
}
}
onFail(DownloadError.ERROR_IO_ERROR, e.getMessage());
} catch (NullPointerException e) {
// might be thrown by connection.getInputStream()

View File

@ -23,7 +23,8 @@ public enum DownloadError {
ERROR_UNAUTHORIZED(14, R.string.download_error_unauthorized),
ERROR_FILE_TYPE(15, R.string.download_error_file_type_type),
ERROR_FORBIDDEN(16, R.string.download_error_forbidden),
ERROR_IO_WRONG_SIZE(17, R.string.download_wrong_size);
ERROR_IO_WRONG_SIZE(17, R.string.download_wrong_size),
ERROR_IO_BLOCKED(18, R.string.download_blocked);
private final int code;
private final int resId;

View File

@ -258,6 +258,7 @@
<string name="download_error_forbidden">Forbidden</string>
<string name="download_canceled_msg">Download canceled</string>
<string name="download_wrong_size">The server connection was lost before completing the download</string>
<string name="download_blocked">The download was blocked by another app on your device</string>
<string name="download_canceled_autodownload_enabled_msg">Download canceled\nDisabled <i>Auto Download</i> for this item</string>
<string name="download_report_title">Downloads completed with error(s)</string>
<string name="auto_download_report_title">Auto-downloads completed</string>