Commits

Julian Brost committed 20012d3

Fixed #119: Handle bogus values for download size and progress.

Comments (0)

Files changed (1)

src/net/x4a42/volksempfaenger/ui/DownloadListFragment.java

 
 			// percentage
 			TextView percentage = (TextView) view.findViewById(R.id.percentage);
-			if (status == DownloadManager.STATUS_RUNNING
-					|| status == DownloadManager.STATUS_PAUSED) {
+			if ((status == DownloadManager.STATUS_RUNNING || status == DownloadManager.STATUS_PAUSED)
+					&& total != 0 && done <= total) {
 				percentage.setVisibility(View.VISIBLE);
 				percentage.setText(String.format("%.0f%%", (double) done
 						/ total * 100));
 			if (status == DownloadManager.STATUS_RUNNING
 					|| status == DownloadManager.STATUS_PAUSED) {
 				progressText.setVisibility(View.VISIBLE);
-				progressText.setText(String.format("%s / %s",
-						Utils.readableFileSize(done),
-						Utils.readableFileSize(total)));
+				if (total != 0 && done <= total) {
+					progressText.setText(String.format("%s / %s",
+							Utils.readableFileSize(done),
+							Utils.readableFileSize(total)));
+				} else {
+					progressText.setText(Utils.readableFileSize(done));
+				}
 			} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
 				progressText.setVisibility(View.VISIBLE);
 				progressText.setText(Utils.readableFileSize(done));
 				progressBar.setVisibility(View.VISIBLE);
 				progressBar.setIndeterminate(true);
 			} else if (status == DownloadManager.STATUS_RUNNING) {
-				int progress;
-				int progressMax;
+				progressBar.setVisibility(View.VISIBLE);
+				if (done > total) {
+					progressBar.setIndeterminate(true);
+				} else {
+					int progress;
+					int progressMax;
 
-				if (done > Integer.MAX_VALUE || total > Integer.MAX_VALUE) {
-					if (done > total) {
-						// this shouldn't happen but we are prepared
-						progress = Integer.MAX_VALUE;
+					if (total > Integer.MAX_VALUE) {
+						progress = (int) ((double) done / Long.MAX_VALUE * Integer.MAX_VALUE);
+						progressMax = Integer.MAX_VALUE;
 					} else {
-						progress = (int) ((double) done / Long.MAX_VALUE * Integer.MAX_VALUE);
+						progress = (int) done;
+						progressMax = (int) total;
 					}
-					progressMax = Integer.MAX_VALUE;
-				} else {
-					progress = (int) done;
-					progressMax = (int) total;
+
+					progressBar.setIndeterminate(false);
+					progressBar.setMax(progressMax);
+					progressBar.setProgress(progress);
 				}
-
-				progressBar.setVisibility(View.VISIBLE);
-				progressBar.setIndeterminate(false);
-				progressBar.setMax(progressMax);
-				progressBar.setProgress(progress);
 			} else {
 				progressBar.setVisibility(View.GONE);
 			}