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:
		
							parent
							
								
									f2b1583239
								
							
						
					
					
						commit
						0164adc386
					
				| @ -172,6 +172,13 @@ | ||||
| 	  - Percentage complete (int between 0-100) | ||||
| 	--> | ||||
| 	<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="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> | ||||
|  | ||||
| @ -1493,18 +1493,26 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A | ||||
|         /** | ||||
|          * Updates progress bar and captions to new values (in bytes). | ||||
|          */ | ||||
|         public void updateProgress(long progress, long total) { | ||||
|             // Avoid division by zero and other weird values | ||||
|             if (progress < 0 || total <= 0) { | ||||
|         public void updateProgress(long bytesDownloaded, long totalBytes) { | ||||
|             if (bytesDownloaded < 0 || totalBytes == 0) { | ||||
|                 // Avoid division by zero and other weird values | ||||
|                 return; | ||||
|             } | ||||
|             long percent = progress * 100 / total; | ||||
|             setProgressVisible(true); | ||||
|             progressBar.setIndeterminate(false); | ||||
|             progressBar.setProgress((int) percent); | ||||
|             progressBar.setMax(100); | ||||
|             progressSize.setText(readableFileSize(progress) + " / " + readableFileSize(total)); | ||||
|             progressPercent.setText(Long.toString(percent) + " %"); | ||||
| 
 | ||||
|             if (totalBytes == -1) { | ||||
|                 setProgressVisible(true); | ||||
|                 progressBar.setIndeterminate(true); | ||||
|                 progressSize.setText(readableFileSize(bytesDownloaded)); | ||||
|                 progressPercent.setText(""); | ||||
|             } 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) + " %"); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         /** | ||||
|  | ||||
| @ -209,12 +209,17 @@ public class UpdateService extends IntentService implements ProgressListener { | ||||
| 
 | ||||
|             String repoAddress = intent.getStringExtra(Downloader.EXTRA_ADDRESS); | ||||
|             int downloadedSize = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, -1); | ||||
|             String downloadedSizeFriendly = Utils.getFriendlySize(downloadedSize); | ||||
|             int totalSize = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, -1); | ||||
|             int percent = (int) ((double) downloadedSize / totalSize * 100); | ||||
|             sendStatus(STATUS_INFO, | ||||
|                     getString(R.string.status_download, repoAddress, | ||||
|                             Utils.getFriendlySize(downloadedSize), | ||||
|                             Utils.getFriendlySize(totalSize), percent)); | ||||
|             String message; | ||||
|             if (totalSize == -1) { | ||||
|                 message = getString(R.string.status_download_unknown_size, repoAddress, downloadedSizeFriendly); | ||||
|             } else { | ||||
|                 String totalSizeFriendly = Utils.getFriendlySize(totalSize); | ||||
|                 message = getString(R.string.status_download, repoAddress, downloadedSizeFriendly, totalSizeFriendly, percent); | ||||
|             } | ||||
|             sendStatus(STATUS_INFO, message); | ||||
|         } | ||||
|     }; | ||||
| 
 | ||||
|  | ||||
| @ -154,11 +154,10 @@ public abstract class Downloader { | ||||
|         throwExceptionIfInterrupted(); | ||||
| 
 | ||||
|         sendProgress(bytesRead, totalBytes); | ||||
|         while (bytesRead < totalBytes) { | ||||
|         while (true) { | ||||
| 
 | ||||
|             int count; | ||||
|             if (input.available()>0) { | ||||
| 
 | ||||
|             if (input.available() > 0) { | ||||
|                 int readLength = Math.min(input.available(), buffer.length); | ||||
|                 count = input.read(buffer, 0, readLength); | ||||
|             } else { | ||||
|  | ||||
| @ -293,6 +293,8 @@ public class SwapAppsView extends ListView implements | ||||
|                         progressView.setIndeterminate(false); | ||||
|                         progressView.setMax(100); | ||||
|                         progressView.setProgress(progress); | ||||
|                     } else { | ||||
|                         progressView.setIndeterminate(true); | ||||
|                     } | ||||
|                 } | ||||
|             }; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Peter Serwylo
						Peter Serwylo