Don't depend on Content-Length headers to download (Fixes #430)

Instead, keep downloading until the `InputStream` returns -1.

Also, required updates to the UI so that when the download size is
not known, there is still a reasonable response to the user.

Note that this still fails when using the Android download manager
if the download attempts to get resumed, and the server did not
send a Connection: close, Content-Length, or Transfer-Encoding: Chunked
header.
This commit is contained in:
Peter Serwylo 2015-10-04 11:01:02 +11:00
parent f2b1583239
commit 0164adc386
5 changed files with 38 additions and 17 deletions

View File

@ -172,6 +172,13 @@
- Percentage complete (int between 0-100) - Percentage complete (int between 0-100)
--> -->
<string name="status_download">Downloading\n%2$s / %3$s (%4$d%%) from\n%1$s</string> <string name="status_download">Downloading\n%2$s / %3$s (%4$d%%) from\n%1$s</string>
<!--
status_download_unknown_size takes two parameters:
- Repository (url)
- Downloaded size (human readable)
-->
<string name="status_download_unknown_size">Downloading\n%2$s from\n%1$s</string>
<string name="update_notification_title">Updating repositories</string> <string name="update_notification_title">Updating repositories</string>
<string name="status_processing_xml_percent">Processing %2$s / %3$s (%4$d%%) from %1$s</string> <string name="status_processing_xml_percent">Processing %2$s / %3$s (%4$d%%) from %1$s</string>
<string name="status_connecting_to_repo">Connecting to\n%1$s</string> <string name="status_connecting_to_repo">Connecting to\n%1$s</string>

View File

@ -1493,18 +1493,26 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
/** /**
* Updates progress bar and captions to new values (in bytes). * Updates progress bar and captions to new values (in bytes).
*/ */
public void updateProgress(long progress, long total) { public void updateProgress(long bytesDownloaded, long totalBytes) {
// Avoid division by zero and other weird values if (bytesDownloaded < 0 || totalBytes == 0) {
if (progress < 0 || total <= 0) { // Avoid division by zero and other weird values
return; return;
} }
long percent = progress * 100 / total;
setProgressVisible(true); if (totalBytes == -1) {
progressBar.setIndeterminate(false); setProgressVisible(true);
progressBar.setProgress((int) percent); progressBar.setIndeterminate(true);
progressBar.setMax(100); progressSize.setText(readableFileSize(bytesDownloaded));
progressSize.setText(readableFileSize(progress) + " / " + readableFileSize(total)); progressPercent.setText("");
progressPercent.setText(Long.toString(percent) + " %"); } else {
long percent = bytesDownloaded * 100 / totalBytes;
setProgressVisible(true);
progressBar.setIndeterminate(false);
progressBar.setProgress((int) percent);
progressBar.setMax(100);
progressSize.setText(readableFileSize(bytesDownloaded) + " / " + readableFileSize(totalBytes));
progressPercent.setText(Long.toString(percent) + " %");
}
} }
/** /**

View File

@ -209,12 +209,17 @@ public class UpdateService extends IntentService implements ProgressListener {
String repoAddress = intent.getStringExtra(Downloader.EXTRA_ADDRESS); String repoAddress = intent.getStringExtra(Downloader.EXTRA_ADDRESS);
int downloadedSize = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, -1); int downloadedSize = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, -1);
String downloadedSizeFriendly = Utils.getFriendlySize(downloadedSize);
int totalSize = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, -1); int totalSize = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, -1);
int percent = (int) ((double) downloadedSize / totalSize * 100); int percent = (int) ((double) downloadedSize / totalSize * 100);
sendStatus(STATUS_INFO, String message;
getString(R.string.status_download, repoAddress, if (totalSize == -1) {
Utils.getFriendlySize(downloadedSize), message = getString(R.string.status_download_unknown_size, repoAddress, downloadedSizeFriendly);
Utils.getFriendlySize(totalSize), percent)); } else {
String totalSizeFriendly = Utils.getFriendlySize(totalSize);
message = getString(R.string.status_download, repoAddress, downloadedSizeFriendly, totalSizeFriendly, percent);
}
sendStatus(STATUS_INFO, message);
} }
}; };

View File

@ -154,11 +154,10 @@ public abstract class Downloader {
throwExceptionIfInterrupted(); throwExceptionIfInterrupted();
sendProgress(bytesRead, totalBytes); sendProgress(bytesRead, totalBytes);
while (bytesRead < totalBytes) { while (true) {
int count; int count;
if (input.available()>0) { if (input.available() > 0) {
int readLength = Math.min(input.available(), buffer.length); int readLength = Math.min(input.available(), buffer.length);
count = input.read(buffer, 0, readLength); count = input.read(buffer, 0, readLength);
} else { } else {

View File

@ -293,6 +293,8 @@ public class SwapAppsView extends ListView implements
progressView.setIndeterminate(false); progressView.setIndeterminate(false);
progressView.setMax(100); progressView.setMax(100);
progressView.setProgress(progress); progressView.setProgress(progress);
} else {
progressView.setIndeterminate(true);
} }
} }
}; };