Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 25bdb702 authored by Frank Preel's avatar Frank Preel
Browse files

Better indictor (unit coherent)

parent ca49becb
Loading
Loading
Loading
Loading
+39 −18
Original line number Diff line number Diff line
@@ -56,11 +56,20 @@ public class DownloadTask extends Task<Boolean>{
    /**
     * Constant size
     */
    private static final long[] CST_SIZE = {1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024};
    private static final long[] CST_SIZE = {1, 1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024};
    /**
     * Constants units
     */
    private static final String[] CST_UNITS = {"KB", "MB", "GB", "TB"};
    private static final String[] CST_UNITS = {"B", "KB", "MB", "GB", "TB"};
    

    private static final DecimalFormat[] CST_FORMAT = {
    	new DecimalFormat("#0"),
    	new DecimalFormat("##0"),
    	new DecimalFormat("#,##0"),
    	new DecimalFormat("#,##0.00"),
    	new DecimalFormat("#,##0.000")
    };
    
    final private ResourceBundle i18n;
    final private String targetUrl;
@@ -219,9 +228,10 @@ public class DownloadTask extends Task<Boolean>{
        logger.debug("full file size = {}", fullFileSize);
        
        //Update UI
        final String formattedFileSize = formatFileSize(fullFileSize); //used for UI
        final int unitIndex = getDownloadUnit(fullFileSize);
        final String formattedFileSize = formatFileSize(fullFileSize, unitIndex); //used for UI
        updateProgress(-1, fullFileSize);
        updateMessage(formatFileSize(previouslyDownloadedAmount)+" / "+formattedFileSize );
        updateMessage(formatFileSize(previouslyDownloadedAmount, unitIndex)+" / "+formattedFileSize );
        
        boolean downloaded = false;
        
@@ -236,10 +246,11 @@ public class DownloadTask extends Task<Boolean>{
            timeoutThread.start();
            
            long downloadAmount =  previouslyDownloadedAmount;
            
            while ( rbc.isOpen() && !isCancelled() && ! downloaded ){

                final long precedentAmount = downloadAmount;
                downloadAmount += fos.getChannel().transferFrom(rbc,downloadAmount,1 << 18);
                downloadAmount += fos.getChannel().transferFrom(rbc,downloadAmount,1 << 20); //~1MB
               
                if(precedentAmount == downloadAmount){ //it means nothing had been downloaded
                	logger.warn("precedent amount = downloaded amount");
@@ -250,7 +261,7 @@ public class DownloadTask extends Task<Boolean>{
                    timeoutRunnable.amountIncreased(); //delay the timeout
                    
                    updateProgress(downloadAmount, fullFileSize);
                    updateMessage( formatFileSize(downloadAmount)+" / "+formattedFileSize);
                    updateMessage( formatFileSize(downloadAmount, unitIndex)+" / "+formattedFileSize);

                    fos.flush();
                    downloaded = (downloadAmount == fullFileSize);
@@ -265,20 +276,30 @@ public class DownloadTask extends Task<Boolean>{
    }
    
    /**
     * Format file size to use correct size name (mb, gb, ...)
     * @todo definitively should be in the UI
     * @param value the file size formatted witht the good size category
     * @return 
     * Get the download file unit index (mb, gb, ...) (1,2...)
     * @param value the file size 
     * @return the index 
     */
    public String formatFileSize(final double value){
        double size;
        for (int i = 0; i < 3; i++) {
    private final int getDownloadUnit(final double value){
        double size = 0;
        for (int i = 0; i < CST_SIZE.length; i++) {
            size=value/CST_SIZE[i];
            if (size <= 1024) {
                return new DecimalFormat("#,##0.#").format(size) + " " + CST_UNITS[i] ;
            	return i;
            }	
        }
        return null;
        return -1;
    }
    
    /**
     * Format file size to use correct size name (mb, gb, ...)
     * @todo definitively should be in the UI
     * @param value the file size 
     * @param unitIndex info about unit
     * @return 
     */
    private final String formatFileSize(final double value, final int unitIndex){
        	return CST_FORMAT[unitIndex].format(value/CST_SIZE[unitIndex]) + " " + CST_UNITS[unitIndex] ;
    }