misc improvements
* don't show notifications while download activity * proper icon in failed download notifications * re-write list auto-refresh (MissionAdapter.java) * improve I/O performance (CircularFile.java) * fix implementation of "save thread position" on multi-thread downloads
This commit is contained in:
parent
6d4e97a877
commit
db0c4642a4
11 changed files with 270 additions and 184 deletions
|
|
@ -221,12 +221,12 @@ public class DownloadMission extends Mission {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get position inside of the block, where thread will be resumed
|
||||
* Get position inside of the thread, where thread will be resumed
|
||||
*
|
||||
* @param threadId the identifier of the thread
|
||||
* @return the relative position in bytes or zero
|
||||
*/
|
||||
long getBlockBytePosition(int threadId) {
|
||||
long getThreadBytePosition(int threadId) {
|
||||
return threadBytePositions.get(threadId);
|
||||
}
|
||||
|
||||
|
|
@ -256,6 +256,8 @@ public class DownloadMission extends Mission {
|
|||
}
|
||||
}
|
||||
|
||||
conn.connect();
|
||||
|
||||
int statusCode = conn.getResponseCode();
|
||||
switch (statusCode) {
|
||||
case 204:
|
||||
|
|
@ -446,6 +448,8 @@ public class DownloadMission extends Mission {
|
|||
return;
|
||||
}
|
||||
|
||||
if (postprocessingRunning) return;
|
||||
|
||||
// wait for all threads are suspended before save the state
|
||||
runAsync(-1, () -> {
|
||||
try {
|
||||
|
|
@ -590,7 +594,7 @@ public class DownloadMission extends Mission {
|
|||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Http status code" + String.valueOf(statusCode);
|
||||
return "Http status code: " + String.valueOf(statusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@ package us.shandian.giga.get;
|
|||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.ClosedByInterruptException;
|
||||
|
||||
import static org.schabi.newpipe.BuildConfig.DEBUG;
|
||||
|
|
@ -38,8 +37,8 @@ public class DownloadRunnable implements Runnable {
|
|||
Log.d(TAG, mId + ":recovered: " + mMission.recovered);
|
||||
}
|
||||
|
||||
BufferedInputStream ipt = null;
|
||||
RandomAccessFile f;
|
||||
InputStream is = null;
|
||||
|
||||
try {
|
||||
f = new RandomAccessFile(mMission.getDownloadedFile(), "rw");
|
||||
|
|
@ -82,9 +81,11 @@ public class DownloadRunnable implements Runnable {
|
|||
mMission.preserveBlock(blockPosition);
|
||||
mMission.setBlockPosition(mId, blockPosition);
|
||||
|
||||
long start = (blockPosition * DownloadMission.BLOCK_SIZE) + mMission.getBlockBytePosition(mId);
|
||||
long start = blockPosition * DownloadMission.BLOCK_SIZE;
|
||||
long end = start + DownloadMission.BLOCK_SIZE - 1;
|
||||
|
||||
start += mMission.getThreadBytePosition(mId);
|
||||
|
||||
if (end >= mMission.length) {
|
||||
end = mMission.length - 1;
|
||||
}
|
||||
|
|
@ -107,11 +108,11 @@ public class DownloadRunnable implements Runnable {
|
|||
|
||||
f.seek(mMission.offsets[mMission.current] + start);
|
||||
|
||||
ipt = new BufferedInputStream(conn.getInputStream());
|
||||
is = conn.getInputStream();
|
||||
byte[] buf = new byte[DownloadMission.BUFFER_SIZE];
|
||||
int len;
|
||||
|
||||
while (start < end && mMission.running && (len = ipt.read(buf, 0, buf.length)) != -1) {
|
||||
while (start < end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) {
|
||||
f.write(buf, 0, len);
|
||||
start += len;
|
||||
total += len;
|
||||
|
|
@ -119,7 +120,8 @@ public class DownloadRunnable implements Runnable {
|
|||
}
|
||||
|
||||
if (DEBUG && mMission.running) {
|
||||
Log.d(TAG, mId + ":position " + blockPosition + " finished, total length " + total);
|
||||
Log.d(TAG, mId + ":position " + blockPosition + " finished, " + total + " bytes downloaded");
|
||||
mMission.setThreadBytePosition(mId, 0L);
|
||||
}
|
||||
|
||||
// if the download is paused, save progress for this thread
|
||||
|
|
@ -132,7 +134,7 @@ public class DownloadRunnable implements Runnable {
|
|||
|
||||
if (e instanceof ClosedByInterruptException) break;
|
||||
|
||||
if (retryCount++ > mMission.maxRetry) {
|
||||
if (retryCount++ >= mMission.maxRetry) {
|
||||
mMission.notifyError(e);
|
||||
break;
|
||||
}
|
||||
|
|
@ -140,6 +142,8 @@ public class DownloadRunnable implements Runnable {
|
|||
if (DEBUG) {
|
||||
Log.d(TAG, mId + ":position " + blockPosition + " retrying due exception", e);
|
||||
}
|
||||
|
||||
retry = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -150,7 +154,7 @@ public class DownloadRunnable implements Runnable {
|
|||
}
|
||||
|
||||
try {
|
||||
if (ipt != null) ipt.close();
|
||||
if (is != null) is.close();
|
||||
} catch (Exception err) {
|
||||
// nothing to do
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import android.annotation.SuppressLint;
|
|||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.nio.channels.ClosedByInterruptException;
|
||||
|
|
@ -24,18 +24,18 @@ public class DownloadRunnableFallback implements Runnable {
|
|||
private final DownloadMission mMission;
|
||||
private int retryCount = 0;
|
||||
|
||||
private BufferedInputStream ipt;
|
||||
private InputStream is;
|
||||
private RandomAccessFile f;
|
||||
|
||||
DownloadRunnableFallback(@NonNull DownloadMission mission) {
|
||||
mMission = mission;
|
||||
ipt = null;
|
||||
is = null;
|
||||
f = null;
|
||||
}
|
||||
|
||||
private void dispose() {
|
||||
try {
|
||||
if (ipt != null) ipt.close();
|
||||
if (is != null) is.close();
|
||||
} catch (IOException e) {
|
||||
// nothing to do
|
||||
}
|
||||
|
|
@ -55,7 +55,7 @@ public class DownloadRunnableFallback implements Runnable {
|
|||
long start = 0;
|
||||
|
||||
if (!mMission.unknownLength) {
|
||||
start = mMission.getBlockBytePosition(0);
|
||||
start = mMission.getThreadBytePosition(0);
|
||||
if (DEBUG && start > 0) {
|
||||
Log.i(TAG, "Resuming a single-thread download at " + start);
|
||||
}
|
||||
|
|
@ -72,18 +72,15 @@ public class DownloadRunnableFallback implements Runnable {
|
|||
f = new RandomAccessFile(mMission.getDownloadedFile(), "rw");
|
||||
f.seek(mMission.offsets[mMission.current] + start);
|
||||
|
||||
ipt = new BufferedInputStream(conn.getInputStream());
|
||||
is = conn.getInputStream();
|
||||
|
||||
byte[] buf = new byte[DownloadMission.BUFFER_SIZE];
|
||||
byte[] buf = new byte[64 * 1024];
|
||||
int len = 0;
|
||||
|
||||
while (mMission.running && (len = ipt.read(buf, 0, buf.length)) != -1) {
|
||||
while (mMission.running && (len = is.read(buf, 0, buf.length)) != -1) {
|
||||
f.write(buf, 0, len);
|
||||
start += len;
|
||||
|
||||
mMission.notifyProgress(len);
|
||||
|
||||
if (Thread.interrupted()) break;
|
||||
}
|
||||
|
||||
// if thread goes interrupted check if the last part is written. This avoid re-download the whole file
|
||||
|
|
@ -96,7 +93,7 @@ public class DownloadRunnableFallback implements Runnable {
|
|||
|
||||
if (e instanceof ClosedByInterruptException) return;
|
||||
|
||||
if (retryCount++ > mMission.maxRetry) {
|
||||
if (retryCount++ >= mMission.maxRetry) {
|
||||
mMission.notifyError(e);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue