and more fixes

* fix content length reading
* use float overflow. Expensive, double is used instead
* fix invalid cast after click the mission body
* use a list for maximum attemps (downloads)
* minor clean up (DownloadManager.java)
* dont pass SharedPreferences instace to DownloadManager
* use a switch instead of checkbox for cross_network_downloads
* notify media scanner after deleting a finished download
This commit is contained in:
kapodamy 2018-11-24 00:14:37 -03:00
parent 3357fc0f17
commit 6d4e97a877
12 changed files with 111 additions and 52 deletions

View file

@ -35,7 +35,9 @@ public class DownloadInitializer implements Runnable {
HttpURLConnection conn = mMission.openConnection(mId, -1, -1);
if (!mMission.running || Thread.interrupted()) return;
mMission.length = conn.getContentLength();
mMission.length = Utility.getContentLength(conn);
if (mMission.length == 0) {
mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null);
return;
@ -97,7 +99,7 @@ public class DownloadInitializer implements Runnable {
for (long i = 0; i < mMission.currentThreadCount; i++) {
mMission.threadBlockPositions.add(i);
mMission.threadBytePositions.add(0);
mMission.threadBytePositions.add(0L);
}
File file;

View file

@ -124,7 +124,7 @@ public class DownloadMission extends Mission {
@SuppressWarnings("UseSparseArrays")// LongSparseArray is not serializable
private final HashMap<Long, Boolean> blockState = new HashMap<>();
final List<Long> threadBlockPositions = new ArrayList<>();
final List<Integer> threadBytePositions = new ArrayList<>();
final List<Long> threadBytePositions = new ArrayList<>();
private transient boolean deleted;
int currentThreadCount;
@ -216,7 +216,7 @@ public class DownloadMission extends Mission {
* @param threadId the identifier of the thread
* @param position the relative position in bytes or zero
*/
void setThreadBytePosition(int threadId, int position) {
void setThreadBytePosition(int threadId, long position) {
threadBytePositions.set(threadId, position);
}
@ -226,7 +226,7 @@ public class DownloadMission extends Mission {
* @param threadId the identifier of the thread
* @return the relative position in bytes or zero
*/
int getBlockBytePosition(int threadId) {
long getBlockBytePosition(int threadId) {
return threadBytePositions.get(threadId);
}

View file

@ -89,7 +89,7 @@ public class DownloadRunnable implements Runnable {
end = mMission.length - 1;
}
int total = 0;
long total = 0;
try {
HttpURLConnection conn = mMission.openConnection(mId, start, end);

View file

@ -1,5 +1,6 @@
package us.shandian.giga.get;
import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.util.Log;
@ -10,9 +11,13 @@ import java.net.HttpURLConnection;
import java.nio.channels.ClosedByInterruptException;
import us.shandian.giga.util.Utility;
import static org.schabi.newpipe.BuildConfig.DEBUG;
// Single-threaded fallback mode
/**
* Single-threaded fallback mode
*/
public class DownloadRunnableFallback implements Runnable {
private static final String TAG = "DownloadRunnableFallback";
@ -43,10 +48,11 @@ public class DownloadRunnableFallback implements Runnable {
}
@Override
@SuppressLint("LongLogTag")
public void run() {
boolean done;
int start = 0;
long start = 0;
if (!mMission.unknownLength) {
start = mMission.getBlockBytePosition(0);
@ -56,11 +62,12 @@ public class DownloadRunnableFallback implements Runnable {
}
try {
int rangeStart = (mMission.unknownLength || start < 1) ? -1 : start;
long rangeStart = (mMission.unknownLength || start < 1) ? -1 : start;
HttpURLConnection conn = mMission.openConnection(1, rangeStart, -1);
// secondary check for the file length
if (!mMission.unknownLength) mMission.unknownLength = conn.getContentLength() == -1;
if (!mMission.unknownLength)
mMission.unknownLength = Utility.getContentLength(conn) == -1;
f = new RandomAccessFile(mMission.getDownloadedFile(), "rw");
f.seek(mMission.offsets[mMission.current] + start);