Use existing byte-format function.

Utils already contains a function to format bytes, removed duplicate
function.
This commit is contained in:
Peter Serwylo 2015-10-04 11:24:39 +11:00
parent 0164adc386
commit 19ae4a76c2
2 changed files with 3 additions and 16 deletions

View File

@ -1502,7 +1502,7 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
if (totalBytes == -1) { if (totalBytes == -1) {
setProgressVisible(true); setProgressVisible(true);
progressBar.setIndeterminate(true); progressBar.setIndeterminate(true);
progressSize.setText(readableFileSize(bytesDownloaded)); progressSize.setText(Utils.getFriendlySize(bytesDownloaded));
progressPercent.setText(""); progressPercent.setText("");
} else { } else {
long percent = bytesDownloaded * 100 / totalBytes; long percent = bytesDownloaded * 100 / totalBytes;
@ -1510,24 +1510,11 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
progressBar.setIndeterminate(false); progressBar.setIndeterminate(false);
progressBar.setProgress((int) percent); progressBar.setProgress((int) percent);
progressBar.setMax(100); progressBar.setMax(100);
progressSize.setText(readableFileSize(bytesDownloaded) + " / " + readableFileSize(totalBytes)); progressSize.setText(Utils.getFriendlySize(bytesDownloaded) + " / " + Utils.getFriendlySize(totalBytes));
progressPercent.setText(Long.toString(percent) + " %"); progressPercent.setText(Long.toString(percent) + " %");
} }
} }
/**
* Converts a number of bytes to a human readable file size (eg 3.5 GiB).
*
* Based on http://stackoverflow.com/a/5599842
*/
public String readableFileSize(long bytes) {
final String[] units = getResources().getStringArray(R.array.file_size_units);
if (bytes <= 0) return "0 " + units[0];
int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
return new DecimalFormat("#,##0.#")
.format(bytes / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
/** /**
* Shows or hides progress bar and related views. * Shows or hides progress bar and related views.
*/ */

View File

@ -186,7 +186,7 @@ public final class Utils {
} }
} }
public static String getFriendlySize(int size) { public static String getFriendlySize(long size) {
double s = size; double s = size;
int i = 0; int i = 0;
while (i < FRIENDLY_SIZE_FORMAT.length - 1 && s >= 1024) { while (i < FRIENDLY_SIZE_FORMAT.length - 1 && s >= 1024) {