send Downloader progress only via its DownloaderProgressListener
Instead of duplicate APIs, standardize on a single API, and use that everywhere via the Downloader.LOCAL_ACTION_PROGRESS event that is already wired in.
This commit is contained in:
		
							parent
							
								
									ae0976d24a
								
							
						
					
					
						commit
						8befba0522
					
				@ -459,12 +459,7 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
 | 
			
		||||
                localBroadcastManager.registerReceiver(downloaderProgressReceiver,
 | 
			
		||||
                        new IntentFilter(Downloader.LOCAL_ACTION_PROGRESS));
 | 
			
		||||
                downloadHandler.setProgressListener(this);
 | 
			
		||||
 | 
			
		||||
                if (downloadHandler.getTotalBytes() == 0) {
 | 
			
		||||
                    headerFragment.startProgress();
 | 
			
		||||
                } else {
 | 
			
		||||
                    headerFragment.updateProgress(downloadHandler.getBytesRead(), downloadHandler.getTotalBytes());
 | 
			
		||||
                }
 | 
			
		||||
                headerFragment.startProgress();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -261,12 +261,4 @@ public class ApkDownloader implements AsyncDownloader.Listener {
 | 
			
		||||
    public Apk getApk() {
 | 
			
		||||
        return curApk;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getBytesRead() {
 | 
			
		||||
        return dlWrapper != null ? dlWrapper.getBytesRead() : 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getTotalBytes() {
 | 
			
		||||
        return dlWrapper != null ? dlWrapper.getTotalBytes() : 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -30,14 +30,6 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
 | 
			
		||||
        this.listener = listener;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getBytesRead() {
 | 
			
		||||
        return downloader.getBytesRead();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getTotalBytes() {
 | 
			
		||||
        return downloader.getTotalBytes();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void download() {
 | 
			
		||||
        downloadThread = new DownloadThread();
 | 
			
		||||
        downloadThread.start();
 | 
			
		||||
 | 
			
		||||
@ -10,10 +10,6 @@ public interface AsyncDownloader {
 | 
			
		||||
        void onDownloadComplete();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int getBytesRead();
 | 
			
		||||
 | 
			
		||||
    int getTotalBytes();
 | 
			
		||||
 | 
			
		||||
    void download();
 | 
			
		||||
 | 
			
		||||
    void attemptCancel();
 | 
			
		||||
 | 
			
		||||
@ -31,8 +31,6 @@ public abstract class Downloader {
 | 
			
		||||
 | 
			
		||||
    protected final URL sourceUrl;
 | 
			
		||||
    protected String cacheTag;
 | 
			
		||||
    private int bytesRead;
 | 
			
		||||
    private int totalBytes;
 | 
			
		||||
 | 
			
		||||
    interface DownloaderProgressListener {
 | 
			
		||||
        void sendProgress(URL sourceUrl, int bytesRead, int totalBytes);
 | 
			
		||||
@ -147,7 +145,7 @@ public abstract class Downloader {
 | 
			
		||||
    private void copyInputToOutputStream(InputStream input, int bufferSize) throws IOException, InterruptedException {
 | 
			
		||||
 | 
			
		||||
        int bytesRead = 0;
 | 
			
		||||
        this.totalBytes = totalDownloadSize();
 | 
			
		||||
        int totalBytes = totalDownloadSize();
 | 
			
		||||
        byte[] buffer = new byte[bufferSize];
 | 
			
		||||
 | 
			
		||||
        // Getting the total download size could potentially take time, depending on how
 | 
			
		||||
@ -182,20 +180,11 @@ public abstract class Downloader {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void sendProgress(int bytesRead, int totalBytes) {
 | 
			
		||||
        this.bytesRead = bytesRead;
 | 
			
		||||
        if (downloaderProgressListener != null) {
 | 
			
		||||
            downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getBytesRead() {
 | 
			
		||||
        return bytesRead;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getTotalBytes() {
 | 
			
		||||
        return totalBytes;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Overrides every method in {@link InputStream} and delegates to the wrapped stream.
 | 
			
		||||
     * The only difference is that when we call the {@link WrappedInputStream#close()} method,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user