Updater: Don't show thousands of days
* When pausing and resuming, the ETA calculation is off by a lot due to the time diff since the last calculation resulting in a big delta and therefore a very low speed * By fixing this, we also need to set the last byte count to the total byte count there, otherwise the downloaded bytes since last calculation will be larger than in reality, resulting in too fast speeds calculated Change-Id: Ica1053cc297ff59221ea2bc0014f20973a080c88
This commit is contained in:
@@ -140,8 +140,20 @@ public class HttpURLConnectionClient implements DownloadClient {
|
|||||||
mResume = resume;
|
mResume = resume;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calculateSpeed() {
|
private void calculateSpeed(boolean justResumed) {
|
||||||
final long millis = SystemClock.elapsedRealtime();
|
final long millis = SystemClock.elapsedRealtime();
|
||||||
|
if (justResumed) {
|
||||||
|
// If we don't start over with these after resumption, we get huge numbers for
|
||||||
|
// ETA since the delta will grow, resulting in a very low speed
|
||||||
|
mLastMillis = millis;
|
||||||
|
mSpeed = -1; // we don't want the moving avg with values from who knows when
|
||||||
|
|
||||||
|
// need to do this as well, otherwise the second time we call calculateSpeed(),
|
||||||
|
// the difference (mTotalBytesRead - mCurSampleBytes) will be larger than expected,
|
||||||
|
// resulting in a higher speed calculation
|
||||||
|
mCurSampleBytes = mTotalBytesRead;
|
||||||
|
return;
|
||||||
|
}
|
||||||
final long delta = millis - mLastMillis;
|
final long delta = millis - mLastMillis;
|
||||||
if (delta > 500) {
|
if (delta > 500) {
|
||||||
final long curSpeed = ((mTotalBytesRead - mCurSampleBytes) * 1000) / delta;
|
final long curSpeed = ((mTotalBytesRead - mCurSampleBytes) * 1000) / delta;
|
||||||
@@ -243,6 +255,7 @@ public class HttpURLConnectionClient implements DownloadClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
boolean justResumed = false;
|
||||||
try {
|
try {
|
||||||
mClient.setInstanceFollowRedirects(!mUseDuplicateLinks);
|
mClient.setInstanceFollowRedirects(!mUseDuplicateLinks);
|
||||||
mClient.connect();
|
mClient.connect();
|
||||||
@@ -256,6 +269,7 @@ public class HttpURLConnectionClient implements DownloadClient {
|
|||||||
mCallback.onResponse(new Headers());
|
mCallback.onResponse(new Headers());
|
||||||
|
|
||||||
if (mResume && isPartialContentCode(responseCode)) {
|
if (mResume && isPartialContentCode(responseCode)) {
|
||||||
|
justResumed = true;
|
||||||
mTotalBytesRead = mDestination.length();
|
mTotalBytesRead = mDestination.length();
|
||||||
Log.d(TAG, "The server fulfilled the partial content request");
|
Log.d(TAG, "The server fulfilled the partial content request");
|
||||||
} else if (mResume || !isSuccessCode(responseCode)) {
|
} else if (mResume || !isSuccessCode(responseCode)) {
|
||||||
@@ -274,8 +288,9 @@ public class HttpURLConnectionClient implements DownloadClient {
|
|||||||
while (!isInterrupted() && (count = inputStream.read(b)) > 0) {
|
while (!isInterrupted() && (count = inputStream.read(b)) > 0) {
|
||||||
outputStream.write(b, 0, count);
|
outputStream.write(b, 0, count);
|
||||||
mTotalBytesRead += count;
|
mTotalBytesRead += count;
|
||||||
calculateSpeed();
|
calculateSpeed(justResumed);
|
||||||
calculateEta();
|
calculateEta();
|
||||||
|
justResumed = false; // otherwise we will never get speed and ETA again
|
||||||
if (mProgressListener != null) {
|
if (mProgressListener != null) {
|
||||||
mProgressListener.update(mTotalBytesRead, mTotalBytes, mSpeed, mEta);
|
mProgressListener.update(mTotalBytesRead, mTotalBytes, mSpeed, mEta);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user