From d1573a0a6e9e409df3bb8c780a4bf2222dca6272 Mon Sep 17 00:00:00 2001 From: kapodamy Date: Wed, 17 Apr 2019 18:17:24 -0300 Subject: [PATCH] misc changes * implement socket timeout error * use 128k buffer size for copy * use NewPipe HTTP user agent in the downloads * automatically recover downloads with network errors that are queued --- .../giga/get/DownloadInitializer.java | 2 +- .../us/shandian/giga/get/DownloadMission.java | 25 ++++++++++++++++--- .../shandian/giga/get/DownloadRunnable.java | 1 - .../giga/get/DownloadRunnableFallback.java | 11 ++++---- .../shandian/giga/io/CircularFileWriter.java | 7 +++--- .../giga/ui/adapter/MissionAdapter.java | 4 +++ app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values-eu/strings.xml | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-it/strings.xml | 1 + app/src/main/res/values-ja/strings.xml | 2 ++ app/src/main/res/values-nl/strings.xml | 1 + app/src/main/res/values-ru/strings.xml | 1 + app/src/main/res/values-zh-rTW/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 16 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java index f6b6b459a..590c3704c 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java @@ -28,7 +28,7 @@ public class DownloadInitializer extends Thread { @Override public void run() { - if (mMission.current > 0) mMission.resetState(false,true, DownloadMission.ERROR_NOTHING); + if (mMission.current > 0) mMission.resetState(false, true, DownloadMission.ERROR_NOTHING); int retryCount = 0; while (true) { diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMission.java b/app/src/main/java/us/shandian/giga/get/DownloadMission.java index 838acc162..9bc46e3af 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMission.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMission.java @@ -4,11 +4,14 @@ import android.os.Handler; import android.os.Message; import android.util.Log; +import org.schabi.newpipe.Downloader; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.ConnectException; import java.net.HttpURLConnection; +import java.net.SocketTimeoutException; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; @@ -45,6 +48,7 @@ public class DownloadMission extends Mission { public static final int ERROR_POSTPROCESSING_HOLD = 1009; public static final int ERROR_INSUFFICIENT_STORAGE = 1010; public static final int ERROR_PROGRESS_LOST = 1011; + public static final int ERROR_TIMEOUT = 1012; public static final int ERROR_HTTP_NO_CONTENT = 204; public static final int ERROR_HTTP_UNSUPPORTED_RANGE = 206; @@ -232,10 +236,9 @@ public class DownloadMission extends Mission { URL url = new URL(urls[current]); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(true); + conn.setRequestProperty("User-Agent", Downloader.USER_AGENT); // BUG workaround: switching between networks can freeze the download forever - - //conn.setRequestProperty("Connection", "close"); conn.setConnectTimeout(30000); conn.setReadTimeout(10000); @@ -329,6 +332,8 @@ public class DownloadMission extends Mission { notifyError(ERROR_CONNECT_HOST, null); } else if (err instanceof UnknownHostException) { notifyError(ERROR_UNKNOWN_HOST, null); + } else if (err instanceof SocketTimeoutException) { + notifyError(ERROR_TIMEOUT, null); } else { notifyError(ERROR_UNKNOWN_EXCEPTION, err); } @@ -338,7 +343,7 @@ public class DownloadMission extends Mission { Log.e(TAG, "notifyError() code = " + code, err); if (err instanceof IOException) { - if (storage.canWrite() || err.getMessage().contains("Permission denied")) { + if (!storage.canWrite() || err.getMessage().contains("Permission denied")) { code = ERROR_PERMISSION_DENIED; err = null; } else if (err.getMessage().contains("ENOSPC")) { @@ -349,7 +354,19 @@ public class DownloadMission extends Mission { errCode = code; errObject = err; - enqueued = false; + + switch (code) { + case ERROR_SSL_EXCEPTION: + case ERROR_UNKNOWN_HOST: + case ERROR_CONNECT_HOST: + case ERROR_TIMEOUT: + // do not change the queue flag for network errors, can be + // recovered silently without the user interaction + break; + default: + // also checks for server errors + if (code < 500 || code > 599) enqueued = false; + } pause(); diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java index 7a68cd778..4380c0c68 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java @@ -27,7 +27,6 @@ public class DownloadRunnable extends Thread { if (mission == null) throw new NullPointerException("mission is null"); mMission = mission; mId = id; - mConn = null; } @Override diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java index 1a4b5d5b6..d7ff208ce 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java @@ -1,6 +1,5 @@ package us.shandian.giga.get; -import android.annotation.SuppressLint; import android.support.annotation.NonNull; import android.util.Log; @@ -19,7 +18,7 @@ import static org.schabi.newpipe.BuildConfig.DEBUG; * Single-threaded fallback mode */ public class DownloadRunnableFallback extends Thread { - private static final String TAG = "DownloadRunnableFallback"; + private static final String TAG = "DownloadRunnableFallbac"; private final DownloadMission mMission; @@ -30,9 +29,6 @@ public class DownloadRunnableFallback extends Thread { DownloadRunnableFallback(@NonNull DownloadMission mission) { mMission = mission; - mIs = null; - mF = null; - mConn = null; } private void dispose() { @@ -46,7 +42,6 @@ public class DownloadRunnableFallback extends Thread { } @Override - @SuppressLint("LongLogTag") public void run() { boolean done; @@ -106,6 +101,10 @@ public class DownloadRunnableFallback extends Thread { return; } + if (DEBUG) { + Log.e(TAG, "got exception, retrying...", e); + } + run();// try again return; } diff --git a/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java b/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java index 327b9149e..f9ceca6ad 100644 --- a/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java +++ b/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java @@ -11,6 +11,7 @@ import java.io.IOException; public class CircularFileWriter extends SharpStream { private final static int QUEUE_BUFFER_SIZE = 8 * 1024;// 8 KiB + private final static int COPY_BUFFER_SIZE = 128 * 1024; // 128 KiB private final static int NOTIFY_BYTES_INTERVAL = 64 * 1024;// 64 KiB private final static int THRESHOLD_AUX_LENGTH = 15 * 1024 * 1024;// 15 MiB @@ -53,6 +54,7 @@ public class CircularFileWriter extends SharpStream { aux.flush(); boolean underflow = aux.offset < aux.length || out.offset < out.length; + byte[] buffer = new byte[COPY_BUFFER_SIZE]; aux.target.seek(0); out.target.seek(out.length); @@ -60,14 +62,14 @@ public class CircularFileWriter extends SharpStream { long length = amount; while (length > 0) { int read = (int) Math.min(length, Integer.MAX_VALUE); - read = aux.target.read(aux.queue, 0, Math.min(read, aux.queue.length)); + read = aux.target.read(buffer, 0, Math.min(read, buffer.length)); if (read < 1) { amount -= length; break; } - out.writeProof(aux.queue, read); + out.writeProof(buffer, read); length -= read; } @@ -100,7 +102,6 @@ public class CircularFileWriter extends SharpStream { // move the excess data to the beginning of the file long readOffset = amount; long writeOffset = 0; - byte[] buffer = new byte[128 * 1024]; // 128 KiB aux.length -= amount; length = aux.length; diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 1892f4437..21cae1835 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -73,6 +73,7 @@ import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_HOLD; import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_STOPPED; import static us.shandian.giga.get.DownloadMission.ERROR_PROGRESS_LOST; import static us.shandian.giga.get.DownloadMission.ERROR_SSL_EXCEPTION; +import static us.shandian.giga.get.DownloadMission.ERROR_TIMEOUT; import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_EXCEPTION; import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_HOST; @@ -487,6 +488,9 @@ public class MissionAdapter extends Adapter { case ERROR_PROGRESS_LOST: msg = R.string.error_progress_lost; break; + case ERROR_TIMEOUT: + msg = R.string.error_timeout; + break; default: if (mission.errCode >= 100 && mission.errCode < 600) { msgEx = "HTTP " + mission.errCode; diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index ae4fda922..9a2518652 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -472,4 +472,5 @@ Downloads, die nicht pausiert werden können, werden wiederholt Konferenzen Ereignisse + Verbindungszeitüberschreitung \ No newline at end of file diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 6a493892d..2e8c62bce 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -457,6 +457,7 @@ abrir en modo popup NewPipe se cerro mientras se trabajaba en el archivo No hay suficiente espacio disponible en el dispositivo Se perdió el progreso porque el archivo fue eliminado + Tiempo de espera excedido API de almacenamiento Seleccione que API utilizar para almacenar las descargas diff --git a/app/src/main/res/values-eu/strings.xml b/app/src/main/res/values-eu/strings.xml index fb41bf8ae..11fed8e64 100644 --- a/app/src/main/res/values-eu/strings.xml +++ b/app/src/main/res/values-eu/strings.xml @@ -468,4 +468,5 @@ Deskarga ezeztatu aurretik saiatu beharreko aldi kopurua Pausatu datu mugikorretara aldatzean Pausatu ezin daitezkeen deskargak berrekingo dira + Konexioaren denbora muga \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index b81195d41..ca9682fdb 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -440,6 +440,7 @@ Dans la file d\'attente En pause Téléchargement échoué + Délai de connection dépassé Conférences Téléchargement terminé %s téléchargements terminés diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index f6d6e42f7..d61b26cfb 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -473,4 +473,5 @@ I download che non possono essere messi in pausa verranno riavviati Eventi Conferenze + Connesione finita \ No newline at end of file diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 76ccfd2dd..be243bffd 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -464,4 +464,6 @@ ダウンロードから %s の保留中の転送を続行します モバイルデータ通信に切り替え時に休止 休止できないダウンロードが再開されます + 保留中 + 接続タイムアウト \ No newline at end of file diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 4e88b6b48..915111043 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -473,4 +473,5 @@ Downloads die niet kunnen worden gepauzeerd zullen worden herstart Gebeurtenissen Conferenties + Time-out van verbinding \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 374b9921f..c75af14e2 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -473,4 +473,5 @@ Останавливать скачивание при переходе на мобильную сеть Закрыть в ожидании + Время соединения вышло \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 023cd00c8..9b58bb79f 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -469,4 +469,5 @@ 無法暫停的下載將會重新開始 事件 會議 + 連接超時 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8433f909d..f10a9f589 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -534,6 +534,7 @@ NewPipe was closed while working on the file No space left on device Progress lost, because the file was deleted + Connection timeout Clear finished downloads Are you sure?